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