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