Medical Imaging Interaction Toolkit  2018.4.99-87d68d9f
Medical Imaging Interaction Toolkit
QmitkTrackingDeviceConfigurationWidget.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 
17 
18 #include <QSettings>
19 
20 const std::string QmitkTrackingDeviceConfigurationWidget::VIEW_ID = "org.mitk.views.trackingdeviceconfigurationwidget";
21 
23  : QWidget(parent, f)
24  , m_Controls(nullptr)
25  , m_DeviceToWidgetIndexMap()
26 {
27  //initializations
28  CreateQtPartControl(this);
30 
32 
33  //initialize a few UI elements
34  AddOutput("<br>First Element selected"); //Order from Collection List
35 
36  //reset a few things
37  ResetOutput();
38 
39  //restore old UI settings
41 }
42 
44 {
46  delete m_Controls;
47 }
48 
50 {
51  if (!m_Controls)
52  {
53  // create GUI widgets
54  m_Controls = new Ui::QmitkTrackingDeviceConfigurationWidgetControls;
55  m_Controls->setupUi(parent);
56  }
57 }
58 
60 {
61  if (m_Controls)
62  {
63  connect((QObject*)(m_Controls->m_TrackingDeviceChooser), SIGNAL(currentIndexChanged(int)), this, SLOT(TrackingDeviceChanged()));
64  }
65 }
66 
68 {
69  const std::string currentDevice = this->GetCurrentDeviceName();
70 
71  //show the correspondig widget
72  m_Controls->m_TrackingSystemWidget->setCurrentIndex(m_DeviceToWidgetIndexMap[currentDevice]);
73 
74  //reset output
75  ResetOutput();
76 
77  AddOutput("<br>");
78  AddOutput(currentDevice);
79  AddOutput(" selected");
80 
81  QmitkAbstractTrackingDeviceWidget* widget = GetWidget(currentDevice);
82 
83  if (widget == nullptr || !widget->IsDeviceInstalled())
84  {
85  AddOutput("<br>ERROR: not installed!");
86  }
87 
89 }
90 
92 {
93  // clean-up of stacked widget, drop-down box and map
94  for (auto& item : m_DeviceToWidgetIndexMap)
95  {
96  m_Controls->m_TrackingSystemWidget->removeWidget(m_Controls->m_TrackingSystemWidget->widget(item.second));
97  MITK_INFO << "removing widget for device '" << item.first << "'";
98  }
99 
100  m_Controls->m_TrackingDeviceChooser->clear();
101 
102  m_DeviceToWidgetIndexMap.clear();
103 
104  // get tracking device type service references
105  us::ModuleContext* context = us::GetModuleContext();
106  std::vector<us::ServiceReference<mitk::TrackingDeviceTypeCollection> > deviceRefs =
107  context->GetServiceReferences<mitk::TrackingDeviceTypeCollection>();
108 
109  if (deviceRefs.empty())
110  {
111  MITK_ERROR << "No tracking device type service found!";
112  return;
113  }
114 
115  // get tracking device configuration widget service references
116  std::vector<us::ServiceReference<mitk::TrackingDeviceWidgetCollection> > widgetRefs =
117  context->GetServiceReferences<mitk::TrackingDeviceWidgetCollection>();
118 
119  if (widgetRefs.empty())
120  {
121  MITK_ERROR << "No tracking device configuration widget service found!";
122  return;
123  }
124 
125  const us::ServiceReference<mitk::TrackingDeviceTypeCollection>& deviceServiceReference = deviceRefs.front();
126  const us::ServiceReference<mitk::TrackingDeviceWidgetCollection>& widgetServiceReference = widgetRefs.front();
127 
128  mitk::TrackingDeviceTypeCollection* deviceTypeCollection =
129  context->GetService<mitk::TrackingDeviceTypeCollection>(deviceServiceReference);
130 
131  mitk::TrackingDeviceWidgetCollection* deviceWidgetCollection =
132  context->GetService<mitk::TrackingDeviceWidgetCollection>(widgetServiceReference);
133 
134  for (auto name : deviceTypeCollection->GetTrackingDeviceTypeNames())
135  {
136  // if the device is not included yet, add name to comboBox and widget to stackedWidget
137  if (m_Controls->m_TrackingDeviceChooser->findText(QString::fromStdString(name)) == -1)
138  {
139  m_Controls->m_TrackingDeviceChooser->addItem(QString::fromStdString(name));
140  QWidget* current = deviceWidgetCollection->GetTrackingDeviceWidgetClone(name);
141  if (current == nullptr)
142  {
143  MITK_WARN << "No widget for tracking device type " << name << " available. Please implement and register it!";
144  current = new QWidget();
145  }
146 
147  m_DeviceToWidgetIndexMap[name] = m_Controls->m_TrackingSystemWidget->addWidget(current);
148  }
149  }
150 
151  if (!m_DeviceToWidgetIndexMap.empty())
152  {
153  m_Controls->m_TrackingDeviceChooser->setCurrentIndex(0);
154  m_Controls->m_TrackingSystemWidget->setCurrentIndex(0);
155  }
156 
157  context->UngetService(deviceServiceReference);
158  context->UngetService(widgetServiceReference);
159 }
160 
161 //######################### internal help methods #######################################
163 {
164  QmitkAbstractTrackingDeviceWidget* currentWidget = this->GetWidget(this->GetCurrentDeviceName());
165 
166  if (currentWidget == nullptr)
167  {
168  return;
169  }
170 
171  currentWidget->ResetOutput();
172  currentWidget->repaint();
173 }
174 
176 {
177  QmitkAbstractTrackingDeviceWidget* currentWidget = this->GetWidget(this->GetCurrentDeviceName());
178 
179  if (currentWidget == nullptr)
180  {
181  return;
182  }
183 
184  currentWidget->AddOutput(s);
185  currentWidget->repaint();
186 }
187 
189 {
190  QmitkAbstractTrackingDeviceWidget* currentWidget = this->GetWidget(this->GetCurrentDeviceName());
191 
192  if (currentWidget == nullptr || !currentWidget->IsDeviceInstalled())
193  {
194  return nullptr;
195  }
196 
197  return currentWidget->GetTrackingDevice();
198 }
199 
201 {
202  std::string id = "org.mitk.modules.igt.ui.trackingdeviceconfigurationwidget";
203 
204  std::string selectedDevice = this->GetCurrentDeviceName();
205 
206  //Save settings for every widget
207  //Don't use m_DeviceTypeCollection here, it's already unregistered, when deconstructor is called...
208  for (int index = 0; index < m_Controls->m_TrackingSystemWidget->count(); index++)
209  {
210  QmitkAbstractTrackingDeviceWidget* widget = dynamic_cast<QmitkAbstractTrackingDeviceWidget*>(m_Controls->m_TrackingSystemWidget->widget(index));
211 
212  if (widget != nullptr)
213  {
214  widget->StoreUISettings();
215  }
216  }
217 
218  if (this->GetPersistenceService()) // now save the settings using the persistence service
219  {
220  mitk::PropertyList::Pointer propList = this->GetPersistenceService()->GetPropertyList(id);
221  propList->Set("SelectedDevice", selectedDevice);
222  }
223  else // QSettings as a fallback if the persistence service is not available
224  {
225  QSettings settings;
226  settings.beginGroup(QString::fromStdString(id));
227  settings.setValue("trackingDeviceChooser", QVariant(QString::fromStdString(selectedDevice)));
228  settings.endGroup();
229  }
230 }
231 
240 {
241  //Load settings for every widget
242  for (int index = 0; index < m_Controls->m_TrackingSystemWidget->count(); index++)
243  {
244  QmitkAbstractTrackingDeviceWidget* widget = dynamic_cast<QmitkAbstractTrackingDeviceWidget*>(m_Controls->m_TrackingSystemWidget->widget(index));
245 
246  if (widget != nullptr)
247  {
248  widget->LoadUISettings();
249  }
250  }
251 
252  std::string id = "org.mitk.modules.igt.ui.trackingdeviceconfigurationwidget";
253  std::string selectedDevice;
254  if (this->GetPersistenceService())
255  {
256  mitk::PropertyList::Pointer propList = this->GetPersistenceService()->GetPropertyList(id);
257  if (propList.IsNull())
258  {
259  MITK_ERROR << "Property list for this UI (" << id << ") is not available, could not load UI settings!"; return;
260  }
261 
262  propList->Get("SelectedDevice", selectedDevice);
263 
264  if (selectedDevice.empty())
265  {
266  MITK_ERROR << "Loaded data from persistence service is invalid (SelectedDevice:" << selectedDevice << "): aborted to restore data!";
267  return;
268  }
269 
270  MITK_INFO << "Successfully restored UI settings";
271  }
272  else
273  {
274  // QSettings as a fallback if the persistence service is not available
275  QSettings settings;
276  settings.beginGroup(QString::fromStdString(id));
277 
278  selectedDevice = settings.value("trackingDeviceChooser", "").toString().toStdString();
279 
280  settings.endGroup();
281  }
282 
283  // The selected device requires some checks because a device that is not installed should not be restored to avoid bugs.
284  // Use NDI Polaris as default if there's no widget registered for selected device or there's a widget for it but no device installed.
285 
286  const auto& deviceIterator = m_DeviceToWidgetIndexMap.find(selectedDevice);
287 
288  if (deviceIterator != m_DeviceToWidgetIndexMap.end())
289  {
291  dynamic_cast<QmitkAbstractTrackingDeviceWidget*>(m_Controls->m_TrackingSystemWidget->widget(deviceIterator->second));
292 
293  if (widget == nullptr || (widget != nullptr && !widget->IsDeviceInstalled()))
294  {
296  }
297  }
298  else
299  {
301  }
302 
303  const int index = m_Controls->m_TrackingDeviceChooser->findText(QString::fromStdString(selectedDevice));
304 
305  if (index >= 0)
306  {
307  m_Controls->m_TrackingDeviceChooser->setCurrentIndex(index);
308  }
309  else
310  {
311  MITK_ERROR << "Failed to load UI setting for tracking device configuration";
312  return;
313  }
314 
315  m_Controls->m_TrackingSystemWidget->setCurrentIndex(m_DeviceToWidgetIndexMap[selectedDevice]);
316 }
317 
318 std::string QmitkTrackingDeviceConfigurationWidget::GetCurrentDeviceName(void) const
319 {
320  return m_Controls->m_TrackingDeviceChooser->currentText().toStdString();
321 }
322 
323 QmitkAbstractTrackingDeviceWidget* QmitkTrackingDeviceConfigurationWidget::GetWidget(const std::string& deviceName) const
324 {
325  const auto& deviceIterator = m_DeviceToWidgetIndexMap.find(deviceName);
326 
327  if (deviceIterator != m_DeviceToWidgetIndexMap.end())
328  {
329  QWidget* widget = m_Controls->m_TrackingSystemWidget->widget(deviceIterator->second);
330 
331  return dynamic_cast<QmitkAbstractTrackingDeviceWidget*>(widget);
332  }
333 
334  return nullptr;
335 }
336 
338 {
339  this->GetWidget(this->GetCurrentDeviceName())->OnConnected(_success);
340 }
342 {
343  this->GetWidget(this->GetCurrentDeviceName())->OnDisconnected(_success);
344 }
345 
347 {
348  this->GetWidget(this->GetCurrentDeviceName())->OnStartTracking(_success);
349 }
351 {
352  this->GetWidget(this->GetCurrentDeviceName())->OnStopTracking(_success);
353 }
354 
356 {
357  this->GetWidget(this->GetCurrentDeviceName())->OnToolStorageChanged();
358 }
virtual void StoreUISettings()
Optional method to store and load settings of your widget (see QmitkNDIPolarisWidget) ...
virtual void ResetOutput()
Optional method to add output to a small screen in the trackingToolbox (see QmitkNDIPolarisWidget) ...
#define MITK_INFO
Definition: mitkLogMacros.h:18
void OnDisconnected(bool _success)
This function is called, when in the TrackingToolboxView "Disconnect" was clicked and the device is s...
#define MITK_ERROR
Definition: mitkLogMacros.h:20
This class is a collection for all TrackingDeviceWidgets (derived from AbstractTrackingDeviceWidget) ...
virtual void LoadUISettings()
Optional method to store and load settings of your widget (see QmitkNDIPolarisWidget) ...
This class is a collection for information of all Tracking Device Types (derived from abstract Tracki...
virtual void OnDisconnected(bool)
This function is called, when in the TrackingToolboxView "Disconnect" was clicked and the device is s...
Ui::QmitkTrackingDeviceConfigurationWidgetControls * m_Controls
virtual void OnToolStorageChanged()
This function is called, when anything in the ToolStorage changed, e.g. AddTool or EditTool...
void OnConnected(bool _success)
This function is called, when in the TrackingToolboxView "Connect" was clicked and the device is succ...
void LoadUISettings()
QmitkTrackingDeviceConfigurationWidget::LoadUISettings.
virtual void AddOutput(std::string)
Optional method to add output to a small screen in the trackingToolbox (see QmitkNDIPolarisWidget) ...
#define MITK_WARN
Definition: mitkLogMacros.h:19
void OnStopTracking(bool _success)
This function is called, when in the TrackingToolboxView "Stop Tracking" was clicked and the device s...
virtual void OnStartTracking(bool)
This function is called, when in the TrackingToolboxView "Start Tracking" was clicked and the device ...
QmitkTrackingDeviceConfigurationWidget(QWidget *parent=nullptr, Qt::WindowFlags f=nullptr)
void OnStartTracking(bool _success)
This function is called, when in the TrackingToolboxView "Start Tracking" was clicked and the device ...
virtual void CreateConnections()
Creation of the connections.
void OnToolStorageChanged()
This function is called, when anything in the ToolStorage changed, e.g. AddTool or EditTool...
virtual void OnConnected(bool)
This function is called, when in the TrackingToolboxView "Connect" was clicked and the device is succ...
virtual void OnStopTracking(bool)
This function is called, when in the TrackingToolboxView "Stop Tracking" was clicked and the device s...
Abstract class to configure a tracking device. Inherited widgets should be registered in the Microser...
mitk::IPropertyPersistence * GetPersistenceService()
virtual bool IsDeviceInstalled()
Optional method to investigate if drivers etc for your device are installed. The default value is "tr...
static ModuleContext * GetModuleContext()
Returns the module context of the calling module.
virtual mitk::TrackingDevice::Pointer GetTrackingDevice()=0