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