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