Keep information about sibling node before delete detail.

--HG--
branch : develop
This commit is contained in:
dismine 2014-10-29 12:57:58 +02:00
parent 205cca7eec
commit 9ad3285769
8 changed files with 45 additions and 20 deletions

View File

@ -51,6 +51,7 @@ static const quint32 null_id = 0;
#define SceneSize 50000 #define SceneSize 50000
#define DefPointRadius 2.0//mm #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 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 nameRegExp;
extern const QString degreeSymbol; extern const QString degreeSymbol;

View File

@ -32,7 +32,7 @@
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
DeleteDetail::DeleteDetail(VPattern *doc, quint32 id, QUndoCommand *parent) 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")); setText(tr("Delete tool"));
nodeId = id; nodeId = id;
@ -41,6 +41,16 @@ DeleteDetail::DeleteDetail(VPattern *doc, quint32 id, QUndoCommand *parent)
{ {
xml = domElement.cloneNode().toElement(); xml = domElement.cloneNode().toElement();
parentNode = domElement.parentNode(); 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 else
{ {
@ -56,7 +66,7 @@ DeleteDetail::~DeleteDetail()
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void DeleteDetail::undo() void DeleteDetail::undo()
{ {
parentNode.appendChild(xml); UndoDeleteAfterSibling(parentNode, siblingId);
emit NeedFullParsing(); emit NeedFullParsing();
} }
@ -68,14 +78,15 @@ void DeleteDetail::redo()
{ {
parentNode.removeChild(domElement); 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<quint32, VDataTool*>* tools = doc->getTools(); QHash<quint32, VDataTool*>* tools = doc->getTools();
SCASSERT(tools != nullptr); SCASSERT(tools != nullptr);
VToolDetail *toolDet = qobject_cast<VToolDetail*>(tools->value(nodeId)); VToolDetail *toolDet = qobject_cast<VToolDetail*>(tools->value(nodeId));
SCASSERT(toolDet != nullptr); SCASSERT(toolDet != nullptr);
toolDet->hide(); toolDet->hide();
emit NeedFullParsing(); emit NeedFullParsing(); // Doesn't work when UnionDetail delete detail.
} }
else else
{ {

View File

@ -43,6 +43,7 @@ public:
private: private:
Q_DISABLE_COPY(DeleteDetail) Q_DISABLE_COPY(DeleteDetail)
QDomNode parentNode; QDomNode parentNode;
quint32 siblingId;
}; };
#endif // DELETEDETAIL_H #endif // DELETEDETAIL_H

View File

@ -33,7 +33,7 @@
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
DelTool::DelTool(VPattern *doc, quint32 id, QUndoCommand *parent) 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")); setText(tr("Delete tool"));
nodeId = id; nodeId = id;
@ -46,12 +46,12 @@ DelTool::DelTool(VPattern *doc, quint32 id, QUndoCommand *parent)
{ {
if (i == 0) if (i == 0)
{ {
cursor = NULL_ID; siblingId = NULL_ID;
} }
else else
{ {
const VToolRecord tool = history.at(i-1); const VToolRecord tool = history.at(i-1);
cursor = tool.getId(); siblingId = tool.getId();
} }
} }
} }
@ -76,18 +76,7 @@ DelTool::~DelTool()
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void DelTool::undo() void DelTool::undo()
{ {
if (cursor == NULL_ID) UndoDeleteAfterSibling(parentNode, siblingId);
{
parentNode.appendChild(xml);
}
else
{
QDomElement refElement = doc->elementById(QString().setNum(cursor));
if (refElement.isElement())
{
parentNode.insertAfter(xml, refElement);
}
}
emit NeedFullParsing(); emit NeedFullParsing();
} }

View File

@ -43,7 +43,7 @@ public:
private: private:
Q_DISABLE_COPY(DelTool) Q_DISABLE_COPY(DelTool)
QDomNode parentNode; QDomNode parentNode;
quint32 cursor; quint32 siblingId;
}; };
#endif // DELTOOL_H #endif // DELTOOL_H

View File

@ -49,3 +49,24 @@ void VUndoCommand::RedoFullParsing()
} }
redoFlag = true; 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.";
}
}
}

View File

@ -65,6 +65,7 @@ protected:
quint32 nodeId; quint32 nodeId;
bool redoFlag; bool redoFlag;
void RedoFullParsing(); void RedoFullParsing();
void UndoDeleteAfterSibling(QDomNode &parentNode, const quint32 &siblingId) const;
private: private:
Q_DISABLE_COPY(VUndoCommand) Q_DISABLE_COPY(VUndoCommand)
}; };

View File

@ -211,6 +211,7 @@ bool VPattern::GetActivDrawElement(QDomElement &element) const
} }
} }
} }
element = QDomElement();
} }
return false; return false;
} }