Medical Imaging Interaction Toolkit  2016.11.0
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,
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 #include "mitkUndoController.h"
18 #include "mitkInteractionConst.h"
19 #include "mitkLimitedLinearUndo.h"
20 #include "mitkRenderingManager.h"
22 
23 // static member-variables init.
24 mitk::UndoModel::Pointer mitk::UndoController::m_CurUndoModel;
25 mitk::UndoController::UndoModelMap mitk::UndoController::m_UndoModelList;
26 mitk::UndoController::UndoType mitk::UndoController::m_CurUndoType;
27 
28 // const mitk::UndoController::UndoType mitk::UndoController::DEFAULTUNDOMODEL = LIMITEDLINEARUNDO;
30 
32 {
33  if (SwitchUndoModel(undoType) == false) // existiert noch nicht in static-Liste
34  {
35  switch (undoType)
36  {
37  case LIMITEDLINEARUNDO:
38  m_CurUndoModel = mitk::LimitedLinearUndo::New();
39  m_CurUndoType = undoType;
40  m_UndoModelList.insert(UndoModelMap::value_type(undoType, m_CurUndoModel));
41  break;
43  m_CurUndoModel = mitk::VerboseLimitedLinearUndo::New();
44  m_CurUndoType = undoType;
45  m_UndoModelList.insert(UndoModelMap::value_type(undoType, m_CurUndoModel));
46  break;
47  // case ###
48  // insert here, in add- and RemoveUndoModel new sets of UndoModels!
49  // break;
50  default:
51  m_CurUndoModel = VerboseLimitedLinearUndo::New();
52  m_CurUndoType = undoType;
53  m_UndoModelList.insert(UndoModelMap::value_type(undoType, m_CurUndoModel));
54  }
55  }
56 }
57 
59 {
60 }
61 
63 {
64  m_CurUndoModel->SetOperationEvent(operationEvent);
65  return true;
66 }
67 
69 {
70  return this->Undo(true);
71 }
72 
74 {
75  bool ret = m_CurUndoModel->Undo(fine);
76 
78 
79  return ret;
80 }
81 
83 {
84  return this->Redo(true);
85 }
86 
88 {
89  bool ret = m_CurUndoModel->Redo(fine);
90 
92 
93  return ret;
94 }
95 
97 {
98  m_CurUndoModel->Clear();
99 }
100 
102 {
103  m_CurUndoModel->ClearRedoList();
104 }
105 
107 {
108  return m_CurUndoModel->RedoListEmpty();
109 }
110 
111 //##Documentation
112 //##Switches the UndoModel to the given Type
113 //##if there is no equal Type in List, then return false
115 {
116  if (m_CurUndoType == undoType)
117  {
118  return true; // already switched, don't need to be switched!
119  }
120 
121  auto undoModelIter = m_UndoModelList.find(undoType);
122  if (undoModelIter == m_UndoModelList.end())
123  { // undoType not found in List
124  return false;
125  }
126 
127  // found-> switch to UndoModel
128  m_CurUndoModel = (undoModelIter)->second;
129  m_CurUndoType = (undoModelIter)->first;
130  return true;
131 }
132 
133 //##Documentation
134 //##adds a new kind of UndoModel to the set of UndoModels
135 //##and switches to that UndoModel
136 //##if the UndoModel exists already in the List, then nothing is done
138 {
139  if (m_UndoModelList.find(undoType) != m_UndoModelList.end())
140  { // UndoModel already exists
141  return false;
142  }
143  // doesn't already exist in list
144  switch (undoType)
145  {
146  case LIMITEDLINEARUNDO:
147  m_CurUndoModel = LimitedLinearUndo::New();
148  m_CurUndoType = undoType;
149  m_UndoModelList.insert(UndoModelMap::value_type(undoType, m_CurUndoModel));
150  break;
151  case VERBOSE_LIMITEDLINEARUNDO:
152  m_CurUndoModel = VerboseLimitedLinearUndo::New();
153  m_CurUndoType = undoType;
154  m_UndoModelList.insert(UndoModelMap::value_type(undoType, m_CurUndoModel));
155  break;
156  default:
157  // that undoType is not implemented!
158  return false;
159  }
160  return true;
161 }
162 
163 //##Documentation
164 //##Removes an UndoModel from the set of UndoModels
165 //##If that UndoModel is currently selected, then the DefaultUndoModel(const) is set.
166 //##If the default is not in List, then the first UndoModel is set.
167 //##UndoList may not be empty, so if the UndoType is the last, then return false;
169 {
170  if (m_UndoModelList.size() < 2)
171  { // for no empty m_UndoModelList
172  return false;
173  }
174  // try deleting Element
175  int ok = m_UndoModelList.erase(undoType);
176  if (ok == 0)
177  { // delete unsucessful; Element of undoType not found
178  return false;
179  }
180 
181  // if m_CurUndoModel is the one removed, then change it to default or to the next or first
182  if (m_CurUndoType == undoType)
183  { // we have to change m_CurUndoModel and m_CurUndoType to an existing Model
184 
185  // if defaultUndoModel exists, then set to default
186  auto undoModelIter = m_UndoModelList.find(DEFAULTUNDOMODEL);
187  if (undoModelIter == m_UndoModelList.end())
188  { // DefaultUndoModel does not exists in m_CurUndoModelList
189  undoModelIter = m_UndoModelList.begin();
190  }
191  m_CurUndoModel = (undoModelIter)->second;
192  m_CurUndoType = (undoModelIter)->first;
193  return true;
194  }
195  // m_CurUndoType was not undoType and is not changed
196  return true;
197 }
198 
200 {
201  return m_CurUndoModel->GetLastObjectEventIdInList();
202 }
203 
205 {
206  return m_CurUndoModel->GetLastGroupEventIdInList();
207 }
208 
210 {
211  return m_CurUndoModel->GetLastOfType(destination, opType);
212 }
213 
215 {
216  return m_CurUndoModel;
217 }
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:36
int OperationType
Definition: mitkOperation.h:27
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 ...