]> Shamusworld >> Repos - architektonas/blob - src/actions/actiondimangular.cpp
Last checkin before major refactor...
[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 // (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 "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 ActionDimAngular::ActionDimAngular(RS_EntityContainer & container, GraphicView & graphicView): ActionDimension("Draw Angular Dimensions",
24                 container, graphicView)
25 {
26         reset();
27 }
28
29 ActionDimAngular::~ActionDimAngular()
30 {
31 }
32
33 /*virtual*/ RS2::ActionType ActionDimAngular::rtti()
34 {
35         return RS2::ActionDimAngular;
36 }
37
38 void ActionDimAngular::reset()
39 {
40         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 ActionDimAngular::trigger()
49 {
50         ActionInterface::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("ActionDimAngular::trigger:"
80                         " Entity is NULL\n");
81 }
82
83 void ActionDimAngular::mouseMoveEvent(QMouseEvent * e)
84 {
85         RS_DEBUG->print("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 && line2 && 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("ActionDimAngular::mouseMoveEvent end");
119 }
120
121 void ActionDimAngular::mouseReleaseEvent(QMouseEvent * e)
122 {
123         if (e->button() == Qt::LeftButton)
124         {
125                 switch (getStatus())
126                 {
127                 case SetLine1:
128                 {
129                         RS_Entity * en = catchEntity(e, RS2::ResolveAll);
130
131                         if (en != NULL
132                             && en->rtti() == RS2::EntityLine)
133                         {
134                                 line1 = (RS_Line *)en;
135                                 setStatus(SetLine2);
136                         }
137                 }
138                 break;
139
140                 case SetLine2:
141                 {
142                         RS_Entity * en = catchEntity(e, RS2::ResolveAll);
143
144                         if (en != NULL
145                             && en->rtti() == RS2::EntityLine)
146                         {
147                                 line2 = (RS_Line *)en;
148
149                                 VectorSolutions sol =
150                                         RS_Information::getIntersectionLineLine(line1, line2);
151
152                                 if (sol.get(0).valid)
153                                 {
154                                         center = sol.get(0);
155
156                                         if (center.distanceTo(line1->getStartpoint())
157                                             < center.distanceTo(line1->getEndpoint()))
158                                         {
159                                                 edata.definitionPoint1 = line1->getStartpoint();
160                                                 edata.definitionPoint2 = line1->getEndpoint();
161                                         }
162                                         else
163                                         {
164                                                 edata.definitionPoint1 = line1->getEndpoint();
165                                                 edata.definitionPoint2 = line1->getStartpoint();
166                                         }
167
168                                         if (center.distanceTo(line2->getStartpoint())
169                                             < center.distanceTo(line2->getEndpoint()))
170                                         {
171                                                 edata.definitionPoint3 = line2->getStartpoint();
172                                                 data.definitionPoint = line2->getEndpoint();
173                                         }
174                                         else
175                                         {
176                                                 edata.definitionPoint3 = line2->getEndpoint();
177                                                 data.definitionPoint = line2->getStartpoint();
178                                         }
179                                         graphicView->moveRelativeZero(center);
180                                         setStatus(SetPos);
181                                 }
182                         }
183                 }
184                 break;
185
186                 case SetPos:
187                 {
188                         Vector ce(snapPoint(e));
189                         coordinateEvent(&ce);
190                 }
191                 break;
192                 }
193         }
194         else if (e->button() == Qt::RightButton)
195         {
196                 deletePreview();
197                 deleteSnapper();
198                 clearPreview();
199                 init(getStatus() - 1);
200         }
201 }
202
203 void ActionDimAngular::coordinateEvent(Vector * e)
204 {
205         if (!e)
206                 return;
207
208         switch (getStatus())
209         {
210         case SetPos:
211                 edata.definitionPoint4 = *e;
212                 trigger();
213                 reset();
214                 setStatus(SetLine1);
215                 break;
216
217         default:
218                 break;
219         }
220 }
221
222 void ActionDimAngular::commandEvent(RS_CommandEvent * e)
223 {
224         QString c = e->getCommand().toLower();
225
226         if (checkCommand("help", c))
227         {
228                 RS_DIALOGFACTORY->commandMessage(msgAvailableCommands()
229                         + getAvailableCommands().join(", "));
230                 return;
231         }
232
233         // setting new text label:
234         if (getStatus() == SetText)
235         {
236                 setText(c);
237                 RS_DIALOGFACTORY->requestOptions(this, true, true);
238                 graphicView->enableCoordinateInput();
239                 setStatus(lastStatus);
240                 return;
241         }
242
243         // command: text
244         if (checkCommand("text", c))
245         {
246                 lastStatus = (Status)getStatus();
247                 graphicView->disableCoordinateInput();
248                 setStatus(SetText);
249         }
250 }
251
252 QStringList ActionDimAngular::getAvailableCommands()
253 {
254         QStringList cmd;
255
256         switch (getStatus())
257         {
258         case SetLine1:
259         case SetLine2:
260         case SetPos:
261                 cmd += command("text");
262                 break;
263
264         default:
265                 break;
266         }
267
268         return cmd;
269 }
270
271 void ActionDimAngular::showOptions()
272 {
273         ActionInterface::showOptions();
274
275         RS_DIALOGFACTORY->requestOptions(this, true);
276 }
277
278 void ActionDimAngular::hideOptions()
279 {
280         ActionInterface::hideOptions();
281
282         RS_DIALOGFACTORY->requestOptions(this, false);
283 }
284
285 void ActionDimAngular::updateMouseButtonHints()
286 {
287         switch (getStatus())
288         {
289         case SetLine1:
290                 RS_DIALOGFACTORY->updateMouseWidget(tr("Select first line"),
291                         tr("Cancel"));
292                 break;
293
294         case SetLine2:
295                 RS_DIALOGFACTORY->updateMouseWidget(tr("Select second line"),
296                         tr("Cancel"));
297                 break;
298
299         case SetPos:
300                 RS_DIALOGFACTORY->updateMouseWidget(
301                         tr("Specify dimension arc line location"), tr("Cancel"));
302                 break;
303
304         case SetText:
305                 RS_DIALOGFACTORY->updateMouseWidget(tr("Enter dimension text:"), "");
306                 break;
307
308         default:
309                 RS_DIALOGFACTORY->updateMouseWidget("", "");
310                 break;
311         }
312 }