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