1 /*# ---------------------------------------------------------------------
3 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
6 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
7 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9 # This software is governed by the CeCILL-B license under French law and
10 # abiding by the rules of distribution of free software. You can use,
11 # modify and/ or redistribute the software under the terms of the CeCILL-B
12 # license as circulated by CEA, CNRS and INRIA at the following URL
13 # http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
14 # or in the file LICENSE.txt.
16 # As a counterpart to the access to the source code and rights to copy,
17 # modify and redistribute granted by the license, users are provided only
18 # with a limited warranty and the software's author, the holder of the
19 # economic rights, and the successive licensors have only limited
22 # The fact that you are presently reading this means that you have had
23 # knowledge of the CeCILL-B license and that you accept its terms.
24 # ------------------------------------------------------------------------ */
27 //----------------------------------------------------------------------------------------------------------------
28 // Class definition include
29 //----------------------------------------------------------------------------------------------------------------
30 #include "CommandsHandler.h"
32 //----------------------------------------------------------------------------------------------------------------
33 // Class implementation
34 //----------------------------------------------------------------------------------------------------------------
35 /** @file CommandsHandler.cxx */
37 //------------------------------------------------------------------------------------------------------------
38 // Constructors & Destructors
39 //------------------------------------------------------------------------------------------------------------
42 * Constructs the CommandsHandler
44 CommandsHandler :: CommandsHandler()
46 redo_actions = new CommandsRegisterStructure();
47 unDo_actions = new CommandsRegisterStructure();
52 isLastREDO_executed = true;
53 isLastUNDO_executed = false;
57 * Destructs the CommandsHandler
59 CommandsHandler :: ~CommandsHandler()
66 //------------------------------------------------------------------------------------------------------------
68 //------------------------------------------------------------------------------------------------------------
71 * Registers in the vectors of doneActions and unDoActions the given commands that all ready corresponds each other to the inverse of the otherone.
72 * If is the first registered action notifies the posibleUNDO avaliability.
73 * @param doneAction Is the action to register in the redo_actions vector.
74 * @param unDoAction Is the action to register in the unDo_actions vector.
76 void CommandsHandler :: registerCommand(CommandObject* doneAction, CommandObject* unDoAction)
78 int actToUNDO = unDo_actions->getActualIndex();
79 redo_actions->setActualIndex( actToUNDO );
81 redo_actions->registerCommand(doneAction);
82 unDo_actions->registerCommand(unDoAction);
87 isLastREDO_executed = true;
88 isLastUNDO_executed = false;
93 * Undo a command. Managing the correspondig vectors and the execution of the inverse action to the - ACTUAL DONE- action
94 * @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.
96 bool CommandsHandler :: undo()
98 bool executed = posibleUNDO;
101 validateOperationsAvaliability();//para borrar!!!!!!!!-----------------------------*********************---------------------
102 executed = theWorksSpaceBoss->executeCommand(getActual_UNDO(), true);
103 isLastUNDO_executed = true;
104 if (!unDo_actions->hasActualNext() && !redo_actions->hasActualNext())
106 if(unDo_actions->hasActualPrevious())
107 executed = unDo_actions->moveBack_Actual() ;
110 if( unDo_actions->hasLastNext() )
111 executed = executed && unDo_actions->moveBack_Last();
112 isLastREDO_executed = false;
114 else if (!unDo_actions->hasActualPrevious() && redo_actions->hasActualPrevious())
116 executed = redo_actions->moveBack_Actual();
117 executed = executed && redo_actions->moveBack_Last();
118 executed = executed && unDo_actions->moveBack_Last();
122 executed = unDo_actions->moveBack_Last();
123 executed = executed && unDo_actions->moveBack_Actual();
124 executed = executed && redo_actions->moveBack_Actual();
125 if( redo_actions->hasLastNext() )
126 executed = executed && redo_actions->moveBack_Last();
128 validateOperationsAvaliability();
134 * Redo a command. Managing the correspondig vectors and the execution of the inverse action to the - ACTUAL DONE- action
135 * @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.
137 bool CommandsHandler :: redo()
139 bool executed = posibleREDO;
142 validateOperationsAvaliability();//para borrar!!!!!!!!-----------------------------*********************---------------------
143 isLastREDO_executed = true;
144 //isLastUNDO_executed = false; // Posible needed
145 executed = theWorksSpaceBoss->executeCommand(getActual_REDO(), true);
146 if (!unDo_actions->hasActualPrevious() && !redo_actions->hasActualPrevious())
148 executed = redo_actions->moveForward_Actual();
149 executed = executed && redo_actions->moveBack_Last();
150 isLastUNDO_executed = false;
152 else if (!unDo_actions->hasActualPrevious() && redo_actions->hasActualPrevious())
154 executed = redo_actions->moveForward_Actual();
155 if(unDo_actions->hasLastNext())
156 executed = executed && unDo_actions->moveForward_Last();
157 executed = executed && unDo_actions->moveForward_Actual();
158 if(redo_actions->hasLastNext())
159 executed = executed && redo_actions->moveForward_Last();
163 executed = unDo_actions->moveForward_Actual();
164 executed = executed && unDo_actions->moveForward_Last();
165 executed = executed && redo_actions->moveForward_Actual();
166 if( redo_actions->hasLastNext() )
167 executed = executed && redo_actions->moveForward_Last();
169 executed = executed && redo_actions->moveBack_Last();
171 if (!unDo_actions->hasActualPrevious() && redo_actions->hasActualPrevious())
173 isLastUNDO_executed = false;
175 if (!unDo_actions->hasActualNext() && !redo_actions->hasActualNext())
177 isLastUNDO_executed = false;
179 validateOperationsAvaliability();
185 * Notitify if posibleREDO is posible or not.
186 * @return Returns the state of posibleUNDO
188 bool CommandsHandler :: isPosibleUNDO()
194 * Indicates if posibleUNDO is posible or not.
195 * @return Returns the state of posibleREDO
197 bool CommandsHandler :: isPosibleREDO()
203 * Sets posibleREDO state.
204 * @param UNDOstate The state of posibleUNDO to set
206 void CommandsHandler :: setPosibleUNDO( bool UNDOstate )
208 posibleUNDO = UNDOstate;
212 * Sets posibleUNDO state.
213 * @param REDOstate The state of posibleREDO to set
215 void CommandsHandler :: setPosibleREDO( bool REDOstate )
217 posibleREDO = REDOstate;
221 * Clear the registered actions in the DO and UNDO vectors.
223 void CommandsHandler :: clearActions()
225 redo_actions->clearActions();
226 unDo_actions->clearActions();
230 * Returns hoy mane paired commands had been registered, if the done and unDo vectors dont match returns -1 as error code.
231 * @return Returns how many paired-commands had been registered
233 int CommandsHandler :: getTotalCommands()
235 int value = redo_actions->getTotalCommandsCount();
236 return value == (unDo_actions->getTotalCommandsCount()) ? value : -1;
241 * Gets the actual command in the UNDO-list
242 * @return Returns a pointer to the actual undo action
244 CommandObject * CommandsHandler :: getActual_UNDO()
246 return unDo_actions->getActual_Pointer();
250 * Gets the actual command in the REDO-list
251 * @return Returns a pointer to the actual do action
253 CommandObject * CommandsHandler :: getActual_REDO()
255 return redo_actions->getActual_Pointer();
259 * Gets the command at the given position in the DO (REDO) vector
260 * @return The pointer to the referenced object by the position
262 CommandObject * CommandsHandler :: get_DO_CommandAt(int position)
264 return redo_actions->getCommandAt(position);
268 * Gets the command at the given position in the UNDO vector
269 * @return The pointer to the referenced object by the position
271 CommandObject * CommandsHandler :: get_UNDO_CommandAt(int position)
273 return unDo_actions->getCommandAt(position);
277 * Sets the model parent of the action handler
278 * @param theModelParent The boss reference
280 void CommandsHandler :: setModelBoss(ICommandsUser * theModelParent)
282 theWorksSpaceBoss = theModelParent;
286 * Validates if it is posible of not to do UNDO and/or REDO and sets the corresponding values
288 void CommandsHandler :: validateOperationsAvaliability()
290 posibleREDO = (redo_actions->hasActualNext() && unDo_actions->hasActualNext() )? true :
291 ( (!redo_actions->hasActualNext()&& unDo_actions->hasActualNext() ) ? true :
292 ( !unDo_actions->hasActualNext() && !redo_actions->hasActualNext() )? false : true && !isLastREDO_executed );
294 posibleUNDO = (redo_actions->hasActualPrevious() && unDo_actions->hasActualPrevious() )? true :
295 ( (!unDo_actions->hasActualPrevious()&& redo_actions->hasActualPrevious() ) ? true :
296 ( !unDo_actions->hasActualPrevious() && !redo_actions->hasActualPrevious() )? false : true && !isLastUNDO_executed );