diff --git a/src/libs/vtools/undocommands/addpiece.cpp b/src/libs/vtools/undocommands/addpiece.cpp new file mode 100644 index 000000000..e9d96e56c --- /dev/null +++ b/src/libs/vtools/undocommands/addpiece.cpp @@ -0,0 +1,111 @@ +/************************************************************************ + ** + ** @file + ** @author Roman Telezhynskyi + ** @date 6 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 "addpiece.h" +#include "../vpatterndb/vpiecenode.h" + +//--------------------------------------------------------------------------------------------------------------------- +AddPiece::AddPiece(const QDomElement &xml, VAbstractPattern *doc, const VPiece &detail, const QString &drawName, + QUndoCommand *parent) + : VUndoCommand(xml, doc, parent), + m_detail(detail), + m_drawName(drawName) +{ + setText(tr("add detail")); + nodeId = doc->GetParametrId(xml); +} + +//--------------------------------------------------------------------------------------------------------------------- +AddPiece::~AddPiece() +{} + +//--------------------------------------------------------------------------------------------------------------------- +void AddPiece::undo() +{ + qCDebug(vUndo, "Undo."); + + QDomElement details = GetDetailsSection(); + if (not details.isNull()) + { + QDomElement domElement = doc->elementById(nodeId); + if (domElement.isElement()) + { + if (details.removeChild(domElement).isNull()) + { + qCDebug(vUndo, "Can't delete node"); + return; + } + + DecrementReferences(m_detail.GetNodes()); + } + else + { + qCDebug(vUndo, "Can't get node by id = %u.", nodeId); + return; + } + } + else + { + qCDebug(vUndo, "Can't find tag %s.", qUtf8Printable(VAbstractPattern::TagDetails)); + return; + } + emit NeedFullParsing(); +} + +//--------------------------------------------------------------------------------------------------------------------- +void AddPiece::redo() +{ + qCDebug(vUndo, "Redo."); + + QDomElement details = GetDetailsSection(); + if (not details.isNull()) + { + details.appendChild(xml); + } + else + { + qCDebug(vUndo, "Can't find tag %s.", qUtf8Printable(VAbstractPattern::TagDetails)); + return; + } + RedoFullParsing(); +} + +//--------------------------------------------------------------------------------------------------------------------- +QDomElement AddPiece::GetDetailsSection() const +{ + QDomElement details; + if (m_drawName.isEmpty()) + { + doc->GetActivNodeElement(VAbstractPattern::TagDetails, details); + } + else + { + details = doc->GetDraw(m_drawName).firstChildElement(VAbstractPattern::TagDetails); + } + return details; +} diff --git a/src/libs/vtools/undocommands/addpiece.h b/src/libs/vtools/undocommands/addpiece.h new file mode 100644 index 000000000..f04c6c7a6 --- /dev/null +++ b/src/libs/vtools/undocommands/addpiece.h @@ -0,0 +1,69 @@ +/************************************************************************ + ** + ** @file + ** @author Roman Telezhynskyi + ** @date 6 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 ADDPIECE_H +#define ADDPIECE_H + +#include +#include +#include +#include +#include +#include + +#include "../tools/vtoolseamallowance.h" +#include "vpiece.h" +#include "vundocommand.h" + +class QDomElement; +class QUndoCommand; +class VAbstractPattern; + + +class AddPiece : public VUndoCommand +{ + Q_OBJECT +public: + AddPiece(const QDomElement &xml, VAbstractPattern *doc, const VPiece &detail, const QString &drawName = QString(), + QUndoCommand *parent = nullptr); + virtual ~AddPiece(); + + // cppcheck-suppress unusedFunction + virtual void undo() Q_DECL_OVERRIDE; + // cppcheck-suppress unusedFunction + virtual void redo() Q_DECL_OVERRIDE; +private: + Q_DISABLE_COPY(AddPiece) + + VPiece m_detail; + QString m_drawName; + + QDomElement GetDetailsSection() const; +}; + +#endif // ADDPIECE_H diff --git a/src/libs/vtools/undocommands/undocommands.pri b/src/libs/vtools/undocommands/undocommands.pri index 216944bb2..fe6ec5f59 100644 --- a/src/libs/vtools/undocommands/undocommands.pri +++ b/src/libs/vtools/undocommands/undocommands.pri @@ -23,7 +23,8 @@ HEADERS += \ $$PWD/delgroup.h \ $$PWD/label/moveabstractlabel.h \ $$PWD/toggledetailinlayout.h \ - $$PWD/label/operationmovelabel.h + $$PWD/label/operationmovelabel.h \ + $$PWD/addpiece.h SOURCES += \ $$PWD/addtocalc.cpp \ @@ -47,4 +48,5 @@ SOURCES += \ $$PWD/delgroup.cpp \ $$PWD/label/moveabstractlabel.cpp \ $$PWD/toggledetailinlayout.cpp \ - $$PWD/label/operationmovelabel.cpp + $$PWD/label/operationmovelabel.cpp \ + $$PWD/addpiece.cpp diff --git a/src/libs/vtools/undocommands/vundocommand.cpp b/src/libs/vtools/undocommands/vundocommand.cpp index a9b56b076..51a3ccd4c 100644 --- a/src/libs/vtools/undocommands/vundocommand.cpp +++ b/src/libs/vtools/undocommands/vundocommand.cpp @@ -33,6 +33,7 @@ #include "../ifc/ifcdef.h" #include "../vmisc/def.h" #include "../vpatterndb/vnodedetail.h" +#include "../vpatterndb/vpiecenode.h" class QDomElement; class QDomNode; @@ -92,3 +93,12 @@ void VUndoCommand::DecrementReferences(const QVector &nodes) const doc->DecrementReferens(nodes.at(i).getId()); } } + +//--------------------------------------------------------------------------------------------------------------------- +void VUndoCommand::DecrementReferences(const QVector &nodes) const +{ + for (qint32 i = 0; i < nodes.size(); ++i) + { + doc->DecrementReferens(nodes.at(i).GetId()); + } +} diff --git a/src/libs/vtools/undocommands/vundocommand.h b/src/libs/vtools/undocommands/vundocommand.h index 31cd57570..9d92a1f0e 100644 --- a/src/libs/vtools/undocommands/vundocommand.h +++ b/src/libs/vtools/undocommands/vundocommand.h @@ -65,6 +65,7 @@ enum class UndoCommand: char { AddPatternPiece, }; class VNodeDetail; +class VPieceNode; class VPattern; class VUndoCommand : public QObject, public QUndoCommand @@ -87,6 +88,7 @@ protected: void IncrementReferences(const QVector &nodes) const; void DecrementReferences(const QVector &nodes) const; + void DecrementReferences(const QVector &nodes) const; private: Q_DISABLE_COPY(VUndoCommand) };