| 164 | } |
| 165 | |
| 166 | bool mitk::ToolManager::ActivateTool(int id) |
| 167 | { |
| 168 | const auto workingDataNode = this->GetWorkingData(0); |
| 169 | const mitk::BaseData* workingData = nullptr; |
| 170 | if (nullptr != workingDataNode) |
| 171 | { |
| 172 | workingData = workingDataNode->GetData(); |
| 173 | } |
| 174 | |
| 175 | const auto referenceDataNode = this->GetReferenceData(0); |
| 176 | const mitk::BaseData* referenceData = nullptr; |
| 177 | if (nullptr != referenceDataNode) |
| 178 | { |
| 179 | referenceData = referenceDataNode->GetData(); |
| 180 | } |
| 181 | |
| 182 | if (id != -1 && !this->GetToolById(id)->CanHandle(referenceData, workingData)) |
| 183 | return false; |
| 184 | |
| 185 | if (this->GetDataStorage()) |
| 186 | { |
| 187 | this->GetDataStorage()->RemoveNodeEvent.AddListener( |
| 188 | mitk::MessageDelegate1<ToolManager, const mitk::DataNode *>(this, &ToolManager::OnNodeRemoved)); |
| 189 | } |
| 190 | |
| 191 | if (GetToolById(id) == m_ActiveTool) |
| 192 | return true; // no change needed |
| 193 | |
| 194 | static int nextTool = -1; |
| 195 | nextTool = id; |
| 196 | |
| 197 | static bool inActivateTool = false; |
| 198 | if (inActivateTool) |
| 199 | { |
| 200 | return true; |
| 201 | } |
| 202 | inActivateTool = true; |
| 203 | |
| 204 | while (nextTool != m_ActiveToolID) |
| 205 | { |
| 206 | // Deactivate all other active tools to ensure a globally single active tool |
| 207 | for (const auto& toolManager : ToolManagerProvider::GetInstance()->GetToolManagers()) |
| 208 | { |
| 209 | if (nullptr != toolManager.second->m_ActiveTool) |
| 210 | { |
| 211 | toolManager.second->m_ActiveTool->Deactivated(); |
| 212 | |
| 213 | try |
| 214 | { |
| 215 | toolManager.second->m_ActiveToolRegistration.Unregister(); |
| 216 | } |
| 217 | catch (const std::logic_error&) |
| 218 | { |
| 219 | // In rare conditions it may happen that this code is called twice for the same tool leading to an attempt to |
| 220 | // unregister an already unregistered tool. We just want to make sure that the tool is unregistered, hence we |
| 221 | // can safely ignore the attempt/exception as the outcome is the same. |
| 222 | |
| 223 | MITK_DEBUG << "Detected an attempt to unregister an already unregistered segmentation tool."; |