| 1 | /*
 | 
|---|
| 2 |  * ActionHistory.hpp
 | 
|---|
| 3 |  *
 | 
|---|
| 4 |  *  Created on: Mar 25, 2010
 | 
|---|
| 5 |  *      Author: crueger
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | #ifndef ACTIONHISTORY_HPP_
 | 
|---|
| 9 | #define ACTIONHISTORY_HPP_
 | 
|---|
| 10 | 
 | 
|---|
| 11 | #include "Patterns/Singleton.hpp"
 | 
|---|
| 12 | 
 | 
|---|
| 13 | #include <deque>
 | 
|---|
| 14 | 
 | 
|---|
| 15 | #include "Actions/Action.hpp"
 | 
|---|
| 16 | 
 | 
|---|
| 17 | 
 | 
|---|
| 18 | class ActionHistory : public Singleton<ActionHistory,false>
 | 
|---|
| 19 | {
 | 
|---|
| 20 |   friend class Singleton<ActionHistory,false>;
 | 
|---|
| 21 | 
 | 
|---|
| 22 |   struct HistoryElement {
 | 
|---|
| 23 |     HistoryElement(Action *_action, Action::state_ptr _state) :
 | 
|---|
| 24 |       action(_action),
 | 
|---|
| 25 |       state(_state)
 | 
|---|
| 26 |     {}
 | 
|---|
| 27 |     Action *action;
 | 
|---|
| 28 |     Action::state_ptr state;
 | 
|---|
| 29 |   };
 | 
|---|
| 30 | 
 | 
|---|
| 31 |   class UndoAction : public Action {
 | 
|---|
| 32 |   public:
 | 
|---|
| 33 |     UndoAction(ActionHistory*);
 | 
|---|
| 34 |     virtual ~UndoAction();
 | 
|---|
| 35 | 
 | 
|---|
| 36 |     virtual bool canUndo();
 | 
|---|
| 37 |     virtual bool shouldUndo();
 | 
|---|
| 38 | 
 | 
|---|
| 39 |     virtual bool isActive();
 | 
|---|
| 40 | 
 | 
|---|
| 41 |   private:
 | 
|---|
| 42 |     virtual Action::state_ptr performCall();
 | 
|---|
| 43 |     virtual Action::state_ptr performUndo(Action::state_ptr);
 | 
|---|
| 44 |     virtual Action::state_ptr performRedo(Action::state_ptr);
 | 
|---|
| 45 | 
 | 
|---|
| 46 |     ActionHistory *hist;
 | 
|---|
| 47 |   };
 | 
|---|
| 48 | 
 | 
|---|
| 49 |   class RedoAction : public Action {
 | 
|---|
| 50 |   public:
 | 
|---|
| 51 |     RedoAction(ActionHistory*);
 | 
|---|
| 52 |     virtual ~RedoAction();
 | 
|---|
| 53 | 
 | 
|---|
| 54 |     virtual bool canUndo();
 | 
|---|
| 55 |     virtual bool shouldUndo();
 | 
|---|
| 56 | 
 | 
|---|
| 57 |     virtual bool isActive();
 | 
|---|
| 58 | 
 | 
|---|
| 59 |   private:
 | 
|---|
| 60 |     virtual Action::state_ptr performCall();
 | 
|---|
| 61 |     virtual Action::state_ptr performUndo(Action::state_ptr);
 | 
|---|
| 62 |     virtual Action::state_ptr performRedo(Action::state_ptr);
 | 
|---|
| 63 | 
 | 
|---|
| 64 |     ActionHistory *hist;
 | 
|---|
| 65 |   };
 | 
|---|
| 66 | 
 | 
|---|
| 67 | public:
 | 
|---|
| 68 | 
 | 
|---|
| 69 |   void undoLast();
 | 
|---|
| 70 |   void redoLast();
 | 
|---|
| 71 | 
 | 
|---|
| 72 |   bool hasUndo();
 | 
|---|
| 73 |   bool hasRedo();
 | 
|---|
| 74 | 
 | 
|---|
| 75 |   void addElement(Action*,Action::state_ptr);
 | 
|---|
| 76 |   void clear();
 | 
|---|
| 77 | protected:
 | 
|---|
| 78 |   ActionHistory();
 | 
|---|
| 79 |   virtual ~ActionHistory();
 | 
|---|
| 80 | private:
 | 
|---|
| 81 |   std::deque<HistoryElement> history;
 | 
|---|
| 82 |   std::deque<HistoryElement> yrotsih;
 | 
|---|
| 83 | 
 | 
|---|
| 84 | public:
 | 
|---|
| 85 |   // when constructing the actions we need the Actionregistry Singleton
 | 
|---|
| 86 |   // Singletons need static variables to work, but we cannot access statics
 | 
|---|
| 87 |   // inside a static initialization, so we have this init function that
 | 
|---|
| 88 |   // needs to be called at a non-static point at the start of the program
 | 
|---|
| 89 |   static void init();
 | 
|---|
| 90 | };
 | 
|---|
| 91 | 
 | 
|---|
| 92 | #endif /* ACTIONHISTORY_HPP_ */
 | 
|---|