source: molecuilder/src/Actions/ActionHistory.cpp@ 91b18cd

Last change on this file since 91b18cd was abd4a1, checked in by Tillmann Crueger <crueger@…>, 16 years ago

Added detailed documentation for the Action class

  • Property mode set to 100644
File size: 2.9 KB
Line 
1/*
2 * ActionHistory.cpp
3 *
4 * Created on: Mar 25, 2010
5 * Author: crueger
6 */
7
8#include "ActionHistory.hpp"
9
10#include <iostream>
11
12#include "Patterns/Singleton_impl.hpp"
13#include "Helpers/Assert.hpp"
14
15using namespace std;
16
17ActionHistory::ActionHistory()
18{}
19
20ActionHistory::~ActionHistory()
21{}
22
23void ActionHistory::undoLast(){
24 ASSERT(history.size(),"Undo performed when the undo-queue was empty");
25 HistoryElement elem = history.back();
26 history.pop_back();
27 Action::state_ptr newState = elem.action->undo(elem.state);
28 yrotsih.push_back(HistoryElement(elem.action,newState));
29}
30void ActionHistory::redoLast(){
31 ASSERT(yrotsih.size(),"Redo performed when the redo-queue was empty");
32 HistoryElement elem = yrotsih.back();
33 yrotsih.pop_back();
34 Action::state_ptr oldState = elem.action->redo(elem.state);
35 history.push_back(HistoryElement(elem.action,oldState));
36}
37
38bool ActionHistory::hasUndo(){
39 return history.size()>0;
40}
41
42bool ActionHistory::hasRedo(){
43 return yrotsih.size()>0;
44}
45
46void ActionHistory::addElement(Action* action,Action::state_ptr state){
47 yrotsih.clear();
48 history.push_back(HistoryElement(action,state));
49}
50
51void ActionHistory::clear(){
52 history.clear();
53 yrotsih.clear();
54}
55
56void ActionHistory::init(){
57 ActionHistory *hist = new ActionHistory();
58 setInstance(hist);
59 new UndoAction(hist);
60 new RedoAction(hist);
61}
62
63CONSTRUCT_SINGLETON(ActionHistory)
64
65/****************** Contained actions *******************/
66ActionHistory::UndoAction::UndoAction(ActionHistory *_hist) :
67 Action("Undo"),
68 hist(_hist)
69{}
70
71ActionHistory::UndoAction::~UndoAction(){}
72
73bool ActionHistory::UndoAction::canUndo(){
74 return false;
75}
76
77bool ActionHistory::UndoAction::shouldUndo(){
78 return false;
79}
80
81bool ActionHistory::UndoAction::isActive(){
82 return hist->hasUndo();
83}
84
85Action::state_ptr ActionHistory::UndoAction::performCall(){
86 hist->undoLast();
87 return Action::success;
88}
89
90Action::state_ptr ActionHistory::UndoAction::performUndo(Action::state_ptr){
91 ASSERT(0,"Cannot undo an undo (should use redo for this");
92 return Action::success;
93}
94
95Action::state_ptr ActionHistory::UndoAction::performRedo(Action::state_ptr){
96 ASSERT(0,"Cannot redo an undo");
97 return Action::success;
98}
99
100ActionHistory::RedoAction::RedoAction(ActionHistory *_hist) :
101 Action("Redo"),
102 hist(_hist)
103{}
104
105ActionHistory::RedoAction::~RedoAction(){}
106
107bool ActionHistory::RedoAction::canUndo(){
108 return false;
109}
110
111bool ActionHistory::RedoAction::shouldUndo(){
112 return false;
113}
114
115bool ActionHistory::RedoAction::isActive(){
116 return hist->hasRedo();
117}
118
119Action::state_ptr ActionHistory::RedoAction::performCall(){
120 hist->redoLast();
121 return Action::success;
122}
123
124Action::state_ptr ActionHistory::RedoAction::performUndo(Action::state_ptr){
125 ASSERT(0,"Cannot undo a redo (should use undo for this");
126 return Action::success;
127}
128
129Action::state_ptr ActionHistory::RedoAction::performRedo(Action::state_ptr){
130 ASSERT(0,"Cannot redo a redo");
131 return Action::success;
132}
Note: See TracBrowser for help on using the repository browser.