Fixed the undo stack.

Originally committed to SVN as r2372.
This commit is contained in:
Rodrigo Braz Monteiro 2008-09-20 21:21:28 +00:00
parent c0aa50f958
commit cc85096bbd
3 changed files with 35 additions and 3 deletions

View File

@ -39,6 +39,14 @@
using namespace Athenasub; using namespace Athenasub;
///////////////
// Constructor
CModel::CModel()
: undoLimit(20), readOnly(false)
{
}
///////////////////////////////////////////////////////// /////////////////////////////////////////////////////////
// Adds a listener to be notified whenever things change // Adds a listener to be notified whenever things change
void CModel::AddListener(View listener) void CModel::AddListener(View listener)
@ -60,6 +68,9 @@ void CModel::DispatchNotifications(Notification notification) const
//////////////////////////// ////////////////////////////
// Processes an action list // Processes an action list
// type == 0 : direct action
// type == 1 : undo
// type == 2 : redo
void CModel::ProcessActionList(CActionList &_actionList,int type) void CModel::ProcessActionList(CActionList &_actionList,int type)
{ {
// Copy the list // Copy the list
@ -85,6 +96,7 @@ void CModel::ProcessActionList(CActionList &_actionList,int type)
// Insert into undo stack // Insert into undo stack
if (actions->undoAble) { if (actions->undoAble) {
stack->push_back(undo); stack->push_back(undo);
if (stack->size() > undoLimit) stack->pop_front();
if (type == 0) redoStack.clear(); if (type == 0) redoStack.clear();
} }
@ -254,3 +266,14 @@ String CModel::GetRedoMessage(const String owner) const
Controller CModel::CreateController() { Controller CModel::CreateController() {
return Controller(new CController(Model(weakThis))); return Controller(new CController(Model(weakThis)));
} }
//////////////////////////////////////////
// Sets the maximum number of undo levels
void CModel::SetUndoLimit(size_t levels)
{
undoLimit = levels;
while (undoStack.size() > undoLimit) {
undoStack.pop_front();
}
}

View File

@ -59,8 +59,9 @@ namespace Athenasub {
ActionStack undoStack; ActionStack undoStack;
ActionStack redoStack; ActionStack redoStack;
ViewList listeners; ViewList listeners;
bool readOnly;
Format format; Format format;
bool readOnly;
size_t undoLimit;
void ProcessActionList(CActionList &actionList,int type=0); void ProcessActionList(CActionList &actionList,int type=0);
@ -72,6 +73,9 @@ namespace Athenasub {
void Redo(const String owner=L""); void Redo(const String owner=L"");
void ActivateStack(ActionStack stack,bool isUndo,const String &owner); 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 DispatchNotifications(Notification notification) const;
void Clear(); void Clear();
@ -85,6 +89,7 @@ namespace Athenasub {
size_t GetSectionCount() const; size_t GetSectionCount() const;
public: public:
CModel();
Controller CreateController(); Controller CreateController();
Format GetFormat() const { return format; } Format GetFormat() const { return format; }
void AddListener(View listener); void AddListener(View listener);

View File

@ -116,6 +116,8 @@ int main()
timer.Pause(); timer.Pause();
cout << "Done in " << timer.Time() << " ms.\n"; cout << "Done in " << timer.Time() << " ms.\n";
system("pause");
// Rollback // Rollback
cout << "Undoing " << n-1 << " times... "; cout << "Undoing " << n-1 << " times... ";
timer.Start(); timer.Start();
@ -125,8 +127,10 @@ int main()
timer.Pause(); timer.Pause();
cout << "Done in " << timer.Time() << " ms.\n"; cout << "Done in " << timer.Time() << " ms.\n";
system("pause");
// Undo // Undo
n = 100; n = 1000;
cout << "Undoing and redoing " << n << " times... "; cout << "Undoing and redoing " << n << " times... ";
timer.Start(); timer.Start();
for (int i=0;i<n;i++) { for (int i=0;i<n;i++) {