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