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