]> Shamusworld >> Repos - architektonas/blob - src/actions/actiondimaligned.cpp
464d63a803ed8b9ad4e740c310626d349070949a
[architektonas] / src / actions / actiondimaligned.cpp
1 // actiondimaligned.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 "actiondimaligned.h"
16
17 #include "rs_commandevent.h"
18 #include "rs_constructionline.h"
19 #include "rs_dialogfactory.h"
20 #include "graphicview.h"
21 #include "rs_preview.h"
22 #include "rs_snapper.h"
23
24 ActionDimAligned::ActionDimAligned(RS_EntityContainer & container, GraphicView & graphicView): ActionDimension("Draw aligned dimensions",
25                 container, graphicView)
26 {
27         reset();
28 }
29
30 ActionDimAligned::~ActionDimAligned()
31 {
32 }
33
34 /*virtual*/ RS2::ActionType ActionDimAligned::rtti()
35 {
36         return RS2::ActionDimAligned;
37 }
38
39 void ActionDimAligned::reset()
40 {
41         ActionDimension::reset();
42         edata = RS_DimAlignedData(Vector(false), Vector(false));
43         lastStatus = SetExtPoint1;
44
45         if (RS_DIALOGFACTORY != NULL)
46                 RS_DIALOGFACTORY->requestOptions(this, true, true);
47 }
48
49 void ActionDimAligned::trigger()
50 {
51         ActionDimension::trigger();
52         preparePreview();
53         graphicView->moveRelativeZero(data.definitionPoint);
54
55         //data.text = getText();
56         RS_DimAligned * dim = new RS_DimAligned(container, data, edata);
57         dim->setLayerToActive();
58         dim->setPenToActive();
59         dim->update();
60         container->addEntity(dim);
61
62         // upd. undo list:
63         if (document != NULL)
64         {
65                 document->startUndoCycle();
66                 document->addUndoable(dim);
67                 document->endUndoCycle();
68         }
69
70         deleteSnapper();
71         Vector rz = graphicView->getRelativeZero();
72         graphicView->moveRelativeZero(Vector(0.0, 0.0));
73         graphicView->drawEntity(dim);
74         graphicView->moveRelativeZero(rz);
75         drawSnapper();
76
77         RS_DEBUG->print("ActionDimAligned::trigger(): dim added: %d", dim->getId());
78 }
79
80 void ActionDimAligned::preparePreview()
81 {
82         Vector dirV;
83         dirV.setPolar(100.0, edata.extensionPoint1.angleTo(edata.extensionPoint2) + M_PI / 2.0);
84         RS_ConstructionLine cl(NULL, RS_ConstructionLineData(edata.extensionPoint2,
85                 edata.extensionPoint2 + dirV));
86         data.definitionPoint = cl.getNearestPointOnEntity(data.definitionPoint);
87 }
88
89 void ActionDimAligned::mouseMoveEvent(QMouseEvent * e)
90 {
91         RS_DEBUG->print("ActionDimAligned::mouseMoveEvent begin");
92         Vector mouse = snapPoint(e);
93
94         switch (getStatus())
95         {
96         case SetExtPoint1:
97                 break;
98
99         case SetExtPoint2:
100
101                 if (edata.extensionPoint1.valid)
102                 {
103                         deletePreview();
104                         clearPreview();
105 //                      preview->addEntity(new RS_Line(preview, RS_LineData(edata.extensionPoint1, mouse)));
106                         drawPreview();
107                 }
108                 break;
109
110         case SetDefPoint:
111
112                 if (edata.extensionPoint1.valid && edata.extensionPoint2.valid)
113                 {
114                         deletePreview();
115                         clearPreview();
116                         data.definitionPoint = mouse;
117
118                         preparePreview();
119
120                         //data.text = getText();
121 //                      RS_DimAligned * dim = new RS_DimAligned(preview, data, edata);
122 //                      dim->update();
123 //                      preview->addEntity(dim);
124                         drawPreview();
125                 }
126                 break;
127
128         default:
129                 break;
130         }
131
132         RS_DEBUG->print("ActionDimAligned::mouseMoveEvent end");
133 }
134
135 void ActionDimAligned::mouseReleaseEvent(QMouseEvent * e)
136 {
137 //      if (RS2::qtToRsButtonState(e->button())==RS2::LeftButton)
138         if (e->button() == Qt::LeftButton)
139         {
140                 Vector ce(snapPoint(e));
141                 coordinateEvent(&ce);
142         }
143 //      else if (RS2::qtToRsButtonState(e->button())==RS2::RightButton)
144         else if (e->button() == Qt::RightButton)
145         {
146                 deletePreview();
147                 deleteSnapper();
148                 init(getStatus() - 1);
149         }
150 }
151
152 void ActionDimAligned::coordinateEvent(Vector * e)
153 {
154         if (e == NULL)
155                 return;
156
157         Vector pos = *e; //->getCoordinate();
158
159         switch (getStatus())
160         {
161         case SetExtPoint1:
162                 edata.extensionPoint1 = pos;
163                 graphicView->moveRelativeZero(pos);
164                 setStatus(SetExtPoint2);
165                 break;
166
167         case SetExtPoint2:
168                 edata.extensionPoint2 = pos;
169                 graphicView->moveRelativeZero(pos);
170                 setStatus(SetDefPoint);
171                 break;
172
173         case SetDefPoint:
174                 data.definitionPoint = pos;
175                 trigger();
176                 reset();
177                 setStatus(SetExtPoint1);
178                 break;
179
180         default:
181                 break;
182         }
183 }
184
185 void ActionDimAligned::commandEvent(RS_CommandEvent * e)
186 {
187         QString c = e->getCommand().toLower();
188
189         if (checkCommand("help", c))
190         {
191                 if (RS_DIALOGFACTORY != NULL)
192                         RS_DIALOGFACTORY->commandMessage(msgAvailableCommands()
193                                 + getAvailableCommands().join(", "));
194
195                 return;
196         }
197
198         switch (getStatus())
199         {
200         case SetText:
201                 setText(c);
202
203                 if (RS_DIALOGFACTORY != NULL)
204                         RS_DIALOGFACTORY->requestOptions(this, true, true);
205
206                 setStatus(lastStatus);
207                 graphicView->enableCoordinateInput();
208                 break;
209
210         default:
211
212                 if (checkCommand("text", c))
213                 {
214                         lastStatus = (Status)getStatus();
215                         graphicView->disableCoordinateInput();
216                         setStatus(SetText);
217                 }
218                 break;
219         }
220 }
221
222 QStringList ActionDimAligned::getAvailableCommands()
223 {
224         QStringList cmd;
225
226         switch (getStatus())
227         {
228         case SetExtPoint1:
229         case SetExtPoint2:
230         case SetDefPoint:
231                 cmd += command("text");
232                 break;
233
234         default:
235                 break;
236         }
237
238         return cmd;
239 }
240
241 void ActionDimAligned::updateMouseButtonHints()
242 {
243         if (RS_DIALOGFACTORY != NULL)
244         {
245                 switch (getStatus())
246                 {
247                 case SetExtPoint1:
248                         RS_DIALOGFACTORY->updateMouseWidget(tr("Specify first extension line origin"), tr("Cancel"));
249                         break;
250
251                 case SetExtPoint2:
252                         RS_DIALOGFACTORY->updateMouseWidget(tr("Specify second extension line origin"), tr("Back"));
253                         break;
254
255                 case SetDefPoint:
256                         RS_DIALOGFACTORY->updateMouseWidget(tr("Specify dimension line location"), tr("Back"));
257                         break;
258
259                 case SetText:
260                         RS_DIALOGFACTORY->updateMouseWidget(tr("Enter dimension text:"), "");
261                         break;
262
263                 default:
264                         RS_DIALOGFACTORY->updateMouseWidget("", "");
265                         break;
266                 }
267         }
268 }
269
270 void ActionDimAligned::hideOptions()
271 {
272         if (RS_DIALOGFACTORY != NULL)
273                 RS_DIALOGFACTORY->requestOptions(this, false);
274
275         ActionDimension::hideOptions();
276 }
277
278 void ActionDimAligned::showOptions()
279 {
280         ActionDimension::showOptions();
281
282         if (RS_DIALOGFACTORY != NULL)
283                 RS_DIALOGFACTORY->requestOptions(this, true);
284 }
285