From 988ce8f7f2ad45801e763265d5294dfc4e1626ab Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Wed, 9 Nov 2016 14:11:58 +0200 Subject: [PATCH] Returned undocommand "Save piece options". --HG-- branch : feature --- src/libs/vpatterndb/vpiece.cpp | 71 +++++++++ src/libs/vpatterndb/vpiece.h | 6 + src/libs/vtools/tools/vtoolseamallowance.cpp | 61 ++++++-- src/libs/vtools/tools/vtoolseamallowance.h | 6 +- .../vtools/undocommands/savepieceoptions.cpp | 136 ++++++++++++++++++ .../vtools/undocommands/savepieceoptions.h | 75 ++++++++++ src/libs/vtools/undocommands/undocommands.pri | 6 +- src/libs/vtools/undocommands/vundocommand.cpp | 9 ++ src/libs/vtools/undocommands/vundocommand.h | 2 + 9 files changed, 360 insertions(+), 12 deletions(-) create mode 100644 src/libs/vtools/undocommands/savepieceoptions.cpp create mode 100644 src/libs/vtools/undocommands/savepieceoptions.h diff --git a/src/libs/vpatterndb/vpiece.cpp b/src/libs/vpatterndb/vpiece.cpp index 5a44c47ae..874681bc7 100644 --- a/src/libs/vpatterndb/vpiece.cpp +++ b/src/libs/vpatterndb/vpiece.cpp @@ -244,6 +244,57 @@ void VPiece::SetMy(qreal value) d->m_my = value; } +//--------------------------------------------------------------------------------------------------------------------- +/** + * @brief Missing find missing nodes in detail. When we deleted object in detail and return this detail need + * understand, what nodes need make invisible. + * @param det changed detail. + * @return list with missing nodes. + */ +QVector VPiece::Missing(const VPiece &det) const +{ + if (d->m_nodes.size() == det.CountNodes()) //-V807 + { + return QVector(); + } + + QSet set1; + for (qint32 i = 0; i < d->m_nodes.size(); ++i) + { + set1.insert(d->m_nodes.at(i).GetId()); + } + + QSet set2; + for (qint32 j = 0; j < det.CountNodes(); ++j) + { + set2.insert(det.at(j).GetId()); + } + + const QList set3 = set1.subtract(set2).toList(); + QVector nodes; + for (qint32 i = 0; i < set3.size(); ++i) + { + const int index = indexOfNode(d->m_nodes, set3.at(i)); + if (index != -1) + { + nodes.append(d->m_nodes.at(index)); + } + } + + return nodes; +} + +//--------------------------------------------------------------------------------------------------------------------- +/** + * @brief indexOfNode return index in list node using id object. + * @param id object (arc, point, spline, splinePath) id. + * @return index in list or -1 id can't find. + */ +int VPiece::indexOfNode(const quint32 &id) const +{ + return indexOfNode(d->m_nodes, id); +} + //--------------------------------------------------------------------------------------------------------------------- QPointF VPiece::StartSegment(const VContainer *data, const int &i, bool reverse) const { @@ -317,3 +368,23 @@ QPointF VPiece::EndSegment(const VContainer *data, const int &i, bool reverse) c } return end; } + +//--------------------------------------------------------------------------------------------------------------------- +/** + * @brief indexOfNode return index in list node using id object. + * @param list list nodes detail. + * @param id object (arc, point, spline, splinePath) id. + * @return index in list or -1 id can't find. + */ +int VPiece::indexOfNode(const QVector &list, quint32 id) +{ + for (int i = 0; i < list.size(); ++i) + { + if (list.at(i).GetId() == id) + { + return i; + } + } + qDebug()<<"Can't find node."; + return -1; +} diff --git a/src/libs/vpatterndb/vpiece.h b/src/libs/vpatterndb/vpiece.h index 4ae3a267a..09261d836 100644 --- a/src/libs/vpatterndb/vpiece.h +++ b/src/libs/vpatterndb/vpiece.h @@ -71,11 +71,17 @@ public: qreal GetMy() const; void SetMy(qreal value); + QVector Missing(const VPiece &det) const; + + int indexOfNode(const quint32 &id) const; + private: QSharedDataPointer d; QPointF StartSegment(const VContainer *data, const int &i, bool reverse) const; QPointF EndSegment(const VContainer *data, const int &i, bool reverse) const; + + static int indexOfNode(const QVector &list, quint32 id); }; Q_DECLARE_TYPEINFO(VPiece, Q_MOVABLE_TYPE); diff --git a/src/libs/vtools/tools/vtoolseamallowance.cpp b/src/libs/vtools/tools/vtoolseamallowance.cpp index a722a31dc..a8e2347e3 100644 --- a/src/libs/vtools/tools/vtoolseamallowance.cpp +++ b/src/libs/vtools/tools/vtoolseamallowance.cpp @@ -43,7 +43,7 @@ #include "../undocommands/addpiece.h" #include "../undocommands/deletepiece.h" #include "../undocommands/movepiece.h" -//#include "../undocommands/savepieceoptions.h" +#include "../undocommands/savepieceoptions.h" //#include "../undocommands/togglepieceinlayout.h" #include "../vwidgets/vmaingraphicsview.h" @@ -241,6 +241,35 @@ void VToolSeamAllowance::AddNodes(VAbstractPattern *doc, QDomElement &domElement } } +//--------------------------------------------------------------------------------------------------------------------- +void VToolSeamAllowance::AddAttributes(VAbstractPattern *doc, QDomElement &domElement, quint32 id, const VPiece &piece) +{ + SCASSERT(doc != nullptr); + + doc->SetAttribute(domElement, VDomDocument::AttrId, id); + doc->SetAttribute(domElement, AttrVersion, QString().setNum(pieceVersion)); + doc->SetAttribute(domElement, AttrMx, qApp->fromPixel(piece.GetMx())); + doc->SetAttribute(domElement, AttrMy, qApp->fromPixel(piece.GetMy())); +} + +//--------------------------------------------------------------------------------------------------------------------- +void VToolSeamAllowance::AddPatternPieceData(VAbstractPattern *doc, QDomElement &domElement, const VPiece &piece) +{ + +} + +//--------------------------------------------------------------------------------------------------------------------- +void VToolSeamAllowance::AddPatternInfo(VAbstractPattern *doc, QDomElement &domElement, const VPiece &piece) +{ + +} + +//--------------------------------------------------------------------------------------------------------------------- +void VToolSeamAllowance::AddGrainline(VAbstractPattern *doc, QDomElement &domElement, const VPiece &piece) +{ + +} + //--------------------------------------------------------------------------------------------------------------------- QString VToolSeamAllowance::getTagName() const { @@ -268,7 +297,21 @@ void VToolSeamAllowance::FullUpdateFromFile() //--------------------------------------------------------------------------------------------------------------------- void VToolSeamAllowance::FullUpdateFromGuiOk(int result) -{} +{ + if (result == QDialog::Accepted) + { + SCASSERT(not m_dialog.isNull()); + DialogSeamAllowance *dialogTool = qobject_cast(m_dialog.data()); + SCASSERT(dialogTool != nullptr); + const VPiece newDet = dialogTool->GetPiece(); + const VPiece oldDet = VAbstractTool::data.GetPiece(id); + + SavePieceOptions *saveCommand = new SavePieceOptions(oldDet, newDet, doc, id, this->scene()); + connect(saveCommand, &SavePieceOptions::NeedLiteParsing, doc, &VAbstractPattern::LiteParseTree); + qApp->getUndoStack()->push(saveCommand); + } + delete m_dialog; +} //--------------------------------------------------------------------------------------------------------------------- void VToolSeamAllowance::EnableToolMove(bool move) @@ -301,10 +344,10 @@ void VToolSeamAllowance::AddToFile() QDomElement domElement = doc->createElement(getTagName()); - doc->SetAttribute(domElement, VDomDocument::AttrId, id); - doc->SetAttribute(domElement, AttrVersion, QString().setNum(pieceVersion)); - doc->SetAttribute(domElement, AttrMx, qApp->fromPixel(piece.GetMx())); - doc->SetAttribute(domElement, AttrMy, qApp->fromPixel(piece.GetMy())); + AddAttributes(doc, domElement, id, piece); + AddPatternPieceData(doc, domElement, piece); + AddPatternInfo(doc, domElement, piece); + AddGrainline(doc, domElement, piece); // nodes AddNodes(doc, domElement, piece); @@ -490,7 +533,7 @@ void VToolSeamAllowance::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) dialog->EnableApply(true); m_dialog = dialog; m_dialog->setModal(true); - connect(m_dialog, &DialogTool::DialogClosed, this, &VToolSeamAllowance::FullUpdateFromGuiOk); + connect(m_dialog.data(), &DialogTool::DialogClosed, this, &VToolSeamAllowance::FullUpdateFromGuiOk); SetDialog(); m_dialog->show(); } @@ -542,7 +585,7 @@ void VToolSeamAllowance::keyReleaseEvent(QKeyEvent *event) //--------------------------------------------------------------------------------------------------------------------- void VToolSeamAllowance::SetDialog() { - SCASSERT(m_dialog != nullptr); + SCASSERT(not m_dialog.isNull()); DialogSeamAllowance *dialogTool = qobject_cast(m_dialog); SCASSERT(dialogTool != nullptr); dialogTool->SetPiece(VAbstractTool::data.GetPiece(id)); @@ -555,7 +598,7 @@ VToolSeamAllowance::VToolSeamAllowance(VAbstractPattern *doc, VContainer *data, const QString &drawName, QGraphicsItem *parent) : VAbstractTool(doc, data, id), VNoBrushScalePathItem(parent), - m_dialog(nullptr), + m_dialog(), m_sceneDetails(scene), m_drawName(drawName) { diff --git a/src/libs/vtools/tools/vtoolseamallowance.h b/src/libs/vtools/tools/vtoolseamallowance.h index 878b3d011..7e57059aa 100644 --- a/src/libs/vtools/tools/vtoolseamallowance.h +++ b/src/libs/vtools/tools/vtoolseamallowance.h @@ -69,6 +69,10 @@ public: static void AddNode(VAbstractPattern *doc, QDomElement &domElement, const VPieceNode &node); static void AddNodes(VAbstractPattern *doc, QDomElement &domElement, const VPiece &piece); + static void AddAttributes(VAbstractPattern *doc, QDomElement &domElement, quint32 id, const VPiece &piece); + static void AddPatternPieceData(VAbstractPattern *doc, QDomElement &domElement, const VPiece &piece); + static void AddPatternInfo(VAbstractPattern *doc, QDomElement &domElement, const VPiece &piece); + static void AddGrainline(VAbstractPattern *doc, QDomElement &domElement, const VPiece &piece); virtual int type() const Q_DECL_OVERRIDE {return Type;} enum { Type = UserType + static_cast(Tool::Piece)}; @@ -101,7 +105,7 @@ private: Q_DISABLE_COPY(VToolSeamAllowance) /** @brief dialog dialog options. */ - DialogTool *m_dialog; + QPointer m_dialog; /** @brief sceneDetails pointer to the scene. */ VMainGraphicsScene *m_sceneDetails; diff --git a/src/libs/vtools/undocommands/savepieceoptions.cpp b/src/libs/vtools/undocommands/savepieceoptions.cpp new file mode 100644 index 000000000..cb78ca455 --- /dev/null +++ b/src/libs/vtools/undocommands/savepieceoptions.cpp @@ -0,0 +1,136 @@ +/************************************************************************ + ** + ** @file + ** @author Roman Telezhynskyi + ** @date 9 11, 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 "savepieceoptions.h" + +#include +#include +#include +#include + +#include "../ifc/xml/vabstractpattern.h" +#include "../ifc/ifcdef.h" +#include "../vmisc/logging.h" +#include "../vmisc/def.h" +#include "../vpatterndb/vpiecenode.h" +#include "../vpatterndb/vpatterninfogeometry.h" +#include "../vpatterndb/vpatternpiecedata.h" +#include "../vpatterndb/vgrainlinegeometry.h" +#include "../tools/vtoolseamallowance.h" +#include "vundocommand.h" + +class QDomElement; +class QUndoCommand; + +//--------------------------------------------------------------------------------------------------------------------- +SavePieceOptions::SavePieceOptions(const VPiece &oldDet, const VPiece &newDet, VAbstractPattern *doc, quint32 id, + QGraphicsScene *scene, QUndoCommand *parent) + : VUndoCommand(QDomElement(), doc, parent), + m_oldDet(oldDet), + m_newDet(newDet), + m_scene(scene) +{ + setText(tr("save detail option")); + nodeId = id; +} + +//--------------------------------------------------------------------------------------------------------------------- +SavePieceOptions::~SavePieceOptions() +{} + +//--------------------------------------------------------------------------------------------------------------------- +void SavePieceOptions::undo() +{ + qCDebug(vUndo, "Undo."); + + QDomElement domElement = doc->elementById(nodeId); + if (domElement.isElement()) + { + VToolSeamAllowance::AddAttributes(doc, domElement, nodeId, m_oldDet); + doc->RemoveAllChildren(domElement);//Very important to clear before rewrite + VToolSeamAllowance::AddPatternPieceData(doc, domElement, m_oldDet); + VToolSeamAllowance::AddPatternInfo(doc, domElement, m_oldDet); + VToolSeamAllowance::AddGrainline(doc, domElement, m_oldDet); + VToolSeamAllowance::AddNodes(doc, domElement, m_oldDet); + + IncrementReferences(m_oldDet.Missing(m_newDet)); + emit NeedLiteParsing(Document::LiteParse); + } + else + { + qCDebug(vUndo, "Can't find detail with id = %u.", nodeId); + return; + } +} + +//--------------------------------------------------------------------------------------------------------------------- +void SavePieceOptions::redo() +{ + qCDebug(vUndo, "Redo."); + + QDomElement domElement = doc->elementById(nodeId); + if (domElement.isElement()) + { + VToolSeamAllowance::AddAttributes(doc, domElement, nodeId, m_newDet); + doc->RemoveAllChildren(domElement);//Very important to clear before rewrite + VToolSeamAllowance::AddPatternPieceData(doc, domElement, m_newDet); + VToolSeamAllowance::AddPatternInfo(doc, domElement, m_newDet); + VToolSeamAllowance::AddGrainline(doc, domElement, m_newDet); + VToolSeamAllowance::AddNodes(doc, domElement, m_newDet); + + DecrementReferences(m_oldDet.Missing(m_newDet)); + emit NeedLiteParsing(Document::LiteParse); + } + else + { + qCDebug(vUndo, "Can't find detail with id = %u.", nodeId); + return; + } +} + +//--------------------------------------------------------------------------------------------------------------------- +bool SavePieceOptions::mergeWith(const QUndoCommand *command) +{ + const SavePieceOptions *saveCommand = static_cast(command); + SCASSERT(saveCommand != nullptr); + const quint32 id = saveCommand->DetId(); + + if (id != nodeId) + { + return false; + } + + m_newDet = saveCommand->NewDet(); + return true; +} + +//--------------------------------------------------------------------------------------------------------------------- +int SavePieceOptions::id() const +{ + return static_cast(UndoCommand::SavePieceOptions); +} diff --git a/src/libs/vtools/undocommands/savepieceoptions.h b/src/libs/vtools/undocommands/savepieceoptions.h new file mode 100644 index 000000000..306a0c4d0 --- /dev/null +++ b/src/libs/vtools/undocommands/savepieceoptions.h @@ -0,0 +1,75 @@ +/************************************************************************ + ** + ** @file + ** @author Roman Telezhynskyi + ** @date 9 11, 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 SAVEPIECEOPTIONS_H +#define SAVEPIECEOPTIONS_H + +#include + +#include "vpiece.h" +#include "vundocommand.h" + +class QDomElement; +class QGraphicsScene; +class QUndoCommand; +class VAbstractPattern; + +class SavePieceOptions : public VUndoCommand +{ +public: + SavePieceOptions(const VPiece &m_oldDet, const VPiece &m_newDet, VAbstractPattern *doc, quint32 id, + QGraphicsScene *m_scene, QUndoCommand *parent = nullptr); + virtual ~SavePieceOptions(); + + 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 DetId() const; + VPiece NewDet() const; +private: + Q_DISABLE_COPY(SavePieceOptions) + + const VPiece m_oldDet; + VPiece m_newDet; + QGraphicsScene *m_scene; +}; + +//--------------------------------------------------------------------------------------------------------------------- +inline quint32 SavePieceOptions::DetId() const +{ + return nodeId; +} + +//--------------------------------------------------------------------------------------------------------------------- +inline VPiece SavePieceOptions::NewDet() const +{ + return m_newDet; +} + +#endif // SAVEPIECEOPTIONS_H diff --git a/src/libs/vtools/undocommands/undocommands.pri b/src/libs/vtools/undocommands/undocommands.pri index d031daf32..2514e4765 100644 --- a/src/libs/vtools/undocommands/undocommands.pri +++ b/src/libs/vtools/undocommands/undocommands.pri @@ -25,7 +25,8 @@ HEADERS += \ $$PWD/label/operationmovelabel.h \ $$PWD/addpiece.h \ $$PWD/deletepiece.h \ - $$PWD/movepiece.h + $$PWD/movepiece.h \ + $$PWD/savepieceoptions.h SOURCES += \ $$PWD/addtocalc.cpp \ @@ -51,4 +52,5 @@ SOURCES += \ $$PWD/label/operationmovelabel.cpp \ $$PWD/addpiece.cpp \ $$PWD/deletepiece.cpp \ - $$PWD/movepiece.cpp + $$PWD/movepiece.cpp \ + $$PWD/savepieceoptions.cpp diff --git a/src/libs/vtools/undocommands/vundocommand.cpp b/src/libs/vtools/undocommands/vundocommand.cpp index 51a3ccd4c..fab10fab9 100644 --- a/src/libs/vtools/undocommands/vundocommand.cpp +++ b/src/libs/vtools/undocommands/vundocommand.cpp @@ -94,6 +94,15 @@ void VUndoCommand::DecrementReferences(const QVector &nodes) const } } +//--------------------------------------------------------------------------------------------------------------------- +void VUndoCommand::IncrementReferences(const QVector &nodes) const +{ + for (qint32 i = 0; i < nodes.size(); ++i) + { + doc->IncrementReferens(nodes.at(i).GetId()); + } +} + //--------------------------------------------------------------------------------------------------------------------- void VUndoCommand::DecrementReferences(const QVector &nodes) const { diff --git a/src/libs/vtools/undocommands/vundocommand.h b/src/libs/vtools/undocommands/vundocommand.h index b8ae606ea..697346f63 100644 --- a/src/libs/vtools/undocommands/vundocommand.h +++ b/src/libs/vtools/undocommands/vundocommand.h @@ -54,6 +54,7 @@ enum class UndoCommand: char { AddPatternPiece, MoveSPoint, SaveToolOptions, SaveDetailOptions, + SavePieceOptions, MovePiece, DeleteTool, DeletePatternPiece, @@ -88,6 +89,7 @@ protected: void IncrementReferences(const QVector &nodes) const; void DecrementReferences(const QVector &nodes) const; + void IncrementReferences(const QVector &nodes) const; void DecrementReferences(const QVector &nodes) const; private: Q_DISABLE_COPY(VUndoCommand)