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