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