33 using namespace YACS::HMI;
35 int Invocator::_ctr = 0;
40 _normalReverse =
true;
51 bool Command::execute()
53 if ( !GuiContext::getCurrent() || !GuiContext::getCurrent()->getInvoc() )
56 bool commandInProgress = !GuiContext::getCurrent()->getInvoc()->isSpecialReverse();
57 if (commandInProgress)
58 GuiContext::getCurrent()->getInvoc()->_commandsInProgress.push_back(
this);
61 if (GuiContext::getCurrent()->getInvoc()->_isRedo && _normalReverse)
63 for (
int i=0;
i<_subCommands.size();
i++)
64 delete _subCommands[
i];
68 if (ret) GuiContext::getCurrent()->setNotSaved(
true);
70 if (commandInProgress)
71 GuiContext::getCurrent()->getInvoc()->_commandsInProgress.pop_back();
79 bool Command::reverse(
bool isNormal)
82 if (! _subCommands.empty())
84 for (
int i=0;
i<_subCommands.size();
i++)
86 ret =_subCommands[
i]->reverse(isNormal);
105 bool Command::executeSubOnly()
108 if (! _subCommands.empty())
110 for (
int i=0;
i<_subCommands.size();
i++)
112 ret =_subCommands[
i]->reverse(
false);
119 std::string Command::dump()
121 return "=== generic command dump ===";
124 std::string Command::recursiveDump(
int level)
127 for (
int i=0;
i<level;
i++) prefix +=
" ";
128 string ret = prefix + dump() +
"\n";
129 for (
int i=0;
i<_subCommands.size();
i++)
130 ret += _subCommands[
i]->recursiveDump(level+1);
136 _subCommands.push_back(command);
143 Invocator::Invocator()
145 _commandsDone.clear();
146 _commandsUndone.clear();
147 _commandsInProgress.clear();
150 _specialReverse =
false;
161 if (GuiContext::getCurrent()->getInvoc()->isSpecialReverse())
166 if (_commandsInProgress.empty())
168 _commandsDone.push_back(command);
169 _commandsUndone.clear();
174 _commandsInProgress.back()->addSubCommand(command);
178 bool Invocator::undo()
185 ret1 <<
"_commandsDone" << endl;
186 for (
int i=0;
i<_commandsDone.size();
i++)
187 ret1 <<
i << _commandsDone[
i]->recursiveDump(2);
190 ret2 <<
"_commandsUndone" << endl;
191 for (
int i=0;
i<_commandsUndone.size();
i++)
192 ret2 <<
i << _commandsUndone[
i]->recursiveDump(2);
196 if (! _commandsDone.empty())
198 bool isNormal = _commandsDone.back()->isNormalReverse();
199 _specialReverse = !isNormal;
202 ret = _commandsDone.back()->reverse(isNormal);
204 ret = _commandsDone.back()->executeSubOnly();
206 _commandsUndone.push_back(_commandsDone.back());
207 _commandsDone.pop_back();
208 _specialReverse =
false;
213 ret1 <<
"_commandsDone" << endl;
214 for (
int i=0;
i<_commandsDone.size();
i++)
215 ret1 <<
i << _commandsDone[
i]->recursiveDump(2);
218 ret2 <<
"_commandsUndone" << endl;
219 for (
int i=0;
i<_commandsUndone.size();
i++)
220 ret2 <<
i << _commandsUndone[
i]->recursiveDump(2);
227 bool Invocator::redo()
234 ret1 <<
"_commandsDone" << endl;
235 for (
int i=0;
i<_commandsDone.size();
i++)
236 ret1 <<
i << _commandsDone[
i]->recursiveDump(2);
239 ret2 <<
"_commandsUndone" << endl;
240 for (
int i=0;
i<_commandsUndone.size();
i++)
241 ret2 <<
i << _commandsUndone[
i]->recursiveDump(2);
245 if (! _commandsUndone.empty())
248 ret = _commandsUndone.back()->execute();
250 _commandsDone.push_back(_commandsUndone.back());
251 _commandsUndone.pop_back();
256 ret1 <<
"_commandsDone" << endl;
257 for (
int i=0;
i<_commandsDone.size();
i++)
258 ret1 <<
i << _commandsDone[
i]->recursiveDump(2);
261 ret2 <<
"_commandsUndone" << endl;
262 for (
int i=0;
i<_commandsUndone.size();
i++)
263 ret2 <<
i << _commandsUndone[
i]->recursiveDump(2);
270 std::list<std::string> Invocator::getDone()
272 list<string> listDone;
274 for (
int i=0;
i<_commandsDone.size();
i++)
275 listDone.push_back(_commandsDone[
i]->recursiveDump());
279 std::list<std::string> Invocator::getUndone()
281 list<string> listUndone;
283 for (
int i=0;
i<_commandsUndone.size();
i++)
284 listUndone.push_back(_commandsUndone[
i]->recursiveDump());