Returned undocommand "Delete piece".

--HG--
branch : feature
This commit is contained in:
Roman Telezhynskyi 2016-11-09 11:57:39 +02:00
parent f593d4ca5b
commit b88663dcb4
5 changed files with 248 additions and 19 deletions

View File

@ -41,9 +41,15 @@
#include "../vgeometry/vsplinepath.h" #include "../vgeometry/vsplinepath.h"
#include "../ifc/xml/vpatternconverter.h" #include "../ifc/xml/vpatternconverter.h"
#include "../undocommands/addpiece.h" #include "../undocommands/addpiece.h"
#include "../undocommands/deletepiece.h"
//#include "../undocommands/movepiece.h"
//#include "../undocommands/savepieceoptions.h"
//#include "../undocommands/togglepieceinlayout.h"
#include <QGraphicsSceneMouseEvent> #include <QGraphicsSceneMouseEvent>
#include <QKeyEvent>
#include <QMenu> #include <QMenu>
#include <QMessageBox>
// Current version of seam allowance tag nned for backward compatibility // Current version of seam allowance tag nned for backward compatibility
const quint8 VToolSeamAllowance::pieceVersion = 2; const quint8 VToolSeamAllowance::pieceVersion = 2;
@ -172,6 +178,20 @@ VToolSeamAllowance *VToolSeamAllowance::Create(quint32 id, const VPiece &newPiec
return nullptr; 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) 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); // const VDetail detail = VAbstractTool::data.GetDetail(id);
// inLayoutOption->setChecked(detail.IsInLayout()); // inLayoutOption->setChecked(detail.IsInLayout());
// QAction *actionRemove = menu.addAction(QIcon::fromTheme("edit-delete"), tr("Delete")); QAction *actionRemove = menu.addAction(QIcon::fromTheme("edit-delete"), tr("Delete"));
// _referens > 1 ? actionRemove->setEnabled(false) : actionRemove->setEnabled(true); _referens > 1 ? actionRemove->setEnabled(false) : actionRemove->setEnabled(true);
QAction *selectedAction = menu.exec(event->screenPos()); QAction *selectedAction = menu.exec(event->screenPos());
if (selectedAction == actionOption) if (selectedAction == actionOption)
@ -415,24 +435,41 @@ void VToolSeamAllowance::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
// connect(togglePrint, &ToggleDetailInLayout::UpdateList, doc, &VAbstractPattern::CheckInLayoutList); // connect(togglePrint, &ToggleDetailInLayout::UpdateList, doc, &VAbstractPattern::CheckInLayoutList);
// qApp->getUndoStack()->push(togglePrint); // qApp->getUndoStack()->push(togglePrint);
// } // }
// else if (selectedAction == actionRemove) else if (selectedAction == actionRemove)
// { {
// try try
// { {
// DeleteTool(); DeleteTool();
// } }
// catch(const VExceptionToolWasDeleted &e) catch(const VExceptionToolWasDeleted &e)
// { {
// Q_UNUSED(e); Q_UNUSED(e);
// return;//Leave this method immediately!!! return;//Leave this method immediately!!!
// } }
// return; //Leave this method immediately after call!!! return; //Leave this method immediately after call!!!
// } }
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void VToolSeamAllowance::keyReleaseEvent(QKeyEvent *event) 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 ); VNoBrushScalePathItem::keyReleaseEvent ( event );
} }
@ -523,7 +560,21 @@ void VToolSeamAllowance::RefreshGeometry()
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void VToolSeamAllowance::DeleteTool(bool ask) 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;
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------

View File

@ -65,6 +65,8 @@ public:
static const QString NodeSpline; static const QString NodeSpline;
static const QString NodeSplinePath; static const QString NodeSplinePath;
void Remove(bool ask);
static void AddNode(VAbstractPattern *doc, QDomElement &domElement, const VPieceNode &node); static void AddNode(VAbstractPattern *doc, QDomElement &domElement, const VPieceNode &node);
static void AddNodes(VAbstractPattern *doc, QDomElement &domElement, const VPiece &piece); 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 contextMenuEvent ( QGraphicsSceneContextMenuEvent * event ) Q_DECL_OVERRIDE;
virtual void keyReleaseEvent(QKeyEvent * event) Q_DECL_OVERRIDE; virtual void keyReleaseEvent(QKeyEvent * event) Q_DECL_OVERRIDE;
virtual void SetVisualization() Q_DECL_OVERRIDE {} virtual void SetVisualization() Q_DECL_OVERRIDE {}
virtual void DeleteTool(bool ask = true) Q_DECL_OVERRIDE;
private: private:
Q_DISABLE_COPY(VToolSeamAllowance) Q_DISABLE_COPY(VToolSeamAllowance)
@ -116,8 +119,6 @@ private:
template <typename Tool> template <typename Tool>
Tool* InitTool(VMainGraphicsScene *scene, const VPieceNode &node); Tool* InitTool(VMainGraphicsScene *scene, const VPieceNode &node);
virtual void DeleteTool(bool ask = true) Q_DECL_OVERRIDE;
}; };
#endif // VTOOLSEAMALLOWANCE_H #endif // VTOOLSEAMALLOWANCE_H

View File

@ -0,0 +1,116 @@
/************************************************************************
**
** @file
** @author Roman Telezhynskyi <dismine(at)gmail.com>
** @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
** <https://bitbucket.org/dismine/valentina> 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 <http://www.gnu.org/licenses/>.
**
*************************************************************************/
#include "deletepiece.h"
#include <QDomElement>
#include <QHash>
#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<quint32, VDataTool*>* tools = doc->getTools();
SCASSERT(tools != nullptr);
VToolSeamAllowance *toolDet = qobject_cast<VToolSeamAllowance*>(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;
}
}

View File

@ -0,0 +1,59 @@
/************************************************************************
**
** @file
** @author Roman Telezhynskyi <dismine(at)gmail.com>
** @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
** <https://bitbucket.org/dismine/valentina> 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 <http://www.gnu.org/licenses/>.
**
*************************************************************************/
#ifndef DELETEPIECE_H
#define DELETEPIECE_H
#include <QtGlobal>
#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

View File

@ -24,7 +24,8 @@ HEADERS += \
$$PWD/label/moveabstractlabel.h \ $$PWD/label/moveabstractlabel.h \
$$PWD/toggledetailinlayout.h \ $$PWD/toggledetailinlayout.h \
$$PWD/label/operationmovelabel.h \ $$PWD/label/operationmovelabel.h \
$$PWD/addpiece.h $$PWD/addpiece.h \
$$PWD/deletepiece.h
SOURCES += \ SOURCES += \
$$PWD/addtocalc.cpp \ $$PWD/addtocalc.cpp \
@ -49,4 +50,5 @@ SOURCES += \
$$PWD/label/moveabstractlabel.cpp \ $$PWD/label/moveabstractlabel.cpp \
$$PWD/toggledetailinlayout.cpp \ $$PWD/toggledetailinlayout.cpp \
$$PWD/label/operationmovelabel.cpp \ $$PWD/label/operationmovelabel.cpp \
$$PWD/addpiece.cpp $$PWD/addpiece.cpp \
$$PWD/deletepiece.cpp