From 7fed35ad1ceaeead639071659bf2dad926ca99a3 Mon Sep 17 00:00:00 2001 From: dismine Date: Fri, 13 Jun 2014 13:29:23 +0300 Subject: [PATCH] New undo command MoveDetail. --HG-- branch : feature --- src/app/tools/vtooldetail.cpp | 21 ++--- src/app/undocommands/movedetail.cpp | 122 ++++++++++++++++++++++++++ src/app/undocommands/movedetail.h | 82 +++++++++++++++++ src/app/undocommands/movespoint.cpp | 6 +- src/app/undocommands/undocommands.h | 3 +- src/app/undocommands/undocommands.pri | 6 +- 6 files changed, 222 insertions(+), 18 deletions(-) create mode 100644 src/app/undocommands/movedetail.cpp create mode 100644 src/app/undocommands/movedetail.h diff --git a/src/app/tools/vtooldetail.cpp b/src/app/tools/vtooldetail.cpp index 8a95a0723..12851add8 100644 --- a/src/app/tools/vtooldetail.cpp +++ b/src/app/tools/vtooldetail.cpp @@ -39,6 +39,7 @@ #include #include #include "../undocommands/savedetailoptions.h" +#include "../undocommands/movedetail.h" const QString VToolDetail::TagName = QStringLiteral("detail"); const QString VToolDetail::TagNode = QStringLiteral("node"); @@ -109,7 +110,6 @@ VToolDetail::VToolDetail(VPattern *doc, VContainer *data, const quint32 &id, con this->setFlag(QGraphicsItem::ItemIsMovable, true); this->setFlag(QGraphicsItem::ItemIsSelectable, true); RefreshGeometry(); - this->setPos(detail.getMx(), detail.getMy()); this->setFlag(QGraphicsItem::ItemSendsGeometryChanges, true); this->setFlag(QGraphicsItem::ItemIsFocusable, true); if (typeCreation == Source::FromGui || typeCreation == Source::FromTool) @@ -313,20 +313,13 @@ QVariant VToolDetail::itemChange(QGraphicsItem::GraphicsItemChange change, const { if (change == ItemPositionHasChanged && scene()) { + this->setFlag(QGraphicsItem::ItemSendsGeometryChanges, false); // value - this is new position. QPointF newPos = value.toPointF(); - //qDebug()<elementById(QString().setNum(id)); - if (domElement.isElement()) - { - doc->SetAttribute(domElement, AttrMx, QString().setNum(qApp->fromPixel(newPos.x()))); - doc->SetAttribute(domElement, AttrMy, QString().setNum(qApp->fromPixel(newPos.y()))); - QList list = this->scene()->views(); - VAbstractTool::NewSceneRect(this->scene(), list[0]); - - doc->haveLiteChange(); - } + MoveDetail *moveDet = new MoveDetail(doc, newPos.x(), newPos.y(), id, this->scene()); + connect(moveDet, &MoveDetail::NeedLiteParsing, doc, &VPattern::LiteParseTree); + qApp->getUndoStack()->push(moveDet); } if (change == QGraphicsItem::ItemSelectedChange) @@ -474,6 +467,10 @@ void VToolDetail::RefreshGeometry() { QPainterPath path = VEquidistant().ContourPath(id, this->getData()); this->setPath(path); + + VDetail detail = VAbstractTool::data.GetDetail(id); + this->setPos(detail.getMx(), detail.getMy()); + this->setFlag(QGraphicsItem::ItemSendsGeometryChanges, true); } //--------------------------------------------------------------------------------------------------------------------- diff --git a/src/app/undocommands/movedetail.cpp b/src/app/undocommands/movedetail.cpp new file mode 100644 index 000000000..c0f859b45 --- /dev/null +++ b/src/app/undocommands/movedetail.cpp @@ -0,0 +1,122 @@ +/************************************************************************ + ** + ** @file movedetail.cpp + ** @author Roman Telezhynskyi + ** @date 13 6, 2014 + ** + ** @brief + ** @copyright + ** This source code is part of the Valentine project, a pattern making + ** program, whose allow create and modeling patterns of clothing. + ** Copyright (C) 2014 Valentina project + ** All Rights Reserved. + ** + ** Valentina is free software: you can redistribute it and/or modify + ** it under the terms of the GNU General Public License as published by + ** the Free Software Foundation, either version 3 of the License, or + ** (at your option) any later version. + ** + ** Valentina is distributed in the hope that it will be useful, + ** but WITHOUT ANY WARRANTY; without even the implied warranty of + ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + ** GNU General Public License for more details. + ** + ** You should have received a copy of the GNU General Public License + ** along with Valentina. If not, see . + ** + *************************************************************************/ + +#include "movedetail.h" +#include +#include +#include "../xml/vpattern.h" +#include "../tools/vabstracttool.h" +#include "../widgets/vapplication.h" +#include "undocommands.h" + +//--------------------------------------------------------------------------------------------------------------------- +MoveDetail::MoveDetail(VPattern *doc, const double &x, const double &y, const quint32 &id, QGraphicsScene *scene, + QUndoCommand *parent) + : QObject(), QUndoCommand(parent), doc(doc), oldX(0.0), oldY(0.0), newX(x), newY(y), detId(id), scene(scene) +{ + setText(QObject::tr("Move detail")); + + SCASSERT(scene != nullptr); + QDomElement domElement = doc->elementById(QString().setNum(id)); + if (domElement.isElement()) + { + oldX = qApp->toPixel(doc->GetParametrDouble(domElement, VAbstractTool::AttrMx, "0.0")); + oldY = qApp->toPixel(doc->GetParametrDouble(domElement, VAbstractTool::AttrMy, "0.0")); + } + else + { + qDebug()<<"Can't find detail with id ="<< detId << Q_FUNC_INFO; + } +} + +//--------------------------------------------------------------------------------------------------------------------- +MoveDetail::~MoveDetail() +{} + +//--------------------------------------------------------------------------------------------------------------------- +void MoveDetail::undo() +{ + QDomElement domElement = doc->elementById(QString().setNum(detId)); + if (domElement.isElement()) + { + doc->SetAttribute(domElement, VAbstractTool::AttrMx, QString().setNum(qApp->fromPixel(oldX))); + doc->SetAttribute(domElement, VAbstractTool::AttrMy, QString().setNum(qApp->fromPixel(oldY))); + + emit NeedLiteParsing(); + + QList list = scene->views(); + VAbstractTool::NewSceneRect(scene, list[0]); + } + else + { + qDebug()<<"Can't find detail with id ="<< detId << Q_FUNC_INFO; + } +} + +//--------------------------------------------------------------------------------------------------------------------- +void MoveDetail::redo() +{ + QDomElement domElement = doc->elementById(QString().setNum(detId)); + if (domElement.isElement()) + { + doc->SetAttribute(domElement, VAbstractTool::AttrMx, QString().setNum(qApp->fromPixel(newX))); + doc->SetAttribute(domElement, VAbstractTool::AttrMy, QString().setNum(qApp->fromPixel(newY))); + + emit NeedLiteParsing(); + + QList list = scene->views(); + VAbstractTool::NewSceneRect(scene, list[0]); + } + else + { + qDebug()<<"Can't find detail with id ="<< detId << Q_FUNC_INFO; + } +} + +//--------------------------------------------------------------------------------------------------------------------- +bool MoveDetail::mergeWith(const QUndoCommand *command) +{ + const MoveDetail *moveCommand = static_cast(command); + SCASSERT(moveCommand != nullptr); + const quint32 id = moveCommand->getDetId(); + + if (id != detId) + { + return false; + } + + newX = moveCommand->getNewX(); + newY = moveCommand->getNewY(); + return true; +} + +//--------------------------------------------------------------------------------------------------------------------- +int MoveDetail::id() const +{ + return static_cast(UndoCommand::MoveDetail); +} diff --git a/src/app/undocommands/movedetail.h b/src/app/undocommands/movedetail.h new file mode 100644 index 000000000..d53413d6b --- /dev/null +++ b/src/app/undocommands/movedetail.h @@ -0,0 +1,82 @@ +/************************************************************************ + ** + ** @file movedetail.h + ** @author Roman Telezhynskyi + ** @date 13 6, 2014 + ** + ** @brief + ** @copyright + ** This source code is part of the Valentine project, a pattern making + ** program, whose allow create and modeling patterns of clothing. + ** Copyright (C) 2014 Valentina project + ** All Rights Reserved. + ** + ** Valentina is free software: you can redistribute it and/or modify + ** it under the terms of the GNU General Public License as published by + ** the Free Software Foundation, either version 3 of the License, or + ** (at your option) any later version. + ** + ** Valentina is distributed in the hope that it will be useful, + ** but WITHOUT ANY WARRANTY; without even the implied warranty of + ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + ** GNU General Public License for more details. + ** + ** You should have received a copy of the GNU General Public License + ** along with Valentina. If not, see . + ** + *************************************************************************/ + +#ifndef MOVEDETAIL_H +#define MOVEDETAIL_H + +#include + +class VPattern; +class QGraphicsScene; + +class MoveDetail : public QObject, public QUndoCommand +{ + Q_OBJECT +public: + MoveDetail(VPattern *doc, const double &x, const double &y, const quint32 &id, QGraphicsScene *scene, + QUndoCommand *parent = 0); + virtual ~MoveDetail(); + virtual void undo(); + virtual void redo(); + virtual bool mergeWith(const QUndoCommand *command); + virtual int id() const; + quint32 getDetId() const; + double getNewX() const; + double getNewY() const; +signals: + void NeedLiteParsing(); +private: + Q_DISABLE_COPY(MoveDetail) + VPattern *doc; + double oldX; + double oldY; + double newX; + double newY; + quint32 detId; + QGraphicsScene *scene; +}; + +//--------------------------------------------------------------------------------------------------------------------- +inline quint32 MoveDetail::getDetId() const +{ + return detId; +} + +//--------------------------------------------------------------------------------------------------------------------- +inline double MoveDetail::getNewX() const +{ + return newX; +} + +//--------------------------------------------------------------------------------------------------------------------- +inline double MoveDetail::getNewY() const +{ + return newY; +} + +#endif // MOVEDETAIL_H diff --git a/src/app/undocommands/movespoint.cpp b/src/app/undocommands/movespoint.cpp index 940612c01..90587b4b5 100644 --- a/src/app/undocommands/movespoint.cpp +++ b/src/app/undocommands/movespoint.cpp @@ -37,7 +37,7 @@ //--------------------------------------------------------------------------------------------------------------------- MoveSPoint::MoveSPoint(VPattern *doc, const double &x, const double &y, const quint32 &id, QGraphicsScene *scene, QUndoCommand *parent) - : QObject(), QUndoCommand(parent), doc(doc), oldX(10.0), oldY(10.0), newX(x), newY(y), sPointId(id), scene(scene) + : QObject(), QUndoCommand(parent), doc(doc), oldX(0.0), oldY(0.0), newX(x), newY(y), sPointId(id), scene(scene) { setText(tr("Move single point")); @@ -45,8 +45,8 @@ MoveSPoint::MoveSPoint(VPattern *doc, const double &x, const double &y, const qu QDomElement domElement = doc->elementById(QString().setNum(id)); if (domElement.isElement()) { - oldX = qApp->toPixel(doc->GetParametrDouble(domElement, VAbstractTool::AttrX, "10.0")); - oldY = qApp->toPixel(doc->GetParametrDouble(domElement, VAbstractTool::AttrY, "10.0")); + oldX = qApp->toPixel(doc->GetParametrDouble(domElement, VAbstractTool::AttrX, "0.0")); + oldY = qApp->toPixel(doc->GetParametrDouble(domElement, VAbstractTool::AttrY, "0.0")); } else { diff --git a/src/app/undocommands/undocommands.h b/src/app/undocommands/undocommands.h index c87f6547d..5c3b8310b 100644 --- a/src/app/undocommands/undocommands.h +++ b/src/app/undocommands/undocommands.h @@ -35,7 +35,8 @@ enum class UndoCommand: char { AddPatternPiece, MoveSplinePath, MoveSPoint, SaveToolOptions, - SaveDetailOptions + SaveDetailOptions, + MoveDetail }; #endif // UNDOCOMMANDS_H diff --git a/src/app/undocommands/undocommands.pri b/src/app/undocommands/undocommands.pri index 80e87b370..c3c0f7a34 100644 --- a/src/app/undocommands/undocommands.pri +++ b/src/app/undocommands/undocommands.pri @@ -6,7 +6,8 @@ HEADERS += \ undocommands/movesplinepath.h \ undocommands/savetooloptions.h \ undocommands/undocommands.h \ - undocommands/savedetailoptions.h + undocommands/savedetailoptions.h \ + undocommands/movedetail.h SOURCES += \ @@ -16,5 +17,6 @@ SOURCES += \ undocommands/movespline.cpp \ undocommands/movesplinepath.cpp \ undocommands/savetooloptions.cpp \ - undocommands/savedetailoptions.cpp + undocommands/savedetailoptions.cpp \ + undocommands/movedetail.cpp