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