source: molecuilder/src/Actions/ActionHistory.cpp@ 32842d8

Last change on this file since 32842d8 was 770138, checked in by Tillmann Crueger <crueger@…>, 16 years ago

Added mechanism that deactivates undo and redo menupoint if not applicable

  • Property mode set to 100644
File size: 3.0 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 cout << "Adding action to end of history" << endl;
48 yrotsih.clear();
49 history.push_back(HistoryElement(action,state));
50}
51
52void ActionHistory::clear(){
53 cout << "History cleared" << endl;
54 history.clear();
55 yrotsih.clear();
56}
57
58void ActionHistory::init(){
59 ActionHistory *hist = new ActionHistory();
60 setInstance(hist);
61 new UndoAction(hist);
62 new RedoAction(hist);
63}
64
65CONSTRUCT_SINGLETON(ActionHistory)
66
67/****************** Contained actions *******************/
68ActionHistory::UndoAction::UndoAction(ActionHistory *_hist) :
69 Action("Undo"),
70 hist(_hist)
71{}
72
73ActionHistory::UndoAction::~UndoAction(){}
74
75bool ActionHistory::UndoAction::canUndo(){
76 return false;
77}
78
79bool ActionHistory::UndoAction::shouldUndo(){
80 return false;
81}
82
83bool ActionHistory::UndoAction::isActive(){
84 return hist->hasUndo();
85}
86
87Action::state_ptr ActionHistory::UndoAction::performCall(){
88 hist->undoLast();
89 return Action::success;
90}
91
92Action::state_ptr ActionHistory::UndoAction::performUndo(Action::state_ptr){
93 ASSERT(0,"Cannot undo an undo (should use redo for this");
94 return Action::success;
95}
96
97Action::state_ptr ActionHistory::UndoAction::performRedo(Action::state_ptr){
98 ASSERT(0,"Cannot redo an undo");
99 return Action::success;
100}
101
102ActionHistory::RedoAction::RedoAction(ActionHistory *_hist) :
103 Action("Redo"),
104 hist(_hist)
105{}
106
107ActionHistory::RedoAction::~RedoAction(){}
108
109bool ActionHistory::RedoAction::canUndo(){
110 return false;
111}
112
113bool ActionHistory::RedoAction::shouldUndo(){
114 return false;
115}
116
117bool ActionHistory::RedoAction::isActive(){
118 return hist->hasRedo();
119}
120
121Action::state_ptr ActionHistory::RedoAction::performCall(){
122 hist->redoLast();
123 return Action::success;
124}
125
126Action::state_ptr ActionHistory::RedoAction::performUndo(Action::state_ptr){
127 ASSERT(0,"Cannot undo a redo (should use undo for this");
128 return Action::success;
129}
130
131Action::state_ptr ActionHistory::RedoAction::performRedo(Action::state_ptr){
132 ASSERT(0,"Cannot redo a redo");
133 return Action::success;
134}
Note: See TracBrowser for help on using the repository browser.