From 2ee19af497bde77195e54e4f8838e5fd0502c6e4 Mon Sep 17 00:00:00 2001 From: Ronan Le Tiec Date: Sat, 31 Mar 2018 11:50:08 +0200 Subject: [PATCH] issue 826 start working on undo redo command --HG-- branch : feature --- src/libs/ifc/xml/vabstractpattern.cpp | 8 +- src/libs/ifc/xml/vabstractpattern.h | 4 +- src/libs/vtools/tools/drawTools/vdrawtool.h | 10 +- .../vtools/undocommands/additemtogroup.cpp | 93 +++++++++++++++++++ src/libs/vtools/undocommands/additemtogroup.h | 59 ++++++++++++ .../undocommands/removeitemfromgroup.cpp | 6 ++ .../vtools/undocommands/removeitemfromgroup.h | 11 +++ src/libs/vtools/undocommands/undocommands.pri | 8 +- 8 files changed, 191 insertions(+), 8 deletions(-) create mode 100644 src/libs/vtools/undocommands/additemtogroup.cpp create mode 100644 src/libs/vtools/undocommands/additemtogroup.h create mode 100644 src/libs/vtools/undocommands/removeitemfromgroup.cpp create mode 100644 src/libs/vtools/undocommands/removeitemfromgroup.h diff --git a/src/libs/ifc/xml/vabstractpattern.cpp b/src/libs/ifc/xml/vabstractpattern.cpp index ff0b8bca0..a02657907 100644 --- a/src/libs/ifc/xml/vabstractpattern.cpp +++ b/src/libs/ifc/xml/vabstractpattern.cpp @@ -2470,7 +2470,7 @@ bool VAbstractPattern::GroupHasItem(const QDomElement &groupDomElement, quint32 * @param objectId * @param groupId */ -void VAbstractPattern::AddItemToGroup(quint32 toolId, quint32 objectId, quint32 groupId) +QDomElement VAbstractPattern::AddItemToGroup(quint32 toolId, quint32 objectId, quint32 groupId) { QDomElement group = elementById(groupId, TagGroup); @@ -2499,6 +2499,8 @@ void VAbstractPattern::AddItemToGroup(quint32 toolId, quint32 objectId, quint32 { ParseGroups(groups); } + + return item; } else { @@ -2513,7 +2515,7 @@ void VAbstractPattern::AddItemToGroup(quint32 toolId, quint32 objectId, quint32 * @param objectId * @param groupId */ -void VAbstractPattern::RemoveItemFromGroup(quint32 toolId, quint32 objectId, quint32 groupId) +QDomElement VAbstractPattern::RemoveItemFromGroup(quint32 toolId, quint32 objectId, quint32 groupId) { QDomElement group = elementById(groupId, TagGroup); @@ -2553,7 +2555,7 @@ void VAbstractPattern::RemoveItemFromGroup(quint32 toolId, quint32 objectId, qui ParseGroups(groups); } - break; + return itemNode; } } } diff --git a/src/libs/ifc/xml/vabstractpattern.h b/src/libs/ifc/xml/vabstractpattern.h index a86ec4f79..2a3d2c067 100644 --- a/src/libs/ifc/xml/vabstractpattern.h +++ b/src/libs/ifc/xml/vabstractpattern.h @@ -196,8 +196,8 @@ public: void SetGroupName(quint32 id, const QString &name); QMap > GetGroups(); QMap GetGroupsContainingItem(quint32 toolId, quint32 objectId, bool containItem); - void AddItemToGroup(quint32 toolId, quint32 objectId, quint32 groupId); - void RemoveItemFromGroup(quint32 toolId, quint32 objectId, quint32 groupId); + QDomElement AddItemToGroup(quint32 toolId, quint32 objectId, quint32 groupId); + QDomElement RemoveItemFromGroup(quint32 toolId, quint32 objectId, quint32 groupId); bool GroupIsEmpty(quint32 id); bool GetGroupVisivility(quint32 id); void SetGroupVisivility(quint32 id, bool visible); diff --git a/src/libs/vtools/tools/drawTools/vdrawtool.h b/src/libs/vtools/tools/drawTools/vdrawtool.h index 59c94d768..5e5c1c7bc 100644 --- a/src/libs/vtools/tools/drawTools/vdrawtool.h +++ b/src/libs/vtools/tools/drawTools/vdrawtool.h @@ -50,6 +50,7 @@ #include "../vwidgets/vmaingraphicsview.h" #include "../vdatatool.h" #include "../vgeometry/vpointf.h" +#include "../vtools/undocommands/addgroup.h" template class QSharedPointer; @@ -279,7 +280,14 @@ void VDrawTool::ContextMenu(QGraphicsSceneContextMenuEvent *event, quint32 itemI else if (selectedAction->actionGroup() == actionsAddToGroup) { quint32 groupId = selectedAction->data().toUInt(); - doc->AddItemToGroup(this->getId(), itemId, groupId); + QDomElement item = doc->AddItemToGroup(this->getId(), itemId, groupId); + + AddItemToGroup *addItemToGroup = new AddItemToGroup(item, doc); + + // where should the signal be connected to? should we have a central "UpdateGroup" slot, like in the mainWindow? + // connect(addItemToGroup, &AddItemToGroup::UpdateGroups, , &VWidgetGroups::UpdateGroups); + qApp->getUndoStack()->push(addGroup); + } else if (selectedAction->actionGroup() == actionsRemoveFromGroup) { diff --git a/src/libs/vtools/undocommands/additemtogroup.cpp b/src/libs/vtools/undocommands/additemtogroup.cpp new file mode 100644 index 000000000..3ed6ccbd1 --- /dev/null +++ b/src/libs/vtools/undocommands/additemtogroup.cpp @@ -0,0 +1,93 @@ +/************************************************************************ + ** + ** @file addgroup.h + ** @author Roman Telezhynskyi + ** @date 31 3, 2018 + ** + ** @brief + ** @copyright + ** This source code is part of the Valentina 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 "additemtogroup.h" + +#include +#include + +#include "../vmisc/logging.h" +#include "../vmisc/vabstractapplication.h" +#include "../vmisc/def.h" +#include "../vwidgets/vmaingraphicsview.h" +#include "../ifc/xml/vabstractpattern.h" +#include "vundocommand.h" + +//--------------------------------------------------------------------------------------------------------------------- +AddItemToGroup::AddItemToGroup(const QDomElement &xml, VAbstractPattern *doc, QUndoCommand *parent) + : VUndoCommand(xml, doc, parent), nameActivDraw(doc->GetNameActivPP()) +{ + setText(tr("add item to group")); + + objectId = doc->GetParametrUInt(xml,QString("object"),NULL_ID_STR); + toolId = doc->GetParametrUInt(xml,QString("tool"),NULL_ID_STR); + nodeId = doc->GetParametrId(xml.parentNode()); // nodeId is the groupId +} + +//--------------------------------------------------------------------------------------------------------------------- +AddItemToGroup::~AddItemToGroup() +{ +} + +//--------------------------------------------------------------------------------------------------------------------- +void AddItemToGroup::undo() +{ + qCDebug(vUndo, "Undo."); + + doc->ChangeActivPP(nameActivDraw);//Without this user will not see this change + + QDomElement groups = doc->CreateGroups(); + if (not groups.isNull()) + { + QDomElement group = doc->elementById(nodeId, VAbstractPattern::TagGroup); + if (group.isElement()) + { + if (group.removeChild(xml).isNull()) + { + qCDebug(vUndo, "Can't delete item."); + return; + } + + doc->ParseGroups(groups); + emit UpdateGroups(); + } + else + { + qCDebug(vUndo, "Can't get group by id = %u.", nodeId); + return; + } + } + else + { + qCDebug(vUndo, "Can't get tag Groups."); + return; + } + + VMainGraphicsView::NewSceneRect(qApp->getCurrentScene(), qApp->getSceneView()); + emit doc->SetCurrentPP(nameActivDraw);//Return current pattern piece after undo +} diff --git a/src/libs/vtools/undocommands/additemtogroup.h b/src/libs/vtools/undocommands/additemtogroup.h new file mode 100644 index 000000000..5b9fe9143 --- /dev/null +++ b/src/libs/vtools/undocommands/additemtogroup.h @@ -0,0 +1,59 @@ +/************************************************************************ + ** + ** @file addgroup.h + ** @author Roman Telezhynskyi + ** @date 31 3, 2018 + ** + ** @brief + ** @copyright + ** This source code is part of the Valentina 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 ADDITEMTOGROUP_H +#define ADDITEMTOGROUP_H + +#include +#include +#include +#include +#include +#include + +#include "vundocommand.h" + +class AddItemToGroup : public VUndoCommand +{ + Q_OBJECT +public: + AddItemToGroup(const QDomElement &xml, VAbstractPattern *doc, QUndoCommand *parent = nullptr); + virtual ~AddGroup(); + virtual void undo() Q_DECL_OVERRIDE; + virtual void redo() Q_DECL_OVERRIDE; +protected: + quint32 toolId; + quint32 objectId; +signals: + void UpdateGroups(); +private: + Q_DISABLE_COPY(AddItemToGroup) + const QString nameActivDraw; +}; + +#endif // ADDITEMTOGROUP_H diff --git a/src/libs/vtools/undocommands/removeitemfromgroup.cpp b/src/libs/vtools/undocommands/removeitemfromgroup.cpp new file mode 100644 index 000000000..370767fd7 --- /dev/null +++ b/src/libs/vtools/undocommands/removeitemfromgroup.cpp @@ -0,0 +1,6 @@ +#include "removeitemfromgroup.h" + +RemoveItemFromGroup::RemoveItemFromGroup() +{ + +} diff --git a/src/libs/vtools/undocommands/removeitemfromgroup.h b/src/libs/vtools/undocommands/removeitemfromgroup.h new file mode 100644 index 000000000..5d6fb42fb --- /dev/null +++ b/src/libs/vtools/undocommands/removeitemfromgroup.h @@ -0,0 +1,11 @@ +#ifndef REMOVEITEMFROMGROUP_H +#define REMOVEITEMFROMGROUP_H + + +class RemoveItemFromGroup : public VUndoCommand +{ +public: + RemoveItemFromGroup(); +}; + +#endif // REMOVEITEMFROMGROUP_H \ No newline at end of file diff --git a/src/libs/vtools/undocommands/undocommands.pri b/src/libs/vtools/undocommands/undocommands.pri index a290553d7..f69bd070f 100644 --- a/src/libs/vtools/undocommands/undocommands.pri +++ b/src/libs/vtools/undocommands/undocommands.pri @@ -27,7 +27,9 @@ HEADERS += \ $$PWD/label/showdoublelabel.h \ $$PWD/label/operationshowlabel.h \ $$PWD/saveplacelabeloptions.h \ - $$PWD/togglepiecestate.h + $$PWD/togglepiecestate.h \ + $$PWD/additemtogroup.h \ + $$PWD/removeitemfromgroup.h SOURCES += \ $$PWD/addtocalc.cpp \ @@ -55,4 +57,6 @@ SOURCES += \ $$PWD/label/showdoublelabel.cpp \ $$PWD/label/operationshowlabel.cpp \ $$PWD/saveplacelabeloptions.cpp \ - $$PWD/togglepiecestate.cpp + $$PWD/togglepiecestate.cpp \ + $$PWD/additemtogroup.cpp \ + $$PWD/removeitemfromgroup.cpp