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