]> Creatis software - creaContours.git/blob - lib/kernel_ManagerContour_NDimensions/CommandsHandler.cxx
Feature #1772 Add licence terms for all files.
[creaContours.git] / lib / kernel_ManagerContour_NDimensions / CommandsHandler.cxx
1 /*# ---------------------------------------------------------------------
2 #
3 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
4 #                        pour la Sant�)
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
8 #
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.
15 #
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
20 #  liability.
21 #
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 # ------------------------------------------------------------------------ */
25
26
27 //----------------------------------------------------------------------------------------------------------------
28 // Class definition include
29 //----------------------------------------------------------------------------------------------------------------
30 #include "CommandsHandler.h"
31
32 //----------------------------------------------------------------------------------------------------------------
33 // Class implementation
34 //----------------------------------------------------------------------------------------------------------------
35 /** @file CommandsHandler.cxx */
36
37 //------------------------------------------------------------------------------------------------------------
38 // Constructors & Destructors
39 //------------------------------------------------------------------------------------------------------------
40         
41         /*
42         * Constructs the CommandsHandler
43         */
44         CommandsHandler :: CommandsHandler()
45         {
46                 redo_actions = new CommandsRegisterStructure();
47                 unDo_actions = new CommandsRegisterStructure();
48
49                 posibleREDO = false;
50                 posibleUNDO = false;
51
52                 isLastREDO_executed = true;
53                 isLastUNDO_executed = false;
54         }
55
56         /*
57         * Destructs the CommandsHandler
58         */
59         CommandsHandler :: ~CommandsHandler()
60         {
61                 clearActions();
62                 delete redo_actions;
63                 delete unDo_actions;
64         }
65
66 //------------------------------------------------------------------------------------------------------------
67 // Methods
68 //------------------------------------------------------------------------------------------------------------
69
70         /*
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.
75         */
76         void CommandsHandler :: registerCommand(CommandObject* doneAction, CommandObject* unDoAction)
77         {
78                 int actToUNDO = unDo_actions->getActualIndex(); 
79                 redo_actions->setActualIndex( actToUNDO );                              
80                 
81                 redo_actions->registerCommand(doneAction);
82                 unDo_actions->registerCommand(unDoAction);
83                 
84                 posibleREDO = false;
85                 posibleUNDO = true;
86
87                 isLastREDO_executed = true;
88                 isLastUNDO_executed = false;
89                 
90         }
91
92         /*
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.
95         */
96         bool CommandsHandler :: undo()
97         {
98                 bool executed = posibleUNDO;
99                 if( posibleUNDO )
100                 {                       
101                         validateOperationsAvaliability();//para borrar!!!!!!!!-----------------------------*********************---------------------
102                         executed = theWorksSpaceBoss->executeCommand(getActual_UNDO(), true);
103                         isLastUNDO_executed = true;
104                         if (!unDo_actions->hasActualNext() && !redo_actions->hasActualNext())
105                         {                               
106                                 if(unDo_actions->hasActualPrevious())
107                                         executed = unDo_actions->moveBack_Actual() ;
108                                 else
109                                         executed = true;
110                                 if( unDo_actions->hasLastNext() ) 
111                                         executed = executed && unDo_actions->moveBack_Last();   
112                                 isLastREDO_executed = false;
113                         }
114                         else if (!unDo_actions->hasActualPrevious() && redo_actions->hasActualPrevious())
115                         {
116                                 executed = redo_actions->moveBack_Actual();     
117                                 executed = executed && redo_actions->moveBack_Last();   
118                                 executed = executed && unDo_actions->moveBack_Last();                           
119                         }
120                         else 
121                         {                                       
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();   
127                         }
128                         validateOperationsAvaliability();                       
129                 }
130                 return executed;
131         }
132
133         /*
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.
136         */
137         bool CommandsHandler :: redo()
138         {
139                 bool executed = posibleREDO;
140                 if( posibleREDO )
141                 {                       
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())
147                         {                                                               
148                                 executed = redo_actions->moveForward_Actual();
149                                 executed = executed && redo_actions->moveBack_Last();
150                                 isLastUNDO_executed = false;
151                         }
152                         else if (!unDo_actions->hasActualPrevious() && redo_actions->hasActualPrevious())
153                         {
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();
160                         }
161                         else 
162                         {       
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();        
168                                 else
169                                         executed = executed && redo_actions->moveBack_Last();   
170                         }
171                         if (!unDo_actions->hasActualPrevious() && redo_actions->hasActualPrevious())
172                         {
173                                 isLastUNDO_executed = false;
174                         }
175                         if (!unDo_actions->hasActualNext() && !redo_actions->hasActualNext())
176                         {
177                                 isLastUNDO_executed = false;
178                         }
179                         validateOperationsAvaliability();
180                 }               
181                 return executed;
182         }
183
184         /*
185         * Notitify if posibleREDO is posible or not.
186         * @return Returns the state of posibleUNDO
187         */
188         bool CommandsHandler :: isPosibleUNDO()
189         {
190                 return posibleUNDO;
191         }
192
193         /*
194         * Indicates if posibleUNDO is posible or not.
195         * @return Returns the state of posibleREDO
196         */
197         bool CommandsHandler :: isPosibleREDO()
198         {
199                 return posibleREDO;
200         }
201
202         /*
203         * Sets  posibleREDO state.
204         * @param UNDOstate The state of posibleUNDO to set
205         */
206         void CommandsHandler :: setPosibleUNDO( bool UNDOstate )
207         {
208                 posibleUNDO = UNDOstate;
209         }
210
211         /*
212         * Sets posibleUNDO state.
213         * @param REDOstate The state of posibleREDO to set
214         */
215         void CommandsHandler :: setPosibleREDO( bool REDOstate )
216         {
217                 posibleREDO = REDOstate;
218         }
219
220         /*
221         * Clear the registered actions in the DO and UNDO vectors.
222         */
223         void CommandsHandler :: clearActions()
224         {
225                 redo_actions->clearActions();
226                 unDo_actions->clearActions();
227         }
228
229         /*
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
232         */
233         int CommandsHandler :: getTotalCommands()
234         {
235                 int value = redo_actions->getTotalCommandsCount();
236                 return value == (unDo_actions->getTotalCommandsCount())  ? value : -1;
237         }
238
239
240         /*
241         * Gets the actual command in the UNDO-list
242         * @return  Returns a pointer to the actual undo action
243         */
244         CommandObject * CommandsHandler :: getActual_UNDO()
245         {
246                 return unDo_actions->getActual_Pointer();
247         }
248
249         /*
250         * Gets the actual command in the REDO-list
251         * @return  Returns a pointer to the actual do action
252         */
253         CommandObject * CommandsHandler :: getActual_REDO()
254         {
255                 return redo_actions->getActual_Pointer();
256         }
257
258         /*
259         * Gets the command at the given position in the DO (REDO) vector
260         * @return The pointer to the referenced object by the position
261         */
262         CommandObject * CommandsHandler :: get_DO_CommandAt(int position)
263         {
264                 return redo_actions->getCommandAt(position);
265         }
266
267         /*
268         * Gets the command at the given position in the UNDO vector
269         * @return The pointer to the referenced object by the position
270         */
271         CommandObject * CommandsHandler :: get_UNDO_CommandAt(int position)
272         {
273                 return unDo_actions->getCommandAt(position);
274         }
275
276                 /*
277         * Sets the model parent of the action handler
278         * @param theModelParent The boss reference
279         */
280         void CommandsHandler :: setModelBoss(ICommandsUser * theModelParent)
281         {
282                 theWorksSpaceBoss = theModelParent;
283         }
284
285         /*
286         * Validates if it is posible of not to do UNDO and/or REDO and sets the corresponding values
287         */
288         void CommandsHandler :: validateOperationsAvaliability()
289         {
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 );   
293
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 );   
297
298         }