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