Medical Imaging Interaction Toolkit  2018.4.99-b585543d
Medical Imaging Interaction Toolkit
mitkUndoController.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 "mitkUndoController.h"
14 #include "mitkInteractionConst.h"
15 #include "mitkLimitedLinearUndo.h"
16 #include "mitkRenderingManager.h"
18 
19 // static member-variables init.
20 mitk::UndoModel::Pointer mitk::UndoController::m_CurUndoModel;
21 mitk::UndoController::UndoModelMap mitk::UndoController::m_UndoModelList;
22 mitk::UndoController::UndoType mitk::UndoController::m_CurUndoType;
23 
24 // const mitk::UndoController::UndoType mitk::UndoController::DEFAULTUNDOMODEL = LIMITEDLINEARUNDO;
26 
28 {
29  if (SwitchUndoModel(undoType) == false) // existiert noch nicht in static-Liste
30  {
31  switch (undoType)
32  {
33  case LIMITEDLINEARUNDO:
34  m_CurUndoModel = mitk::LimitedLinearUndo::New();
35  m_CurUndoType = undoType;
36  m_UndoModelList.insert(UndoModelMap::value_type(undoType, m_CurUndoModel));
37  break;
39  m_CurUndoModel = mitk::VerboseLimitedLinearUndo::New();
40  m_CurUndoType = undoType;
41  m_UndoModelList.insert(UndoModelMap::value_type(undoType, m_CurUndoModel));
42  break;
43  // case ###
44  // insert here, in add- and RemoveUndoModel new sets of UndoModels!
45  // break;
46  default:
47  m_CurUndoModel = VerboseLimitedLinearUndo::New();
48  m_CurUndoType = undoType;
49  m_UndoModelList.insert(UndoModelMap::value_type(undoType, m_CurUndoModel));
50  }
51  }
52 }
53 
55 {
56 }
57 
59 {
60  m_CurUndoModel->SetOperationEvent(operationEvent);
61  return true;
62 }
63 
65 {
66  return this->Undo(true);
67 }
68 
70 {
71  bool ret = m_CurUndoModel->Undo(fine);
72 
74 
75  return ret;
76 }
77 
79 {
80  return this->Redo(true);
81 }
82 
84 {
85  bool ret = m_CurUndoModel->Redo(fine);
86 
88 
89  return ret;
90 }
91 
93 {
94  m_CurUndoModel->Clear();
95 }
96 
98 {
99  m_CurUndoModel->ClearRedoList();
100 }
101 
103 {
104  return m_CurUndoModel->RedoListEmpty();
105 }
106 
107 //##Documentation
108 //##Switches the UndoModel to the given Type
109 //##if there is no equal Type in List, then return false
111 {
112  if (m_CurUndoType == undoType)
113  {
114  return true; // already switched, don't need to be switched!
115  }
116 
117  auto undoModelIter = m_UndoModelList.find(undoType);
118  if (undoModelIter == m_UndoModelList.end())
119  { // undoType not found in List
120  return false;
121  }
122 
123  // found-> switch to UndoModel
124  m_CurUndoModel = (undoModelIter)->second;
125  m_CurUndoType = (undoModelIter)->first;
126  return true;
127 }
128 
129 //##Documentation
130 //##adds a new kind of UndoModel to the set of UndoModels
131 //##and switches to that UndoModel
132 //##if the UndoModel exists already in the List, then nothing is done
134 {
135  if (m_UndoModelList.find(undoType) != m_UndoModelList.end())
136  { // UndoModel already exists
137  return false;
138  }
139  // doesn't already exist in list
140  switch (undoType)
141  {
142  case LIMITEDLINEARUNDO:
143  m_CurUndoModel = LimitedLinearUndo::New();
144  m_CurUndoType = undoType;
145  m_UndoModelList.insert(UndoModelMap::value_type(undoType, m_CurUndoModel));
146  break;
148  m_CurUndoModel = VerboseLimitedLinearUndo::New();
149  m_CurUndoType = undoType;
150  m_UndoModelList.insert(UndoModelMap::value_type(undoType, m_CurUndoModel));
151  break;
152  default:
153  // that undoType is not implemented!
154  return false;
155  }
156  return true;
157 }
158 
159 //##Documentation
160 //##Removes an UndoModel from the set of UndoModels
161 //##If that UndoModel is currently selected, then the DefaultUndoModel(const) is set.
162 //##If the default is not in List, then the first UndoModel is set.
163 //##UndoList may not be empty, so if the UndoType is the last, then return false;
165 {
166  if (m_UndoModelList.size() < 2)
167  { // for no empty m_UndoModelList
168  return false;
169  }
170  // try deleting Element
171  int ok = m_UndoModelList.erase(undoType);
172  if (ok == 0)
173  { // delete unsucessful; Element of undoType not found
174  return false;
175  }
176 
177  // if m_CurUndoModel is the one removed, then change it to default or to the next or first
178  if (m_CurUndoType == undoType)
179  { // we have to change m_CurUndoModel and m_CurUndoType to an existing Model
180 
181  // if defaultUndoModel exists, then set to default
182  auto undoModelIter = m_UndoModelList.find(DEFAULTUNDOMODEL);
183  if (undoModelIter == m_UndoModelList.end())
184  { // DefaultUndoModel does not exists in m_CurUndoModelList
185  undoModelIter = m_UndoModelList.begin();
186  }
187  m_CurUndoModel = (undoModelIter)->second;
188  m_CurUndoType = (undoModelIter)->first;
189  return true;
190  }
191  // m_CurUndoType was not undoType and is not changed
192  return true;
193 }
194 
196 {
197  return m_CurUndoModel->GetLastObjectEventIdInList();
198 }
199 
201 {
202  return m_CurUndoModel->GetLastGroupEventIdInList();
203 }
204 
206 {
207  return m_CurUndoModel->GetLastOfType(destination, opType);
208 }
209 
211 {
212  return m_CurUndoModel;
213 }
bool Undo()
calls the UndoMechanism to undo the last change
bool Redo()
calls the RedoMechanism to redo the operations undone
std::map< UndoType, UndoModel::Pointer > UndoModelMap
bool RemoveUndoModel(UndoType undoType)
bool SwitchUndoModel(UndoType undoType)
Constants for most interaction classes, due to the generic StateMachines.
bool SetOperationEvent(UndoStackItem *operationEvent)
abstract class, that can be used by Undo to undo an operation.
OperationEvent * GetLastOfType(OperationActor *destination, OperationType opType)
returns the last specified OperationEvent in Undo-list corresponding to the given value; if nothing f...
UndoController(UndoType undoType=DEFAULTUNDOMODEL)
static const UndoType DEFAULTUNDOMODEL
Default UndoModel to use.
void Clear()
Clears the Undo and the RedoList.
bool RedoListEmpty()
returns true, if the RedoList is empty
static RenderingManager * GetInstance()
bool AddUndoModel(UndoType undoType)
Represents an entry of the undo or redo stack.
superclass for all UndoModels
Definition: mitkUndoModel.h:32
int OperationType
Definition: mitkOperation.h:23
static UndoModel * GetCurrentUndoModel()
gives access to the currently used UndoModel Introduced to access special functions of more specific ...
static Pointer New()
void RequestUpdateAll(RequestType type=REQUEST_UPDATE_ALL)
Represents a pair of operations: undo and the according redo.
int GetLastObjectEventIdInList()
returns the ObjectEventId of the top Element in the OperationHistory of the selected UndoModel ...
void ClearRedoList()
Clears the RedoList.
int GetLastGroupEventIdInList()
returns the GroupEventId of the top Element in the OperationHistory of the selected UndoModel ...