Medical Imaging Interaction Toolkit  2018.4.99-4c24e3cb
Medical Imaging Interaction Toolkit
mitkNavigationToolStorage.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 
14 
15 //Microservices
16 #include <usGetModuleContext.h>
17 #include <usModule.h>
18 #include <usModuleContext.h>
19 
20 const std::string mitk::NavigationToolStorage::US_INTERFACE_NAME = "org.mitk.services.NavigationToolStorage"; // Name of the interface
21 const std::string mitk::NavigationToolStorage::US_PROPKEY_SOURCE_ID = US_INTERFACE_NAME + ".sourceID";
22 const 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;
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.
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 
122 bool 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 
148 mitk::NavigationTool::Pointer mitk::NavigationToolStorage::GetTool(int number)
149 {
150  return m_ToolCollection.at(number);
151 }
152 
153 mitk::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 
159 mitk::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 
190 bool 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::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...
#define MITK_DEBUG
Definition: mitkLogMacros.h:22
STL namespace.
DataCollection - Class to facilitate loading/accessing structured data.
static const std::string US_PROPKEY_SOURCE_ID
An object of this class represents a navigation tool in the view of the software. A few informations ...
std::vector< mitk::NavigationTool::Pointer > m_ToolCollection
virtual void UnRegisterMicroservice()
Registers this object as a Microservice, making it available to every module and/or plugin...
static const std::string US_PROPKEY_STORAGE_NAME
bool DeleteAllTools()
Deletes all tools from the collection. Warning, this method operates on the data storage and is not t...
#define MITK_WARN
Definition: mitkLogMacros.h:19
mitk::DataStorage::Pointer m_DataStorage
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)
mitk::NavigationTool::Pointer GetTool(int number)
virtual void RegisterAsMicroservice()
Registers this object as a Microservice, making it available to every module and/or plugin...
void SetProperties(const ServiceProperties &properties)
static ModuleContext * GetModuleContext()
Returns the module context of the calling module.
bool AssignToolNumber(std::string identifier1, int number2)