]> Shamusworld >> Repos - architektonas/blob - src/actions/rs_actiondrawspline.cpp
f62a25260cd498f3e1c99561c4de81a8918618ff
[architektonas] / src / actions / rs_actiondrawspline.cpp
1 /****************************************************************************
2 ** $Id: rs_actiondrawspline.cpp 1140 2004-07-24 23:14:15Z andrew $
3 **
4 ** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
5 **
6 ** This file is part of the qcadlib Library project.
7 **
8 ** This file may be distributed and/or modified under the terms of the
9 ** GNU General Public License version 2 as published by the Free Software
10 ** Foundation and appearing in the file LICENSE.GPL included in the
11 ** packaging of this file.
12 **
13 ** Licensees holding valid qcadlib Professional Edition licenses may use
14 ** this file in accordance with the qcadlib Commercial License
15 ** Agreement provided with the Software.
16 **
17 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
18 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 **
20 ** See http://www.ribbonsoft.com for further details.
21 **
22 ** Contact info@ribbonsoft.com if any conditions of this licensing are
23 ** not clear to you.
24 **
25 **********************************************************************/
26
27 #include "rs_actiondrawspline.h"
28
29 #include "rs_actioneditundo.h"
30 #include "rs_snapper.h"
31
32 RS_ActionDrawSpline::RS_ActionDrawSpline(RS_EntityContainer & container,
33         RS_GraphicView & graphicView):
34         RS_PreviewActionInterface("Draw splines", container, graphicView)
35 {
36         reset();
37 #warning "!!! Need to port setAutoDelete() behaviour from Qt3 to Qt4 !!!"
38 //      history.setAutoDelete(true);
39         data = RS_SplineData(3, false);
40         //bHistory.setAutoDelete(true);
41 }
42
43 RS_ActionDrawSpline::~RS_ActionDrawSpline()
44 {
45 }
46
47 /*virtual*/ RS2::ActionType RS_ActionDrawSpline::rtti()
48 {
49         return RS2::ActionDrawSpline;
50 }
51
52 QAction * RS_ActionDrawSpline::createGUIAction(RS2::ActionType /*type*/, QObject * /*parent*/)
53 {
54         QAction * action = new QAction(tr("&Spline"), 0);
55 //      QAction* action = new QAction(tr("Spline"), tr("&Spline"),
56 //                                                                      QKeySequence(), NULL);
57         action->setStatusTip(tr("Draw splines"));
58         return action;
59 }
60
61 void RS_ActionDrawSpline::reset()
62 {
63         spline = NULL;
64     //start = Vector(false);
65     history.clear();
66     //bHistory.clear();
67 }
68
69
70
71 void RS_ActionDrawSpline::init(int status) {
72     RS_PreviewActionInterface::init(status);
73
74     reset();
75 }
76
77
78
79 void RS_ActionDrawSpline::trigger() {
80     RS_PreviewActionInterface::trigger();
81
82         if (spline==NULL) {
83                 return;
84         }
85
86         // add the entity
87     //RS_Spline* spline = new RS_Spline(container, data);
88     spline->setLayerToActive();
89     spline->setPenToActive();
90         spline->update();
91     container->addEntity(spline);
92
93     // upd. undo list:
94     if (document!=NULL) {
95         document->startUndoCycle();
96         document->addUndoable(spline);
97         document->endUndoCycle();
98     }
99
100         // upd view
101     deleteSnapper();
102         Vector r = graphicView->getRelativeZero();
103     graphicView->moveRelativeZero(Vector(0.0,0.0));
104     graphicView->drawEntity(spline);
105     graphicView->moveRelativeZero(r);
106     drawSnapper();
107     RS_DEBUG->print("RS_ActionDrawSpline::trigger(): spline added: %d", spline->getId());
108
109         spline = NULL;
110     //history.clear();
111 }
112
113 void RS_ActionDrawSpline::mouseMoveEvent(QMouseEvent * e)
114 {
115         RS_DEBUG->print("RS_ActionDrawSpline::mouseMoveEvent begin");
116
117         Vector mouse = snapPoint(e);
118
119         if (getStatus() == SetNextPoint && spline != NULL /*&& point.valid*/)
120         {
121                 deletePreview();
122                 clearPreview();
123
124                 RS_Spline * tmpSpline = (RS_Spline *)spline->clone();
125                 tmpSpline->addControlPoint(mouse);
126                 tmpSpline->update();
127                 preview->addEntity(tmpSpline);
128
129 //              Q3ValueList<Vector> cpts = tmpSpline->getControlPoints();
130 //              Q3ValueList<Vector>::iterator it;
131                 QList<Vector> cpts = tmpSpline->getControlPoints();
132                 QList<Vector>::iterator it;
133
134                 for(it=cpts.begin(); it!=cpts.end(); ++it)
135                         preview->addEntity(new RS_Point(preview, RS_PointData(*it)));
136
137                 drawPreview();
138         }
139
140         RS_DEBUG->print("RS_ActionDrawSpline::mouseMoveEvent end");
141 }
142
143 void RS_ActionDrawSpline::mouseReleaseEvent(QMouseEvent * e)
144 {
145     if (RS2::qtToRsButtonState(e->button()) == RS2::LeftButton)
146         {
147         RS_CoordinateEvent ce(snapPoint(e));
148         coordinateEvent(&ce);
149     }
150     else if (RS2::qtToRsButtonState(e->button()) == RS2::RightButton)
151         {
152                 if (getStatus() == SetNextPoint)
153                 {
154                         trigger();
155                 }
156         deletePreview();
157         clearPreview();
158         deleteSnapper();
159         init(getStatus()-1);
160     }
161 }
162
163
164
165 void RS_ActionDrawSpline::coordinateEvent(RS_CoordinateEvent* e) {
166     if (e==NULL) {
167         return;
168     }
169
170     Vector mouse = e->getCoordinate();
171
172     switch (getStatus()) {
173     case SetStartpoint:
174                 //data.startpoint = mouse;
175         //point = mouse;
176         history.clear();
177         history.append(new Vector(mouse));
178                 if (spline==NULL) {
179                         spline = new RS_Spline(container, data);
180                         spline->addControlPoint(mouse);
181                 }
182         //bHistory.clear();
183         //bHistory.append(new double(0.0));
184         //start = mouse;
185         setStatus(SetNextPoint);
186         graphicView->moveRelativeZero(mouse);
187         updateMouseButtonHints();
188         break;
189
190     case SetNextPoint:
191         graphicView->moveRelativeZero(mouse);
192         //point = mouse;
193         history.append(new Vector(mouse));
194         //bHistory.append(new double(0.0));
195                 if (spline!=NULL) {
196                         //graphicView->deleteEntity(spline);
197                         spline->addControlPoint(mouse);
198                         //spline->setEndpoint(mouse);
199                         //if (spline->count()==1) {
200                         //spline->setLayerToActive();
201                         //spline->setPenToActive();
202                                 //container->addEntity(spline);
203                         //}
204                         deletePreview();
205                         clearPreview();
206                         deleteSnapper();
207                         //graphicView->drawEntity(spline);
208                         drawSnapper();
209                 }
210         //trigger();
211         //data.startpoint = data.endpoint;
212         updateMouseButtonHints();
213         //graphicView->moveRelativeZero(mouse);
214         break;
215
216     default:
217         break;
218     }
219 }
220
221
222
223 void RS_ActionDrawSpline::commandEvent(RS_CommandEvent* e) {
224     QString c = e->getCommand().toLower();
225
226     switch (getStatus()) {
227     case SetStartpoint:
228         if (checkCommand("help", c)) {
229             RS_DIALOGFACTORY->commandMessage(msgAvailableCommands()
230                                              + getAvailableCommands().join(", "));
231             return;
232         }
233         break;
234
235     case SetNextPoint:
236         /*if (checkCommand("close", c)) {
237             close();
238             updateMouseButtonHints();
239             return;
240         }*/
241
242         if (checkCommand("undo", c)) {
243             undo();
244             updateMouseButtonHints();
245             return;
246         }
247         break;
248
249     default:
250         break;
251     }
252 }
253
254
255
256 QStringList RS_ActionDrawSpline::getAvailableCommands() {
257     QStringList cmd;
258
259     switch (getStatus()) {
260     case SetStartpoint:
261         break;
262     case SetNextPoint:
263         if (history.count()>=2) {
264             cmd += command("undo");
265         }
266         if (history.count()>=3) {
267             cmd += command("close");
268         }
269         break;
270     default:
271         break;
272     }
273
274     return cmd;
275 }
276
277
278
279 void RS_ActionDrawSpline::updateMouseButtonHints() {
280     switch (getStatus()) {
281     case SetStartpoint:
282         RS_DIALOGFACTORY->updateMouseWidget(tr("Specify first control point"),
283                                             tr("Cancel"));
284         break;
285     case SetNextPoint: {
286             QString msg = "";
287
288             if (history.count()>=3) {
289                 msg += RS_COMMANDS->command("close");
290                 msg += "/";
291             }
292             if (history.count()>=2) {
293                 msg += RS_COMMANDS->command("undo");
294             }
295
296             if (history.count()>=2) {
297                 RS_DIALOGFACTORY->updateMouseWidget(
298                     tr("Specify next control point or [%1]").arg(msg),
299                     tr("Back"));
300             } else {
301                 RS_DIALOGFACTORY->updateMouseWidget(
302                     tr("Specify next control point"),
303                     tr("Back"));
304             }
305         }
306         break;
307     default:
308         RS_DIALOGFACTORY->updateMouseWidget("", "");
309         break;
310     }
311 }
312
313
314 void RS_ActionDrawSpline::showOptions() {
315     RS_ActionInterface::showOptions();
316
317     RS_DIALOGFACTORY->requestOptions(this, true);
318 }
319
320
321
322 void RS_ActionDrawSpline::hideOptions() {
323     RS_ActionInterface::hideOptions();
324
325     RS_DIALOGFACTORY->requestOptions(this, false);
326 }
327
328
329 void RS_ActionDrawSpline::updateMouseCursor() {
330     graphicView->setMouseCursor(RS2::CadCursor);
331 }
332
333
334 void RS_ActionDrawSpline::updateToolBar() {
335     if (!isFinished()) {
336         RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarSnap);
337     } else {
338         //RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarSplines);
339         RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarMain);
340     }
341 }
342
343
344 /*
345 void RS_ActionDrawSpline::close() {
346     if (history.count()>2 && start.valid) {
347         //data.endpoint = start;
348         //trigger();
349                 if (spline!=NULL) {
350                         RS_CoordinateEvent e(spline->getStartpoint());
351                         coordinateEvent(&e);
352                 }
353                 trigger();
354         setStatus(SetStartpoint);
355         graphicView->moveRelativeZero(start);
356     } else {
357         RS_DIALOGFACTORY->commandMessage(
358             tr("Cannot close sequence of lines: "
359                "Not enough entities defined yet."));
360     }
361 }
362 */
363
364 void RS_ActionDrawSpline::undo() {
365     if (history.count()>1) {
366         history.removeLast();
367         //bHistory.removeLast();
368         deletePreview();
369         clearPreview();
370         //graphicView->setCurrentAction(
371         //    new RS_ActionEditUndo(true, *container, *graphicView));
372                 if (history.last()!=NULL) {
373                 //point = *history.last();
374                 }
375                 if (spline!=NULL) {
376                         spline->removeLastControlPoint();
377                         Vector* v = history.last();
378                         if (v!=NULL) {
379                         graphicView->moveRelativeZero(*v);
380                         }
381                         graphicView->redraw();
382                 }
383     } else {
384         RS_DIALOGFACTORY->commandMessage(
385             tr("Cannot undo: "
386                "Not enough entities defined yet."));
387     }
388 }
389
390
391 void RS_ActionDrawSpline::setDegree(int deg) {
392         data.degree = deg;
393         if (spline!=NULL) {
394                 spline->setDegree(deg);
395         }
396 }
397
398 int RS_ActionDrawSpline::getDegree() {
399         return data.degree;
400 }
401
402 void RS_ActionDrawSpline::setClosed(bool c) {
403         data.closed = c;
404         if (spline!=NULL) {
405                 spline->setClosed(c);
406         }
407 }
408
409 bool RS_ActionDrawSpline::isClosed() {
410         return data.closed;
411 }
412
413 // EOF