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