From 9ad3285769811150fbf7a16449d35fb4e159dd25 Mon Sep 17 00:00:00 2001 From: dismine Date: Wed, 29 Oct 2014 12:57:58 +0200 Subject: [PATCH] Keep information about sibling node before delete detail. --HG-- branch : develop --- src/app/options.h | 1 + src/app/undocommands/deletedetail.cpp | 19 +++++++++++++++---- src/app/undocommands/deletedetail.h | 1 + src/app/undocommands/deltool.cpp | 19 ++++--------------- src/app/undocommands/deltool.h | 2 +- src/app/undocommands/vundocommand.cpp | 21 +++++++++++++++++++++ src/app/undocommands/vundocommand.h | 1 + src/app/xml/vpattern.cpp | 1 + 8 files changed, 45 insertions(+), 20 deletions(-) diff --git a/src/app/options.h b/src/app/options.h index c384fca68..fb12f3d19 100644 --- a/src/app/options.h +++ b/src/app/options.h @@ -51,6 +51,7 @@ static const quint32 null_id = 0; #define SceneSize 50000 #define DefPointRadius 2.0//mm #define NULL_ID null_id//use this value for initialization variables that keeps id values. 0 mean uknown id value. +#define NULL_ID_STR "0" extern const QString nameRegExp; extern const QString degreeSymbol; diff --git a/src/app/undocommands/deletedetail.cpp b/src/app/undocommands/deletedetail.cpp index 0993fe29d..c06c4e95a 100644 --- a/src/app/undocommands/deletedetail.cpp +++ b/src/app/undocommands/deletedetail.cpp @@ -32,7 +32,7 @@ //--------------------------------------------------------------------------------------------------------------------- DeleteDetail::DeleteDetail(VPattern *doc, quint32 id, QUndoCommand *parent) - : VUndoCommand(QDomElement(), doc, parent), parentNode(QDomNode()) + : VUndoCommand(QDomElement(), doc, parent), parentNode(QDomNode()), siblingId(NULL_ID) { setText(tr("Delete tool")); nodeId = id; @@ -41,6 +41,16 @@ DeleteDetail::DeleteDetail(VPattern *doc, quint32 id, QUndoCommand *parent) { xml = domElement.cloneNode().toElement(); parentNode = domElement.parentNode(); + QDomNode previousDetail = domElement.previousSibling(); + if (previousDetail.isNull()) + { + siblingId = NULL_ID; + } + else + { + // Better save id of previous detail instead of reference to node. + siblingId = doc->GetParametrUInt(previousDetail.toElement(), VPattern::AttrId, NULL_ID_STR); + } } else { @@ -56,7 +66,7 @@ DeleteDetail::~DeleteDetail() //--------------------------------------------------------------------------------------------------------------------- void DeleteDetail::undo() { - parentNode.appendChild(xml); + UndoDeleteAfterSibling(parentNode, siblingId); emit NeedFullParsing(); } @@ -68,14 +78,15 @@ void DeleteDetail::redo() { parentNode.removeChild(domElement); - //When UnionDetail delete detail we can't use FullParsing. So we hide detail on scene directly. + // 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* tools = doc->getTools(); SCASSERT(tools != nullptr); VToolDetail *toolDet = qobject_cast(tools->value(nodeId)); SCASSERT(toolDet != nullptr); toolDet->hide(); - emit NeedFullParsing(); + emit NeedFullParsing(); // Doesn't work when UnionDetail delete detail. } else { diff --git a/src/app/undocommands/deletedetail.h b/src/app/undocommands/deletedetail.h index 90e2427e1..a5b5f62a0 100644 --- a/src/app/undocommands/deletedetail.h +++ b/src/app/undocommands/deletedetail.h @@ -43,6 +43,7 @@ public: private: Q_DISABLE_COPY(DeleteDetail) QDomNode parentNode; + quint32 siblingId; }; #endif // DELETEDETAIL_H diff --git a/src/app/undocommands/deltool.cpp b/src/app/undocommands/deltool.cpp index b9c072da2..386b91f74 100644 --- a/src/app/undocommands/deltool.cpp +++ b/src/app/undocommands/deltool.cpp @@ -33,7 +33,7 @@ //--------------------------------------------------------------------------------------------------------------------- DelTool::DelTool(VPattern *doc, quint32 id, QUndoCommand *parent) - : VUndoCommand(QDomElement(), doc, parent), parentNode(QDomNode()), cursor(NULL_ID) + : VUndoCommand(QDomElement(), doc, parent), parentNode(QDomNode()), siblingId(NULL_ID) { setText(tr("Delete tool")); nodeId = id; @@ -46,12 +46,12 @@ DelTool::DelTool(VPattern *doc, quint32 id, QUndoCommand *parent) { if (i == 0) { - cursor = NULL_ID; + siblingId = NULL_ID; } else { const VToolRecord tool = history.at(i-1); - cursor = tool.getId(); + siblingId = tool.getId(); } } } @@ -76,18 +76,7 @@ DelTool::~DelTool() //--------------------------------------------------------------------------------------------------------------------- void DelTool::undo() { - if (cursor == NULL_ID) - { - parentNode.appendChild(xml); - } - else - { - QDomElement refElement = doc->elementById(QString().setNum(cursor)); - if (refElement.isElement()) - { - parentNode.insertAfter(xml, refElement); - } - } + UndoDeleteAfterSibling(parentNode, siblingId); emit NeedFullParsing(); } diff --git a/src/app/undocommands/deltool.h b/src/app/undocommands/deltool.h index 433f2c809..dfa19dabe 100644 --- a/src/app/undocommands/deltool.h +++ b/src/app/undocommands/deltool.h @@ -43,7 +43,7 @@ public: private: Q_DISABLE_COPY(DelTool) QDomNode parentNode; - quint32 cursor; + quint32 siblingId; }; #endif // DELTOOL_H diff --git a/src/app/undocommands/vundocommand.cpp b/src/app/undocommands/vundocommand.cpp index a022f4f63..9391f8dd0 100644 --- a/src/app/undocommands/vundocommand.cpp +++ b/src/app/undocommands/vundocommand.cpp @@ -49,3 +49,24 @@ void VUndoCommand::RedoFullParsing() } redoFlag = true; } + +//--------------------------------------------------------------------------------------------------------------------- +void VUndoCommand::UndoDeleteAfterSibling(QDomNode &parentNode, const quint32 &siblingId) const +{ + if (siblingId == NULL_ID) + { + parentNode.appendChild(xml); + } + else + { + const QDomElement refElement = doc->elementById(QString().setNum(siblingId)); + if (refElement.isElement()) + { + parentNode.insertAfter(xml, refElement); + } + else + { + qDebug()<<"Can't find sibling node."; + } + } +} diff --git a/src/app/undocommands/vundocommand.h b/src/app/undocommands/vundocommand.h index 1b142007c..57120e6fb 100644 --- a/src/app/undocommands/vundocommand.h +++ b/src/app/undocommands/vundocommand.h @@ -65,6 +65,7 @@ protected: quint32 nodeId; bool redoFlag; void RedoFullParsing(); + void UndoDeleteAfterSibling(QDomNode &parentNode, const quint32 &siblingId) const; private: Q_DISABLE_COPY(VUndoCommand) }; diff --git a/src/app/xml/vpattern.cpp b/src/app/xml/vpattern.cpp index 3cd182be0..93730c6ac 100644 --- a/src/app/xml/vpattern.cpp +++ b/src/app/xml/vpattern.cpp @@ -211,6 +211,7 @@ bool VPattern::GetActivDrawElement(QDomElement &element) const } } } + element = QDomElement(); } return false; }