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