diff --git a/src/libs/vtools/tools/vtoolseamallowance.cpp b/src/libs/vtools/tools/vtoolseamallowance.cpp index 2287b4828..c6a283037 100644 --- a/src/libs/vtools/tools/vtoolseamallowance.cpp +++ b/src/libs/vtools/tools/vtoolseamallowance.cpp @@ -41,9 +41,15 @@ #include "../vgeometry/vsplinepath.h" #include "../ifc/xml/vpatternconverter.h" #include "../undocommands/addpiece.h" +#include "../undocommands/deletepiece.h" +//#include "../undocommands/movepiece.h" +//#include "../undocommands/savepieceoptions.h" +//#include "../undocommands/togglepieceinlayout.h" #include +#include #include +#include // Current version of seam allowance tag nned for backward compatibility const quint8 VToolSeamAllowance::pieceVersion = 2; @@ -172,6 +178,20 @@ VToolSeamAllowance *VToolSeamAllowance::Create(quint32 id, const VPiece &newPiec return nullptr; } +//--------------------------------------------------------------------------------------------------------------------- +void VToolSeamAllowance::Remove(bool ask) +{ + try + { + DeleteTool(ask); + } + catch(const VExceptionToolWasDeleted &e) + { + Q_UNUSED(e); + return;//Leave this method immediately!!! + } +} + //--------------------------------------------------------------------------------------------------------------------- void VToolSeamAllowance::AddNode(VAbstractPattern *doc, QDomElement &domElement, const VPieceNode &node) { @@ -394,8 +414,8 @@ void VToolSeamAllowance::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) // const VDetail detail = VAbstractTool::data.GetDetail(id); // inLayoutOption->setChecked(detail.IsInLayout()); -// QAction *actionRemove = menu.addAction(QIcon::fromTheme("edit-delete"), tr("Delete")); -// _referens > 1 ? actionRemove->setEnabled(false) : actionRemove->setEnabled(true); + QAction *actionRemove = menu.addAction(QIcon::fromTheme("edit-delete"), tr("Delete")); + _referens > 1 ? actionRemove->setEnabled(false) : actionRemove->setEnabled(true); QAction *selectedAction = menu.exec(event->screenPos()); if (selectedAction == actionOption) @@ -415,24 +435,41 @@ void VToolSeamAllowance::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) // connect(togglePrint, &ToggleDetailInLayout::UpdateList, doc, &VAbstractPattern::CheckInLayoutList); // qApp->getUndoStack()->push(togglePrint); // } -// else if (selectedAction == actionRemove) -// { -// try -// { -// DeleteTool(); -// } -// catch(const VExceptionToolWasDeleted &e) -// { -// Q_UNUSED(e); -// return;//Leave this method immediately!!! -// } -// return; //Leave this method immediately after call!!! - // } + else if (selectedAction == actionRemove) + { + try + { + DeleteTool(); + } + catch(const VExceptionToolWasDeleted &e) + { + Q_UNUSED(e); + return;//Leave this method immediately!!! + } + return; //Leave this method immediately after call!!! + } } //--------------------------------------------------------------------------------------------------------------------- void VToolSeamAllowance::keyReleaseEvent(QKeyEvent *event) { + switch (event->key()) + { + case Qt::Key_Delete: + try + { + DeleteTool(); + } + catch(const VExceptionToolWasDeleted &e) + { + Q_UNUSED(e); + return;//Leave this method immediately!!! + } + break; + default: + break; + } + VNoBrushScalePathItem::keyReleaseEvent ( event ); } @@ -523,7 +560,21 @@ void VToolSeamAllowance::RefreshGeometry() //--------------------------------------------------------------------------------------------------------------------- void VToolSeamAllowance::DeleteTool(bool ask) { + DeletePiece *delDet = new DeletePiece(doc, id, VAbstractTool::data.GetPiece(id)); + if (ask) + { + if (ConfirmDeletion() == QMessageBox::No) + { + return; + } + /* If UnionDetails tool delete detail no need emit FullParsing.*/ + connect(delDet, &DeletePiece::NeedFullParsing, doc, &VAbstractPattern::NeedFullParsing); + } + qApp->getUndoStack()->push(delDet); + // Throw exception, this will help prevent case when we forget to immediately quit function. + VExceptionToolWasDeleted e("Tool was used after deleting."); + throw e; } //--------------------------------------------------------------------------------------------------------------------- diff --git a/src/libs/vtools/tools/vtoolseamallowance.h b/src/libs/vtools/tools/vtoolseamallowance.h index 836849aa9..878b3d011 100644 --- a/src/libs/vtools/tools/vtoolseamallowance.h +++ b/src/libs/vtools/tools/vtoolseamallowance.h @@ -65,6 +65,8 @@ public: static const QString NodeSpline; static const QString NodeSplinePath; + void Remove(bool ask); + static void AddNode(VAbstractPattern *doc, QDomElement &domElement, const VPieceNode &node); static void AddNodes(VAbstractPattern *doc, QDomElement &domElement, const VPiece &piece); @@ -93,6 +95,7 @@ protected: virtual void contextMenuEvent ( QGraphicsSceneContextMenuEvent * event ) Q_DECL_OVERRIDE; virtual void keyReleaseEvent(QKeyEvent * event) Q_DECL_OVERRIDE; virtual void SetVisualization() Q_DECL_OVERRIDE {} + virtual void DeleteTool(bool ask = true) Q_DECL_OVERRIDE; private: Q_DISABLE_COPY(VToolSeamAllowance) @@ -116,8 +119,6 @@ private: template Tool* InitTool(VMainGraphicsScene *scene, const VPieceNode &node); - - virtual void DeleteTool(bool ask = true) Q_DECL_OVERRIDE; }; #endif // VTOOLSEAMALLOWANCE_H diff --git a/src/libs/vtools/undocommands/deletepiece.cpp b/src/libs/vtools/undocommands/deletepiece.cpp new file mode 100644 index 000000000..44d79a54e --- /dev/null +++ b/src/libs/vtools/undocommands/deletepiece.cpp @@ -0,0 +1,116 @@ +/************************************************************************ + ** + ** @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 "deletepiece.h" + +#include +#include + +#include "../ifc/ifcdef.h" +#include "../ifc/xml/vabstractpattern.h" +#include "../ifc/xml/vdomdocument.h" +#include "../vmisc/logging.h" +#include "../vmisc/def.h" +#include "../tools/vdatatool.h" +#include "vundocommand.h" +#include "../vpatterndb/vpiecenode.h" + +class QUndoCommand; + +//--------------------------------------------------------------------------------------------------------------------- +DeletePiece::DeletePiece(VAbstractPattern *doc, quint32 id, const VPiece &detail, QUndoCommand *parent) + : VUndoCommand(QDomElement(), doc, parent), + m_parentNode(), + m_siblingId(NULL_ID), + m_detail(detail) +{ + setText(tr("delete tool")); + nodeId = id; + QDomElement domElement = doc->elementById(id); + if (domElement.isElement()) + { + xml = domElement.cloneNode().toElement(); + m_parentNode = domElement.parentNode(); + QDomNode previousDetail = domElement.previousSibling(); + if (previousDetail.isNull()) + { + m_siblingId = NULL_ID; + } + else + { + // Better save id of previous detail instead of reference to node. + m_siblingId = doc->GetParametrUInt(previousDetail.toElement(), VAbstractPattern::AttrId, NULL_ID_STR); + } + } + else + { + qCDebug(vUndo, "Can't get detail by id = %u.", nodeId); + return; + } +} + +//--------------------------------------------------------------------------------------------------------------------- +DeletePiece::~DeletePiece() +{} + +//--------------------------------------------------------------------------------------------------------------------- +void DeletePiece::undo() +{ + qCDebug(vUndo, "Undo."); + + UndoDeleteAfterSibling(m_parentNode, m_siblingId); + emit NeedFullParsing(); +} + +//--------------------------------------------------------------------------------------------------------------------- +void DeletePiece::redo() +{ + qCDebug(vUndo, "Redo."); + + QDomElement domElement = doc->elementById(nodeId); + if (domElement.isElement()) + { + m_parentNode.removeChild(domElement); + + // UnionDetails delete two old details and create one new. + // So when UnionDetail delete detail we can't use FullParsing. So we hide detail on scene directly. + QHash* tools = doc->getTools(); + SCASSERT(tools != nullptr); + VToolSeamAllowance *toolDet = qobject_cast(tools->value(nodeId)); + SCASSERT(toolDet != nullptr); + toolDet->hide(); + + DecrementReferences(m_detail.GetNodes()); + emit NeedFullParsing(); // Doesn't work when UnionDetail delete detail. + } + else + { + qCDebug(vUndo, "Can't get detail by id = %u.", nodeId); + return; + } +} diff --git a/src/libs/vtools/undocommands/deletepiece.h b/src/libs/vtools/undocommands/deletepiece.h new file mode 100644 index 000000000..b06a77613 --- /dev/null +++ b/src/libs/vtools/undocommands/deletepiece.h @@ -0,0 +1,59 @@ +/************************************************************************ + ** + ** @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 DELETEPIECE_H +#define DELETEPIECE_H + +#include + +#include "../tools/vtoolseamallowance.h" +#include "vpiece.h" +#include "vundocommand.h" + +class QGraphicsItem; +class QUndoCommand; +class VAbstractPattern; + +class DeletePiece : public VUndoCommand +{ + Q_OBJECT +public: + DeletePiece(VAbstractPattern *doc, quint32 id, const VPiece &m_detail, QUndoCommand *parent = nullptr); + virtual ~DeletePiece(); + + virtual void undo() Q_DECL_OVERRIDE; + virtual void redo() Q_DECL_OVERRIDE; +private: + Q_DISABLE_COPY(DeletePiece) + + QDomNode m_parentNode; + quint32 m_siblingId; + VPiece m_detail; +}; + +#endif // DELETEPIECE_H diff --git a/src/libs/vtools/undocommands/undocommands.pri b/src/libs/vtools/undocommands/undocommands.pri index fe6ec5f59..5bdcf4b90 100644 --- a/src/libs/vtools/undocommands/undocommands.pri +++ b/src/libs/vtools/undocommands/undocommands.pri @@ -24,7 +24,8 @@ HEADERS += \ $$PWD/label/moveabstractlabel.h \ $$PWD/toggledetailinlayout.h \ $$PWD/label/operationmovelabel.h \ - $$PWD/addpiece.h + $$PWD/addpiece.h \ + $$PWD/deletepiece.h SOURCES += \ $$PWD/addtocalc.cpp \ @@ -49,4 +50,5 @@ SOURCES += \ $$PWD/label/moveabstractlabel.cpp \ $$PWD/toggledetailinlayout.cpp \ $$PWD/label/operationmovelabel.cpp \ - $$PWD/addpiece.cpp + $$PWD/addpiece.cpp \ + $$PWD/deletepiece.cpp