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