New undo command SaveDetailOptions.

--HG--
branch : feature
This commit is contained in:
dismine 2014-06-12 16:38:28 +03:00
parent 154c2c0708
commit c23a93037c
21 changed files with 342 additions and 92 deletions

View File

@ -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"<<Q_FUNC_INFO;
}
}
else
{
qDebug()<<"parent isNull"<<Q_FUNC_INFO;
}
}
else
{
qDebug()<<"Can't get element by id form file = "<<id<<Q_FUNC_INFO;
}
RemoveReferens();//deincrement referens
}
}
void VAbstractNode::RestoreNode()
{
if (_referens <= 1)
{
RestoreReferens();
}
}
@ -113,3 +95,29 @@ void VAbstractNode::decrementReferens()
}
}
}
//---------------------------------------------------------------------------------------------------------------------
void VAbstractNode::RemoveReferens()
{
if (idTool != 0)
{
doc->DecrementReferens(idTool);
}
else
{
doc->DecrementReferens(idNode);
}
}
//---------------------------------------------------------------------------------------------------------------------
void VAbstractNode::RestoreReferens()
{
if (idTool != 0)
{
doc->IncrementReferens(idTool);
}
else
{
doc->IncrementReferens(idNode);
}
}

View File

@ -53,6 +53,7 @@ public:
static const QString AttrIdObject;
static const QString AttrIdTool;
virtual void DeleteNode();
virtual void RestoreNode();
protected:
/**
* @brief idNodenode id.
@ -71,6 +72,8 @@ protected:
* @brief decrementReferens decrement reference for all parent objects.
*/
virtual void decrementReferens();
virtual void RemoveReferens();
virtual void RestoreReferens();
};
#endif // VABSTRACTNODE_H

View File

