Medical Imaging Interaction Toolkit  2016.11.0
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,
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 
20 
21 #include <QSettings>
22 
23 const std::string QmitkTrackingDeviceConfigurationWidget::VIEW_ID = "org.mitk.views.trackingdeviceconfigurationwidget";
24 
26  : QWidget(parent, f)
27  , m_Controls(nullptr)
28  , m_TrackingDevice(nullptr)
29  , m_DeviceToWidgetIndexMap()
30 {
31  //initializations
32  CreateQtPartControl(this);
34 
36 
37  //initialize a few UI elements
38  AddOutput("<br>First Element selected"); //Order from Collection List
39 
40  //reset a few things
41  ResetOutput();
42 
43  //restore old UI settings
45 }
46 
48 {
50  delete m_Controls;
51  m_TrackingDevice = nullptr;
52 }
53 
55 {
56  if (!m_Controls)
57  {
58  // create GUI widgets
59  m_Controls = new Ui::QmitkTrackingDeviceConfigurationWidgetControls;
60  m_Controls->setupUi(parent);
61  }
62 }
63 
65 {
66  if (m_Controls)
67  {
68  connect((QObject*)(m_Controls->m_TrackingDeviceChooser), SIGNAL(currentIndexChanged(int)), this, SLOT(TrackingDeviceChanged()));
69  }
70 }
71 
73 {
74  const std::string currentDevice = this->GetCurrentDeviceName();
75 
76  //show the correspondig widget
77  m_Controls->m_TrackingSystemWidget->setCurrentIndex(m_DeviceToWidgetIndexMap[currentDevice]);
78 
79  //reset output
80  ResetOutput();
81 
82  AddOutput("<br>");
83  AddOutput(currentDevice);
84  AddOutput(" selected");
85 
86  QmitkAbstractTrackingDeviceWidget* widget = GetWidget(currentDevice);
87 
88  if (widget == nullptr || !widget->IsDeviceInstalled())
89  {
90  AddOutput("<br>ERROR: not installed!");
91  }
92 
94 }
95 
97 {
98  // clean-up of stacked widget, drop-down box and map
99  for (auto& item : m_DeviceToWidgetIndexMap)
100  {
101  m_Controls->m_TrackingSystemWidget->removeWidget(m_Controls->m_TrackingSystemWidget->widget(item.second));
102  MITK_INFO << "removing widget for device '" << item.first << "'";
103  }
104 
105  m_Controls->m_TrackingDeviceChooser->clear();
106 
107  m_DeviceToWidgetIndexMap.clear();
108 
109  // get tracking device type service references
110  us::ModuleContext* context = us::GetModuleContext();
111  std::vector<us::ServiceReference<mitk::TrackingDeviceTypeCollection> > deviceRefs =
112  context->GetServiceReferences<mitk::TrackingDeviceTypeCollection>();
113 
114  if (deviceRefs.empty())
115  {
116  MITK_ERROR << "No tracking device type service found!";
117  return;
118  }
119 
120  // get tracking device configuration widget service references
121  std::vector<us::ServiceReference<mitk::TrackingDeviceWidgetCollection> > widgetRefs =
122  context->GetServiceReferences<mitk::TrackingDeviceWidgetCollection>();
123 
124  if (widgetRefs.empty())
125  {
126  MITK_ERROR << "No tracking device configuration widget service found!";
127  return;
128  }
129 
130  const us::ServiceReference<mitk::TrackingDeviceTypeCollection>& deviceServiceReference = deviceRefs.front();
131  const us::ServiceReference<mitk::TrackingDeviceWidgetCollection>& widgetServiceReference = widgetRefs.front();
132 
133  mitk::TrackingDeviceTypeCollection* deviceTypeCollection =
134  context->GetService<mitk::TrackingDeviceTypeCollection>(deviceServiceReference);
135 
136  mitk::TrackingDeviceWidgetCollection* deviceWidgetCollection =
137  context->GetService<mitk::TrackingDeviceWidgetCollection>(widgetServiceReference);
138 
139  for (auto name : deviceTypeCollection->GetTrackingDeviceTypeNames())
140  {
141  // if the device is not included yet, add name to comboBox and widget to stackedWidget
142  if (m_Controls->m_TrackingDeviceChooser->findText(QString::fromStdString(name)) == -1)
143  {
144  m_Controls->m_TrackingDeviceChooser->addItem(QString::fromStdString(name));
145  QWidget* current = deviceWidgetCollection->GetTrackingDeviceWidgetClone(name);
146  if (current == nullptr)
147  {
148  MITK_WARN << "No widget for tracking device type " << name << " available. Please implement and register it!";
149  current = new QWidget();
150  }
151 
152  m_DeviceToWidgetIndexMap[name] = m_Controls->m_TrackingSystemWidget->addWidget(current);
153  }
154  }
155 
156  if (!m_DeviceToWidgetIndexMap.empty())
157  {
158  m_Controls->m_TrackingDeviceChooser->setCurrentIndex(0);
159  m_Controls->m_TrackingSystemWidget->setCurrentIndex(0);
160  }
161 
162  context->UngetService(deviceServiceReference);
163  context->UngetService(widgetServiceReference);
164 }
165 
166 //######################### internal help methods #######################################
168 {
169  QmitkAbstractTrackingDeviceWidget* currentWidget = this->GetWidget(this->GetCurrentDeviceName());
170 
171  if (currentWidget == nullptr)
172  {
173  return;
174  }
175 
176  currentWidget->ResetOutput();
177  currentWidget->repaint();
178 }
179 
181 {
182  QmitkAbstractTrackingDeviceWidget* currentWidget = this->GetWidget(this->GetCurrentDeviceName());
183 
184  if (currentWidget == nullptr)
185  {
186  return;
187  }
188 
189  currentWidget->AddOutput(s);
190  currentWidget->repaint();
191 }
192 
194 {
195  QmitkAbstractTrackingDeviceWidget* currentWidget = this->GetWidget(this->GetCurrentDeviceName());
196 
197  if (currentWidget == nullptr)
198  {
199  return nullptr;
200  }
201 
202  return currentWidget->ConstructTrackingDevice();
203 }
204 
206 {
208  if (m_TrackingDevice.IsNull() || !m_TrackingDevice->IsDeviceInstalled()) return nullptr;
209  else return this->m_TrackingDevice;
210 }
211 
213 {
214  std::string id = "org.mitk.modules.igt.ui.trackingdeviceconfigurationwidget";
215 
216  std::string selectedDevice = this->GetCurrentDeviceName();
217 
218  //Save settings for every widget
219  //Don't use m_DeviceTypeCollection here, it's already unregistered, when deconstructor is called...
220  for (int index = 0; index < m_Controls->m_TrackingSystemWidget->count(); index++)
221  {
222  QmitkAbstractTrackingDeviceWidget* widget = dynamic_cast<QmitkAbstractTrackingDeviceWidget*>(m_Controls->m_TrackingSystemWidget->widget(index));
223 
224  if (widget != nullptr)
225  {
226  widget->StoreUISettings();
227  }
228  }
229 
230  if (this->GetPersistenceService()) // now save the settings using the persistence service
231  {
232  mitk::PropertyList::Pointer propList = this->GetPersistenceService()->GetPropertyList(id);
233  propList->Set("SelectedDevice", selectedDevice);
234  }
235  else // QSettings as a fallback if the persistence service is not available
236  {
237  QSettings settings;
238  settings.beginGroup(QString::fromStdString(id));
239  settings.setValue("trackingDeviceChooser", QVariant(QString::fromStdString(selectedDevice)));
240  settings.endGroup();
241  }
242 }
243 
252 {
253  //Load settings for every widget
254  for (int index = 0; index < m_Controls->m_TrackingSystemWidget->count(); index++)
255  {
256  QmitkAbstractTrackingDeviceWidget* widget = dynamic_cast<QmitkAbstractTrackingDeviceWidget*>(m_Controls->m_TrackingSystemWidget->widget(index));
257 
258  if (widget != nullptr)
259  {
260  widget->LoadUISettings();
261  }
262  }
263 
264  std::string id = "org.mitk.modules.igt.ui.trackingdeviceconfigurationwidget";
265  std::string selectedDevice;
266  if (this->GetPersistenceService())
267  {
268  mitk::PropertyList::Pointer propList = this->GetPersistenceService()->GetPropertyList(id);
269  if (propList.IsNull())
270  {
271  MITK_ERROR << "Property list for this UI (" << id << ") is not available, could not load UI settings!"; return;
272  }
273 
274  propList->Get("SelectedDevice", selectedDevice);
275 
276  if (selectedDevice.empty())
277  {
278  MITK_ERROR << "Loaded data from persistence service is invalid (SelectedDevice:" << selectedDevice << "): aborted to restore data!";
279  return;
280  }
281 
282  MITK_INFO << "Successfully restored UI settings";
283  }
284  else
285  {
286  // QSettings as a fallback if the persistence service is not available
287  QSettings settings;
288  settings.beginGroup(QString::fromStdString(id));
289 
290  selectedDevice = settings.value("trackingDeviceChooser", "").toString().toStdString();
291 
292  settings.endGroup();
293  }
294 
295  // The selected device requires some checks because a device that is not installed should not be restored to avoid bugs.
296  // 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.
297 
298  const auto& deviceIterator = m_DeviceToWidgetIndexMap.find(selectedDevice);
299 
300  if (deviceIterator != m_DeviceToWidgetIndexMap.end())
301  {
303  dynamic_cast<QmitkAbstractTrackingDeviceWidget*>(m_Controls->m_TrackingSystemWidget->widget(deviceIterator->second));
304 
305  if (widget == nullptr || (widget != nullptr && !widget->IsDeviceInstalled()))
306  {
308  }
309  }
310  else
311  {
313  }
314 
315  const int index = m_Controls->m_TrackingDeviceChooser->findText(QString::fromStdString(selectedDevice));
316 
317  if (index >= 0)
318  {
319  m_Controls->m_TrackingDeviceChooser->setCurrentIndex(index);
320  }
321  else
322  {
323  MITK_ERROR << "Failed to load UI setting for tracking device configuration";
324  return;
325  }
326 
327  m_Controls->m_TrackingSystemWidget->setCurrentIndex(m_DeviceToWidgetIndexMap[selectedDevice]);
328 }
329 
330 std::string QmitkTrackingDeviceConfigurationWidget::GetCurrentDeviceName(void) const
331 {
332  return m_Controls->m_TrackingDeviceChooser->currentText().toStdString();
333 }
334 
335 QmitkAbstractTrackingDeviceWidget* QmitkTrackingDeviceConfigurationWidget::GetWidget(const std::string& deviceName) const
336 {
337  const auto& deviceIterator = m_DeviceToWidgetIndexMap.find(deviceName);
338 
339  if (deviceIterator != m_DeviceToWidgetIndexMap.end())
340  {
341  QWidget* widget = m_Controls->m_TrackingSystemWidget->widget(deviceIterator->second);
342 
343  return dynamic_cast<QmitkAbstractTrackingDeviceWidget*>(widget);
344  }
345 
346  return nullptr;
347 }
itk::SmartPointer< Self > Pointer
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:22
#define MITK_ERROR
Definition: mitkLogMacros.h:24
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...
Ui::QmitkTrackingDeviceConfigurationWidgetControls * m_Controls
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:23
virtual void CreateConnections()
Creation of the connections.
QmitkTrackingDeviceConfigurationWidget(QWidget *parent=0, Qt::WindowFlags f=0)
virtual mitk::TrackingDevice::Pointer ConstructTrackingDevice()=0
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.