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