Hugintrunk  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CommandHistory.cpp
Go to the documentation of this file.
1 // -*- c-basic-offset: 4 -*-
2 
27 // standard include
28 #include "hugin_config.h"
29 #include "panoinc_WX.h"
30 #include "panoinc.h"
31 #include "CommandHistory.h"
32 
33 #include "hugin/config_defaults.h"
34 
35 namespace PanoCommand
36 {
37 
39  : nextCmd(0)
40  {
41  }
42 
44  {
45  std::vector<PanoCommand*>::iterator it;
46  for (it = commands.begin(); it != commands.end(); ++it)
47  {
48  delete *it;
49  };
50  }
51 
53  {
54  std::vector<PanoCommand*>::iterator it;
55  for (it = commands.begin(); it != commands.end(); ++it)
56  {
57  delete *it;
58  };
59  commands.clear();
60  nextCmd = 0;
61  }
62 
64  {
65  size_t nrDelete = commands.size() - nextCmd;
66  for (size_t i = 0; i < nrDelete; i++)
67  {
68  delete commands.back();
69  commands.pop_back();
70  }
71  }
72 
73  void CommandHistory::addCommand(PanoCommand *command, bool execute)
74  {
75  assert(command);
76  if (nextCmd > commands.size())
77  {
78  DEBUG_FATAL("Invalid state in Command History: nextCmd:" << nextCmd
79  << " size:" << commands.size());
80  }
81  else
82  {
83  if (nextCmd < (commands.size()))
84  {
85  // case: there were redoable commands, remove them now, the
86  // current command has invalidated them.
88  };
89  };
90  commands.push_back(command);
91  nextCmd++;
92 
93  if (execute)
94  {
95  // execute command
96  command->execute();
97  };
98  }
99 
101  {
102  if (nextCmd > 0)
103  {
104  // undo the current command
105  DEBUG_DEBUG("undo: " << commands[nextCmd - 1]->getName());
106  // change nextCmd before the panorama, so panorama changed listeners get
107  // correct results from canUndo() and canRedo().
108  nextCmd--;
109  commands[nextCmd]->undo();
110 
111  // smart undo: keep undoing simple visibility toggles according to user preference
112  bool t = (wxConfigBase::Get()->Read(wxT("smartUndo"), HUGIN_SMART_UNDO) != 0);
113  if (t)
114  {
115  while ((commands[nextCmd]->getName() == "change active images") && (nextCmd > 0))
116  {
117  commands[nextCmd - 1]->undo();
118  nextCmd--;
119  }
120  };
121  // TODO: reestablish visibility based on preferences
122  }
123  else
124  {
125  DEBUG_ERROR("no command in undo history");
126  }
127  }
128 
130  {
131  if (nextCmd < commands.size())
132  {
133  DEBUG_DEBUG("redo: " << commands[nextCmd]->getName());
134  nextCmd++;
135  commands[nextCmd - 1]->execute();
136  // smart redo: keep redoing simple visibility toggles according to user preference
137  bool t = (wxConfigBase::Get()->Read(wxT("smartUndo"), HUGIN_SMART_UNDO) != 0);
138  if (t)
139  {
140  while ((nextCmd < commands.size()) && (commands[nextCmd]->getName() == "change active images"))
141  {
142  commands[nextCmd]->execute();
143  nextCmd++;
144  }
145  }
146  // TODO: reestablish visibility based on preferences
147  }
148  else
149  {
150  DEBUG_ERROR("no command in redo history");
151  }
152  }
153 
155  {
156  return nextCmd > 0;
157  }
158 
160  {
161  return nextCmd < commands.size();
162  }
163 
165  {
166  if (canUndo())
167  {
168  return commands[nextCmd-1]->getName();
169  }
170  return std::string();
171  }
172 
174  {
175  if (canUndo())
176  {
177  return commands[nextCmd - 1];
178  };
179  return nullptr;
180  }
181 
182  // ======================================================================
183  // ======================================================================
184 
185 
187 
189  {
190  }
191 
193  {
194  if (!instance)
195  {
196  instance = new GlobalCmdHist();
197  };
198  return *instance;
199  }
200 }
Base class for all panorama commands.
Definition: Command.h:38
Singleton CommandHistory.
include file for the hugin project
#define DEBUG_FATAL(msg)
Definition: utils.h:78
#define HUGIN_SMART_UNDO
const PanoCommand * getLastCommand() const
return the last PanoCommand
std::string getLastCommandName() const
returns the name of the last command
bool canRedo() const
Return true iff there is a command to redo.
void clearRedoQueue()
clear all commands in the redo queue
static GlobalCmdHist * instance
static GlobalCmdHist & getInstance()
void addCommand(PanoCommand *command, bool execute=true)
Adds a command to the history.
void clear()
Erases all the undo/redo history.
#define DEBUG_ERROR(msg)
Definition: utils.h:76
include file for the hugin project
bool canUndo() const
Return true iff there is a command to undo.
#define DEBUG_DEBUG(msg)
Definition: utils.h:68
virtual void redo()
Redoes the last undone action.
virtual void undo()
Undoes the last action.
std::vector< PanoCommand * > commands