]> Shamusworld >> Repos - architektonas/blob - src/actions/rs_actiondrawcircle3p.cpp
a65954354af1b5616a5a73c1ac1a01b963e04e99
[architektonas] / src / actions / rs_actiondrawcircle3p.cpp
1 // rs_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 "rs_actiondrawcircle3p.h"
16
17 #include "rs_dialogfactory.h"
18 #include "rs_graphicview.h"
19 #include "rs_preview.h"
20
21 RS_ActionDrawCircle3P::RS_ActionDrawCircle3P(RS_EntityContainer & container, RS_GraphicView & graphicView): RS_PreviewActionInterface("Draw circles",
22                 container, graphicView)
23 {
24         reset();
25 }
26
27 RS_ActionDrawCircle3P::~RS_ActionDrawCircle3P()
28 {
29 }
30
31 void RS_ActionDrawCircle3P::reset()
32 {
33         data.reset();
34         point1 = Vector(false);
35         point2 = Vector(false);
36         point3 = Vector(false);
37 }
38
39 void RS_ActionDrawCircle3P::init(int status)
40 {
41         RS_PreviewActionInterface::init(status);
42
43         reset();
44 }
45
46 void RS_ActionDrawCircle3P::trigger()
47 {
48         RS_PreviewActionInterface::trigger();
49
50         preparePreview();
51
52         if (data.isValid())
53         {
54                 RS_Circle * circle = new RS_Circle(container,
55                                 data);
56                 circle->setLayerToActive();
57                 circle->setPenToActive();
58                 container->addEntity(circle);
59
60                 // upd. undo list:
61                 if (document != NULL)
62                 {
63                         document->startUndoCycle();
64                         document->addUndoable(circle);
65                         document->endUndoCycle();
66                 }
67                 deleteSnapper();
68                 Vector rz = graphicView->getRelativeZero();
69                 graphicView->moveRelativeZero(Vector(0.0, 0.0));
70                 graphicView->drawEntity(circle);
71                 graphicView->moveRelativeZero(rz);
72                 drawSnapper();
73
74                 setStatus(SetPoint1);
75                 reset();
76         }
77         else
78                 RS_DIALOGFACTORY->requestWarningDialog(tr("Invalid circle data."));
79 }
80
81 void RS_ActionDrawCircle3P::preparePreview()
82 {
83         data.reset();
84
85         if (point1.valid && point2.valid && point3.valid)
86         {
87                 RS_Circle circle(NULL, data);
88                 bool suc = circle.createFrom3P(point1, point2, point3);
89
90                 if (suc)
91                         data = circle.getData();
92         }
93 }
94
95 void RS_ActionDrawCircle3P::mouseMoveEvent(QMouseEvent * e)
96 {
97         Vector mouse = snapPoint(e);
98
99         switch (getStatus())
100         {
101         case SetPoint1:
102                 point1 = mouse;
103                 break;
104
105         case SetPoint2:
106                 point2 = mouse;
107                 break;
108
109         case SetPoint3:
110                 point3 = mouse;
111                 preparePreview();
112
113                 if (data.isValid())
114                 {
115                         RS_Circle * circle = new RS_Circle(preview, data);
116
117                         deletePreview();
118                         clearPreview();
119                         preview->addEntity(circle);
120                         drawPreview();
121                 }
122                 break;
123         }
124 }
125
126 void RS_ActionDrawCircle3P::mouseReleaseEvent(QMouseEvent * e)
127 {
128 //      if (RS2::qtToRsButtonState(e->button())==RS2::LeftButton)
129         if (e->button() == Qt::LeftButton)
130         {
131                 Vector ce(snapPoint(e));
132                 coordinateEvent(&ce);
133         }
134 //      else if (RS2::qtToRsButtonState(e->button())==RS2::RightButton)
135         else if (e->button() == Qt::RightButton)
136         {
137                 deletePreview();
138                 deleteSnapper();
139                 init(getStatus() - 1);
140         }
141 }
142
143 void RS_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 RS_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 RS_ActionDrawCircle3P::getAvailableCommands()
187 {
188         QStringList cmd;
189         return cmd;
190 }
191
192 void RS_ActionDrawCircle3P::updateMouseButtonHints()
193 {
194         switch (getStatus())
195         {
196         case SetPoint1:
197                 RS_DIALOGFACTORY->updateMouseWidget(tr("Specify first point"),
198                         tr("Cancel"));
199                 break;
200
201         case SetPoint2:
202                 RS_DIALOGFACTORY->updateMouseWidget(tr("Specify second point"),
203                         tr("Back"));
204                 break;
205
206         case SetPoint3:
207                 RS_DIALOGFACTORY->updateMouseWidget(tr("Specify third point"),
208                         tr("Back"));
209                 break;
210
211         default:
212                 RS_DIALOGFACTORY->updateMouseWidget("", "");
213                 break;
214         }
215 }
216
217 void RS_ActionDrawCircle3P::updateMouseCursor()
218 {
219         graphicView->setMouseCursor(RS2::CadCursor);
220 }
221
222 void RS_ActionDrawCircle3P::updateToolBar()
223 {
224         if (!isFinished())
225                 RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarSnap);
226         else
227                 RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarCircles);
228 }
229
230 // EOF
231