From 0900025e2db61ed63cd4bc857940e4ae5f7ded6b Mon Sep 17 00:00:00 2001 From: wmayer Date: Thu, 15 Aug 2013 16:47:56 +0200 Subject: [PATCH] + better check for opening a transaction when removing an object from document --- src/App/Document.cpp | 25 +++++++++++++++++-------- src/App/Document.h | 2 +- src/App/Transactions.cpp | 6 ++++++ src/App/Transactions.h | 2 ++ 4 files changed, 26 insertions(+), 9 deletions(-) diff --git a/src/App/Document.cpp b/src/App/Document.cpp index ee92e46da..5f476c04c 100644 --- a/src/App/Document.cpp +++ b/src/App/Document.cpp @@ -229,8 +229,8 @@ bool Document::undo(void) if (d->iUndoMode) { if (d->activeUndoTransaction) commitTransaction(); - else - assert(mUndoTransactions.size()!=0); + else if (mUndoTransactions.empty()) + return false; // redo d->activeUndoTransaction = new Transaction(); @@ -313,12 +313,21 @@ void Document::openTransaction(const char* name) } } -void Document::_checkTransaction(void) +void Document::_checkTransaction(DocumentObject* pcObject) { // if the undo is active but no transaction open, open one! if (d->iUndoMode) { - if (!d->activeUndoTransaction) - openTransaction(); + if (!d->activeUndoTransaction) { + // When the object is going to be deleted we have to check if it has already been added to + // the undo transactions + std::list::iterator it; + for (it = mUndoTransactions.begin(); it != mUndoTransactions.end(); ++it) { + if ((*it)->hasObject(pcObject)) { + openTransaction(); + break; + } + } + } } } @@ -1440,14 +1449,14 @@ void Document::_addObject(DocumentObject* pcObject, const char* pObjectName) /// Remove an object out of the document void Document::remObject(const char* sName) { - _checkTransaction(); - std::map::iterator pos = d->objectMap.find(sName); // name not found? if (pos == d->objectMap.end()) return; + _checkTransaction(pos->second); + if (d->activeObject == pos->second) d->activeObject = 0; @@ -1499,7 +1508,7 @@ void Document::remObject(const char* sName) /// Remove an object out of the document (internal) void Document::_remObject(DocumentObject* pcObject) { - _checkTransaction(); + _checkTransaction(pcObject); std::map::iterator pos = d->objectMap.find(pcObject->getNameInDocument()); diff --git a/src/App/Document.h b/src/App/Document.h index 3ab0cb639..f192244e8 100644 --- a/src/App/Document.h +++ b/src/App/Document.h @@ -293,7 +293,7 @@ protected: DocumentObject* _copyObject(DocumentObject* obj, std::map&, bool recursive=false, bool keepdigitsatend=false); /// checks if a valid transaction is open - void _checkTransaction(void); + void _checkTransaction(DocumentObject* pcObject); void breakDependency(DocumentObject* pcObject, bool clear); std::vector readObjects(Base::XMLReader& reader); void writeObjects(const std::vector&, Base::Writer &writer) const; diff --git a/src/App/Transactions.cpp b/src/App/Transactions.cpp index f61b97be9..d52376370 100644 --- a/src/App/Transactions.cpp +++ b/src/App/Transactions.cpp @@ -102,6 +102,12 @@ int Transaction::getPos(void) const return iPos; } +bool Transaction::hasObject(DocumentObject *Obj) const +{ + std::map::const_iterator it; + it = _Objects.find(Obj); + return (it != _Objects.end()); +} //************************************************************************** // separator for other implemetation aspects diff --git a/src/App/Transactions.h b/src/App/Transactions.h index d9657ed3c..5c66fa6f2 100644 --- a/src/App/Transactions.h +++ b/src/App/Transactions.h @@ -93,6 +93,8 @@ public: /// get the position in the transaction history int getPos(void) const; + /// check if this object is used in a transaction + bool hasObject(DocumentObject *Obj) const; friend class Document;