Medical Imaging Interaction Toolkit  2018.4.99-c0f884b2
Medical Imaging Interaction Toolkit
mitkOperationEvent.cpp
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 #include "mitkOperationEvent.h"
14 #include <itkCommand.h>
15 
16 int mitk::UndoStackItem::m_CurrObjectEventId = 0;
17 int mitk::UndoStackItem::m_CurrGroupEventId = 0;
18 
19 mitk::UndoStackItem::UndoStackItem(std::string description) : m_Reversed(false), m_Description(description)
20 {
21  m_ObjectEventId = GetCurrObjectEventId();
22  m_GroupEventId = GetCurrGroupEventId();
23 }
24 
26 {
27  // nothing to do
28 }
29 
31 {
32  return m_CurrObjectEventId;
33 }
34 
36 {
37  return m_CurrGroupEventId;
38 }
39 
41 {
42  ++m_CurrObjectEventId;
43 }
44 
46 {
47  ++m_CurrGroupEventId;
48 }
49 
51 {
52  return m_ObjectEventId;
53 }
54 
56 {
57  return m_GroupEventId;
58 }
59 
61 {
62  return m_Description;
63 }
64 
66 {
68 }
69 
71 {
73 }
74 
75 // ******************** mitk::OperationEvent ********************
76 
78 {
79  return m_Operation;
80 }
81 
83  Operation *operation,
84  Operation *undoOperation,
85  std::string description)
86  : UndoStackItem(description),
87  m_Destination(destination),
88  m_Operation(operation),
89  m_UndoOperation(undoOperation),
90  m_Invalid(false)
91 {
92  // connect to delete event
93  if (auto *object = dynamic_cast<itk::Object *>(m_Destination))
94  {
95  itk::SimpleMemberCommand<OperationEvent>::Pointer command = itk::SimpleMemberCommand<OperationEvent>::New();
96  command->SetCallbackFunction(this, &OperationEvent::OnObjectDeleted);
97  m_DeleteTag = object->AddObserver(itk::DeleteEvent(), command);
98  }
99 }
100 
102 {
103  // remove the observer if the data m_Destination still is present
104  if (!m_Invalid)
105  {
106  if (auto *object = dynamic_cast<itk::Object *>(m_Destination))
107  {
108  object->RemoveObserver(m_DeleteTag);
109  }
110  }
111 
112  delete m_Operation;
113  delete m_UndoOperation;
114 }
115 
116 //##Documentation
117 //## swaps the Undo and Redo- operation and changes m_Reversed
119 {
120  if (m_Operation == nullptr)
121  return;
122 
123  Operation *tempOperation = m_Operation;
124  m_Operation = m_UndoOperation;
125  m_UndoOperation = tempOperation;
126 
128 }
129 
131 {
133  if (m_Destination && m_Operation && !m_Invalid)
134  m_Destination->ExecuteOperation(m_Operation);
135 }
136 
138 {
139  return m_Destination;
140 }
141 
143 {
144  m_Invalid = true;
145 }
146 
148 {
149  return !m_Invalid;
150 }
int GetObjectEventId()
Returns the ObjectEventId for this object.
Base class of all Operation-classes.
Definition: mitkOperation.h:29
std::string GetDescription()
Returns the textual description of this object.
static int GetCurrGroupEventId()
For combining operations in groups.
OperationActor * GetDestination()
abstract class, that can be used by Undo to undo an operation.
bool m_Reversed
true, if operation and undooperation have been swaped/changed
virtual void ReverseAndExecute()
virtual void ReverseOperations()
void ReverseAndExecute() override
int GetGroupEventId()
Returns the GroupEventId for this object.
Represents an entry of the undo or redo stack.
virtual void ExecuteOperation(Operation *operation)=0
UndoStackItem(std::string description="")
OperationEvent(OperationActor *destination, Operation *operation, Operation *undoOperation, std::string description="")
void ReverseOperations() override
static void IncCurrObjectEventId()
Increases the current ObjectEventId For example if a button click generates operations the ObjectEven...
static int GetCurrObjectEventId()
For combining operations in Objects.
static void IncCurrGroupEventId()
Increases the current GroupEventId For example if a button click generates operations the GroupEventI...