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