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