]> Shamusworld >> Repos - architektonas/blob - src/actions/actiondrawspline.cpp
Bugfixes related to removing Snapper class.
[architektonas] / src / actions / actiondrawspline.cpp
1 // actiondrawspline.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/04/2010  Added this text. :-)
15 //
16
17 #include "actiondrawspline.h"
18
19 #include "commandevent.h"
20 #include "commands.h"
21 #include "debug.h"
22 #include "dialogfactory.h"
23 #include "graphicview.h"
24 #include "preview.h"
25
26 ActionDrawSpline::ActionDrawSpline(EntityContainer & container, GraphicView & graphicView):
27         ActionInterface("Draw splines", container, graphicView)
28 {
29         reset();
30 #warning "!!! Need to port setAutoDelete() behaviour from Qt3 to Qt4 !!!"
31 //      history.setAutoDelete(true);
32         data = SplineData(3, false);
33         //bHistory.setAutoDelete(true);
34 }
35
36 ActionDrawSpline::~ActionDrawSpline()
37 {
38 }
39
40 /*virtual*/ RS2::ActionType ActionDrawSpline::rtti()
41 {
42         return RS2::ActionDrawSpline;
43 }
44
45 void ActionDrawSpline::reset()
46 {
47         spline = NULL;
48         //start = Vector(false);
49         history.clear();
50         //bHistory.clear();
51 }
52
53 void ActionDrawSpline::init(int status)
54 {
55         ActionInterface::init(status);
56
57         reset();
58 }
59
60 void ActionDrawSpline::trigger()
61 {
62         ActionInterface::trigger();
63
64         if (spline == NULL)
65                 return;
66
67         // add the entity
68         spline->setLayerToActive();
69         spline->setPenToActive();
70         spline->update();
71         container->addEntity(spline);
72
73         // upd. undo list:
74         if (document)
75         {
76                 document->startUndoCycle();
77                 document->addUndoable(spline);
78                 document->endUndoCycle();
79         }
80
81         // upd view
82         deleteSnapper();
83         Vector r = graphicView->getRelativeZero();
84         graphicView->moveRelativeZero(Vector(0.0, 0.0));
85         graphicView->drawEntity(spline);
86         graphicView->moveRelativeZero(r);
87         drawSnapper();
88         DEBUG->print("ActionDrawSpline::trigger(): spline added: %d", spline->getId());
89
90         spline = NULL;
91         //history.clear();
92 }
93
94 void ActionDrawSpline::mouseMoveEvent(QMouseEvent * e)
95 {
96         DEBUG->print("ActionDrawSpline::mouseMoveEvent begin");
97
98         Vector mouse = snapPoint(e);
99
100         if (getStatus() == SetNextPoint && spline)
101         {
102                 deletePreview();
103                 clearPreview();
104
105                 Spline * tmpSpline = (Spline *)spline->clone();
106                 tmpSpline->addControlPoint(mouse);
107                 tmpSpline->update();
108 //              preview->addEntity(tmpSpline);
109
110                 QList<Vector> cpts = tmpSpline->getControlPoints();
111                 QList<Vector>::iterator it;
112
113 //              for(it=cpts.begin(); it!=cpts.end(); it+)
114 //                      preview->addEntity(new Point(preview, PointData(*it)));
115
116                 drawPreview();
117         }
118
119         DEBUG->print("ActionDrawSpline::mouseMoveEvent end");
120 }
121
122 void ActionDrawSpline::mouseReleaseEvent(QMouseEvent * e)
123 {
124         if (e->button() == Qt::LeftButton)
125         {
126                 Vector ce(snapPoint(e));
127                 coordinateEvent(&ce);
128         }
129         else if (e->button() == Qt::RightButton)
130         {
131                 if (getStatus() == SetNextPoint)
132                         trigger();
133
134                 deletePreview();
135                 clearPreview();
136                 deleteSnapper();
137                 init(getStatus() - 1);
138         }
139 }
140
141 void ActionDrawSpline::coordinateEvent(Vector * e)
142 {
143         if (!e)
144                 return;
145
146         Vector mouse = *e;
147
148         switch (getStatus())
149         {
150         case SetStartpoint:
151                 history.clear();
152                 history.append(new Vector(mouse));
153
154                 if (!spline)
155                 {
156                         spline = new Spline(container, data);
157                         spline->addControlPoint(mouse);
158                 }
159
160                 setStatus(SetNextPoint);
161                 graphicView->moveRelativeZero(mouse);
162                 updateMouseButtonHints();
163                 break;
164
165         case SetNextPoint:
166                 graphicView->moveRelativeZero(mouse);
167                 history.append(new Vector(mouse));
168
169                 if (spline)
170                 {
171                         spline->addControlPoint(mouse);
172                         deletePreview();
173                         clearPreview();
174                         deleteSnapper();
175                         drawSnapper();
176                 }
177
178                 updateMouseButtonHints();
179                 break;
180
181         default:
182                 break;
183         }
184 }
185
186 void ActionDrawSpline::commandEvent(CommandEvent * e)
187 {
188         QString c = e->getCommand().toLower();
189
190         switch (getStatus())
191         {
192         case SetStartpoint:
193                 if (checkCommand("help", c))
194                 {
195                         DIALOGFACTORY->commandMessage(msgAvailableCommands()
196                                 + getAvailableCommands().join(", "));
197                         return;
198                 }
199
200                 break;
201
202         case SetNextPoint:
203                 if (checkCommand("undo", c))
204                 {
205                         undo();
206                         updateMouseButtonHints();
207                         return;
208                 }
209
210                 break;
211
212         default:
213                 break;
214         }
215 }
216
217 QStringList ActionDrawSpline::getAvailableCommands()
218 {
219         QStringList cmd;
220
221         switch (getStatus())
222         {
223         case SetStartpoint:
224                 break;
225
226         case SetNextPoint:
227                 if (history.count() >= 2)
228                         cmd += command("undo");
229
230                 if (history.count() >= 3)
231                         cmd += command("close");
232
233                 break;
234
235         default:
236                 break;
237         }
238
239         return cmd;
240 }
241
242 void ActionDrawSpline::updateMouseButtonHints()
243 {
244         switch (getStatus())
245         {
246         case SetStartpoint:
247                 DIALOGFACTORY->updateMouseWidget(tr("Specify first control point"),
248                         tr("Cancel"));
249                 break;
250
251         case SetNextPoint:
252         {
253                 QString msg = "";
254
255                 if (history.count() >= 3)
256                 {
257                         msg += COMMANDS->command("close");
258                         msg += "/";
259                 }
260
261                 if (history.count() >= 2)
262                         msg += COMMANDS->command("undo");
263
264                 if (history.count() >= 2)
265                         DIALOGFACTORY->updateMouseWidget(
266                                 tr("Specify next control point or [%1]").arg(msg),
267                                 tr("Back"));
268                 else
269                         DIALOGFACTORY->updateMouseWidget(
270                                 tr("Specify next control point"),
271                                 tr("Back"));
272         }
273                 break;
274
275         default:
276                 DIALOGFACTORY->updateMouseWidget("", "");
277                 break;
278         }
279 }
280
281 void ActionDrawSpline::showOptions()
282 {
283         ActionInterface::showOptions();
284         DIALOGFACTORY->requestOptions(this, true);
285 }
286
287 void ActionDrawSpline::hideOptions()
288 {
289         ActionInterface::hideOptions();
290         DIALOGFACTORY->requestOptions(this, false);
291 }
292
293 void ActionDrawSpline::updateMouseCursor()
294 {
295         graphicView->setMouseCursor(RS2::CadCursor);
296 }
297
298 void ActionDrawSpline::updateToolBar()
299 {
300         if (!isFinished())
301                 DIALOGFACTORY->requestToolBar(RS2::ToolBarSnap);
302         else
303                 DIALOGFACTORY->requestToolBar(RS2::ToolBarMain);
304 }
305
306 void ActionDrawSpline::undo()
307 {
308         if (history.count() > 1)
309         {
310                 history.removeLast();
311                 deletePreview();
312                 clearPreview();
313
314                 if (history.last())
315                 {
316                         //point = *history.last();
317                 }
318
319                 if (spline)
320                 {
321                         spline->removeLastControlPoint();
322                         Vector * v = history.last();
323
324                         if (v)
325                                 graphicView->moveRelativeZero(*v);
326
327                         graphicView->redraw();
328                 }
329         }
330         else
331                 DIALOGFACTORY->commandMessage(
332                         tr("Cannot undo: Not enough entities defined yet."));
333 }
334
335 void ActionDrawSpline::setDegree(int deg)
336 {
337         data.degree = deg;
338
339         if (spline)
340                 spline->setDegree(deg);
341 }
342
343 int ActionDrawSpline::getDegree()
344 {
345         return data.degree;
346 }
347
348 void ActionDrawSpline::setClosed(bool c)
349 {
350         data.closed = c;
351
352         if (spline)
353                 spline->setClosed(c);
354 }
355
356 bool ActionDrawSpline::isClosed()
357 {
358         return data.closed;
359 }