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 // Q3ValueList<Vector> cpts = tmpSpline->getControlPoints();
108 // Q3ValueList<Vector>::iterator it;
109 QList<Vector> cpts = tmpSpline->getControlPoints();
110 QList<Vector>::iterator it;
112 for (it = cpts.begin(); it != cpts.end(); ++it)
113 preview->addEntity(new RS_Point(preview, RS_PointData(*it)));
118 RS_DEBUG->print("RS_ActionDrawSpline::mouseMoveEvent end");
121 void RS_ActionDrawSpline::mouseReleaseEvent(QMouseEvent * e)
123 if (RS2::qtToRsButtonState(e->button()) == RS2::LeftButton)
125 Vector ce(snapPoint(e));
126 coordinateEvent(&ce);
128 else if (RS2::qtToRsButtonState(e->button()) == RS2::RightButton)
130 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()