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