1 // rs_actiondrawspline.cpp
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
8 // JLH = James L. Hammons <jlhamm@acm.org>
11 // --- ---------- -----------------------------------------------------------
12 // JLH 06/04/2010 Added this text. :-)
15 #include "rs_actiondrawspline.h"
18 #include "rs_dialogfactory.h"
19 #include "rs_graphicview.h"
20 #include "rs_preview.h"
22 RS_ActionDrawSpline::RS_ActionDrawSpline(RS_EntityContainer & container, RS_GraphicView & graphicView):
23 RS_PreviewActionInterface("Draw splines", container, graphicView)
26 #warning "!!! Need to port setAutoDelete() behaviour from Qt3 to Qt4 !!!"
27 // history.setAutoDelete(true);
28 data = RS_SplineData(3, false);
29 //bHistory.setAutoDelete(true);
32 RS_ActionDrawSpline::~RS_ActionDrawSpline()
36 /*virtual*/ RS2::ActionType RS_ActionDrawSpline::rtti()
38 return RS2::ActionDrawSpline;
41 void RS_ActionDrawSpline::reset()
44 //start = Vector(false);
49 void RS_ActionDrawSpline::init(int status)
51 RS_PreviewActionInterface::init(status);
56 void RS_ActionDrawSpline::trigger()
58 RS_PreviewActionInterface::trigger();
64 //RS_Spline* spline = new RS_Spline(container, data);
65 spline->setLayerToActive();
66 spline->setPenToActive();
68 container->addEntity(spline);
73 document->startUndoCycle();
74 document->addUndoable(spline);
75 document->endUndoCycle();
80 Vector r = graphicView->getRelativeZero();
81 graphicView->moveRelativeZero(Vector(0.0, 0.0));
82 graphicView->drawEntity(spline);
83 graphicView->moveRelativeZero(r);
85 RS_DEBUG->print("RS_ActionDrawSpline::trigger(): spline added: %d", spline->getId());
91 void RS_ActionDrawSpline::mouseMoveEvent(QMouseEvent * e)
93 RS_DEBUG->print("RS_ActionDrawSpline::mouseMoveEvent begin");
95 Vector mouse = snapPoint(e);
97 if (getStatus() == SetNextPoint && spline != NULL /*&& point.valid*/)
102 RS_Spline * tmpSpline = (RS_Spline *)spline->clone();
103 tmpSpline->addControlPoint(mouse);
105 preview->addEntity(tmpSpline);
107 QList<Vector> cpts = tmpSpline->getControlPoints();
108 QList<Vector>::iterator it;
110 for(it = cpts.begin(); it != cpts.end(); ++it)
111 preview->addEntity(new RS_Point(preview, RS_PointData(*it)));
116 RS_DEBUG->print("RS_ActionDrawSpline::mouseMoveEvent end");
119 void RS_ActionDrawSpline::mouseReleaseEvent(QMouseEvent * e)
121 if (e->button() == Qt::LeftButton)
123 Vector ce(snapPoint(e));
124 coordinateEvent(&ce);
126 else if (e->button() == Qt::RightButton)
128 if (getStatus() == SetNextPoint)
134 init(getStatus() - 1);
138 void RS_ActionDrawSpline::coordinateEvent(Vector * e)
148 //data.startpoint = mouse;
151 history.append(new Vector(mouse));
155 spline = new RS_Spline(container, data);
156 spline->addControlPoint(mouse);
160 //bHistory.append(new double(0.0));
162 setStatus(SetNextPoint);
163 graphicView->moveRelativeZero(mouse);
164 updateMouseButtonHints();
168 graphicView->moveRelativeZero(mouse);
170 history.append(new Vector(mouse));
171 //bHistory.append(new double(0.0));
175 //graphicView->deleteEntity(spline);
176 spline->addControlPoint(mouse);
177 //spline->setEndpoint(mouse);
178 //if (spline->count()==1) {
179 //spline->setLayerToActive();
180 //spline->setPenToActive();
181 //container->addEntity(spline);
186 //graphicView->drawEntity(spline);
191 //data.startpoint = data.endpoint;
192 updateMouseButtonHints();
193 //graphicView->moveRelativeZero(mouse);
201 void RS_ActionDrawSpline::commandEvent(RS_CommandEvent * e)
203 QString c = e->getCommand().toLower();
209 if (checkCommand("help", c))
211 RS_DIALOGFACTORY->commandMessage(msgAvailableCommands()
212 + getAvailableCommands().join(", "));
218 /*if (checkCommand("close", c)) {
220 updateMouseButtonHints();
224 if (checkCommand("undo", c))
227 updateMouseButtonHints();
237 QStringList RS_ActionDrawSpline::getAvailableCommands()
248 if (history.count() >= 2)
249 cmd += command("undo");
252 if (history.count() >= 3)
253 cmd += command("close");
263 void RS_ActionDrawSpline::updateMouseButtonHints()
268 RS_DIALOGFACTORY->updateMouseWidget(tr("Specify first control point"),
275 if (history.count() >= 3)
277 msg += RS_COMMANDS->command("close");
281 if (history.count() >= 2)
282 msg += RS_COMMANDS->command("undo");
284 if (history.count() >= 2)
285 RS_DIALOGFACTORY->updateMouseWidget(
286 tr("Specify next control point or [%1]").arg(msg),
289 RS_DIALOGFACTORY->updateMouseWidget(
290 tr("Specify next control point"),
296 RS_DIALOGFACTORY->updateMouseWidget("", "");
301 void RS_ActionDrawSpline::showOptions()
303 RS_ActionInterface::showOptions();
305 RS_DIALOGFACTORY->requestOptions(this, true);
308 void RS_ActionDrawSpline::hideOptions()
310 RS_ActionInterface::hideOptions();
312 RS_DIALOGFACTORY->requestOptions(this, false);
315 void RS_ActionDrawSpline::updateMouseCursor()
317 graphicView->setMouseCursor(RS2::CadCursor);
320 void RS_ActionDrawSpline::updateToolBar()
323 RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarSnap);
325 //RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarSplines);
326 RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarMain);
330 void RS_ActionDrawSpline::close() {
331 if (history.count()>2 && start.valid) {
332 //data.endpoint = start;
335 Vector e(spline->getStartpoint());
339 setStatus(SetStartpoint);
340 graphicView->moveRelativeZero(start);
342 RS_DIALOGFACTORY->commandMessage(
343 tr("Cannot close sequence of lines: "
344 "Not enough entities defined yet."));
349 void RS_ActionDrawSpline::undo()
351 if (history.count() > 1)
353 history.removeLast();
354 //bHistory.removeLast();
358 //graphicView->setCurrentAction(
359 // new RS_ActionEditUndo(true, *container, *graphicView));
360 if (history.last() != NULL)
362 //point = *history.last();
367 spline->removeLastControlPoint();
368 Vector * v = history.last();
371 graphicView->moveRelativeZero(*v);
372 graphicView->redraw();
376 RS_DIALOGFACTORY->commandMessage(
378 "Not enough entities defined yet."));
381 void RS_ActionDrawSpline::setDegree(int deg)
386 spline->setDegree(deg);
389 int RS_ActionDrawSpline::getDegree()
394 void RS_ActionDrawSpline::setClosed(bool c)
399 spline->setClosed(c);
402 bool RS_ActionDrawSpline::isClosed()