]> Shamusworld >> Repos - architektonas/blob - src/actions/rs_actiondimaligned.cpp
90401f4541d3949b78f53f84e93d2477d4096863
[architektonas] / src / actions / rs_actiondimaligned.cpp
1 // rs_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 "rs_actiondimaligned.h"
16
17 #include "rs_constructionline.h"
18 #include "rs_dialogfactory.h"
19 #include "rs_graphicview.h"
20 #include "rs_preview.h"
21 #include "rs_snapper.h"
22
23 RS_ActionDimAligned::RS_ActionDimAligned(RS_EntityContainer & container, RS_GraphicView & graphicView): RS_ActionDimension("Draw aligned dimensions",
24                 container, graphicView)
25 {
26         reset();
27 }
28
29 RS_ActionDimAligned::~RS_ActionDimAligned()
30 {
31 }
32
33 /*virtual*/ RS2::ActionType RS_ActionDimAligned::rtti()
34 {
35         return RS2::ActionDimAligned;
36 }
37
38 void RS_ActionDimAligned::reset()
39 {
40         RS_ActionDimension::reset();
41         edata = RS_DimAlignedData(Vector(false), Vector(false));
42         lastStatus = SetExtPoint1;
43
44         if (RS_DIALOGFACTORY != NULL)
45                 RS_DIALOGFACTORY->requestOptions(this, true, true);
46 }
47
48 void RS_ActionDimAligned::trigger()
49 {
50         RS_ActionDimension::trigger();
51         preparePreview();
52         graphicView->moveRelativeZero(data.definitionPoint);
53
54         //data.text = getText();
55         RS_DimAligned * dim = new RS_DimAligned(container, data, edata);
56         dim->setLayerToActive();
57         dim->setPenToActive();
58         dim->update();
59         container->addEntity(dim);
60
61         // upd. undo list:
62         if (document != NULL)
63         {
64                 document->startUndoCycle();
65                 document->addUndoable(dim);
66                 document->endUndoCycle();
67         }
68
69         deleteSnapper();
70         Vector rz = graphicView->getRelativeZero();
71         graphicView->moveRelativeZero(Vector(0.0, 0.0));
72         graphicView->drawEntity(dim);
73         graphicView->moveRelativeZero(rz);
74         drawSnapper();
75
76         RS_DEBUG->print("RS_ActionDimAligned::trigger(): dim added: %d", dim->getId());
77 }
78
79 void RS_ActionDimAligned::preparePreview()
80 {
81         Vector dirV;
82         dirV.setPolar(100.0, edata.extensionPoint1.angleTo(edata.extensionPoint2) + M_PI / 2.0);
83         RS_ConstructionLine cl(NULL, RS_ConstructionLineData(edata.extensionPoint2,
84                         edata.extensionPoint2 + dirV));
85         data.definitionPoint = cl.getNearestPointOnEntity(data.definitionPoint);
86 }
87
88 void RS_ActionDimAligned::mouseMoveEvent(QMouseEvent * e)
89 {
90         RS_DEBUG->print("RS_ActionDimAligned::mouseMoveEvent begin");
91         Vector mouse = snapPoint(e);
92
93         switch (getStatus())
94         {
95         case SetExtPoint1:
96                 break;
97
98         case SetExtPoint2:
99
100                 if (edata.extensionPoint1.valid)
101                 {
102                         deletePreview();
103                         clearPreview();
104                         preview->addEntity(new RS_Line(preview,
105                                         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("RS_ActionDimAligned::mouseMoveEvent end");
133 }
134
135 void RS_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 RS_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 RS_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 RS_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 RS_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 RS_ActionDimAligned::hideOptions()
271 {
272         if (RS_DIALOGFACTORY != NULL)
273                 RS_DIALOGFACTORY->requestOptions(this, false);
274
275         RS_ActionDimension::hideOptions();
276 }
277
278 void RS_ActionDimAligned::showOptions()
279 {
280         RS_ActionDimension::showOptions();
281
282         if (RS_DIALOGFACTORY != NULL)
283                 RS_DIALOGFACTORY->requestOptions(this, true);
284 }
285