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