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