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