Undocommand "add piece".

--HG--
branch : feature
This commit is contained in:
Roman Telezhynskyi 2016-11-08 12:54:33 +02:00
parent 37173fb0e4
commit 2b75519d66
5 changed files with 196 additions and 2 deletions

View file

@ -0,0 +1,111 @@
/************************************************************************
**
** @file
** @author Roman Telezhynskyi <dismine(at)gmail.com>
** @date 6 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 "addpiece.h"
#include "../vpatterndb/vpiecenode.h"
//---------------------------------------------------------------------------------------------------------------------
AddPiece::AddPiece(const QDomElement &xml, VAbstractPattern *doc, const VPiece &detail, const QString &drawName,
QUndoCommand *parent)
: VUndoCommand(xml, doc, parent),
m_detail(detail),
m_drawName(drawName)
{
setText(tr("add detail"));
nodeId = doc->GetParametrId(xml);
}
//---------------------------------------------------------------------------------------------------------------------
AddPiece::~AddPiece()
{}
//---------------------------------------------------------------------------------------------------------------------
void AddPiece::undo()
{
qCDebug(vUndo, "Undo.");
QDomElement details = GetDetailsSection();
if (not details.isNull())
{
QDomElement domElement = doc->elementById(nodeId);
if (domElement.isElement())
{
if (details.removeChild(domElement).isNull())
{
qCDebug(vUndo, "Can't delete node");
return;
}
DecrementReferences(m_detail.GetNodes());
}
else
{
qCDebug(vUndo, "Can't get node by id = %u.", nodeId);
return;
}
}
else
{
qCDebug(vUndo, "Can't find tag %s.", qUtf8Printable(VAbstractPattern::TagDetails));
return;
}
emit NeedFullParsing();
}
//---------------------------------------------------------------------------------------------------------------------
void AddPiece::redo()
{
qCDebug(vUndo, "Redo.");
QDomElement details = GetDetailsSection();
if (not details.isNull())
{
details.appendChild(xml);
}
else
{
qCDebug(vUndo, "Can't find tag %s.", qUtf8Printable(VAbstractPattern::TagDetails));
return;
}
RedoFullParsing();
}
//---------------------------------------------------------------------------------------------------------------------
QDomElement AddPiece::GetDetailsSection() const
{
QDomElement details;
if (m_drawName.isEmpty())
{
doc->GetActivNodeElement(VAbstractPattern::TagDetails, details);
}
else
{
details = doc->GetDraw(m_drawName).firstChildElement(VAbstractPattern::TagDetails);
}
return details;
}

View file

@ -0,0 +1,69 @@
/************************************************************************
**
** @file
** @author Roman Telezhynskyi <dismine(at)gmail.com>
** @date 6 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 ADDPIECE_H
#define ADDPIECE_H
#include <qcompilerdetection.h>
#include <QDomElement>
#include <QMetaObject>
#include <QObject>
#include <QString>
#include <QtGlobal>
#include "../tools/vtoolseamallowance.h"
#include "vpiece.h"
#include "vundocommand.h"
class QDomElement;
class QUndoCommand;
class VAbstractPattern;
class AddPiece : public VUndoCommand
{
Q_OBJECT
public:
AddPiece(const QDomElement &xml, VAbstractPattern *doc, const VPiece &detail, const QString &drawName = QString(),
QUndoCommand *parent = nullptr);
virtual ~AddPiece();
// cppcheck-suppress unusedFunction
virtual void undo() Q_DECL_OVERRIDE;
// cppcheck-suppress unusedFunction
virtual void redo() Q_DECL_OVERRIDE;
private:
Q_DISABLE_COPY(AddPiece)
VPiece m_detail;
QString m_drawName;
QDomElement GetDetailsSection() const;
};
#endif // ADDPIECE_H

View file

@ -23,7 +23,8 @@ HEADERS += \
$$PWD/delgroup.h \
$$PWD/label/moveabstractlabel.h \
$$PWD/toggledetailinlayout.h \
$$PWD/label/operationmovelabel.h
$$PWD/label/operationmovelabel.h \
$$PWD/addpiece.h
SOURCES += \
$$PWD/addtocalc.cpp \
@ -47,4 +48,5 @@ SOURCES += \
$$PWD/delgroup.cpp \
$$PWD/label/moveabstractlabel.cpp \
$$PWD/toggledetailinlayout.cpp \
$$PWD/label/operationmovelabel.cpp
$$PWD/label/operationmovelabel.cpp \
$$PWD/addpiece.cpp

View file

@ -33,6 +33,7 @@
#include "../ifc/ifcdef.h"
#include "../vmisc/def.h"
#include "../vpatterndb/vnodedetail.h"
#include "../vpatterndb/vpiecenode.h"
class QDomElement;
class QDomNode;
@ -92,3 +93,12 @@ void VUndoCommand::DecrementReferences(const QVector<VNodeDetail> &nodes) const
doc->DecrementReferens(nodes.at(i).getId());
}
}
//---------------------------------------------------------------------------------------------------------------------
void VUndoCommand::DecrementReferences(const QVector<VPieceNode> &nodes) const
{
for (qint32 i = 0; i < nodes.size(); ++i)
{
doc->DecrementReferens(nodes.at(i).GetId());
}
}

View file

@ -65,6 +65,7 @@ enum class UndoCommand: char { AddPatternPiece,
};
class VNodeDetail;
class VPieceNode;
class VPattern;
class VUndoCommand : public QObject, public QUndoCommand
@ -87,6 +88,7 @@ protected:
void IncrementReferences(const QVector<VNodeDetail> &nodes) const;
void DecrementReferences(const QVector<VNodeDetail> &nodes) const;
void DecrementReferences(const QVector<VPieceNode> &nodes) const;
private:
Q_DISABLE_COPY(VUndoCommand)
};