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"
17 #include "rs_commandevent.h"
19 #include "rs_dialogfactory.h"
20 #include "graphicview.h"
21 #include "rs_preview.h"
23 RS_ActionDrawSpline::RS_ActionDrawSpline(RS_EntityContainer & container, GraphicView & graphicView):
24 RS_PreviewActionInterface("Draw splines", container, graphicView)
27 #warning "!!! Need to port setAutoDelete() behaviour from Qt3 to Qt4 !!!"
28 // history.setAutoDelete(true);
29 data = RS_SplineData(3, false);
30 //bHistory.setAutoDelete(true);
33 RS_ActionDrawSpline::~RS_ActionDrawSpline()
37 /*virtual*/ RS2::ActionType RS_ActionDrawSpline::rtti()
39 return RS2::ActionDrawSpline;
42 void RS_ActionDrawSpline::reset()
45 //start = Vector(false);
50 void RS_ActionDrawSpline::init(int status)
52 RS_PreviewActionInterface::init(status);
57 void RS_ActionDrawSpline::trigger()
59 RS_PreviewActionInterface::trigger();
65 //RS_Spline* spline = new RS_Spline(container, data);
66 spline->setLayerToActive();
67 spline->setPenToActive();
69 container->addEntity(spline);
74 document->startUndoCycle();
75 document->addUndoable(spline);
76 document->endUndoCycle();
81 Vector r = graphicView->getRelativeZero();
82 graphicView->moveRelativeZero(Vector(0.0, 0.0));
83 graphicView->drawEntity(spline);
84 graphicView->moveRelativeZero(r);
86 RS_DEBUG->print("RS_ActionDrawSpline::trigger(): spline added: %d", spline->getId());
92 void RS_ActionDrawSpline::mouseMoveEvent(QMouseEvent * e)
94 RS_DEBUG->print("RS_ActionDrawSpline::mouseMoveEvent begin");
96 Vector mouse = snapPoint(e);
98 if (getStatus() == SetNextPoint && spline != NULL /*&& point.valid*/)
103 RS_Spline * tmpSpline = (RS_Spline *)spline->clone();
104 tmpSpline->addControlPoint(mouse);
106 preview->addEntity(tmpSpline);
108 QList<Vector> cpts = tmpSpline->getControlPoints();
109 QList<Vector>::iterator it;
111 for(it = cpts.begin(); it != cpts.end(); ++it)
112 preview->addEntity(new RS_Point(preview, RS_PointData(*it)));
117 RS_DEBUG->print("RS_ActionDrawSpline::mouseMoveEvent end");
120 void RS_ActionDrawSpline::mouseReleaseEvent(QMouseEvent * e)
122 if (e->button() == Qt::LeftButton)
124 Vector ce(snapPoint(e));
125 coordinateEvent(&ce);
127 else if (e->button() == Qt::RightButton)
129 if (getStatus() == SetNextPoint)
135 init(getStatus() - 1);
139 void RS_ActionDrawSpline::coordinateEvent(Vector * e)
149 //data.startpoint = mouse;
152 history.append(new Vector(mouse));
156 spline = new RS_Spline(container, data);
157 spline->addControlPoint(mouse);
161 //bHistory.append(new double(0.0));
163 setStatus(SetNextPoint);
164 graphicView->moveRelativeZero(mouse);
165 updateMouseButtonHints();
169 graphicView->moveRelativeZero(mouse);
171 history.append(new Vector(mouse));
172 //bHistory.append(new double(0.0));
176 //graphicView->deleteEntity(spline);
177 spline->addControlPoint(mouse);
178 //spline->setEndpoint(mouse);
179 //if (spline->count()==1) {
180 //spline->setLayerToActive();
181 //spline->setPenToActive();
182 //container->addEntity(spline);
187 //graphicView->drawEntity(spline);
192 //data.startpoint = data.endpoint;
193 updateMouseButtonHints();
194 //graphicView->moveRelativeZero(mouse);
202 void RS_ActionDrawSpline::commandEvent(RS_CommandEvent * e)
204 QString c = e->getCommand().toLower();
210 if (checkCommand("help", c))
212 RS_DIALOGFACTORY->commandMessage(msgAvailableCommands()
213 + getAvailableCommands().join(", "));
219 /*if (checkCommand("close", c)) {
221 updateMouseButtonHints();
225 if (checkCommand("undo", c))
228 updateMouseButtonHints();
238 QStringList RS_ActionDrawSpline::getAvailableCommands()
249 if (history.count() >= 2)
250 cmd += command("undo");
253 if (history.count() >= 3)
254 cmd += command("close");
264 void RS_ActionDrawSpline::updateMouseButtonHints()
269 RS_DIALOGFACTORY->updateMouseWidget(tr("Specify first control point"),
276 if (history.count() >= 3)
278 msg += RS_COMMANDS->command("close");
282 if (history.count() >= 2)
283 msg += RS_COMMANDS->command("undo");
285 if (history.count() >= 2)
286 RS_DIALOGFACTORY->updateMouseWidget(
287 tr("Specify next control point or [%1]").arg(msg),
290 RS_DIALOGFACTORY->updateMouseWidget(
291 tr("Specify next control point"),
297 RS_DIALOGFACTORY->updateMouseWidget("", "");
302 void RS_ActionDrawSpline::showOptions()
304 RS_ActionInterface::showOptions();
306 RS_DIALOGFACTORY->requestOptions(this, true);
309 void RS_ActionDrawSpline::hideOptions()
311 RS_ActionInterface::hideOptions();
313 RS_DIALOGFACTORY->requestOptions(this, false);
316 void RS_ActionDrawSpline::updateMouseCursor()
318 graphicView->setMouseCursor(RS2::CadCursor);
321 void RS_ActionDrawSpline::updateToolBar()
324 RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarSnap);
326 //RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarSplines);
327 RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarMain);
331 void RS_ActionDrawSpline::close() {
332 if (history.count()>2 && start.valid) {
333 //data.endpoint = start;
336 Vector e(spline->getStartpoint());
340 setStatus(SetStartpoint);
341 graphicView->moveRelativeZero(start);
343 RS_DIALOGFACTORY->commandMessage(
344 tr("Cannot close sequence of lines: "
345 "Not enough entities defined yet."));
350 void RS_ActionDrawSpline::undo()
352 if (history.count() > 1)
354 history.removeLast();
355 //bHistory.removeLast();
359 //graphicView->setCurrentAction(
360 // new RS_ActionEditUndo(true, *container, *graphicView));
361 if (history.last() != NULL)
363 //point = *history.last();
368 spline->removeLastControlPoint();
369 Vector * v = history.last();
372 graphicView->moveRelativeZero(*v);
373 graphicView->redraw();
377 RS_DIALOGFACTORY->commandMessage(
379 "Not enough entities defined yet."));
382 void RS_ActionDrawSpline::setDegree(int deg)
387 spline->setDegree(deg);
390 int RS_ActionDrawSpline::getDegree()
395 void RS_ActionDrawSpline::setClosed(bool c)
400 spline->setClosed(c);
403 bool RS_ActionDrawSpline::isClosed()