Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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,
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 
18 
19 //Microservices
20 #include <usGetModuleContext.h>
21 #include <usModule.h>
22 #include <usModuleContext.h>
23 
24 const std::string mitk::NavigationToolStorage::US_INTERFACE_NAME = "org.mitk.services.NavigationToolStorage"; // Name of the interface
25 const std::string mitk::NavigationToolStorage::US_PROPKEY_SOURCE_ID = US_INTERFACE_NAME + ".sourceID";
26 const std::string mitk::NavigationToolStorage::US_PROPKEY_STORAGE_NAME = US_INTERFACE_NAME + ".name";
27 
29  : m_ToolCollection(std::vector<mitk::NavigationTool::Pointer>()),
30  m_DataStorage(NULL),
31  m_storageLocked(false)
32  {
33  this->SetName("ToolStorage (no name given)");
34  }
35 
37  {
38  m_ToolCollection = std::vector<mitk::NavigationTool::Pointer>();
39  this->m_DataStorage = ds;
40  }
41 
43  {
44  m_Name = n;
45  m_props[ US_PROPKEY_STORAGE_NAME ] = m_Name;
46  }
47 
49  {
50  if (m_ServiceRegistration) {m_ServiceRegistration.SetProperties(m_props);}
51  }
52 
53 
55  {
56  if (m_DataStorage.IsNotNull()) //remove all nodes from the data storage
57  {
58  for(std::vector<mitk::NavigationTool::Pointer>::iterator it = m_ToolCollection.begin(); it != m_ToolCollection.end(); it++)
59  m_DataStorage->Remove((*it)->GetDataNode());
60  }
61  }
62 
63 
65 
66  if ( sourceID.empty() ) mitkThrow() << "Empty or null string passed to NavigationToolStorage::registerAsMicroservice().";
67 
68  // Get Context
69  us::ModuleContext* context = us::GetModuleContext();
70 
71  // Define ServiceProps
72  m_props[ US_PROPKEY_SOURCE_ID ] = sourceID;
73  m_ServiceRegistration = context->RegisterService(this, m_props);
74 }
75 
76 
78  if ( ! m_ServiceRegistration )
79  {
80  MITK_WARN("NavigationToolStorage")
81  << "Cannot unregister microservice as it wasn't registered before.";
82  return;
83  }
84 
85  m_ServiceRegistration.Unregister();
86  m_ServiceRegistration = 0;
87 }
88 
89 
91  {
92  if (m_storageLocked)
93  {
94  MITK_WARN << "Storage is locked, cannot modify it!";
95  return false;
96  }
97 
98  else if ((unsigned int)number > m_ToolCollection.size())
99  {
100  MITK_WARN << "Tool no " << number << "doesn't exist, can't delete it!";
101  return false;
102  }
103  std::vector<mitk::NavigationTool::Pointer>::iterator it = m_ToolCollection.begin() + number;
104  if(m_DataStorage.IsNotNull())
105  m_DataStorage->Remove((*it)->GetDataNode());
106  m_ToolCollection.erase(it);
107 
108  return true;
109  }
110 
112  {
113  if (m_storageLocked)
114  {
115  MITK_WARN << "Storage is locked, cannot modify it!";
116  return false;
117  }
118 
119  while(m_ToolCollection.size() > 0) if (!DeleteTool(0)) return false;
120  return true;
121  }
122 
124  {
125  if (m_storageLocked)
126  {
127  MITK_WARN << "Storage is locked, cannot modify it!";
128  return false;
129  }
130  else if (GetTool(tool->GetIdentifier()).IsNotNull())
131  {
132  MITK_WARN << "Tool ID already exists in storage, can't add!";
133  return false;
134  }
135  else
136  {
137  m_ToolCollection.push_back(tool);
138  if(m_DataStorage.IsNotNull())
139  {
140  if (!m_DataStorage->Exists(tool->GetDataNode()))
141  m_DataStorage->Add(tool->GetDataNode());
142  }
143  return true;
144  }
145  }
146 
148  {
149  return m_ToolCollection.at(number);
150  }
151 
153  {
154  for (int i=0; i<GetToolCount(); i++) if ((GetTool(i)->GetIdentifier())==identifier) return GetTool(i);
155  return NULL;
156  }
157 
159  {
160  for (int i=0; i<GetToolCount(); i++) if ((GetTool(i)->GetToolName())==name) return GetTool(i);
161  return NULL;
162  }
163 
165  {
166  return m_ToolCollection.size();
167  }
168 
170  {
171  return m_ToolCollection.empty();
172  }
173 
175  {
176  m_storageLocked = true;
177  }
178 
180  {
181  m_storageLocked = false;
182  }
183 
185  {
186  return m_storageLocked;
187  }
188 
189 bool mitk::NavigationToolStorage::AssignToolNumber(std::string identifier1, int number2)
190  {
191  if (this->GetTool(identifier1).IsNull())
192  {
193  MITK_WARN << "Identifier does not exist, cannot assign new number";
194  return false;
195  }
196 
197  if ((number2 >= m_ToolCollection.size()) || (number2 < 0))
198  {
199  MITK_WARN << "Invalid number, cannot assign new number";
200  return false;
201  }
202 
203  mitk::NavigationTool::Pointer tool2 = m_ToolCollection.at(number2);
204 
205  int number1 = -1;
206 
207  for(int i = 0; i<m_ToolCollection.size(); i++)
208  {
209  if (m_ToolCollection.at(i)->GetIdentifier() == identifier1) {number1=i;}
210  }
211 
212  m_ToolCollection[number2] = m_ToolCollection.at(number1);
213 
214  m_ToolCollection[number1] = tool2;
215 
216  MITK_DEBUG << "Swapped tool " << number2 << " with tool " << number1;
217 
218  return true;
219 }
mitk::DataStorage::Pointer m_DataStorage
itk::SmartPointer< Self > Pointer
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:26
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:23
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...
#define mitkThrow()
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(std::string sourceID)
Registers this object as a Microservice, making it available to every module and/or plugin...
static ModuleContext * GetModuleContext()
Returns the module context of the calling module.
bool AssignToolNumber(std::string identifier1, int number2)