From c4cefa677eb6b4b0091ae269a9713d12c6cc52b0 Mon Sep 17 00:00:00 2001 From: Anatoly Mihalchenko Date: Wed, 14 Dec 2011 16:02:25 +0300 Subject: [PATCH] SANKORE-300 / SANKORE-332 : fix clear item when we add new page, and this item not in scene --- src/board/UBBoardController.cpp | 45 +++++++++++++++++++++++++- src/board/UBBoardController.h | 3 ++ src/domain/UBGraphicsItemUndoCommand.h | 3 ++ 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/board/UBBoardController.cpp b/src/board/UBBoardController.cpp index 829c07ef..60ef79ee 100644 --- a/src/board/UBBoardController.cpp +++ b/src/board/UBBoardController.cpp @@ -1132,7 +1132,7 @@ void UBBoardController::setActiveDocumentScene(UBDocumentProxy* pDocumentProxy, if(sceneChange) emit activeSceneWillChange(); - UBApplication::undoStack->clear(); + ClearUndoStack(); mActiveScene = targetScene; mActiveDocument = pDocumentProxy; @@ -1173,6 +1173,49 @@ void UBBoardController::setActiveDocumentScene(UBDocumentProxy* pDocumentProxy, } } +void UBBoardController::ClearUndoStack() +{ + QSet uniqueItems; + QUndoStack *stack = UBApplication::undoStack; + // go through all stack command + for(int i = 0; i < stack->count(); i++) + { + UBGraphicsItemUndoCommand *cmd = (UBGraphicsItemUndoCommand*)stack->command(i); + + // go through all added and removed objects, for create list of unique objects + QSetIterator itAdded(cmd->GetAddedList()); + while (itAdded.hasNext()) + { + QGraphicsItem* item = itAdded.next(); + if( !uniqueItems.contains(item) ) + uniqueItems.insert(item); + } + + QSetIterator itRemoved(cmd->GetRemovedList()); + while (itRemoved.hasNext()) + { + QGraphicsItem* item = itRemoved.next(); + if( !uniqueItems.contains(item) ) + uniqueItems.insert(item); + } + } + + // clear stack, and command list + stack->clear(); + + // go through all unique items, and check, ot on scene, or not. + // if not on scene, than item can be deleted + + QSetIterator itUniq(uniqueItems); + while (itUniq.hasNext()) + { + QGraphicsItem* item = itUniq.next(); + UBGraphicsScene *scene = (UBGraphicsScene*)item->scene(); + if(!scene) + delete item; + } + +} void UBBoardController::adjustDisplayViews() { diff --git a/src/board/UBBoardController.h b/src/board/UBBoardController.h index 218130ab..2b17670b 100644 --- a/src/board/UBBoardController.h +++ b/src/board/UBBoardController.h @@ -150,6 +150,9 @@ class UBBoardController : public QObject void notifyPageChanged(); void displayMetaData(QMap metadatas); + void ClearUndoStack(); + + public slots: void setActiveDocumentScene(UBDocumentProxy* pDocumentProxy, int pSceneIndex = 0); void showDocumentsDialog(); diff --git a/src/domain/UBGraphicsItemUndoCommand.h b/src/domain/UBGraphicsItemUndoCommand.h index 4d513915..84711bd4 100644 --- a/src/domain/UBGraphicsItemUndoCommand.h +++ b/src/domain/UBGraphicsItemUndoCommand.h @@ -32,6 +32,9 @@ class UBGraphicsItemUndoCommand : public QUndoCommand virtual ~UBGraphicsItemUndoCommand(); + QSet GetAddedList() { return mAddedItems; }; + QSet GetRemovedList() { return mRemovedItems; }; + protected: virtual void undo(); virtual void redo();