]> Shamusworld >> Repos - architektonas/blob - src/actions/rs_actiondrawcircle.cpp
cf20b47424cdb33ef05256e876e4f4a6b5b6ca72
[architektonas] / src / actions / rs_actiondrawcircle.cpp
1 // rs_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 "rs_actiondrawcircle.h"
16
17 #include "rs_commandevent.h"
18 #include "rs_dialogfactory.h"
19 #include "graphicview.h"
20 #include "rs_preview.h"
21
22 RS_ActionDrawCircle::RS_ActionDrawCircle(RS_EntityContainer & container, GraphicView & graphicView):
23         RS_PreviewActionInterface("Draw circles", container, graphicView)
24 {
25         reset();
26 }
27
28 RS_ActionDrawCircle::~RS_ActionDrawCircle()
29 {
30 }
31
32 /*virtual*/ RS2::ActionType RS_ActionDrawCircle::rtti()
33 {
34         return RS2::ActionDrawCircle;
35 }
36
37 void RS_ActionDrawCircle::reset()
38 {
39         data = RS_CircleData(Vector(false), 0.0);
40 }
41
42 void RS_ActionDrawCircle::init(int status)
43 {
44         RS_PreviewActionInterface::init(status);
45
46         reset();
47 }
48
49 void RS_ActionDrawCircle::trigger()
50 {
51         RS_PreviewActionInterface::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("RS_ActionDrawCircle::trigger(): circle added: %d", circle->getId());
77 }
78
79 void RS_ActionDrawCircle::mouseMoveEvent(QMouseEvent * e)
80 {
81         RS_DEBUG->print("RS_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,
99                                         data));
100                         drawPreview();
101                 }
102                 break;
103         }
104
105         RS_DEBUG->print("RS_ActionDrawCircle::mouseMoveEvent end");
106 }
107
108 void RS_ActionDrawCircle::mouseReleaseEvent(QMouseEvent * e)
109 {
110         if (e->button() == Qt::LeftButton)
111         {
112                 Vector ce(snapPoint(e));
113                 coordinateEvent(&ce);
114         }
115         else if (e->button() == Qt::RightButton)
116         {
117                 deletePreview();
118                 deleteSnapper();
119                 init(getStatus() - 1);
120         }
121 }
122
123 void RS_ActionDrawCircle::coordinateEvent(Vector * e)
124 {
125         if (e == NULL)
126                 return;
127
128         Vector mouse = *e;
129
130         switch (getStatus())
131         {
132         case SetCenter:
133                 data.center = mouse;
134                 graphicView->moveRelativeZero(mouse);
135                 setStatus(SetRadius);
136                 break;
137
138         case SetRadius:
139
140                 if (data.center.valid)
141                 {
142                         graphicView->moveRelativeZero(mouse);
143                         data.radius = data.center.distanceTo(mouse);
144                         trigger();
145                 }
146                 //setStatus(SetCenter);
147                 break;
148
149         default:
150                 break;
151         }
152 }
153
154 void RS_ActionDrawCircle::commandEvent(RS_CommandEvent * e)
155 {
156         QString c = e->getCommand().toLower();
157
158         if (checkCommand("help", c))
159         {
160                 if (RS_DIALOGFACTORY != NULL)
161                         RS_DIALOGFACTORY->commandMessage(msgAvailableCommands()
162                                 + getAvailableCommands().join(", "));
163                 return;
164         }
165
166         switch (getStatus())
167         {
168         case SetRadius: {
169                 bool ok;
170                 double r = RS_Math::eval(c, &ok);
171
172                 if (ok == true)
173                         data.radius = r;
174                 else if (RS_DIALOGFACTORY != NULL)
175                         RS_DIALOGFACTORY->commandMessage(
176                                 tr("Not a valid expression"));
177
178                 trigger();
179                 //setStatus(SetCenter);
180         }
181         break;
182
183         default:
184                 break;
185         }
186 }
187
188 QStringList RS_ActionDrawCircle::getAvailableCommands()
189 {
190         QStringList cmd;
191         return cmd;
192 }
193
194 void RS_ActionDrawCircle::updateMouseButtonHints()
195 {
196         switch (getStatus())
197         {
198         case SetCenter:
199
200                 if (RS_DIALOGFACTORY != NULL)
201                         RS_DIALOGFACTORY->updateMouseWidget(
202                                 tr("Specify center"), tr("Cancel"));
203                 break;
204
205         case SetRadius:
206
207                 if (RS_DIALOGFACTORY != NULL)
208                         RS_DIALOGFACTORY->updateMouseWidget(tr("Specify radius"), tr("Back"));
209                 break;
210
211         default:
212
213                 if (RS_DIALOGFACTORY != NULL)
214                         RS_DIALOGFACTORY->updateMouseWidget("", "");
215                 break;
216         }
217 }
218
219 void RS_ActionDrawCircle::showOptions()
220 {
221         RS_ActionInterface::showOptions();
222 }
223
224 void RS_ActionDrawCircle::hideOptions()
225 {
226         RS_ActionInterface::hideOptions();
227 }
228
229 void RS_ActionDrawCircle::updateMouseCursor()
230 {
231         graphicView->setMouseCursor(RS2::CadCursor);
232 }
233
234 void RS_ActionDrawCircle::updateToolBar()
235 {
236         if (!isFinished())
237         {
238                 if (RS_DIALOGFACTORY != NULL)
239                         RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarSnap);
240         }
241         else if (RS_DIALOGFACTORY != NULL)
242                 RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarCircles);
243
244 }
245
246 // EOF
247