]> Shamusworld >> Repos - architektonas/blob - src/actions/rs_actiondimangular.cpp
Last checkin before major refactor...
[architektonas] / src / actions / rs_actiondimangular.cpp
1 // rs_actiondimangular.cpp
2 //
3 // Part of the Architektonas Project
4 // Originally part of QCad Community Edition by Andrew Mustun
5 // Extensively rewritten and refactored by James L. Hammons
6 // (C) 2010 Underground Software
7 //
8 // JLH = James L. Hammons <jlhamm@acm.org>
9 //
10 // Who  When        What
11 // ---  ----------  -----------------------------------------------------------
12 // JLH  06/03/2010  Added this text. :-)
13 //
14
15 #include "rs_actiondimangular.h"
16
17 #include "rs_commandevent.h"
18 #include "rs_dialogfactory.h"
19 #include "graphicview.h"
20 #include "rs_information.h"
21 #include "rs_preview.h"
22
23 RS_ActionDimAngular::RS_ActionDimAngular(RS_EntityContainer & container, GraphicView & graphicView): RS_ActionDimension("Draw Angular Dimensions",
24                 container, graphicView)
25 {
26         reset();
27 }
28
29 RS_ActionDimAngular::~RS_ActionDimAngular()
30 {
31 }
32
33 /*virtual*/ RS2::ActionType RS_ActionDimAngular::rtti()
34 {
35         return RS2::ActionDimAngular;
36 }
37
38 void RS_ActionDimAngular::reset()
39 {
40         RS_ActionDimension::reset();
41         edata = RS_DimAngularData(Vector(false), Vector(false), Vector(false), Vector(false));
42         line1 = NULL;
43         line2 = NULL;
44         center = Vector(false);
45         RS_DIALOGFACTORY->requestOptions(this, true, true);
46 }
47
48 void RS_ActionDimAngular::trigger()
49 {
50         RS_PreviewActionInterface::trigger();
51
52         if (line1 != NULL && line2 != NULL)
53         {
54                 RS_DimAngular * newEntity = NULL;
55
56                 newEntity = new RS_DimAngular(container,
57                                 data,
58                                 edata);
59
60                 newEntity->setLayerToActive();
61                 newEntity->setPenToActive();
62                 newEntity->update();
63                 container->addEntity(newEntity);
64
65                 // upd. undo list:
66                 if (document != NULL)
67                 {
68                         document->startUndoCycle();
69                         document->addUndoable(newEntity);
70                         document->endUndoCycle();
71                 }
72                 deleteSnapper();
73                 Vector rz = graphicView->getRelativeZero();
74                 graphicView->moveRelativeZero(Vector(0.0, 0.0));
75                 graphicView->drawEntity(newEntity);
76                 graphicView->moveRelativeZero(rz);
77         }
78         else
79                 RS_DEBUG->print("RS_ActionDimAngular::trigger:"
80                         " Entity is NULL\n");
81 }
82
83 void RS_ActionDimAngular::mouseMoveEvent(QMouseEvent * e)
84 {
85         RS_DEBUG->print("RS_ActionDimAngular::mouseMoveEvent begin");
86
87         Vector mouse(graphicView->toGraphX(e->x()), graphicView->toGraphY(e->y()));
88
89         switch (getStatus())
90         {
91         case SetLine1:
92                 break;
93
94         case SetLine2:
95                 break;
96
97         case SetPos:
98
99                 if (line1 != NULL && line2 != NULL && center.valid)
100                 {
101                         Vector mouse = snapPoint(e);
102                         edata.definitionPoint4 = mouse;
103
104                         RS_DimAngular * d = new RS_DimAngular(preview, data, edata);
105                         d->update();
106
107                         deletePreview();
108                         clearPreview();
109                         preview->addEntity(d);
110                         drawPreview();
111                 }
112                 break;
113
114         default:
115                 break;
116         }
117
118         RS_DEBUG->print("RS_ActionDimAngular::mouseMoveEvent end");
119 }
120
121 void RS_ActionDimAngular::mouseReleaseEvent(QMouseEvent * e)
122 {
123         if (e->button() == Qt::LeftButton)
124         {
125                 switch (getStatus())
126                 {
127                 case SetLine1: {
128                         RS_Entity * en = catchEntity(e, RS2::ResolveAll);
129
130                         if (en != NULL
131                             && en->rtti() == RS2::EntityLine)
132                         {
133                                 line1 = (RS_Line *)en;
134                                 setStatus(SetLine2);
135                         }
136                 }
137                 break;
138
139                 case SetLine2: {
140                         RS_Entity * en = catchEntity(e, RS2::ResolveAll);
141
142                         if (en != NULL
143                             && en->rtti() == RS2::EntityLine)
144                         {
145                                 line2 = (RS_Line *)en;
146
147                                 VectorSolutions sol =
148                                         RS_Information::getIntersectionLineLine(line1, line2);
149
150                                 if (sol.get(0).valid)
151                                 {
152                                         center = sol.get(0);
153
154                                         if (center.distanceTo(line1->getStartpoint())
155                                             < center.distanceTo(line1->getEndpoint()))
156                                         {
157                                                 edata.definitionPoint1 = line1->getStartpoint();
158                                                 edata.definitionPoint2 = line1->getEndpoint();
159                                         }
160                                         else
161                                         {
162                                                 edata.definitionPoint1 = line1->getEndpoint();
163                                                 edata.definitionPoint2 = line1->getStartpoint();
164                                         }
165
166                                         if (center.distanceTo(line2->getStartpoint())
167                                             < center.distanceTo(line2->getEndpoint()))
168                                         {
169                                                 edata.definitionPoint3 = line2->getStartpoint();
170                                                 data.definitionPoint = line2->getEndpoint();
171                                         }
172                                         else
173                                         {
174                                                 edata.definitionPoint3 = line2->getEndpoint();
175                                                 data.definitionPoint = line2->getStartpoint();
176                                         }
177                                         graphicView->moveRelativeZero(center);
178                                         setStatus(SetPos);
179                                 }
180                         }
181                 }
182                 break;
183
184                 case SetPos: {
185                         Vector ce(snapPoint(e));
186                         coordinateEvent(&ce);
187                 }
188                 break;
189                 }
190         }
191         else if (e->button() == Qt::RightButton)
192         {
193                 deletePreview();
194                 deleteSnapper();
195                 clearPreview();
196                 init(getStatus() - 1);
197         }
198 }
199
200 void RS_ActionDimAngular::coordinateEvent(Vector * e)
201 {
202         if (e == NULL)
203                 return;
204
205         switch (getStatus())
206         {
207         case SetPos:
208                 edata.definitionPoint4 = *e;
209                 trigger();
210                 reset();
211                 setStatus(SetLine1);
212                 break;
213
214         default:
215                 break;
216         }
217 }
218
219 void RS_ActionDimAngular::commandEvent(RS_CommandEvent * e)
220 {
221         QString c = e->getCommand().toLower();
222
223         if (checkCommand("help", c))
224         {
225                 RS_DIALOGFACTORY->commandMessage(msgAvailableCommands()
226                         + getAvailableCommands().join(", "));
227                 return;
228         }
229
230         // setting new text label:
231         if (getStatus() == SetText)
232         {
233                 setText(c);
234                 RS_DIALOGFACTORY->requestOptions(this, true, true);
235                 graphicView->enableCoordinateInput();
236                 setStatus(lastStatus);
237                 return;
238         }
239
240         // command: text
241         if (checkCommand("text", c))
242         {
243                 lastStatus = (Status)getStatus();
244                 graphicView->disableCoordinateInput();
245                 setStatus(SetText);
246         }
247 }
248
249 QStringList RS_ActionDimAngular::getAvailableCommands()
250 {
251         QStringList cmd;
252
253         switch (getStatus())
254         {
255         case SetLine1:
256         case SetLine2:
257         case SetPos:
258                 cmd += command("text");
259                 break;
260
261         default:
262                 break;
263         }
264
265         return cmd;
266 }
267
268 void RS_ActionDimAngular::showOptions()
269 {
270         RS_ActionInterface::showOptions();
271
272         RS_DIALOGFACTORY->requestOptions(this, true);
273 }
274
275 void RS_ActionDimAngular::hideOptions()
276 {
277         RS_ActionInterface::hideOptions();
278
279         RS_DIALOGFACTORY->requestOptions(this, false);
280 }
281
282 void RS_ActionDimAngular::updateMouseButtonHints()
283 {
284         switch (getStatus())
285         {
286         case SetLine1:
287                 RS_DIALOGFACTORY->updateMouseWidget(tr("Select first line"),
288                         tr("Cancel"));
289                 break;
290
291         case SetLine2:
292                 RS_DIALOGFACTORY->updateMouseWidget(tr("Select second line"),
293                         tr("Cancel"));
294                 break;
295
296         case SetPos:
297                 RS_DIALOGFACTORY->updateMouseWidget(
298                         tr("Specify dimension arc line location"), tr("Cancel"));
299                 break;
300
301         case SetText:
302                 RS_DIALOGFACTORY->updateMouseWidget(tr("Enter dimension text:"), "");
303                 break;
304
305         default:
306                 RS_DIALOGFACTORY->updateMouseWidget("", "");
307                 break;
308         }
309 }
310
311 // EOF