00001 /*========================================================================= 00002 00003 Program: Medical Imaging & Interaction Toolkit 00004 Language: C++ 00005 Date: $Date$ 00006 Version: $Revision$ 00007 00008 Copyright (c) German Cancer Research Center, Division of Medical and 00009 Biological Informatics. All rights reserved. 00010 See MITKCopyright.txt or https://www.mitk.org/copyright.html for details. 00011 00012 This software is distributed WITHOUT ANY WARRANTY; without even 00013 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 00014 PURPOSE. See the above copyright notices for more information. 00015 00016 =========================================================================*/ 00017 00018 00019 #include "mitkUndoController.h" 00020 #include "mitkLimitedLinearUndo.h" 00021 #include "mitkVerboseLimitedLinearUndo.h" 00022 #include "mitkInteractionConst.h" 00023 #include "mitkRenderingManager.h" 00024 00025 //static member-variables init. 00026 mitk::UndoModel::Pointer mitk::UndoController::m_CurUndoModel; 00027 mitk::UndoController::UndoModelMap mitk::UndoController::m_UndoModelList; 00028 mitk::UndoController::UndoType mitk::UndoController::m_CurUndoType; 00029 00030 //const mitk::UndoController::UndoType mitk::UndoController::DEFAULTUNDOMODEL = LIMITEDLINEARUNDO; 00031 const mitk::UndoController::UndoType mitk::UndoController::DEFAULTUNDOMODEL = VERBOSE_LIMITEDLINEARUNDO; 00032 00033 00034 mitk::UndoController::UndoController(UndoType undoType) 00035 { 00036 if (SwitchUndoModel(undoType)==false) //existiert noch nicht in static-Liste 00037 { 00038 switch (undoType) 00039 { 00040 case LIMITEDLINEARUNDO: 00041 m_CurUndoModel = mitk::LimitedLinearUndo::New(); 00042 m_CurUndoType = undoType; 00043 m_UndoModelList.insert(UndoModelMap::value_type(undoType, m_CurUndoModel)); 00044 break; 00045 case VERBOSE_LIMITEDLINEARUNDO: 00046 m_CurUndoModel = mitk::VerboseLimitedLinearUndo::New(); 00047 m_CurUndoType = undoType; 00048 m_UndoModelList.insert(UndoModelMap::value_type(undoType, m_CurUndoModel)); 00049 break; 00050 //case ### 00051 //insert here, in add- and RemoveUndoModel new sets of UndoModels! 00052 //break; 00053 default : 00054 m_CurUndoModel = VerboseLimitedLinearUndo::New(); 00055 m_CurUndoType = undoType; 00056 m_UndoModelList.insert(UndoModelMap::value_type(undoType, m_CurUndoModel)); 00057 } 00058 } 00059 } 00060 00061 mitk::UndoController::~UndoController() 00062 { 00063 00064 } 00065 00066 bool mitk::UndoController::SetOperationEvent(UndoStackItem* operationEvent) 00067 { 00068 m_CurUndoModel->SetOperationEvent(operationEvent); 00069 return true; 00070 } 00071 00072 bool mitk::UndoController::Undo() 00073 { 00074 return this->Undo(true); 00075 } 00076 00077 bool mitk::UndoController::Undo(bool fine) 00078 { 00079 bool ret = m_CurUndoModel->Undo(fine); 00080 00081 mitk::RenderingManager::GetInstance()->RequestUpdateAll(); 00082 00083 return ret; 00084 } 00085 00086 bool mitk::UndoController::Redo() 00087 { 00088 return this->Redo(true); 00089 } 00090 00091 bool mitk::UndoController::Redo(bool fine) 00092 { 00093 bool ret = m_CurUndoModel->Redo(fine); 00094 00095 mitk::RenderingManager::GetInstance()->RequestUpdateAll(); 00096 00097 return ret; 00098 } 00099 00100 void mitk::UndoController::Clear() 00101 { 00102 m_CurUndoModel->Clear(); 00103 } 00104 00105 void mitk::UndoController::ClearRedoList() 00106 { 00107 m_CurUndoModel->ClearRedoList(); 00108 } 00109 00110 bool mitk::UndoController::RedoListEmpty() 00111 { 00112 return m_CurUndoModel->RedoListEmpty(); 00113 } 00114 00115 //##Documentation 00116 //##Switches the UndoModel to the given Type 00117 //##if there is no equal Type in List, then return false 00118 bool mitk::UndoController::SwitchUndoModel(UndoType undoType) 00119 { 00120 if (m_CurUndoType == undoType) 00121 { 00122 return true;//already switched, don't need to be switched! 00123 } 00124 00125 UndoModelMapIter undoModelIter = m_UndoModelList.find(undoType); 00126 if (undoModelIter == m_UndoModelList.end()) 00127 {//undoType not found in List 00128 return false; 00129 } 00130 00131 //found-> switch to UndoModel 00132 m_CurUndoModel = (undoModelIter)->second; 00133 m_CurUndoType = (undoModelIter)->first; 00134 return true; 00135 } 00136 00137 //##Documentation 00138 //##adds a new kind of UndoModel to the set of UndoModels 00139 //##and switches to that UndoModel 00140 //##if the UndoModel exists already in the List, then nothing is done 00141 bool mitk::UndoController::AddUndoModel(UndoType undoType) 00142 { 00143 if (m_UndoModelList.find(undoType) != m_UndoModelList.end()) 00144 { //UndoModel already exists 00145 return false; 00146 } 00147 //doesn't already exist in list 00148 switch (undoType) 00149 { 00150 case LIMITEDLINEARUNDO: 00151 m_CurUndoModel = LimitedLinearUndo::New(); 00152 m_CurUndoType = undoType; 00153 m_UndoModelList.insert(UndoModelMap::value_type(undoType, m_CurUndoModel)); 00154 break; 00155 case VERBOSE_LIMITEDLINEARUNDO: 00156 m_CurUndoModel = VerboseLimitedLinearUndo::New(); 00157 m_CurUndoType = undoType; 00158 m_UndoModelList.insert(UndoModelMap::value_type(undoType, m_CurUndoModel)); 00159 break; 00160 default: 00161 //that undoType is not implemented! 00162 return false; 00163 } 00164 return true; 00165 } 00166 00167 //##Documentation 00168 //##Removes an UndoModel from the set of UndoModels 00169 //##If that UndoModel is currently selected, then the DefaultUndoModel(const) is set. 00170 //##If the default is not in List, then the first UndoModel is set. 00171 //##UndoList may not be empty, so if the UndoType is the last, then return false; 00172 bool mitk::UndoController::RemoveUndoModel(UndoType undoType) 00173 { 00174 if (m_UndoModelList.size() < 2) 00175 {//for no empty m_UndoModelList 00176 return false; 00177 } 00178 //try deleting Element 00179 int ok = m_UndoModelList.erase(undoType); 00180 if (ok == 0) 00181 {//delete unsucessful; Element of undoType not found 00182 return false; 00183 } 00184 00185 //if m_CurUndoModel is the one removed, then change it to default or to the next or first 00186 if (m_CurUndoType == undoType) 00187 {//we have to change m_CurUndoModel and m_CurUndoType to an existing Model 00188 00189 //if defaultUndoModel exists, then set to default 00190 UndoModelMapIter undoModelIter = m_UndoModelList.find(DEFAULTUNDOMODEL); 00191 if (undoModelIter == m_UndoModelList.end()) 00192 {//DefaultUndoModel does not exists in m_CurUndoModelList 00193 undoModelIter = m_UndoModelList.begin(); 00194 } 00195 m_CurUndoModel = (undoModelIter)->second; 00196 m_CurUndoType = (undoModelIter)->first; 00197 return true; 00198 } 00199 //m_CurUndoType was not undoType and is not changed 00200 return true; 00201 } 00202 00203 int mitk::UndoController::GetLastObjectEventIdInList() 00204 { 00205 return m_CurUndoModel->GetLastObjectEventIdInList(); 00206 } 00207 00208 int mitk::UndoController::GetLastGroupEventIdInList() 00209 { 00210 return m_CurUndoModel->GetLastGroupEventIdInList(); 00211 } 00212 00213 00214 mitk::OperationEvent* mitk::UndoController::GetLastOfType(OperationActor* destination, OperationType opType) 00215 { 00216 return m_CurUndoModel->GetLastOfType(destination, opType); 00217 } 00218 00219 mitk::UndoModel* mitk::UndoController::GetCurrentUndoModel() 00220 { 00221 return m_CurUndoModel; 00222 }