]> Shamusworld >> Repos - architektonas/blob - src/actions/actiondrawlineangle.cpp
Bugfixes related to removing Snapper class.
[architektonas] / src / actions / actiondrawlineangle.cpp
1 // actiondrawlineangle.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 // JLH  06/03/2010  Moved implementation from header to this file
16 //
17
18 #include "actiondrawlineangle.h"
19
20 #include "commandevent.h"
21 #include "debug.h"
22 #include "dialogfactory.h"
23 #include "graphicview.h"
24 #include "preview.h"
25
26 ActionDrawLineAngle::ActionDrawLineAngle(EntityContainer & container,
27         GraphicView & graphicView, double a, bool fa):
28         ActionInterface("Draw lines with given angle", container, graphicView),
29         pos(Vector(false)), angle(a), length(1.0), fixedAngle(fa), snpPoint(0)
30 {
31         reset();
32 }
33
34 ActionDrawLineAngle::~ActionDrawLineAngle()
35 {
36 }
37
38 /*virtual*/ RS2::ActionType ActionDrawLineAngle::rtti()
39 {
40         return RS2::ActionDrawLineAngle;
41 }
42
43 void ActionDrawLineAngle::reset()
44 {
45         data = LineData(Vector(false), Vector(false));
46 }
47
48 void ActionDrawLineAngle::init(int status)
49 {
50         ActionInterface::init(status);
51         reset();
52 }
53
54 void ActionDrawLineAngle::trigger()
55 {
56         ActionInterface::trigger();
57         preparePreview();
58         Line * line = new Line(container, data);
59         line->setLayerToActive();
60         line->setPenToActive();
61         container->addEntity(line);
62
63         // Update undo list:
64         if (document)
65         {
66                 document->startUndoCycle();
67                 document->addUndoable(line);
68                 document->endUndoCycle();
69         }
70
71 //      deleteSnapper();
72         graphicView->moveRelativeZero(Vector(0.0, 0.0));
73         graphicView->drawEntity(line);
74         graphicView->moveRelativeZero(data.startpoint);
75         graphicView->redraw();
76         DEBUG->print("ActionDrawLineAngle::trigger(): line added: %d", line->getId());
77 }
78
79 void ActionDrawLineAngle::mouseMoveEvent(QMouseEvent * e)
80 {
81         DEBUG->print("ActionDrawLineAngle::mouseMoveEvent begin");
82
83         if (getStatus() == SetPos)
84         {
85                 pos = snapPoint(e);
86 //              deletePreview();
87 //              clearPreview();
88 //              preparePreview();
89 //              preview->addEntity(new Line(preview, data));
90 //              drawPreview();
91                 preparePreview();
92                 graphicView->preview.clear();
93                 graphicView->preview.addEntity(new Line(&(graphicView->preview), data));
94         }
95
96         graphicView->redraw();
97         DEBUG->print("ActionDrawLineAngle::mouseMoveEvent end");
98 }
99
100 void ActionDrawLineAngle::mouseReleaseEvent(QMouseEvent * e)
101 {
102         if (e->button() == Qt::LeftButton)
103         {
104                 if (getStatus() == SetPos)
105                 {
106 //                      Vector ce(snapPoint(e));
107                         Vector ce(graphicView->SnapPoint(e));
108                         coordinateEvent(&ce);
109                 }
110         }
111         else if (e->button() == Qt::RightButton)
112         {
113 //              deletePreview();
114 //              deleteSnapper();
115                 init(getStatus() - 1);
116         }
117
118         graphicView->preview.clear();                           // Remove entities from container
119         graphicView->redraw();
120 }
121
122 void ActionDrawLineAngle::preparePreview()
123 {
124         Vector p1, p2;
125
126         // End:
127         if (snpPoint == 2)
128                 p2.setPolar(length * -1, angle);
129         else
130                 p2.setPolar(length, angle);
131
132         // Middle:
133         if (snpPoint == 1)
134                 p1 = pos - (p2 / 2);
135         else
136                 p1 = pos;
137
138         p2 += p1;
139         data = LineData(p1, p2);
140 }
141
142 void ActionDrawLineAngle::coordinateEvent(Vector * e)
143 {
144         if (!e)
145                 return;
146
147         switch (getStatus())
148         {
149         case SetPos:
150                 pos = *e;
151                 trigger();
152                 break;
153
154         default:
155                 break;
156         }
157 }
158
159 void ActionDrawLineAngle::commandEvent(CommandEvent * e)
160 {
161         QString c = e->getCommand().toLower();
162
163         if (checkCommand("help", c))
164         {
165                 DIALOGFACTORY->commandMessage(msgAvailableCommands()
166                         + getAvailableCommands().join(", "));
167                 return;
168         }
169
170         switch (getStatus())
171         {
172         case SetPos:
173                 if (!fixedAngle && checkCommand("angle", c))
174                 {
175 //                      deleteSnapper();
176 //                      deletePreview();
177 //                      clearPreview();
178                         setStatus(SetAngle);
179                 }
180                 else if (checkCommand("length", c))
181                 {
182 //                      deleteSnapper();
183 //                      deletePreview();
184 //                      clearPreview();
185                         setStatus(SetLength);
186                 }
187                 break;
188
189         case SetAngle:
190         {
191                 bool ok;
192                 double a = Math::eval(c, &ok);
193
194                 if (ok)
195                         angle = Math::deg2rad(a);
196                 else
197                         DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
198
199                 DIALOGFACTORY->requestOptions(this, true, true);
200                 setStatus(SetPos);
201         }
202                 break;
203
204         case SetLength:
205         {
206                 bool ok;
207                 double l = Math::eval(c, &ok);
208
209                 if (ok)
210                         length = l;
211                 else
212                         DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
213
214                 DIALOGFACTORY->requestOptions(this, true, true);
215                 setStatus(SetPos);
216         }
217                 break;
218
219         default:
220                 break;
221         }
222 }
223
224 QStringList ActionDrawLineAngle::getAvailableCommands()
225 {
226         QStringList cmd;
227
228         switch (getStatus())
229         {
230         case SetPos:
231                 if (!fixedAngle)
232                         cmd += command("angle");
233
234                 cmd += command("length");
235                 break;
236
237         default:
238                 break;
239         }
240
241         return cmd;
242 }
243
244 void ActionDrawLineAngle::updateMouseButtonHints()
245 {
246         switch (getStatus())
247         {
248         case SetPos:
249                 DIALOGFACTORY->updateMouseWidget(tr("Specify position"), tr("Cancel"));
250                 break;
251
252         case SetAngle:
253                 DIALOGFACTORY->updateMouseWidget(tr("Enter angle:"), tr("Back"));
254                 break;
255
256         case SetLength:
257                 DIALOGFACTORY->updateMouseWidget(tr("Enter length:"), tr("Back"));
258                 break;
259
260         default:
261                 break;
262         }
263 }
264
265 void ActionDrawLineAngle::showOptions()
266 {
267         ActionInterface::showOptions();
268         DIALOGFACTORY->requestOptions(this, true);
269 }
270
271 void ActionDrawLineAngle::hideOptions()
272 {
273         ActionInterface::hideOptions();
274         DIALOGFACTORY->requestOptions(this, false);
275 }
276
277 void ActionDrawLineAngle::updateMouseCursor()
278 {
279         graphicView->setMouseCursor(RS2::CadCursor);
280 }
281
282 void ActionDrawLineAngle::updateToolBar()
283 {
284         if (!isFinished())
285                 DIALOGFACTORY->requestToolBar(RS2::ToolBarSnap);
286         else
287                 DIALOGFACTORY->requestToolBar(RS2::ToolBarLines);
288 }
289
290 void ActionDrawLineAngle::setSnapPoint(int sp)
291 {
292         snpPoint = sp;
293 //      graphicView->redraw();
294 }
295
296 int ActionDrawLineAngle::getSnapPoint()
297 {
298         return snpPoint;
299 }
300
301 void ActionDrawLineAngle::setAngle(double a)
302 {
303         angle = a;
304 //      graphicView->redraw();
305 }
306
307 double ActionDrawLineAngle::getAngle()
308 {
309         return angle;
310 }
311
312 void ActionDrawLineAngle::setLength(double l)
313 {
314         length = l;
315 //      graphicView->redraw();
316 }
317
318 double ActionDrawLineAngle::getLength()
319 {
320         return length;
321 }
322
323 bool ActionDrawLineAngle::hasFixedAngle()
324 {
325         return fixedAngle;
326 }