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