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