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