]> Shamusworld >> Repos - architektonas/blob - src/actions/actiondrawcirclecr.cpp
GPL compliance check...
[architektonas] / src / actions / actiondrawcirclecr.cpp
1 // 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 // Portions copyright (C) 2001-2003 RibbonSoft
7 // Copyright (C) 2010 Underground Software
8 // See the README and GPLv2 files for licensing and warranty information
9 //
10 // JLH = James L. Hammons <jlhamm@acm.org>
11 //
12 // Who  When        What
13 // ---  ----------  -----------------------------------------------------------
14 // JLH  06/03/2010  Added this text. :-)
15 //
16
17 #include "actiondrawcirclecr.h"
18
19 #include "rs_commandevent.h"
20 #include "rs_dialogfactory.h"
21 #include "graphicview.h"
22 #include "rs_preview.h"
23
24 /**
25  * Constructor.
26  */
27 ActionDrawCircleCR::ActionDrawCircleCR(RS_EntityContainer & container, GraphicView & graphicView): ActionInterface("Draw circles CR",
28                 container, graphicView)
29 {
30         reset();
31 }
32
33 ActionDrawCircleCR::~ActionDrawCircleCR()
34 {
35 }
36
37 /*virtual*/ RS2::ActionType ActionDrawCircleCR::rtti()
38 {
39         return RS2::ActionDrawCircleCR;
40 }
41
42 void ActionDrawCircleCR::reset()
43 {
44         data = RS_CircleData(Vector(false), 0.0);
45 }
46
47 void ActionDrawCircleCR::init(int status)
48 {
49         ActionInterface::init(status);
50 }
51
52 void ActionDrawCircleCR::trigger()
53 {
54         ActionInterface::trigger();
55
56         RS_Circle * circle = new RS_Circle(container, data);
57         circle->setLayerToActive();
58         circle->setPenToActive();
59         container->addEntity(circle);
60
61         // upd. undo list:
62         if (document != NULL)
63         {
64                 document->startUndoCycle();
65                 document->addUndoable(circle);
66                 document->endUndoCycle();
67         }
68
69         deleteSnapper();
70         Vector rz = graphicView->getRelativeZero();
71         graphicView->moveRelativeZero(Vector(0.0, 0.0));
72         graphicView->drawEntity(circle);
73         graphicView->moveRelativeZero(circle->getCenter());
74         drawSnapper();
75
76         setStatus(SetCenter);
77
78         RS_DEBUG->print("ActionDrawCircleCR::trigger(): circle added: %d", circle->getId());
79 }
80
81 void ActionDrawCircleCR::mouseMoveEvent(QMouseEvent * e)
82 {
83         RS_DEBUG->print("ActionDrawCircleCR::mouseMoveEvent begin");
84         Vector mouse = snapPoint(e);
85
86         switch (getStatus())
87         {
88         case SetCenter:
89                 data.center = mouse;
90                 deletePreview();
91                 clearPreview();
92 //              preview->addEntity(new RS_Circle(preview, data));
93                 drawPreview();
94                 break;
95         }
96
97         RS_DEBUG->print("ActionDrawCircleCR::mouseMoveEvent end");
98 }
99
100 void ActionDrawCircleCR::mouseReleaseEvent(QMouseEvent * e)
101 {
102         if (e->button() == Qt::LeftButton)
103         {
104                 Vector ce(snapPoint(e));
105                 coordinateEvent(&ce);
106         }
107         else if (e->button() == Qt::RightButton)
108         {
109                 deletePreview();
110                 deleteSnapper();
111                 init(getStatus() - 1);
112         }
113 }
114
115 void 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 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 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 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 ActionDrawCircleCR::showOptions()
214 {
215         ActionInterface::showOptions();
216
217         RS_DIALOGFACTORY->requestOptions(this, true);
218 }
219
220 void ActionDrawCircleCR::hideOptions()
221 {
222         ActionInterface::hideOptions();
223
224         RS_DIALOGFACTORY->requestOptions(this, false);
225 }
226
227 void ActionDrawCircleCR::updateMouseCursor()
228 {
229         graphicView->setMouseCursor(RS2::CadCursor);
230 }
231
232 void ActionDrawCircleCR::updateToolBar()
233 {
234         if (!isFinished())
235                 RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarSnap);
236         else
237                 RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarCircles);
238 }
239
240 double ActionDrawCircleCR::getRadius()
241 {
242         return data.radius;
243 }
244
245 void ActionDrawCircleCR::setRadius(double r)
246 {
247         data.radius = r;
248 }