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