]> Shamusworld >> Repos - architektonas/blob - src/actions/actiondrawarc3p.cpp
Bugfixes related to removing Snapper class.
[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 // Portions copyright (C) 2001-2003 RibbonSoft
7 // Copyright (C) 2010 Underground Software
8 // See the README and GPLv2 files for licensing and warranty information
9 //
10 // JLH = James L. Hammons <jlhamm@acm.org>
11 //
12 // Who  When        What
13 // ---  ----------  -----------------------------------------------------------
14 // JLH  06/03/2010  Added this text. :-)
15 // JLH  09/17/2010  Fixed preview/snapper rendering.
16 //
17
18 #include "actiondrawarc3p.h"
19
20 #include "actiondrawarc.h"
21 #include "commandevent.h"
22 #include "commands.h"
23 #include "dialogfactory.h"
24 #include "graphicview.h"
25 #include "preview.h"
26
27 ActionDrawArc3P::ActionDrawArc3P(EntityContainer & container, GraphicView & graphicView):
28         ActionInterface("Draw arcs 3P", container, graphicView)
29 {
30         reset();
31 }
32
33 ActionDrawArc3P::~ActionDrawArc3P()
34 {
35 }
36
37 void ActionDrawArc3P::reset()
38 {
39         data.reset();
40         point1 = Vector(false);
41         point2 = Vector(false);
42         point3 = Vector(false);
43 }
44
45 void ActionDrawArc3P::init(int status)
46 {
47         ActionInterface::init(status);
48
49         //reset();
50 }
51
52 void ActionDrawArc3P::trigger()
53 {
54         ActionInterface::trigger();
55         preparePreview();
56
57         if (data.isValid())
58         {
59                 Arc * arc = new Arc(container, data);
60                 arc->setLayerToActive();
61                 arc->setPenToActive();
62                 container->addEntity(arc);
63
64                 // upd. undo list:
65                 if (document)
66                 {
67                         document->startUndoCycle();
68                         document->addUndoable(arc);
69                         document->endUndoCycle();
70                 }
71
72 //              deleteSnapper();
73 //              graphicView->moveRelativeZero(Vector(0.0, 0.0));
74 //              graphicView->drawEntity(arc);
75                 graphicView->moveRelativeZero(arc->getEndpoint());
76 //              drawSnapper();
77                 graphicView->redraw();
78                 setStatus(SetPoint1);
79                 reset();
80         }
81         else
82                 DIALOGFACTORY->commandMessage(tr("Invalid arc data."));
83 }
84
85 void ActionDrawArc3P::preparePreview()
86 {
87         data.reset();
88
89         if (point1.valid && point2.valid && point3.valid)
90         {
91                 Arc arc(NULL, data);
92                 bool success = arc.createFrom3P(point1, point2, point3);
93
94                 if (success)
95                         data = arc.getData();
96         }
97 }
98
99 void ActionDrawArc3P::mouseMoveEvent(QMouseEvent * e)
100 {
101         Vector mouse = snapPoint(e);
102
103         switch (getStatus())
104         {
105         case SetPoint1:
106                 point1 = mouse;
107                 break;
108
109         case SetPoint2:
110                 point2 = mouse;
111
112                 if (point1.valid)
113                 {
114 //                      Line * line = new Line(preview, LineData(point1, point2));
115 //
116 //                      deletePreview();
117 //                      clearPreview();
118 //                      preview->addEntity(line);
119 //                      drawPreview();
120                         graphicView->preview.clear();
121                         graphicView->preview.addEntity(new Line(&(graphicView->preview), LineData(point1, point2)));
122                 }
123                 break;
124
125         case SetPoint3:
126                 point3 = mouse;
127                 preparePreview();
128
129                 if (data.isValid())
130                 {
131 //                      Arc * arc = new Arc(preview, data);
132 //
133 //                      deletePreview();
134 //                      clearPreview();
135 //                      preview->addEntity(arc);
136 //                      drawPreview();
137                         graphicView->preview.clear();
138                         graphicView->preview.addEntity(new Arc(&(graphicView->preview), data));
139                 }
140                 break;
141
142         default:
143                 break;
144         }
145
146         graphicView->redraw();
147 }
148
149 void ActionDrawArc3P::mouseReleaseEvent(QMouseEvent * e)
150 {
151         if (e->button() == Qt::LeftButton)
152         {
153                 Vector ce(snapPoint(e));
154                 coordinateEvent(&ce);
155         }
156         else if (e->button() == Qt::RightButton)
157         {
158 //              deletePreview();
159 //              deleteSnapper();
160                 init(getStatus() - 1);
161                 graphicView->redraw();
162         }
163 }
164
165 void ActionDrawArc3P::coordinateEvent(Vector * e)
166 {
167         if (e == NULL)
168                 return;
169
170         Vector mouse = *e;
171
172         switch (getStatus())
173         {
174         case SetPoint1:
175                 point1 = mouse;
176                 graphicView->moveRelativeZero(mouse);
177                 setStatus(SetPoint2);
178                 break;
179
180         case SetPoint2:
181                 point2 = mouse;
182                 graphicView->moveRelativeZero(mouse);
183                 setStatus(SetPoint3);
184                 break;
185
186         case SetPoint3:
187                 point3 = mouse;
188                 trigger();
189                 break;
190
191         default:
192                 break;
193         }
194 }
195
196 void ActionDrawArc3P::commandEvent(CommandEvent * e)
197 {
198         QString c = e->getCommand().toLower();
199
200         if (checkCommand("help", c))
201         {
202                 DIALOGFACTORY->commandMessage(msgAvailableCommands()
203                         + getAvailableCommands().join(", "));
204                 return;
205         }
206
207         if (COMMANDS->checkCommand("center", c, rtti()))
208         {
209                 finish();
210                 graphicView->setCurrentAction(new ActionDrawArc(*container, *graphicView));
211         }
212 }
213
214 QStringList ActionDrawArc3P::getAvailableCommands()
215 {
216         QStringList cmd;
217         return cmd;
218 }
219
220 void ActionDrawArc3P::updateMouseButtonHints()
221 {
222         switch (getStatus())
223         {
224         case SetPoint1:
225                 DIALOGFACTORY->updateMouseWidget(tr("Specify startpoint or [Center]"), tr("Cancel"));
226                 break;
227
228         case SetPoint2:
229                 DIALOGFACTORY->updateMouseWidget(tr("Specify second point"), tr("Back"));
230                 break;
231
232         case SetPoint3:
233                 DIALOGFACTORY->updateMouseWidget(tr("Specify endpoint"), tr("Back"));
234                 break;
235
236         default:
237                 DIALOGFACTORY->updateMouseWidget("", "");
238                 break;
239         }
240 }
241
242 void ActionDrawArc3P::updateMouseCursor()
243 {
244         graphicView->setMouseCursor(RS2::CadCursor);
245 }
246
247 void ActionDrawArc3P::updateToolBar()
248 {
249         if (!isFinished())
250                 DIALOGFACTORY->requestToolBar(RS2::ToolBarSnap);
251         else
252                 DIALOGFACTORY->requestToolBar(RS2::ToolBarArcs);
253 }
254