@ -87,6 +87,15 @@ void VNodeArc::DeleteNode()
this->setVisible(false);
}
void VNodeArc::RestoreNode()
{
if (this->isVisible() == false)
{
VAbstractNode::RestoreNode();
this->setVisible(true);
}
}
//---------------------------------------------------------------------------------------------------------------------
void VNodeArc::FullUpdateFromFile()
{

View File

@ -73,6 +73,7 @@ public:
* @brief DeleteNode delete node from detail.
*/
virtual void DeleteNode();
virtual void RestoreNode();
public slots:
/**
* @brief FullUpdateFromFile update tool data form file.

View File

@ -100,6 +100,15 @@ void VNodePoint::DeleteNode()
this->setVisible(false);
}
void VNodePoint::RestoreNode()
{
if (this->isVisible() == false)
{
VAbstractNode::RestoreNode();
this->setVisible(true);
}
}
//---------------------------------------------------------------------------------------------------------------------
void VNodePoint::FullUpdateFromFile()
{

View File

@ -77,6 +77,7 @@ public:
* @brief DeleteNode delete node from detail.
*/
virtual void DeleteNode();
virtual void RestoreNode();
public slots:
/**
* @brief FullUpdateFromFile update tool data form file.

View File

@ -91,6 +91,15 @@ void VNodeSpline::DeleteNode()
this->setVisible(false);
}
void VNodeSpline::RestoreNode()
{
if (this->isVisible() == false)
{
VAbstractNode::RestoreNode();
this->setVisible(true);
}
}
//---------------------------------------------------------------------------------------------------------------------
void VNodeSpline::FullUpdateFromFile()
{

View File

@ -73,6 +73,7 @@ public:
* @brief DeleteNode delete node from detail.
*/
virtual void DeleteNode();
virtual void RestoreNode();
public slots:
/**
* @brief FullUpdateFromFile update tool data form file.

View File

@ -94,6 +94,15 @@ void VNodeSplinePath::DeleteNode()
this->setVisible(false);
}
void VNodeSplinePath::RestoreNode()
{
if (this->isVisible() == false)
{
VAbstractNode::RestoreNode();
this->setVisible(true);
}
}
//---------------------------------------------------------------------------------------------------------------------
void VNodeSplinePath::FullUpdateFromFile()
{

View File

@ -73,6 +73,7 @@ public:
* @brief DeleteNode delete node from detail.
*/
virtual void DeleteNode();
virtual void RestoreNode();
public slots:
/**
* @brief FullUpdateFromFile update tool data form file.

View File

@ -202,18 +202,6 @@ QPointF VAbstractTool::addVector(const QPointF &p, const QPointF &p1, const QPoi
return QPointF (p.x() + (p2.x() - p1.x()) * k, p.y() + (p2.y() - p1.y()) * k);
}
//---------------------------------------------------------------------------------------------------------------------
void VAbstractTool::RemoveAllChild(QDomElement &domElement)
{
if ( domElement.hasChildNodes() )
{
while ( domElement.childNodes().length() >= 1 )
{
domElement.removeChild( domElement.firstChild() );
}
}
}
//---------------------------------------------------------------------------------------------------------------------
void VAbstractTool::DeleteTool(QGraphicsItem *tool)
{

View File

@ -32,6 +32,7 @@
#include "vdatatool.h"
class VPattern;
class QDomElement;
/**
* @brief The VAbstractTool abstract class for all tools.
@ -202,11 +203,6 @@ protected:
* @brief RemoveReferens decrement value of reference.
*/
virtual void RemoveReferens(){}
/**
* @brief RemoveAllChild remove all child from file.
* @param domElement tag in xml tree.
*/
void RemoveAllChild(QDomElement &domElement);
/**
* @brief DeleteTool full delete object form scene and file.
* @param tool tool

View File

@ -38,6 +38,7 @@
#include <QGraphicsSceneMouseEvent>
#include <QMenu>
#include <QGraphicsView>
#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<DialogDetail*>(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<quint32> list = detail.Missing(det);
QHash<quint32, VDataTool*>* tools = doc->getTools();
if (list.size()>0)
{
for (qint32 i = 0; i < list.size(); ++i)
{
VAbstractNode *node = qobject_cast<VAbstractNode *>(tools->value(list.at(i)));
node->DeleteNode();
}
}
emit LiteUpdateTree();
emit toolhaveChange();
}
SCASSERT(dialog != nullptr);
DialogDetail *dialogTool = qobject_cast<DialogDetail*>(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);

View File

@ -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 <typename Tool>
/**
* @brief InitTool initial node item on scene

View File

@ -0,0 +1,139 @@
/************************************************************************
**
** @file savedetailoptions.cpp
** @author Roman Telezhynskyi <dismine(at)gmail.com>
** @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
** <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 "savedetailoptions.h"
#include "undocommands.h"
#include "../tools/nodeDetails/vabstractnode.h"
#include <QGraphicsView>
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<VNodeDetail> nodes = oldDet.getNodes();
QHash<quint32, VDataTool*>* tools = doc->getTools();
if (nodes.size()>0)
{
for (qint32 i = 0; i < nodes.size(); ++i)
{
VAbstractNode *node = qobject_cast<VAbstractNode *>(tools->value(nodes.at(i).getId()));
node->RestoreNode();
}
}
emit NeedLiteParsing();
QList<QGraphicsView*> 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<quint32> list = oldDet.Missing(newDet);
QHash<quint32, VDataTool*>* tools = doc->getTools();
if (list.size()>0)
{
for (qint32 i = 0; i < list.size(); ++i)
{
VAbstractNode *node = qobject_cast<VAbstractNode *>(tools->value(list.at(i)));
node->DeleteNode();
}
}
emit NeedLiteParsing();
QList<QGraphicsView*> 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<const SaveDetailOptions *>(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<int>(UndoCommand::SaveDetailOptions);
}

View File

@ -0,0 +1,75 @@
/************************************************************************
**
** @file savedetailoptions.h
** @author Roman Telezhynskyi <dismine(at)gmail.com>
** @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
** <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 SAVEDETAILOPTIONS_H
#define SAVEDETAILOPTIONS_H
#include <QUndoCommand>
#include <QDomElement>
#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

View File

@ -78,16 +78,16 @@ void SaveToolOptions::redo()
//---------------------------------------------------------------------------------------------------------------------
bool SaveToolOptions::mergeWith(const QUndoCommand *command)
{
const SaveToolOptions *moveCommand = static_cast<const SaveToolOptions *>(command);
SCASSERT(moveCommand != nullptr);
const quint32 id = moveCommand->getToolId();
const SaveToolOptions *saveCommand = static_cast<const SaveToolOptions *>(command);
SCASSERT(saveCommand != nullptr);
const quint32 id = saveCommand->getToolId();
if (id != toolId)
{
return false;
}
newXml = moveCommand->getNewXml();
newXml = saveCommand->getNewXml();
return true;
}

View File

@ -34,7 +34,8 @@ enum class UndoCommand: char { AddPatternPiece,
MoveSpline,
MoveSplinePath,
MoveSPoint,
SaveToolOptions
SaveToolOptions,
SaveDetailOptions
};
#endif // UNDOCOMMANDS_H

View File

@ -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

View File

@ -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() );
}
}
}

View File

@ -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.