]> Shamusworld >> Repos - architektonas/blob - src/actions/actiondrawcircle.cpp
Last checkin before major refactor...
[architektonas] / src / actions / actiondrawcircle.cpp
1 // actiondrawcircle.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  06/03/2010  Added this text. :-)
13 //
14
15 #include "actiondrawcircle.h"
16
17 #include "rs_commandevent.h"
18 #include "rs_dialogfactory.h"
19 #include "graphicview.h"
20 #include "rs_preview.h"
21
22 ActionDrawCircle::ActionDrawCircle(RS_EntityContainer & container, GraphicView & graphicView):
23         ActionInterface("Draw circles", container, graphicView)
24 {
25         reset();
26 }
27
28 ActionDrawCircle::~ActionDrawCircle()
29 {
30 }
31
32 /*virtual*/ RS2::ActionType ActionDrawCircle::rtti()
33 {
34         return RS2::ActionDrawCircle;
35 }
36
37 void ActionDrawCircle::reset()
38 {
39         data = RS_CircleData(Vector(false), 0.0);
40 }
41
42 void ActionDrawCircle::init(int status)
43 {
44         ActionInterface::init(status);
45
46         reset();
47 }
48
49 void ActionDrawCircle::trigger()
50 {
51         ActionInterface::trigger();
52
53         RS_Circle * circle = new RS_Circle(container, data);
54         circle->setLayerToActive();
55         circle->setPenToActive();
56         container->addEntity(circle);
57
58         // upd. undo list:
59         if (document != NULL)
60         {
61                 document->startUndoCycle();
62                 document->addUndoable(circle);
63                 document->endUndoCycle();
64         }
65
66         deleteSnapper();
67         Vector rz = graphicView->getRelativeZero();
68         graphicView->moveRelativeZero(Vector(0.0, 0.0));
69         graphicView->drawEntity(circle);
70         graphicView->moveRelativeZero(circle->getCenter());
71         drawSnapper();
72
73         setStatus(SetCenter);
74         reset();
75
76         RS_DEBUG->print("ActionDrawCircle::trigger(): circle added: %d", circle->getId());
77 }
78
79 void ActionDrawCircle::mouseMoveEvent(QMouseEvent * e)
80 {
81         RS_DEBUG->print("ActionDrawCircle::mouseMoveEvent begin");
82
83         Vector mouse = snapPoint(e);
84
85         switch (getStatus())
86         {
87         case SetCenter:
88                 data.center = mouse;
89                 break;
90
91         case SetRadius:
92
93                 if (data.center.valid)
94                 {
95                         data.radius = data.center.distanceTo(mouse);
96                         deletePreview();
97                         clearPreview();
98 //                      preview->addEntity(new RS_Circle(preview, data));
99                         drawPreview();
100                 }
101                 break;
102         }
103
104         RS_DEBUG->print("ActionDrawCircle::mouseMoveEvent end");
105 }
106
107 void ActionDrawCircle::mouseReleaseEvent(QMouseEvent * e)
108 {
109         if (e->button() == Qt::LeftButton)
110         {
111                 Vector ce(snapPoint(e));
112                 coordinateEvent(&ce);
113         }
114         else if (e->button() == Qt::RightButton)
115         {
116                 deletePreview();
117                 deleteSnapper();
118                 init(getStatus() - 1);
119         }
120 }
121
122 void ActionDrawCircle::coordinateEvent(Vector * e)
123 {
124         if (!e)
125                 return;
126
127         Vector mouse = *e;
128
129         switch (getStatus())
130         {
131         case SetCenter:
132                 data.center = mouse;
133                 graphicView->moveRelativeZero(mouse);
134                 setStatus(SetRadius);
135                 break;
136
137         case SetRadius:
138
139                 if (data.center.valid)
140                 {
141                         graphicView->moveRelativeZero(mouse);
142                         data.radius = data.center.distanceTo(mouse);
143                         trigger();
144                 }
145                 //setStatus(SetCenter);
146                 break;
147
148         default:
149                 break;
150         }
151 }
152
153 void ActionDrawCircle::commandEvent(RS_CommandEvent * e)
154 {
155         QString c = e->getCommand().toLower();
156
157         if (checkCommand("help", c))
158         {
159                 if (RS_DIALOGFACTORY)
160                         RS_DIALOGFACTORY->commandMessage(msgAvailableCommands()
161                                 + getAvailableCommands().join(", "));
162                 return;
163         }
164
165         switch (getStatus())
166         {
167         case SetRadius:
168         {
169                 bool ok;
170                 double r = RS_Math::eval(c, &ok);
171
172                 if (ok)
173                         data.radius = r;
174                 else if (RS_DIALOGFACTORY)
175                         RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
176
177                 trigger();
178                 //setStatus(SetCenter);
179         }
180                 break;
181
182         default:
183                 break;
184         }
185 }
186
187 QStringList ActionDrawCircle::getAvailableCommands()
188 {
189         QStringList cmd;
190         return cmd;
191 }
192
193 void ActionDrawCircle::updateMouseButtonHints()
194 {
195         switch (getStatus())
196         {
197         case SetCenter:
198                 if (RS_DIALOGFACTORY)
199                         RS_DIALOGFACTORY->updateMouseWidget(tr("Specify center"), tr("Cancel"));
200                 break;
201
202         case SetRadius:
203                 if (RS_DIALOGFACTORY)
204                         RS_DIALOGFACTORY->updateMouseWidget(tr("Specify radius"), tr("Back"));
205                 break;
206
207         default:
208                 if (RS_DIALOGFACTORY)
209                         RS_DIALOGFACTORY->updateMouseWidget("", "");
210                 break;
211         }
212 }
213
214 void ActionDrawCircle::showOptions()
215 {
216         ActionInterface::showOptions();
217 }
218
219 void ActionDrawCircle::hideOptions()
220 {
221         ActionInterface::hideOptions();
222 }
223
224 void ActionDrawCircle::updateMouseCursor()
225 {
226         graphicView->setMouseCursor(RS2::CadCursor);
227 }
228
229 void ActionDrawCircle::updateToolBar()
230 {
231         if (!isFinished())
232         {
233                 if (RS_DIALOGFACTORY)
234                         RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarSnap);
235         }
236         else if (RS_DIALOGFACTORY)
237                 RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarCircles);
238
239 }