From c23a93037c80bed1886e10d9131c3d96e24ebbea Mon Sep 17 00:00:00 2001 From: dismine Date: Thu, 12 Jun 2014 16:38:28 +0300 Subject: [PATCH] New undo command SaveDetailOptions. --HG-- branch : feature --- src/app/tools/nodeDetails/vabstractnode.cpp | 62 ++++---- src/app/tools/nodeDetails/vabstractnode.h | 3 + src/app/tools/nodeDetails/vnodearc.cpp | 9 ++ src/app/tools/nodeDetails/vnodearc.h | 1 + src/app/tools/nodeDetails/vnodepoint.cpp | 9 ++ src/app/tools/nodeDetails/vnodepoint.h | 1 + src/app/tools/nodeDetails/vnodespline.cpp | 9 ++ src/app/tools/nodeDetails/vnodespline.h | 1 + src/app/tools/nodeDetails/vnodesplinepath.cpp | 9 ++ src/app/tools/nodeDetails/vnodesplinepath.h | 1 + src/app/tools/vabstracttool.cpp | 12 -- src/app/tools/vabstracttool.h | 6 +- src/app/tools/vtooldetail.cpp | 48 ++---- src/app/tools/vtooldetail.h | 13 +- src/app/undocommands/savedetailoptions.cpp | 139 ++++++++++++++++++ src/app/undocommands/savedetailoptions.h | 75 ++++++++++ src/app/undocommands/savetooloptions.cpp | 8 +- src/app/undocommands/undocommands.h | 3 +- src/app/undocommands/undocommands.pri | 6 +- src/app/xml/vdomdocument.cpp | 16 ++ src/app/xml/vdomdocument.h | 3 +- 21 files changed, 342 insertions(+), 92 deletions(-) create mode 100644 src/app/undocommands/savedetailoptions.cpp create mode 100644 src/app/undocommands/savedetailoptions.h diff --git a/src/app/tools/nodeDetails/vabstractnode.cpp b/src/app/tools/nodeDetails/vabstractnode.cpp index 2baf4fc59..a8dcc4b83 100644 --- a/src/app/tools/nodeDetails/vabstractnode.cpp +++ b/src/app/tools/nodeDetails/vabstractnode.cpp @@ -46,33 +46,15 @@ void VAbstractNode::DeleteNode() { if (_referens <= 1) { - //remove from xml file - QDomElement domElement = doc->elementById(QString().setNum(id)); - if (domElement.isElement()) - { - QDomNode element = domElement.parentNode(); - if (element.isNull() == false) - { - if (element.isElement()) - { - RemoveReferens();//deincrement referens - element.removeChild(domElement);//remove form file - emit toolhaveChange();//set enabled save button - } - else - { - qDebug()<<"parent isn't element"< #include #include +#include "../undocommands/savedetailoptions.h" const QString VToolDetail::TagName = QStringLiteral("detail"); const QString VToolDetail::TagNode = QStringLiteral("node"); @@ -247,36 +248,15 @@ void VToolDetail::FullUpdateFromGuiOk(int result) { if (result == QDialog::Accepted) { - QDomElement domElement = doc->elementById(QString().setNum(id)); - if (domElement.isElement()) - { - SCASSERT(dialog != nullptr); - DialogDetail *dialogTool = qobject_cast(dialog); - SCASSERT(dialogTool != nullptr); - VDetail det = dialogTool->getDetails(); - doc->SetAttribute(domElement, AttrName, det.getName()); - doc->SetAttribute(domElement, AttrSupplement, QString().setNum(det.getSeamAllowance())); - doc->SetAttribute(domElement, AttrClosed, QString().setNum(det.getClosed())); - doc->SetAttribute(domElement, AttrWidth, QString().setNum(det.getWidth())); - RemoveAllChild(domElement); - for (ptrdiff_t i = 0; i < det.CountNode(); ++i) - { - AddNode(domElement, det.at(i)); - } - VDetail detail = VAbstractTool::data.GetDetail(id); - QList list = detail.Missing(det); - QHash* tools = doc->getTools(); - if (list.size()>0) - { - for (qint32 i = 0; i < list.size(); ++i) - { - VAbstractNode *node = qobject_cast(tools->value(list.at(i))); - node->DeleteNode(); - } - } - emit LiteUpdateTree(); - emit toolhaveChange(); - } + SCASSERT(dialog != nullptr); + DialogDetail *dialogTool = qobject_cast(dialog); + SCASSERT(dialogTool != nullptr); + VDetail newDet = dialogTool->getDetails(); + VDetail oldDet = VAbstractTool::data.GetDetail(id); + + SaveDetailOptions *saveCommand = new SaveDetailOptions(oldDet, newDet, doc, id, this->scene()); + connect(saveCommand, &SaveDetailOptions::NeedLiteParsing, doc, &VPattern::LiteParseTree); + qApp->getUndoStack()->push(saveCommand); } delete dialog; dialog = nullptr; @@ -298,7 +278,7 @@ void VToolDetail::AddToFile() for (ptrdiff_t i = 0; i < detail.CountNode(); ++i) { - AddNode(domElement, detail.at(i)); + AddNode(doc, domElement, detail.at(i)); } QDomElement element; @@ -320,10 +300,10 @@ void VToolDetail::RefreshDataInFile() doc->SetAttribute(domElement, AttrSupplement, QString().setNum(det.getSeamAllowance())); doc->SetAttribute(domElement, AttrClosed, QString().setNum(det.getClosed())); doc->SetAttribute(domElement, AttrWidth, QString().setNum(det.getWidth())); - RemoveAllChild(domElement); + doc->RemoveAllChild(domElement); for (ptrdiff_t i = 0; i < det.CountNode(); ++i) { - AddNode(domElement, det.at(i)); + AddNode(doc, domElement, det.at(i)); } } } @@ -432,7 +412,7 @@ void VToolDetail::RemoveReferens() } //--------------------------------------------------------------------------------------------------------------------- -void VToolDetail::AddNode(QDomElement &domElement, const VNodeDetail &node) +void VToolDetail::AddNode(VPattern *doc, QDomElement &domElement, const VNodeDetail &node) { QDomElement nod = doc->createElement(TagNode); diff --git a/src/app/tools/vtooldetail.h b/src/app/tools/vtooldetail.h index c0d4bfbc3..4978fa6ee 100644 --- a/src/app/tools/vtooldetail.h +++ b/src/app/tools/vtooldetail.h @@ -112,6 +112,13 @@ public: * @brief Remove full delete detail. */ void Remove(); + /** + * @brief AddNode add node to the file. + * @param dom document container + * @param domElement tag in xml tree. + * @param node node of detail. + */ + static void AddNode(VPattern *doc, QDomElement &domElement, const VNodeDetail &node); public slots: /** * @brief FullUpdateFromFile update tool data form file. @@ -171,12 +178,6 @@ private: * @brief RefreshGeometry refresh item on scene. */ void RefreshGeometry (); - /** - * @brief AddNode add node to the file. - * @param domElement tag in xml tree. - * @param node node of detail. - */ - void AddNode(QDomElement &domElement, const VNodeDetail &node); template /** * @brief InitTool initial node item on scene diff --git a/src/app/undocommands/savedetailoptions.cpp b/src/app/undocommands/savedetailoptions.cpp new file mode 100644 index 000000000..a056fdd61 --- /dev/null +++ b/src/app/undocommands/savedetailoptions.cpp @@ -0,0 +1,139 @@ +/************************************************************************ + ** + ** @file savedetailoptions.cpp + ** @author Roman Telezhynskyi + ** @date 12 6, 2014 + ** + ** @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) 2014 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 "savedetailoptions.h" +#include "undocommands.h" +#include "../tools/nodeDetails/vabstractnode.h" +#include + +SaveDetailOptions::SaveDetailOptions(const VDetail &oldDet, const VDetail &newDet, VPattern *doc, const quint32 &id, + QGraphicsScene *scene, QUndoCommand *parent) + : QObject(), QUndoCommand(parent), oldDet(oldDet), newDet(newDet), doc(doc), detId(id), scene(scene) +{ + setText(tr("Save detail option")); +} + +//--------------------------------------------------------------------------------------------------------------------- +SaveDetailOptions::~SaveDetailOptions() +{} + +//--------------------------------------------------------------------------------------------------------------------- +void SaveDetailOptions::undo() +{ + QDomElement domElement = doc->elementById(QString().setNum(detId)); + if (domElement.isElement()) + { + doc->SetAttribute(domElement, VAbstractTool::AttrName, oldDet.getName()); + doc->SetAttribute(domElement, VToolDetail::AttrSupplement, QString().setNum(oldDet.getSeamAllowance())); + doc->SetAttribute(domElement, VToolDetail::AttrClosed, QString().setNum(oldDet.getClosed())); + doc->SetAttribute(domElement, VToolDetail::AttrWidth, QString().setNum(oldDet.getWidth())); + doc->RemoveAllChild(domElement); + for (ptrdiff_t i = 0; i < oldDet.CountNode(); ++i) + { + VToolDetail::AddNode(doc, domElement, oldDet.at(i)); + } + QVector nodes = oldDet.getNodes(); + QHash* tools = doc->getTools(); + if (nodes.size()>0) + { + for (qint32 i = 0; i < nodes.size(); ++i) + { + VAbstractNode *node = qobject_cast(tools->value(nodes.at(i).getId())); + node->RestoreNode(); + } + } + emit NeedLiteParsing(); + + QList list = scene->views(); + VAbstractTool::NewSceneRect(scene, list[0]); + } + else + { + qDebug()<<"Can't find detail with id ="<< detId << Q_FUNC_INFO; + } +} + +//--------------------------------------------------------------------------------------------------------------------- +void SaveDetailOptions::redo() +{ + QDomElement domElement = doc->elementById(QString().setNum(detId)); + if (domElement.isElement()) + { + doc->SetAttribute(domElement, VAbstractTool::AttrName, newDet.getName()); + doc->SetAttribute(domElement, VToolDetail::AttrSupplement, QString().setNum(newDet.getSeamAllowance())); + doc->SetAttribute(domElement, VToolDetail::AttrClosed, QString().setNum(newDet.getClosed())); + doc->SetAttribute(domElement, VToolDetail::AttrWidth, QString().setNum(newDet.getWidth())); + doc->RemoveAllChild(domElement); + for (ptrdiff_t i = 0; i < newDet.CountNode(); ++i) + { + VToolDetail::AddNode(doc, domElement, newDet.at(i)); + } + QList list = oldDet.Missing(newDet); + QHash* tools = doc->getTools(); + if (list.size()>0) + { + for (qint32 i = 0; i < list.size(); ++i) + { + VAbstractNode *node = qobject_cast(tools->value(list.at(i))); + node->DeleteNode(); + } + } + emit NeedLiteParsing(); + + QList listV = scene->views(); + VAbstractTool::NewSceneRect(scene, listV[0]); + } + else + { + qDebug()<<"Can't find detail with id ="<< detId << Q_FUNC_INFO; + } +} + +//--------------------------------------------------------------------------------------------------------------------- +bool SaveDetailOptions::mergeWith(const QUndoCommand *command) +{ + const SaveDetailOptions *saveCommand = static_cast(command); + SCASSERT(saveCommand != nullptr); + const quint32 id = saveCommand->getDetId(); + + if (id != detId) + { + return false; + } + + newDet = saveCommand->getNewDet(); + return true; +} + +//--------------------------------------------------------------------------------------------------------------------- +int SaveDetailOptions::id() const +{ + return static_cast(UndoCommand::SaveDetailOptions); +} + + diff --git a/src/app/undocommands/savedetailoptions.h b/src/app/undocommands/savedetailoptions.h new file mode 100644 index 000000000..3f8cda414 --- /dev/null +++ b/src/app/undocommands/savedetailoptions.h @@ -0,0 +1,75 @@ +/************************************************************************ + ** + ** @file savedetailoptions.h + ** @author Roman Telezhynskyi + ** @date 12 6, 2014 + ** + ** @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) 2014 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 SAVEDETAILOPTIONS_H +#define SAVEDETAILOPTIONS_H + +#include +#include +#include "../tools/vtooldetail.h" + +class VPattern; +class QGraphicsScene; + +class SaveDetailOptions : public QObject, public QUndoCommand +{ + Q_OBJECT +public: + SaveDetailOptions(const VDetail &oldDet, const VDetail &newDet, VPattern *doc, const quint32 &id, + QGraphicsScene *scene, QUndoCommand *parent = 0); + virtual ~SaveDetailOptions(); + virtual void undo(); + virtual void redo(); + virtual bool mergeWith(const QUndoCommand *command); + virtual int id() const; + quint32 getDetId() const; + VDetail getNewDet() const; +signals: + void NeedLiteParsing(); +private: + Q_DISABLE_COPY(SaveDetailOptions) + const VDetail oldDet; + VDetail newDet; + VPattern *doc; + const quint32 detId; + QGraphicsScene *scene; +}; + +//--------------------------------------------------------------------------------------------------------------------- +inline quint32 SaveDetailOptions::getDetId() const +{ + return detId; +} + +//--------------------------------------------------------------------------------------------------------------------- +inline VDetail SaveDetailOptions::getNewDet() const +{ + return newDet; +} + +#endif // SAVEDETAILOPTIONS_H diff --git a/src/app/undocommands/savetooloptions.cpp b/src/app/undocommands/savetooloptions.cpp index 29e1c77dd..321156679 100644 --- a/src/app/undocommands/savetooloptions.cpp +++ b/src/app/undocommands/savetooloptions.cpp @@ -78,16 +78,16 @@ void SaveToolOptions::redo() //--------------------------------------------------------------------------------------------------------------------- bool SaveToolOptions::mergeWith(const QUndoCommand *command) { - const SaveToolOptions *moveCommand = static_cast(command); - SCASSERT(moveCommand != nullptr); - const quint32 id = moveCommand->getToolId(); + const SaveToolOptions *saveCommand = static_cast(command); + SCASSERT(saveCommand != nullptr); + const quint32 id = saveCommand->getToolId(); if (id != toolId) { return false; } - newXml = moveCommand->getNewXml(); + newXml = saveCommand->getNewXml(); return true; } diff --git a/src/app/undocommands/undocommands.h b/src/app/undocommands/undocommands.h index 2a565a008..c87f6547d 100644 --- a/src/app/undocommands/undocommands.h +++ b/src/app/undocommands/undocommands.h @@ -34,7 +34,8 @@ enum class UndoCommand: char { AddPatternPiece, MoveSpline, MoveSplinePath, MoveSPoint, - SaveToolOptions + SaveToolOptions, + SaveDetailOptions }; #endif // UNDOCOMMANDS_H diff --git a/src/app/undocommands/undocommands.pri b/src/app/undocommands/undocommands.pri index 0d425fcf7..80e87b370 100644 --- a/src/app/undocommands/undocommands.pri +++ b/src/app/undocommands/undocommands.pri @@ -5,7 +5,8 @@ HEADERS += \ undocommands/movespline.h \ undocommands/movesplinepath.h \ undocommands/savetooloptions.h \ - undocommands/undocommands.h + undocommands/undocommands.h \ + undocommands/savedetailoptions.h SOURCES += \ @@ -14,5 +15,6 @@ SOURCES += \ undocommands/movespoint.cpp \ undocommands/movespline.cpp \ undocommands/movesplinepath.cpp \ - undocommands/savetooloptions.cpp + undocommands/savetooloptions.cpp \ + undocommands/savedetailoptions.cpp diff --git a/src/app/xml/vdomdocument.cpp b/src/app/xml/vdomdocument.cpp index b1de1d07f..01f64b6cc 100644 --- a/src/app/xml/vdomdocument.cpp +++ b/src/app/xml/vdomdocument.cpp @@ -527,3 +527,19 @@ void VDomDocument::setTagText(const QString &tag, const QString &text) } } } + +//--------------------------------------------------------------------------------------------------------------------- +/** + * @brief RemoveAllChild remove all child from file. + * @param domElement tag in xml tree. + */ +void VDomDocument::RemoveAllChild(QDomElement &domElement) +{ + if ( domElement.hasChildNodes() ) + { + while ( domElement.childNodes().length() >= 1 ) + { + domElement.removeChild( domElement.firstChild() ); + } + } +} diff --git a/src/app/xml/vdomdocument.h b/src/app/xml/vdomdocument.h index cb1ed24b6..3588d8a28 100644 --- a/src/app/xml/vdomdocument.h +++ b/src/app/xml/vdomdocument.h @@ -140,12 +140,13 @@ public: */ static void ValidateXML(const QString &schema, const QString &fileName); void setContent(const QString &fileName); - static Unit StrToUnits(const QString &unit); + static Unit StrToUnits(const QString &unit); static QString UnitsToStr(const Unit &unit, const bool translate = false); virtual bool SaveDocument(const QString &fileName); QString Major() const; QString Minor() const; QString Patch() const; + static void RemoveAllChild(QDomElement &domElement); protected: /** * @brief data container with data.