Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkOperationEvent.h
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #ifndef OPERATIONEVENT_H_HEADER_INCLUDED_C16E83FC
18 #define OPERATIONEVENT_H_HEADER_INCLUDED_C16E83FC
19 
20 #include "mitkOperation.h"
21 #include "mitkOperationActor.h"
22 #include "mitkUndoModel.h"
23 #include <MitkCoreExports.h>
24 #include <list>
25 #include <string>
26 
27 namespace mitk
28 {
29  //##Documentation
30  //## @brief Represents an entry of the undo or redo stack.
31  //##
32  //## This basic entry includes a textual description of the item and a pair of IDs. Static
33  //## member functions handle creation and incrementing of these IDs.
34  //##
35  //## The GroupEventID is intended for logical grouping of several related Operations.
36  //## Currently this is used only by PointSetDataInteractor. How this is done and when to use
37  //## GroupEventIDs is still undocumented.
38  //## @ingroup Undo
40  {
41  public:
42  UndoStackItem(std::string description = "");
43 
44  virtual ~UndoStackItem();
45 
46  //##Documentation
47  //## @brief For combining operations in groups
48  //##
49  //## This ID is used in the undo mechanism.
50  //## For separation of the seperate operations
51  //## If the GroupEventId of two OperationEvents is equal,
52  //## then they share one group and will be undone in case of Undo(fine==false)
53  static int GetCurrGroupEventId();
54 
55  //##Documentation
56  //## @brief For combining operations in Objects
57  //##
58  //## This ID is used in the Undo-Mechanism.
59  //## For separation of the seperate operations
60  //## If the ObjectEventId of two OperationEvents is equal,
61  //## then they share one Object and will be undone in all cases of Undo(true and false).
62  //## they shal not be seperated, because they were produced to realize one object-change.
63  //## for example: OE_statechange and OE_addlastpoint
64  static int GetCurrObjectEventId();
65 
66  //##Documentation
67  //## @brief Returns the GroupEventId for this object
68  int GetGroupEventId();
69 
70  //##Documentation
71  //## @brief Returns the ObjectEventId for this object
72  int GetObjectEventId();
73 
74  //##Documentation
75  //## @brief Returns the textual description of this object
76  std::string GetDescription();
77 
78  virtual void ReverseOperations();
79  virtual void ReverseAndExecute();
80 
81  //##Documentation
82  //## @brief Increases the current ObjectEventId
83  //## For example if a button click generates operations the ObjectEventId has to be incremented to be able to undo
84  //the
85  // operations.
86  //## Difference between ObjectEventId and GroupEventId: The ObjectEventId capsulates all operations caused by one
87  // event.
88  //## A GroupEventId capsulates several ObjectEventIds so that several operations caused by several events can be
89  // undone with one Undo call.
90  static void IncCurrObjectEventId();
91 
92  //##Documentation
93  //## @brief Increases the current GroupEventId
94  //## For example if a button click generates operations the GroupEventId has to be incremented to be able to undo
95  //the
96  // operations.
97  //## Difference between ObjectEventId and GroupEventId: The ObjectEventId capsulates all operations caused by one
98  // event.
99  //## A GroupEventId capsulates several ObjectEventIds so that several operations caused by several events can be
100  // undone with one Undo call.
101  static void IncCurrGroupEventId();
102 
103  protected:
104  //##Documentation
105  //## @brief true, if operation and undooperation have been swaped/changed
107 
108  private:
109  static int m_CurrObjectEventId;
110 
111  static int m_CurrGroupEventId;
112 
113  int m_ObjectEventId;
114 
115  int m_GroupEventId;
116 
117  std::string m_Description;
118 
119  UndoStackItem(UndoStackItem &); // hide copy constructor
120  void operator=(const UndoStackItem &); // hide operator=
121  };
122 
123  //##Documentation
124  //## @brief Represents a pair of operations: undo and the according redo.
125  //##
126  //## Additionally to the base class UndoStackItem, which only provides a description of an
127  //## item, OperationEvent does the actual accounting of the undo/redo stack. This class
128  //## holds two Operation objects (operation and its inverse operation) and the corresponding
129  //## OperationActor. The operations may be swapped by the
130  //## undo models, when an OperationEvent is moved from their undo to their redo
131  //## stack or vice versa.
132  //##
133  //## Note, that memory management of operation and undooperation is done by this class.
134  //## Memory of both objects is freed in the destructor. For this, the method IsValid() is needed which holds
135  //## information of the state of m_Destination. In case the object referenced by m_Destination is already deleted,
136  //## isValid() returns false.
137  //## In more detail if the destination happens to be an itk::Object (often the case), OperationEvent is informed as
138  //soon
139  //## as the object is deleted - from this moment on the OperationEvent gets invalid. You should
140  //## check this flag before you call anything on destination
141  //##
142  //## @ingroup Undo
144  {
145  public:
146  //## @brief default constructor
147  OperationEvent(OperationActor *destination,
148  Operation *operation,
149  Operation *undoOperation,
150  std::string description = "");
151 
152  //## @brief default destructor
153  //##
154  //## removes observers if destination is valid
155  //## and frees memory referenced by m_Operation and m_UndoOperation
156  virtual ~OperationEvent();
157 
158  //## @brief Returns the operation
159  Operation *GetOperation();
160 
161  //## @brief Returns the destination of the operations
162  OperationActor *GetDestination();
163 
164  friend class UndoModel;
165 
166  //## @brief Swaps the two operations and sets a flag,
167  //## that it has been swapped and doOp is undoOp and undoOp is doOp
168  virtual void ReverseOperations() override;
169 
170  //##reverses and executes both operations (used, when moved from undo to redo stack)
171  virtual void ReverseAndExecute() override;
172 
173  //## @brief returns true if the destination still is present
174  //## and false if it already has been deleted
175  virtual bool IsValid();
176 
177  protected:
178  void OnObjectDeleted();
179 
180  private:
181  // Has to be observed for itk::DeleteEvents.
182  // When destination is deleted, this stack item is invalid!
183  OperationActor *m_Destination;
184 
185  //## reference to the operation
186  Operation *m_Operation;
187 
188  //## reference to the undo operation
189  Operation *m_UndoOperation;
190 
191  //## hide copy constructor
193  //## hide operator=
194  void operator=(const OperationEvent &);
195 
196  // observertag used to listen to m_Destination
197  unsigned long m_DeleteTag;
198 
199  //## stores if destination is valid or already has been freed
200  bool m_Invalid;
201  };
202 
203 } // namespace mitk
204 
205 #endif /* OPERATIONEVENT_H_HEADER_INCLUDED_C16E83FC */
#define MITKCORE_EXPORT
Base class of all Operation-classes.
Definition: mitkOperation.h:33
DataCollection - Class to facilitate loading/accessing structured data.
abstract class, that can be used by Undo to undo an operation.
bool m_Reversed
true, if operation and undooperation have been swaped/changed
Represents an entry of the undo or redo stack.
superclass for all UndoModels
Definition: mitkUndoModel.h:36
Represents a pair of operations: undo and the according redo.