]> Shamusworld >> Repos - architektonas/blob - src/actions/actiondimlinear.cpp
Bugfixes related to removing Snapper class.
[architektonas] / src / actions / actiondimlinear.cpp
1 // actiondimlinear.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  05/22/2010  Added this text. :-)
15 //
16
17 #include "actiondimlinear.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 /**
27  * Constructor.
28  *
29  * @param angle Initial angle in rad.
30  * @param fixedAngle true: The user can't change the angle.
31  *                   false: The user can change the angle in a option widget.
32  */
33 ActionDimLinear::ActionDimLinear(EntityContainer & container,
34         GraphicView & graphicView, double angle, bool fixedAngle):
35         ActionDimension("Draw linear dimensions", container, graphicView)
36 {
37         edata.angle = angle;
38         this->fixedAngle = fixedAngle;
39         lastStatus = SetExtPoint1;
40         //hm. doesn't work.
41 //      graphicView.snapper.SetVisible();
42         graphicView.SetSnapperVisible();
43         reset();
44 }
45
46 ActionDimLinear::~ActionDimLinear()
47 {
48 }
49
50 /*virtual*/ RS2::ActionType ActionDimLinear::rtti()
51 {
52         return RS2::ActionDimLinear;
53 }
54
55 void ActionDimLinear::reset()
56 {
57         ActionDimension::reset();
58         edata = DimLinearData(Vector(false), Vector(false), (fixedAngle ? edata.angle : 0.0), 0.0);
59
60         if (DIALOGFACTORY)
61                 DIALOGFACTORY->requestOptions(this, true, true);
62 }
63
64 void ActionDimLinear::trigger()
65 {
66         ActionDimension::trigger();
67         preparePreview();
68         DimLinear * dim = new DimLinear(container, data, edata);
69         dim->setLayerToActive();
70         dim->setPenToActive();
71         dim->update();
72         container->addEntity(dim);
73
74         // upd. undo list:
75         if (document)
76         {
77                 document->startUndoCycle();
78                 document->addUndoable(dim);
79                 document->endUndoCycle();
80         }
81
82 //      deleteSnapper();
83 //      Vector rz = graphicView->getRelativeZero();
84 //      graphicView->moveRelativeZero(Vector(0.0, 0.0));
85 //      graphicView->drawEntity(dim);
86 //      graphicView->moveRelativeZero(rz);
87 //      drawSnapper();
88         graphicView->SetSnapperVisible(false);
89         graphicView->redraw();
90
91         DEBUG->print("ActionDimLinear::trigger(): dim added: %d", dim->getId());
92 }
93
94 void ActionDimLinear::preparePreview()
95 {
96         Vector dirV;
97         dirV.setPolar(100.0, edata.angle + M_PI / 2.0);
98
99         ConstructionLine cl(NULL, ConstructionLineData(edata.extensionPoint2,
100                 edata.extensionPoint2 + dirV));
101
102         data.definitionPoint = cl.getNearestPointOnEntity(data.definitionPoint);
103 }
104
105 void ActionDimLinear::mouseMoveEvent(QMouseEvent * e)
106 {
107         DEBUG->print("ActionDimLinear::mouseMoveEvent begin");
108
109         Vector mouse = snapPoint(e);
110
111         switch (getStatus())
112         {
113         case SetExtPoint1:
114 //              graphicView->snapper.SetVisible();
115 //              graphicView->redraw();
116                 break;
117
118         case SetExtPoint2:
119
120                 if (edata.extensionPoint1.valid)
121                 {
122 //                      deletePreview();
123 //                      clearPreview();
124 //                      preview->addEntity(new Line(preview,
125 //                              LineData(edata.extensionPoint1, mouse)));
126 //                      drawPreview();
127                         graphicView->preview.clear();
128                         graphicView->preview.addEntity(new Line(&(graphicView->preview),
129                                 LineData(edata.extensionPoint1, mouse)));
130                         graphicView->preview.SetVisible();
131                         graphicView->redraw();
132                 }
133
134                 break;
135
136         case SetDefPoint:
137
138                 if (edata.extensionPoint1.valid && edata.extensionPoint2.valid)
139                 {
140 //                      deletePreview();
141 //                      clearPreview();
142                         data.definitionPoint = mouse;
143 //                      preparePreview();
144 //                      DimLinear * dim = new DimLinear(preview, data, edata);
145 //                      dim->update();
146 //                      preview->addEntity(dim);
147 //                      drawPreview();
148                         graphicView->preview.clear();
149                         preparePreview();
150                         DimLinear * dim = new DimLinear(&(graphicView->preview), data, edata);
151                         dim->update();
152                         graphicView->preview.addEntity(dim);
153                         graphicView->preview.SetVisible();
154                         graphicView->redraw();
155                 }
156
157                 break;
158         }
159
160         DEBUG->print("ActionDimLinear::mouseMoveEvent end");
161 }
162
163 void ActionDimLinear::mouseReleaseEvent(QMouseEvent * e)
164 {
165         if (e->button() == Qt::LeftButton)
166         {
167                 Vector ce(snapPoint(e));
168                 coordinateEvent(&ce);
169 //              graphicView->snapper.SetVisible();
170                 graphicView->SetSnapperVisible();
171         }
172         else if (e->button() == Qt::RightButton)
173         {
174                 if (getStatus() == 0)
175                 {
176                         graphicView->preview.SetVisible(false);
177 //                      graphicView->snapper.SetVisible(false);
178                         graphicView->SetSnapperVisible(false);
179                 }
180
181 //              deletePreview();
182 //              deleteSnapper();
183                 init(getStatus() - 1);
184         }
185 }
186
187 void ActionDimLinear::coordinateEvent(Vector * e)
188 {
189         if (!e)
190                 return;
191
192         Vector pos = *e;
193
194         switch (getStatus())
195         {
196         case SetExtPoint1:
197                 edata.extensionPoint1 = pos;
198                 graphicView->moveRelativeZero(pos);
199                 setStatus(SetExtPoint2);
200                 break;
201
202         case SetExtPoint2:
203                 edata.extensionPoint2 = pos;
204                 graphicView->moveRelativeZero(pos);
205                 setStatus(SetDefPoint);
206                 break;
207
208         case SetDefPoint:
209                 data.definitionPoint = pos;
210                 trigger();
211                 reset();
212                 setStatus(SetExtPoint1);
213                 break;
214
215         default:
216                 break;
217         }
218 }
219
220 void ActionDimLinear::commandEvent(CommandEvent * e)
221 {
222         QString c = e->getCommand().toLower();
223
224         if (checkCommand("help", c))
225         {
226                 if (DIALOGFACTORY)
227                         DIALOGFACTORY->commandMessage(msgAvailableCommands()
228                                 + getAvailableCommands().join(", "));
229                 return;
230         }
231
232         switch (getStatus())
233         {
234         case SetText:
235                 setText(c);
236
237                 if (DIALOGFACTORY)
238                         DIALOGFACTORY->requestOptions(this, true, true);
239
240                 graphicView->enableCoordinateInput();
241                 setStatus(lastStatus);
242                 break;
243
244         case SetAngle:
245         {
246                 bool ok;
247                 double a = Math::eval(c, &ok);
248
249                 if (ok)
250                         setAngle(Math::deg2rad(a));
251                 else if (DIALOGFACTORY)
252                         DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
253
254                 if (DIALOGFACTORY)
255                         DIALOGFACTORY->requestOptions(this, true, true);
256
257                 setStatus(lastStatus);
258         }
259                 break;
260
261         default:
262                 lastStatus = (Status)getStatus();
263 //              deleteSnapper();
264 //              deletePreview();
265 //              clearPreview();
266
267                 if (checkCommand("text", c))
268                 {
269                         graphicView->disableCoordinateInput();
270                         setStatus(SetText);
271                         return;
272                 }
273                 else if (!fixedAngle && (checkCommand("angle", c)))
274                         setStatus(SetAngle);
275
276                 break;
277         }
278 }
279
280 QStringList ActionDimLinear::getAvailableCommands()
281 {
282         QStringList cmd;
283
284         switch (getStatus())
285         {
286         case SetExtPoint1:
287         case SetExtPoint2:
288         case SetDefPoint:
289                 cmd += command("text");
290
291                 if (!fixedAngle)
292                         cmd += command("angle");
293
294                 break;
295
296         default:
297                 break;
298         }
299
300         return cmd;
301 }
302
303 void ActionDimLinear::updateMouseButtonHints()
304 {
305         if (DIALOGFACTORY)
306         {
307                 switch (getStatus())
308                 {
309                 case SetExtPoint1:
310                         DIALOGFACTORY->updateMouseWidget(
311                                 tr("Specify first extension line origin"), tr("Cancel"));
312                         break;
313
314                 case SetExtPoint2:
315                         DIALOGFACTORY->updateMouseWidget(
316                                 tr("Specify second extension line origin"), tr("Back"));
317                         break;
318
319                 case SetDefPoint:
320                         DIALOGFACTORY->updateMouseWidget(
321                                 tr("Specify dimension line location"), tr("Back"));
322                         break;
323
324                 case SetText:
325                         DIALOGFACTORY->updateMouseWidget(tr("Enter dimension text:"), "");
326                         break;
327
328                 case SetAngle:
329                         DIALOGFACTORY->updateMouseWidget(tr("Enter dimension line angle:"), "");
330                         break;
331
332                 default:
333                         DIALOGFACTORY->updateMouseWidget("", "");
334                         break;
335                 }
336         }
337 }
338
339 void ActionDimLinear::showOptions()
340 {
341         ActionInterface::showOptions();
342
343         if (DIALOGFACTORY)
344                 DIALOGFACTORY->requestOptions(this, true, true);
345 }
346
347 void ActionDimLinear::hideOptions()
348 {
349         ActionInterface::hideOptions();
350
351         if (DIALOGFACTORY)
352                 DIALOGFACTORY->requestOptions(this, false);
353 }
354
355 double ActionDimLinear::getAngle()
356 {
357         return edata.angle;
358 }
359
360 void ActionDimLinear::setAngle(double a)
361 {
362         edata.angle = a;
363 }
364
365 bool ActionDimLinear::hasFixedAngle()
366 {
367         return fixedAngle;
368 }