From cc85096bbd140ae9ed3416cc70df4bc138dd10ad Mon Sep 17 00:00:00 2001 From: Rodrigo Braz Monteiro Date: Sat, 20 Sep 2008 21:21:28 +0000 Subject: [PATCH] Fixed the undo stack. Originally committed to SVN as r2372. --- aegilib/src/model.cpp | 25 ++++++++++++++++++++++++- aegilib/src/model.h | 7 ++++++- aegilib/test/src/main.cpp | 6 +++++- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/aegilib/src/model.cpp b/aegilib/src/model.cpp index a373e44a3..8c1bdc365 100644 --- a/aegilib/src/model.cpp +++ b/aegilib/src/model.cpp @@ -39,6 +39,14 @@ using namespace Athenasub; +/////////////// +// Constructor +CModel::CModel() +: undoLimit(20), readOnly(false) +{ +} + + ///////////////////////////////////////////////////////// // Adds a listener to be notified whenever things change void CModel::AddListener(View listener) @@ -60,6 +68,9 @@ void CModel::DispatchNotifications(Notification notification) const //////////////////////////// // Processes an action list +// type == 0 : direct action +// type == 1 : undo +// type == 2 : redo void CModel::ProcessActionList(CActionList &_actionList,int type) { // Copy the list @@ -85,6 +96,7 @@ void CModel::ProcessActionList(CActionList &_actionList,int type) // Insert into undo stack if (actions->undoAble) { stack->push_back(undo); + if (stack->size() > undoLimit) stack->pop_front(); if (type == 0) redoStack.clear(); } @@ -253,4 +265,15 @@ String CModel::GetRedoMessage(const String owner) const // Create controller Controller CModel::CreateController() { return Controller(new CController(Model(weakThis))); -} \ No newline at end of file +} + + +////////////////////////////////////////// +// Sets the maximum number of undo levels +void CModel::SetUndoLimit(size_t levels) +{ + undoLimit = levels; + while (undoStack.size() > undoLimit) { + undoStack.pop_front(); + } +} diff --git a/aegilib/src/model.h b/aegilib/src/model.h index 51bd7b381..6487889c6 100644 --- a/aegilib/src/model.h +++ b/aegilib/src/model.h @@ -59,8 +59,9 @@ namespace Athenasub { ActionStack undoStack; ActionStack redoStack; ViewList listeners; - bool readOnly; Format format; + bool readOnly; + size_t undoLimit; void ProcessActionList(CActionList &actionList,int type=0); @@ -72,6 +73,9 @@ namespace Athenasub { void Redo(const String owner=L""); void ActivateStack(ActionStack stack,bool isUndo,const String &owner); + void SetUndoLimit(size_t levels); + size_t GetUndoLimit() const { return undoLimit; } + void DispatchNotifications(Notification notification) const; void Clear(); @@ -85,6 +89,7 @@ namespace Athenasub { size_t GetSectionCount() const; public: + CModel(); Controller CreateController(); Format GetFormat() const { return format; } void AddListener(View listener); diff --git a/aegilib/test/src/main.cpp b/aegilib/test/src/main.cpp index becc68e42..eeaa082d2 100644 --- a/aegilib/test/src/main.cpp +++ b/aegilib/test/src/main.cpp @@ -116,6 +116,8 @@ int main() timer.Pause(); cout << "Done in " << timer.Time() << " ms.\n"; + system("pause"); + // Rollback cout << "Undoing " << n-1 << " times... "; timer.Start(); @@ -125,8 +127,10 @@ int main() timer.Pause(); cout << "Done in " << timer.Time() << " ms.\n"; + system("pause"); + // Undo - n = 100; + n = 1000; cout << "Undoing and redoing " << n << " times... "; timer.Start(); for (int i=0;i