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