2 //----------------------------------------------------------------------------------------------------------------
3 // Class definition include
4 //----------------------------------------------------------------------------------------------------------------
5 #include "CommandsHandler.h"
7 //----------------------------------------------------------------------------------------------------------------
8 // Class implementation
9 //----------------------------------------------------------------------------------------------------------------
10 /** @file CommandsHandler.cxx */
12 //------------------------------------------------------------------------------------------------------------
13 // Constructors & Destructors
14 //------------------------------------------------------------------------------------------------------------
17 * Constructs the CommandsHandler
19 CommandsHandler :: CommandsHandler()
21 redo_actions = new CommandsRegisterStructure();
22 unDo_actions = new CommandsRegisterStructure();
27 isLastREDO_executed = true;
28 isLastUNDO_executed = false;
32 * Destructs the CommandsHandler
34 CommandsHandler :: ~CommandsHandler()
41 //------------------------------------------------------------------------------------------------------------
43 //------------------------------------------------------------------------------------------------------------
46 * Registers in the vectors of doneActions and unDoActions the given commands that all ready corresponds each other to the inverse of the otherone.
47 * If is the first registered action notifies the posibleUNDO avaliability.
48 * @param doneAction Is the action to register in the redo_actions vector.
49 * @param unDoAction Is the action to register in the unDo_actions vector.
51 void CommandsHandler :: registerCommand(CommandObject* doneAction, CommandObject* unDoAction)
53 int actToUNDO = unDo_actions->getActualIndex();
54 redo_actions->setActualIndex( actToUNDO );
56 redo_actions->registerCommand(doneAction);
57 unDo_actions->registerCommand(unDoAction);
62 isLastREDO_executed = true;
63 isLastUNDO_executed = false;
68 * Undo a command. Managing the correspondig vectors and the execution of the inverse action to the - ACTUAL DONE- action
69 * @return Returns true if the inverse command ( the actual to execute in UNDO actions) is executed. If it is false the state of the vector must not change.
71 bool CommandsHandler :: undo()
73 bool executed = posibleUNDO;
76 validateOperationsAvaliability();//para borrar!!!!!!!!-----------------------------*********************---------------------
77 executed = theWorksSpaceBoss->executeCommand(getActual_UNDO(), true);
78 isLastUNDO_executed = true;
79 if (!unDo_actions->hasActualNext() && !redo_actions->hasActualNext())
81 if(unDo_actions->hasActualPrevious())
82 executed = unDo_actions->moveBack_Actual() ;
85 if( unDo_actions->hasLastNext() )
86 executed = executed && unDo_actions->moveBack_Last();
87 isLastREDO_executed = false;
89 else if (!unDo_actions->hasActualPrevious() && redo_actions->hasActualPrevious())
91 executed = redo_actions->moveBack_Actual();
92 executed = executed && redo_actions->moveBack_Last();
93 executed = executed && unDo_actions->moveBack_Last();
97 executed = unDo_actions->moveBack_Last();
98 executed = executed && unDo_actions->moveBack_Actual();
99 executed = executed && redo_actions->moveBack_Actual();
100 if( redo_actions->hasLastNext() )
101 executed = executed && redo_actions->moveBack_Last();
103 validateOperationsAvaliability();
109 * Redo a command. Managing the correspondig vectors and the execution of the inverse action to the - ACTUAL DONE- action
110 * @return Returns true if the actual command to execute ( the actual to execute in REDO actions )has been executed. If it is false the state of the vector must not change.
112 bool CommandsHandler :: redo()
114 bool executed = posibleREDO;
117 validateOperationsAvaliability();//para borrar!!!!!!!!-----------------------------*********************---------------------
118 isLastREDO_executed = true;
119 //isLastUNDO_executed = false; // Posible needed
120 executed = theWorksSpaceBoss->executeCommand(getActual_REDO(), true);
121 if (!unDo_actions->hasActualPrevious() && !redo_actions->hasActualPrevious())
123 executed = redo_actions->moveForward_Actual();
124 executed = executed && redo_actions->moveBack_Last();
125 isLastUNDO_executed = false;
127 else if (!unDo_actions->hasActualPrevious() && redo_actions->hasActualPrevious())
129 executed = redo_actions->moveForward_Actual();
130 if(unDo_actions->hasLastNext())
131 executed = executed && unDo_actions->moveForward_Last();
132 executed = executed && unDo_actions->moveForward_Actual();
133 if(redo_actions->hasLastNext())
134 executed = executed && redo_actions->moveForward_Last();
138 executed = unDo_actions->moveForward_Actual();
139 executed = executed && unDo_actions->moveForward_Last();
140 executed = executed && redo_actions->moveForward_Actual();
141 if( redo_actions->hasLastNext() )
142 executed = executed && redo_actions->moveForward_Last();
144 executed = executed && redo_actions->moveBack_Last();
146 if (!unDo_actions->hasActualPrevious() && redo_actions->hasActualPrevious())
148 isLastUNDO_executed = false;
150 if (!unDo_actions->hasActualNext() && !redo_actions->hasActualNext())
152 isLastUNDO_executed = false;
154 validateOperationsAvaliability();
160 * Notitify if posibleREDO is posible or not.
161 * @return Returns the state of posibleUNDO
163 bool CommandsHandler :: isPosibleUNDO()
169 * Indicates if posibleUNDO is posible or not.
170 * @return Returns the state of posibleREDO
172 bool CommandsHandler :: isPosibleREDO()
178 * Sets posibleREDO state.
179 * @param UNDOstate The state of posibleUNDO to set
181 void CommandsHandler :: setPosibleUNDO( bool UNDOstate )
183 posibleUNDO = UNDOstate;
187 * Sets posibleUNDO state.
188 * @param REDOstate The state of posibleREDO to set
190 void CommandsHandler :: setPosibleREDO( bool REDOstate )
192 posibleREDO = REDOstate;
196 * Clear the registered actions in the DO and UNDO vectors.
198 void CommandsHandler :: clearActions()
200 redo_actions->clearActions();
201 unDo_actions->clearActions();
205 * Returns hoy mane paired commands had been registered, if the done and unDo vectors dont match returns -1 as error code.
206 * @return Returns how many paired-commands had been registered
208 int CommandsHandler :: getTotalCommands()
210 int value = redo_actions->getTotalCommandsCount();
211 return value == (unDo_actions->getTotalCommandsCount()) ? value : -1;
216 * Gets the actual command in the UNDO-list
217 * @return Returns a pointer to the actual undo action
219 CommandObject * CommandsHandler :: getActual_UNDO()
221 return unDo_actions->getActual_Pointer();
225 * Gets the actual command in the REDO-list
226 * @return Returns a pointer to the actual do action
228 CommandObject * CommandsHandler :: getActual_REDO()
230 return redo_actions->getActual_Pointer();
234 * Gets the command at the given position in the DO (REDO) vector
235 * @return The pointer to the referenced object by the position
237 CommandObject * CommandsHandler :: get_DO_CommandAt(int position)
239 return redo_actions->getCommandAt(position);
243 * Gets the command at the given position in the UNDO vector
244 * @return The pointer to the referenced object by the position
246 CommandObject * CommandsHandler :: get_UNDO_CommandAt(int position)
248 return unDo_actions->getCommandAt(position);
252 * Sets the model parent of the action handler
253 * @param theModelParent The boss reference
255 void CommandsHandler :: setModelBoss(ICommandsUser * theModelParent)
257 theWorksSpaceBoss = theModelParent;
261 * Validates if it is posible of not to do UNDO and/or REDO and sets the corresponding values
263 void CommandsHandler :: validateOperationsAvaliability()
265 posibleREDO = (redo_actions->hasActualNext() && unDo_actions->hasActualNext() )? true :
266 ( (!redo_actions->hasActualNext()&& unDo_actions->hasActualNext() ) ? true :
267 ( !unDo_actions->hasActualNext() && !redo_actions->hasActualNext() )? false : true && !isLastREDO_executed );
269 posibleUNDO = (redo_actions->hasActualPrevious() && unDo_actions->hasActualPrevious() )? true :
270 ( (!unDo_actions->hasActualPrevious()&& redo_actions->hasActualPrevious() ) ? true :
271 ( !unDo_actions->hasActualPrevious() && !redo_actions->hasActualPrevious() )? false : true && !isLastUNDO_executed );