]> Creatis software - creaContours.git/blob - lib/kernel_ManagerContour_NDimensions/CommandsHandler.cxx
a3a3dafdb9aecdcb18e781f22ee3a095104468ea
[creaContours.git] / lib / kernel_ManagerContour_NDimensions / CommandsHandler.cxx
1
2 //----------------------------------------------------------------------------------------------------------------
3 // Class definition include
4 //----------------------------------------------------------------------------------------------------------------
5 #include "CommandsHandler.h"
6
7 //----------------------------------------------------------------------------------------------------------------
8 // Class implementation
9 //----------------------------------------------------------------------------------------------------------------
10 /** @file CommandsHandler.cxx */
11
12 //------------------------------------------------------------------------------------------------------------
13 // Constructors & Destructors
14 //------------------------------------------------------------------------------------------------------------
15         
16         /*
17         * Constructs the CommandsHandler
18         */
19         CommandsHandler :: CommandsHandler()
20         {
21                 redo_actions = new CommandsRegisterStructure();
22                 unDo_actions = new CommandsRegisterStructure();
23
24                 posibleREDO = false;
25                 posibleUNDO = false;
26
27                 isLastREDO_executed = true;
28                 isLastUNDO_executed = false;
29         }
30
31         /*
32         * Destructs the CommandsHandler
33         */
34         CommandsHandler :: ~CommandsHandler()
35         {
36                 clearActions();
37                 delete redo_actions;
38                 delete unDo_actions;
39         }
40
41 //------------------------------------------------------------------------------------------------------------
42 // Methods
43 //------------------------------------------------------------------------------------------------------------
44
45         /*
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.
50         */
51         void CommandsHandler :: registerCommand(CommandObject* doneAction, CommandObject* unDoAction)
52         {
53                 int actToUNDO = unDo_actions->getActualIndex(); 
54                 redo_actions->setActualIndex( actToUNDO );                              
55                 
56                 redo_actions->registerCommand(doneAction);
57                 unDo_actions->registerCommand(unDoAction);
58                 
59                 posibleREDO = false;
60                 posibleUNDO = true;
61
62                 isLastREDO_executed = true;
63                 isLastUNDO_executed = false;
64                 
65         }
66
67         /*
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.
70         */
71         bool CommandsHandler :: undo()
72         {
73                 bool executed = posibleUNDO;
74                 if( posibleUNDO )
75                 {                       
76                         validateOperationsAvaliability();//para borrar!!!!!!!!-----------------------------*********************---------------------
77                         executed = theWorksSpaceBoss->executeCommand(getActual_UNDO(), true);
78                         isLastUNDO_executed = true;
79                         if (!unDo_actions->hasActualNext() && !redo_actions->hasActualNext())
80                         {                               
81                                 if(unDo_actions->hasActualPrevious())
82                                         executed = unDo_actions->moveBack_Actual() ;
83                                 else
84                                         executed = true;
85                                 if( unDo_actions->hasLastNext() ) 
86                                         executed = executed && unDo_actions->moveBack_Last();   
87                                 isLastREDO_executed = false;
88                         }
89                         else if (!unDo_actions->hasActualPrevious() && redo_actions->hasActualPrevious())
90                         {
91                                 executed = redo_actions->moveBack_Actual();     
92                                 executed = executed && redo_actions->moveBack_Last();   
93                                 executed = executed && unDo_actions->moveBack_Last();                           
94                         }
95                         else 
96                         {                                       
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();   
102                         }
103                         validateOperationsAvaliability();                       
104                 }
105                 return executed;
106         }
107
108         /*
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.
111         */
112         bool CommandsHandler :: redo()
113         {
114                 bool executed = posibleREDO;
115                 if( posibleREDO )
116                 {                       
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())
122                         {                                                               
123                                 executed = redo_actions->moveForward_Actual();
124                                 executed = executed && redo_actions->moveBack_Last();
125                                 isLastUNDO_executed = false;
126                         }
127                         else if (!unDo_actions->hasActualPrevious() && redo_actions->hasActualPrevious())
128                         {
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();
135                         }
136                         else 
137                         {       
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();        
143                                 else
144                                         executed = executed && redo_actions->moveBack_Last();   
145                         }
146                         if (!unDo_actions->hasActualPrevious() && redo_actions->hasActualPrevious())
147                         {
148                                 isLastUNDO_executed = false;
149                         }
150                         if (!unDo_actions->hasActualNext() && !redo_actions->hasActualNext())
151                         {
152                                 isLastUNDO_executed = false;
153                         }
154                         validateOperationsAvaliability();
155                 }               
156                 return executed;
157         }
158
159         /*
160         * Notitify if posibleREDO is posible or not.
161         * @return Returns the state of posibleUNDO
162         */
163         bool CommandsHandler :: isPosibleUNDO()
164         {
165                 return posibleUNDO;
166         }
167
168         /*
169         * Indicates if posibleUNDO is posible or not.
170         * @return Returns the state of posibleREDO
171         */
172         bool CommandsHandler :: isPosibleREDO()
173         {
174                 return posibleREDO;
175         }
176
177         /*
178         * Sets  posibleREDO state.
179         * @param UNDOstate The state of posibleUNDO to set
180         */
181         void CommandsHandler :: setPosibleUNDO( bool UNDOstate )
182         {
183                 posibleUNDO = UNDOstate;
184         }
185
186         /*
187         * Sets posibleUNDO state.
188         * @param REDOstate The state of posibleREDO to set
189         */
190         void CommandsHandler :: setPosibleREDO( bool REDOstate )
191         {
192                 posibleREDO = REDOstate;
193         }
194
195         /*
196         * Clear the registered actions in the DO and UNDO vectors.
197         */
198         void CommandsHandler :: clearActions()
199         {
200                 redo_actions->clearActions();
201                 unDo_actions->clearActions();
202         }
203
204         /*
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
207         */
208         int CommandsHandler :: getTotalCommands()
209         {
210                 int value = redo_actions->getTotalCommandsCount();
211                 return value == (unDo_actions->getTotalCommandsCount())  ? value : -1;
212         }
213
214
215         /*
216         * Gets the actual command in the UNDO-list
217         * @return  Returns a pointer to the actual undo action
218         */
219         CommandObject * CommandsHandler :: getActual_UNDO()
220         {
221                 return unDo_actions->getActual_Pointer();
222         }
223
224         /*
225         * Gets the actual command in the REDO-list
226         * @return  Returns a pointer to the actual do action
227         */
228         CommandObject * CommandsHandler :: getActual_REDO()
229         {
230                 return redo_actions->getActual_Pointer();
231         }
232
233         /*
234         * Gets the command at the given position in the DO (REDO) vector
235         * @return The pointer to the referenced object by the position
236         */
237         CommandObject * CommandsHandler :: get_DO_CommandAt(int position)
238         {
239                 return redo_actions->getCommandAt(position);
240         }
241
242         /*
243         * Gets the command at the given position in the UNDO vector
244         * @return The pointer to the referenced object by the position
245         */
246         CommandObject * CommandsHandler :: get_UNDO_CommandAt(int position)
247         {
248                 return unDo_actions->getCommandAt(position);
249         }
250
251                 /*
252         * Sets the model parent of the action handler
253         * @param theModelParent The boss reference
254         */
255         void CommandsHandler :: setModelBoss(ICommandsUser * theModelParent)
256         {
257                 theWorksSpaceBoss = theModelParent;
258         }
259
260         /*
261         * Validates if it is posible of not to do UNDO and/or REDO and sets the corresponding values
262         */
263         void CommandsHandler :: validateOperationsAvaliability()
264         {
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 );   
268
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 );   
272
273         }