]> Shamusworld >> Repos - architektonas/blob - src/actions/rs_actiondimaligned.cpp
Refactoring: Moved RS_GraphicView to GraphicView.
[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_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 RS_ActionDimAligned::RS_ActionDimAligned(RS_EntityContainer & container, GraphicView & graphicView): RS_ActionDimension("Draw aligned dimensions",
25                 container, graphicView)
26 {
27         reset();
28 }
29
30 RS_ActionDimAligned::~RS_ActionDimAligned()
31 {
32 }
33
34 /*virtual*/ RS2::ActionType RS_ActionDimAligned::rtti()
35 {
36         return RS2::ActionDimAligned;
37 }
38
39 void RS_ActionDimAligned::reset()
40 {
41         RS_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 RS_ActionDimAligned::trigger()
50 {
51         RS_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("RS_ActionDimAligned::trigger(): dim added: %d", dim->getId());
78 }
79
80 void RS_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 RS_ActionDimAligned::mouseMoveEvent(QMouseEvent * e)
90 {
91         RS_DEBUG->print("RS_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,
106                                         RS_LineData(edata.extensionPoint1, mouse)));
107                         drawPreview();
108                 }
109                 break;
110
111         case SetDefPoint:
112
113                 if (edata.extensionPoint1.valid && edata.extensionPoint2.valid)
114                 {
115                         deletePreview();
116                         clearPreview();
117                         data.definitionPoint = mouse;
118
119                         preparePreview();
120
121                         //data.text = getText();
122                         RS_DimAligned * dim = new RS_DimAligned(preview, data, edata);
123                         dim->update();
124                         preview->addEntity(dim);
125                         drawPreview();
126                 }
127                 break;
128
129         default:
130                 break;
131         }
132
133         RS_DEBUG->print("RS_ActionDimAligned::mouseMoveEvent end");
134 }
135
136 void RS_ActionDimAligned::mouseReleaseEvent(QMouseEvent * e)
137 {
138 //      if (RS2::qtToRsButtonState(e->button())==RS2::LeftButton)
139         if (e->button() == Qt::LeftButton)
140         {
141                 Vector ce(snapPoint(e));
142                 coordinateEvent(&ce);
143         }
144 //      else if (RS2::qtToRsButtonState(e->button())==RS2::RightButton)
145         else if (e->button() == Qt::RightButton)
146         {
147                 deletePreview();
148                 deleteSnapper();
149                 init(getStatus() - 1);
150         }
151 }
152
153 void RS_ActionDimAligned::coordinateEvent(Vector * e)
154 {
155         if (e == NULL)
156                 return;
157
158         Vector pos = *e; //->getCoordinate();
159
160         switch (getStatus())
161         {
162         case SetExtPoint1:
163                 edata.extensionPoint1 = pos;
164                 graphicView->moveRelativeZero(pos);
165                 setStatus(SetExtPoint2);
166                 break;
167
168         case SetExtPoint2:
169                 edata.extensionPoint2 = pos;
170                 graphicView->moveRelativeZero(pos);
171                 setStatus(SetDefPoint);
172                 break;
173
174         case SetDefPoint:
175                 data.definitionPoint = pos;
176                 trigger();
177                 reset();
178                 setStatus(SetExtPoint1);
179                 break;
180
181         default:
182                 break;
183         }
184 }
185
186 void RS_ActionDimAligned::commandEvent(RS_CommandEvent * e)
187 {
188         QString c = e->getCommand().toLower();
189
190         if (checkCommand("help", c))
191         {
192                 if (RS_DIALOGFACTORY != NULL)
193                         RS_DIALOGFACTORY->commandMessage(msgAvailableCommands()
194                                 + getAvailableCommands().join(", "));
195
196                 return;
197         }
198
199         switch (getStatus())
200         {
201         case SetText:
202                 setText(c);
203
204                 if (RS_DIALOGFACTORY != NULL)
205                         RS_DIALOGFACTORY->requestOptions(this, true, true);
206
207                 setStatus(lastStatus);
208                 graphicView->enableCoordinateInput();
209                 break;
210
211         default:
212
213                 if (checkCommand("text", c))
214                 {
215                         lastStatus = (Status)getStatus();
216                         graphicView->disableCoordinateInput();
217                         setStatus(SetText);
218                 }
219                 break;
220         }
221 }
222
223 QStringList RS_ActionDimAligned::getAvailableCommands()
224 {
225         QStringList cmd;
226
227         switch (getStatus())
228         {
229         case SetExtPoint1:
230         case SetExtPoint2:
231         case SetDefPoint:
232                 cmd += command("text");
233                 break;
234
235         default:
236                 break;
237         }
238
239         return cmd;
240 }
241
242 void RS_ActionDimAligned::updateMouseButtonHints()
243 {
244         if (RS_DIALOGFACTORY != NULL)
245         {
246                 switch (getStatus())
247                 {
248                 case SetExtPoint1:
249                         RS_DIALOGFACTORY->updateMouseWidget(tr("Specify first extension line origin"), tr("Cancel"));
250                         break;
251
252                 case SetExtPoint2:
253                         RS_DIALOGFACTORY->updateMouseWidget(tr("Specify second extension line origin"), tr("Back"));
254                         break;
255
256                 case SetDefPoint:
257                         RS_DIALOGFACTORY->updateMouseWidget(tr("Specify dimension line location"), tr("Back"));
258                         break;
259
260                 case SetText:
261                         RS_DIALOGFACTORY->updateMouseWidget(tr("Enter dimension text:"), "");
262                         break;
263
264                 default:
265                         RS_DIALOGFACTORY->updateMouseWidget("", "");
266                         break;
267                 }
268         }
269 }
270
271 void RS_ActionDimAligned::hideOptions()
272 {
273         if (RS_DIALOGFACTORY != NULL)
274                 RS_DIALOGFACTORY->requestOptions(this, false);
275
276         RS_ActionDimension::hideOptions();
277 }
278
279 void RS_ActionDimAligned::showOptions()
280 {
281         RS_ActionDimension::showOptions();
282
283         if (RS_DIALOGFACTORY != NULL)
284                 RS_DIALOGFACTORY->requestOptions(this, true);
285 }
286