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