MITK-IGT
IGT Extension of MITK
Loading...
Searching...
No Matches
mitkNavigationToolStorage.cpp
Go to the documentation of this file.
1/*============================================================================
2
3The Medical Imaging Interaction Toolkit (MITK)
4
5Copyright (c) German Cancer Research Center (DKFZ)
6All rights reserved.
7
8Use of this source code is governed by a 3-clause BSD license that can be
9found in the LICENSE file.
10
11============================================================================*/
12
14
15//Microservices
16#include <usGetModuleContext.h>
17#include <usModule.h>
18#include <usModuleContext.h>
19
20const std::string mitk::NavigationToolStorage::US_INTERFACE_NAME = "org.mitk.services.NavigationToolStorage"; // Name of the interface
21const std::string mitk::NavigationToolStorage::US_PROPKEY_SOURCE_ID = US_INTERFACE_NAME + ".sourceID";
22const std::string mitk::NavigationToolStorage::US_PROPKEY_STORAGE_NAME = US_INTERFACE_NAME + ".name";
23
25 : m_ToolCollection(std::vector<mitk::NavigationTool::Pointer>()),
26 m_DataStorage(nullptr),
27 m_storageLocked(false)
28{
29 this->SetName("ToolStorage (no name given)");
30}
31
33 : m_storageLocked(false)
34{
35 m_ToolCollection = std::vector<mitk::NavigationTool::Pointer>();
36 this->m_DataStorage = ds;
37 this->SetName("Tool Storage (no name given)");
38}
39
41{
42 m_Name = n;
43 m_props[US_PROPKEY_STORAGE_NAME] = m_Name;
44}
45
47{
48 return m_Name;
49}
50
52{
53 if (m_ServiceRegistration) { m_ServiceRegistration.SetProperties(m_props); }
54}
55
57{
58 if (m_DataStorage.IsNotNull()) //remove all nodes from the data storage
59 {
60 for (std::vector<mitk::NavigationTool::Pointer>::iterator it = m_ToolCollection.begin(); it != m_ToolCollection.end(); it++)
61 m_DataStorage->Remove((*it)->GetDataNode());
62 }
63}
64
66 // Get Context
67 us::ModuleContext* context = us::GetModuleContext();
68
69 // Define ServiceProps
70 m_ServiceRegistration = context->RegisterService(this, m_props);
71 //Tell all widgets, that there is a new toolStorage registered, e.g. the old one might have changed.
72 UpdateMicroservice();
73}
74
76 if (!m_ServiceRegistration)
77 {
78 MITK_WARN("NavigationToolStorage")
79 << "Cannot unregister microservice as it wasn't registered before.";
80 return;
81 }
82
83 m_ServiceRegistration.Unregister();
84 m_ServiceRegistration = 0;
85}
86
88{
89 if (m_storageLocked)
90 {
91 MITK_WARN << "Storage is locked, cannot modify it!";
92 return false;
93 }
94
95 else if ((unsigned int)number > m_ToolCollection.size())
96 {
97 MITK_WARN << "Tool no " << number << "doesn't exist, can't delete it!";
98 return false;
99 }
100 std::vector<mitk::NavigationTool::Pointer>::iterator it = m_ToolCollection.begin() + number;
101 if (m_DataStorage.IsNotNull())
102 m_DataStorage->Remove((*it)->GetDataNode());
103 m_ToolCollection.erase(it);
104
105 //This line is important so that other widgets can get a notice that the toolStorage has changed!
106 this->UpdateMicroservice();
107 return true;
108}
109
111{
112 if (m_storageLocked)
113 {
114 MITK_WARN << "Storage is locked, cannot modify it!";
115 return false;
116 }
117
118 while (m_ToolCollection.size() > 0) if (!DeleteTool(0)) return false;
119 return true;
120}
121
122bool mitk::NavigationToolStorage::AddTool(mitk::NavigationTool::Pointer tool)
123{
124 if (m_storageLocked)
125 {
126 MITK_WARN << "Storage is locked, cannot modify it!";
127 return false;
128 }
129 else if (GetTool(tool->GetIdentifier()).IsNotNull())
130 {
131 MITK_WARN << "Tool ID already exists in storage, can't add!";
132 return false;
133 }
134 else
135 {
136 m_ToolCollection.push_back(tool);
137 if (m_DataStorage.IsNotNull())
138 {
139 if (!m_DataStorage->Exists(tool->GetDataNode()))
140 m_DataStorage->Add(tool->GetDataNode());
141 }
142 //This line is important so that other widgets can get a notice that the toolStorage has changed!
143 this->UpdateMicroservice();
144 return true;
145 }
146}
147
148mitk::NavigationTool::Pointer mitk::NavigationToolStorage::GetTool(int number)
149{
150 return m_ToolCollection.at(number);
151}
152
153mitk::NavigationTool::Pointer mitk::NavigationToolStorage::GetTool(std::string identifier)
154{
155 for (unsigned int i = 0; i < GetToolCount(); i++) if ((GetTool(i)->GetIdentifier()) == identifier) return GetTool(i);
156 return nullptr;
157}
158
159mitk::NavigationTool::Pointer mitk::NavigationToolStorage::GetToolByName(std::string name)
160{
161 for (unsigned int i = 0; i < GetToolCount(); i++) if ((GetTool(i)->GetToolName()) == name) return GetTool(i);
162 return nullptr;
163}
164
166{
167 return m_ToolCollection.size();
168}
169
171{
172 return m_ToolCollection.empty();
173}
174
176{
177 m_storageLocked = true;
178}
179
181{
182 m_storageLocked = false;
183}
184
186{
187 return m_storageLocked;
188}
189
190bool mitk::NavigationToolStorage::AssignToolNumber(std::string identifier1, int number2)
191{
192 if (this->GetTool(identifier1).IsNull())
193 {
194 MITK_WARN << "Identifier does not exist, cannot assign new number";
195 return false;
196 }
197
198 if ((number2 >= static_cast<int>(m_ToolCollection.size())) || (number2 < 0))
199 {
200 MITK_WARN << "Invalid number, cannot assign new number";
201 return false;
202 }
203
204 mitk::NavigationTool::Pointer tool2 = m_ToolCollection.at(number2);
205
206 int number1 = -1;
207
208 for(int i = 0; i<static_cast<int>(m_ToolCollection.size()); i++)
209 {
210 if (m_ToolCollection.at(i)->GetIdentifier() == identifier1) { number1 = i; }
211 }
212
213 m_ToolCollection[number2] = m_ToolCollection.at(number1);
214
215 m_ToolCollection[number1] = tool2;
216
217 MITK_DEBUG << "Swapped tool " << number2 << " with tool " << number1;
218
219 //This line is important so that other widgets can get a notice that the toolStorage has changed!
220 this->UpdateMicroservice();
221
222 return true;
223}
224
226{
227 m_SourceID = _id;
228 m_props[US_PROPKEY_SOURCE_ID] = m_SourceID;
229}
230
233{
234 return m_SourceID;
235}
mitk::NavigationTool::Pointer GetTool(int number)
bool AssignToolNumber(std::string identifier1, int number2)
static const std::string US_PROPKEY_SOURCE_ID
virtual void RegisterAsMicroservice()
Registers this object as a Microservice, making it available to every module and/or plugin....
bool DeleteAllTools()
Deletes all tools from the collection. Warning, this method operates on the data storage and is not t...
bool AddTool(mitk::NavigationTool::Pointer tool)
Adds a tool to the storage. Be sure that the tool has a unique identifier which is not already part o...
static const std::string US_INTERFACE_NAME
These constants are used in conjunction with Microservices.
mitk::NavigationTool::Pointer GetToolByName(std::string name)
virtual void UnRegisterMicroservice()
Registers this object as a Microservice, making it available to every module and/or plugin.
std::vector< mitk::NavigationTool::Pointer > m_ToolCollection
static const std::string US_PROPKEY_STORAGE_NAME
mitk::DataStorage::Pointer m_DataStorage
bool DeleteTool(int number)
Deletes a tool from the collection. Warning, this method operates on the data storage and is not thre...
An object of this class represents a navigation tool in the view of the software. A few informations ...
IGT Exceptions.