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;
///////////////
// 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)));
}
}
//////////////////////////////////////////
// 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 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);

View file

@ -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<n;i++) {