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