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