From fc4054e49e6ebd2970f6f47a50a84cf45af51802 Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Fri, 13 May 2016 16:50:30 +0300 Subject: [PATCH] Undocommand move a label of rotated point. --HG-- branch : feature --- .../drawTools/operation/vtoolrotation.cpp | 7 +- .../vtools/undocommands/rotationmovelabel.cpp | 177 ++++++++++++++++++ .../vtools/undocommands/rotationmovelabel.h | 91 +++++++++ src/libs/vtools/undocommands/undocommands.pri | 6 +- src/libs/vtools/undocommands/vundocommand.h | 3 +- 5 files changed, 278 insertions(+), 6 deletions(-) create mode 100644 src/libs/vtools/undocommands/rotationmovelabel.cpp create mode 100644 src/libs/vtools/undocommands/rotationmovelabel.h diff --git a/src/libs/vtools/tools/drawTools/operation/vtoolrotation.cpp b/src/libs/vtools/tools/drawTools/operation/vtoolrotation.cpp index 8372e1b0b..3e0e62cd0 100644 --- a/src/libs/vtools/tools/drawTools/operation/vtoolrotation.cpp +++ b/src/libs/vtools/tools/drawTools/operation/vtoolrotation.cpp @@ -38,6 +38,7 @@ #include "../../../visualization/line/vistoolrotation.h" #include "../vwidgets/vsimplepoint.h" #include "../vwidgets/vsimplecurve.h" +#include "../../../undocommands/rotationmovelabel.h" const QString VToolRotation::TagName = QStringLiteral("operation"); const QString VToolRotation::TagItem = QStringLiteral("item"); @@ -702,9 +703,9 @@ void VToolRotation::DoChangePosition(quint32 id, qreal mx, qreal my) void VToolRotation::UpdateNamePosition(quint32 id) { const QSharedPointer point = VAbstractTool::data.GeometricObject(id); -// auto moveLabel = new MoveLabel(doc, point->mx(), point->my(), id, scene()); -// connect(moveLabel, &MoveLabel::ChangePosition, this, &VToolSinglePoint::DoChangePosition); -// qApp->getUndoStack()->push(moveLabel); + auto moveLabel = new RotationMoveLabel(this->id, doc, point->mx(), point->my(), id, scene()); + connect(moveLabel, &RotationMoveLabel::ChangePosition, this, &VToolRotation::DoChangePosition); + qApp->getUndoStack()->push(moveLabel); } //--------------------------------------------------------------------------------------------------------------------- diff --git a/src/libs/vtools/undocommands/rotationmovelabel.cpp b/src/libs/vtools/undocommands/rotationmovelabel.cpp new file mode 100644 index 000000000..12712a264 --- /dev/null +++ b/src/libs/vtools/undocommands/rotationmovelabel.cpp @@ -0,0 +1,177 @@ +/************************************************************************ + ** + ** @file moverotationlabel.cpp + ** @author Roman Telezhynskyi + ** @date 13 5, 2016 + ** + ** @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) 2016 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 "rotationmovelabel.h" +#include "../tools/drawTools/operation/vtoolrotation.h" + +//--------------------------------------------------------------------------------------------------------------------- +RotationMoveLabel::RotationMoveLabel(quint32 idTool, VAbstractPattern *doc, double x, double y, quint32 idPoint, + QGraphicsScene *scene, QUndoCommand *parent) + : VUndoCommand(QDomElement(), doc, parent), + m_oldMx(0.0), + m_oldMy(0.0), + m_newMx(x), + m_newMy(y), + m_isRedo(false), + m_scene(scene), + m_idTool(idTool) +{ + setText(tr("move point label")); + nodeId = idPoint; + qCDebug(vUndo, "Tool id %u", m_idTool); + qCDebug(vUndo, "Point id %u", nodeId); + + qCDebug(vUndo, "Label new Mx %f", m_newMx); + qCDebug(vUndo, "Label new My %f", m_newMy); + + SCASSERT(scene != nullptr); + QDomElement element = GetDestinationObject(m_idTool, nodeId); + if (element.isElement()) + { + m_oldMx = qApp->toPixel(doc->GetParametrDouble(element, AttrMx, "0.0")); + m_oldMy = qApp->toPixel(doc->GetParametrDouble(element, AttrMy, "0.0")); + + qCDebug(vUndo, "Label old Mx %f", m_oldMx); + qCDebug(vUndo, "Label old My %f", m_oldMy); + } + else + { + qCDebug(vUndo, "Can't find point with id = %u.", nodeId); + return; + } +} + +//--------------------------------------------------------------------------------------------------------------------- +RotationMoveLabel::~RotationMoveLabel() +{ +} + +//--------------------------------------------------------------------------------------------------------------------- +void RotationMoveLabel::undo() +{ + qCDebug(vUndo, "Undo."); + + Do(m_oldMx, m_oldMy); + m_isRedo = true; + emit ChangePosition(nodeId, m_oldMx, m_oldMy); +} + +//--------------------------------------------------------------------------------------------------------------------- +void RotationMoveLabel::redo() +{ + qCDebug(vUndo, "Redo."); + + Do(m_newMx, m_newMy); + if (m_isRedo) + { + emit ChangePosition(nodeId, m_newMx, m_newMy); + } +} + +//--------------------------------------------------------------------------------------------------------------------- +bool RotationMoveLabel::mergeWith(const QUndoCommand *command) +{ + const RotationMoveLabel *moveCommand = static_cast(command); + SCASSERT(moveCommand != nullptr); + + if (moveCommand->GetToolId() != m_idTool && moveCommand->GetPointId() != nodeId) + { + return false; + } + + qCDebug(vUndo, "Mergin undo."); + m_newMx = moveCommand->getNewMx(); + m_newMy = moveCommand->getNewMy(); + qCDebug(vUndo, "Label new Mx %f", m_newMx); + qCDebug(vUndo, "Label new My %f", m_newMy); + return true; +} + +//--------------------------------------------------------------------------------------------------------------------- +int RotationMoveLabel::id() const +{ + return static_cast(UndoCommand::RotationMoveLabel); +} + +//--------------------------------------------------------------------------------------------------------------------- +void RotationMoveLabel::Do(double mx, double my) +{ + qCDebug(vUndo, "New mx %f", mx); + qCDebug(vUndo, "New my %f", my); + + QDomElement domElement = GetDestinationObject(m_idTool, nodeId); + if (not domElement.isNull() && domElement.isElement()) + { + doc->SetAttribute(domElement, AttrMx, QString().setNum(qApp->fromPixel(mx))); + doc->SetAttribute(domElement, AttrMy, QString().setNum(qApp->fromPixel(my))); + } + else + { + qCDebug(vUndo, "Can't find point with id = %u.", nodeId); + return; + } +} + +//--------------------------------------------------------------------------------------------------------------------- +QDomElement RotationMoveLabel::GetDestinationObject(quint32 idTool, quint32 idPoint) const +{ + const QDomElement tool = doc->elementById(idTool); + if (tool.isElement()) + { + QDomElement correctDest; + const QDomNodeList nodeList = tool.childNodes(); + for (qint32 i = 0; i < nodeList.size(); ++i) + { + const QDomElement dest = nodeList.at(i).toElement(); + if (not dest.isNull() && dest.isElement() && dest.tagName() == VToolRotation::TagDestination) + { + correctDest = dest; + break; + } + } + + if (not correctDest.isNull()) + { + const QDomNodeList destObjects = correctDest.childNodes(); + for (qint32 i = 0; i < destObjects.size(); ++i) + { + const QDomElement obj = destObjects.at(i).toElement(); + if (not obj.isNull() && obj.isElement()) + { + const quint32 id = doc->GetParametrUInt(obj, AttrIdObject, NULL_ID_STR); + if (idPoint == id) + { + return obj; + } + } + } + } + } + + return QDomElement(); +} diff --git a/src/libs/vtools/undocommands/rotationmovelabel.h b/src/libs/vtools/undocommands/rotationmovelabel.h new file mode 100644 index 000000000..fe19ffe0c --- /dev/null +++ b/src/libs/vtools/undocommands/rotationmovelabel.h @@ -0,0 +1,91 @@ +/************************************************************************ + ** + ** @file moverotationlabel.h + ** @author Roman Telezhynskyi + ** @date 13 5, 2016 + ** + ** @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) 2016 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 ROTATIONMOVELABEL_H +#define ROTATIONMOVELABEL_H + +#include "vundocommand.h" + +class QGraphicsScene; + +class RotationMoveLabel : public VUndoCommand +{ + Q_OBJECT +public: + RotationMoveLabel(quint32 idTool, VAbstractPattern *doc, double x, double y, quint32 idPoint, QGraphicsScene *scene, + QUndoCommand *parent = nullptr); + virtual ~RotationMoveLabel(); + virtual void undo() Q_DECL_OVERRIDE; + virtual void redo() Q_DECL_OVERRIDE; + virtual bool mergeWith(const QUndoCommand *command) Q_DECL_OVERRIDE; + virtual int id() const Q_DECL_OVERRIDE; + quint32 GetPointId() const; + quint32 GetToolId() const; + double getNewMx() const; + double getNewMy() const; +signals: + void ChangePosition(quint32 id, qreal mx, qreal my); +private: + Q_DISABLE_COPY(RotationMoveLabel) + double m_oldMx; + double m_oldMy; + double m_newMx; + double m_newMy; + bool m_isRedo; + QGraphicsScene *m_scene; + quint32 m_idTool; + + void Do(double mx, double my); + QDomElement GetDestinationObject(quint32 idTool, quint32 idPoint) const; +}; + +//--------------------------------------------------------------------------------------------------------------------- +inline quint32 RotationMoveLabel::GetPointId() const +{ + return nodeId; +} + +//--------------------------------------------------------------------------------------------------------------------- +inline double RotationMoveLabel::getNewMx() const +{ + return m_newMx; +} + +//--------------------------------------------------------------------------------------------------------------------- +inline double RotationMoveLabel::getNewMy() const +{ + return m_newMy; +} + +//--------------------------------------------------------------------------------------------------------------------- +inline quint32 RotationMoveLabel::GetToolId() const +{ + return m_idTool; +} + +#endif // ROTATIONMOVELABEL_H diff --git a/src/libs/vtools/undocommands/undocommands.pri b/src/libs/vtools/undocommands/undocommands.pri index 0ce6cb9ec..ffd3417e4 100644 --- a/src/libs/vtools/undocommands/undocommands.pri +++ b/src/libs/vtools/undocommands/undocommands.pri @@ -20,7 +20,8 @@ HEADERS += \ $$PWD/movelabel.h \ $$PWD/movedoublelabel.h \ $$PWD/addgroup.h \ - $$PWD/delgroup.h + $$PWD/delgroup.h \ + $$PWD/rotationmovelabel.h SOURCES += \ $$PWD/addtocalc.cpp \ @@ -41,4 +42,5 @@ SOURCES += \ $$PWD/movelabel.cpp \ $$PWD/movedoublelabel.cpp \ $$PWD/addgroup.cpp \ - $$PWD/delgroup.cpp + $$PWD/delgroup.cpp \ + $$PWD/rotationmovelabel.cpp diff --git a/src/libs/vtools/undocommands/vundocommand.h b/src/libs/vtools/undocommands/vundocommand.h index b97134e6b..e86cfce82 100644 --- a/src/libs/vtools/undocommands/vundocommand.h +++ b/src/libs/vtools/undocommands/vundocommand.h @@ -49,7 +49,8 @@ enum class UndoCommand: char { AddPatternPiece, DeletePatternPiece, RenamePP, MoveLabel, - MoveDoubleLabel + MoveDoubleLabel, + RotationMoveLabel }; class VPattern;