1 /****************************************************************************
2 ** $Id: rs_actiondrawspline.cpp 1140 2004-07-24 23:14:15Z andrew $
4 ** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
6 ** This file is part of the qcadlib Library project.
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.
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.
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.
20 ** See http://www.ribbonsoft.com for further details.
22 ** Contact info@ribbonsoft.com if any conditions of this licensing are
25 **********************************************************************/
27 #include "rs_actiondrawspline.h"
29 #include "rs_actioneditundo.h"
30 #include "rs_snapper.h"
32 RS_ActionDrawSpline::RS_ActionDrawSpline(RS_EntityContainer & container,
33 RS_GraphicView & graphicView):
34 RS_PreviewActionInterface("Draw splines", container, graphicView)
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);
43 RS_ActionDrawSpline::~RS_ActionDrawSpline()
47 /*virtual*/ RS2::ActionType RS_ActionDrawSpline::rtti()
49 return RS2::ActionDrawSpline;
52 QAction * RS_ActionDrawSpline::createGUIAction(RS2::ActionType /*type*/, QObject * /*parent*/)
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"));
61 void RS_ActionDrawSpline::reset()
64 //start = Vector(false);
71 void RS_ActionDrawSpline::init(int status) {
72 RS_PreviewActionInterface::init(status);
79 void RS_ActionDrawSpline::trigger() {
80 RS_PreviewActionInterface::trigger();
87 //RS_Spline* spline = new RS_Spline(container, data);
88 spline->setLayerToActive();
89 spline->setPenToActive();
91 container->addEntity(spline);
95 document->startUndoCycle();
96 document->addUndoable(spline);
97 document->endUndoCycle();
102 Vector r = graphicView->getRelativeZero();
103 graphicView->moveRelativeZero(Vector(0.0,0.0));
104 graphicView->drawEntity(spline);
105 graphicView->moveRelativeZero(r);
107 RS_DEBUG->print("RS_ActionDrawSpline::trigger(): spline added: %d", spline->getId());
113 void RS_ActionDrawSpline::mouseMoveEvent(QMouseEvent * e)
115 RS_DEBUG->print("RS_ActionDrawSpline::mouseMoveEvent begin");
117 Vector mouse = snapPoint(e);
119 if (getStatus() == SetNextPoint && spline != NULL /*&& point.valid*/)
124 RS_Spline * tmpSpline = (RS_Spline *)spline->clone();
125 tmpSpline->addControlPoint(mouse);
127 preview->addEntity(tmpSpline);
129 // Q3ValueList<Vector> cpts = tmpSpline->getControlPoints();
130 // Q3ValueList<Vector>::iterator it;
131 QList<Vector> cpts = tmpSpline->getControlPoints();
132 QList<Vector>::iterator it;
134 for(it=cpts.begin(); it!=cpts.end(); ++it)
135 preview->addEntity(new RS_Point(preview, RS_PointData(*it)));
140 RS_DEBUG->print("RS_ActionDrawSpline::mouseMoveEvent end");
143 void RS_ActionDrawSpline::mouseReleaseEvent(QMouseEvent * e)
145 if (RS2::qtToRsButtonState(e->button()) == RS2::LeftButton)
147 RS_CoordinateEvent ce(snapPoint(e));
148 coordinateEvent(&ce);
150 else if (RS2::qtToRsButtonState(e->button()) == RS2::RightButton)
152 if (getStatus() == SetNextPoint)
165 void RS_ActionDrawSpline::coordinateEvent(RS_CoordinateEvent* e) {
170 Vector mouse = e->getCoordinate();
172 switch (getStatus()) {
174 //data.startpoint = mouse;
177 history.append(new Vector(mouse));
179 spline = new RS_Spline(container, data);
180 spline->addControlPoint(mouse);
183 //bHistory.append(new double(0.0));
185 setStatus(SetNextPoint);
186 graphicView->moveRelativeZero(mouse);
187 updateMouseButtonHints();
191 graphicView->moveRelativeZero(mouse);
193 history.append(new Vector(mouse));
194 //bHistory.append(new double(0.0));
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);
207 //graphicView->drawEntity(spline);
211 //data.startpoint = data.endpoint;
212 updateMouseButtonHints();
213 //graphicView->moveRelativeZero(mouse);
223 void RS_ActionDrawSpline::commandEvent(RS_CommandEvent* e) {
224 QString c = e->getCommand().toLower();
226 switch (getStatus()) {
228 if (checkCommand("help", c)) {
229 RS_DIALOGFACTORY->commandMessage(msgAvailableCommands()
230 + getAvailableCommands().join(", "));
236 /*if (checkCommand("close", c)) {
238 updateMouseButtonHints();
242 if (checkCommand("undo", c)) {
244 updateMouseButtonHints();
256 QStringList RS_ActionDrawSpline::getAvailableCommands() {
259 switch (getStatus()) {
263 if (history.count()>=2) {
264 cmd += command("undo");
266 if (history.count()>=3) {
267 cmd += command("close");
279 void RS_ActionDrawSpline::updateMouseButtonHints() {
280 switch (getStatus()) {
282 RS_DIALOGFACTORY->updateMouseWidget(tr("Specify first control point"),
288 if (history.count()>=3) {
289 msg += RS_COMMANDS->command("close");
292 if (history.count()>=2) {
293 msg += RS_COMMANDS->command("undo");
296 if (history.count()>=2) {
297 RS_DIALOGFACTORY->updateMouseWidget(
298 tr("Specify next control point or [%1]").arg(msg),
301 RS_DIALOGFACTORY->updateMouseWidget(
302 tr("Specify next control point"),
308 RS_DIALOGFACTORY->updateMouseWidget("", "");
314 void RS_ActionDrawSpline::showOptions() {
315 RS_ActionInterface::showOptions();
317 RS_DIALOGFACTORY->requestOptions(this, true);
322 void RS_ActionDrawSpline::hideOptions() {
323 RS_ActionInterface::hideOptions();
325 RS_DIALOGFACTORY->requestOptions(this, false);
329 void RS_ActionDrawSpline::updateMouseCursor() {
330 graphicView->setMouseCursor(RS2::CadCursor);
334 void RS_ActionDrawSpline::updateToolBar() {
336 RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarSnap);
338 //RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarSplines);
339 RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarMain);
345 void RS_ActionDrawSpline::close() {
346 if (history.count()>2 && start.valid) {
347 //data.endpoint = start;
350 RS_CoordinateEvent e(spline->getStartpoint());
354 setStatus(SetStartpoint);
355 graphicView->moveRelativeZero(start);
357 RS_DIALOGFACTORY->commandMessage(
358 tr("Cannot close sequence of lines: "
359 "Not enough entities defined yet."));
364 void RS_ActionDrawSpline::undo() {
365 if (history.count()>1) {
366 history.removeLast();
367 //bHistory.removeLast();
370 //graphicView->setCurrentAction(
371 // new RS_ActionEditUndo(true, *container, *graphicView));
372 if (history.last()!=NULL) {
373 //point = *history.last();
376 spline->removeLastControlPoint();
377 Vector* v = history.last();
379 graphicView->moveRelativeZero(*v);
381 graphicView->redraw();
384 RS_DIALOGFACTORY->commandMessage(
386 "Not enough entities defined yet."));
391 void RS_ActionDrawSpline::setDegree(int deg) {
394 spline->setDegree(deg);
398 int RS_ActionDrawSpline::getDegree() {
402 void RS_ActionDrawSpline::setClosed(bool c) {
405 spline->setClosed(c);
409 bool RS_ActionDrawSpline::isClosed() {