Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
QmitkIGTConnectionWidget.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 
19 
21 #include "mitkNDITrackingDevice.h"
23 
26 
27 #include <QFileDialog>
28 #include <QMessageBox>
29 
30 const std::string QmitkIGTConnectionWidget::VIEW_ID = "org.mitk.views.igtconnectionwidget";
31 
32 QmitkIGTConnectionWidget::QmitkIGTConnectionWidget(QWidget* parent, Qt::WindowFlags f)
33  : QWidget(parent, f)
34 {
35  m_Controls = NULL;
36  CreateQtPartControl(this);
38  m_TrackingDevice = NULL;
41  m_DataStorage = NULL;
42  m_ErrorMessage = "";
43 }
44 
45 
47 {
48 }
49 
51 {
52  if (!m_Controls)
53  {
54  // create GUI widgets
55  m_Controls = new Ui::QmitkIGTConnectionWidgetControls;
56  m_Controls->setupUi(parent);
57  }
58 }
59 
61 {
62  if ( m_Controls )
63  {
64  connect( (QObject*)(m_Controls->connectButton), SIGNAL(clicked()), this, SLOT(OnConnect()) );
65  }
66 }
67 
69 {
70  if (m_Controls->connectButton->isChecked()) // Load tools and connect tracking device
71  {
72  m_Controls->connectButton->setChecked(false);
73  // create TrackingDevice
74  m_TrackingDevice = m_Controls->trackingDeviceConfigurationWidget->GetTrackingDevice();
75  if (m_TrackingDevice.IsNotNull())
76  {
77  QString fileName = QFileDialog::getOpenFileName(NULL,tr("Open Navigation tool storage"), "/", tr("Toolfile (*.tfl)"));
78  if (LoadToolfile(fileName))
79  {
80  // Create TrackingDeviceSource and add tools
81  mitk::TrackingDeviceSourceConfigurator::Pointer myTrackingDeviceSourceFactory =
83  m_TrackingDeviceSource = myTrackingDeviceSourceFactory->CreateTrackingDeviceSource();
84  m_TrackingDeviceSource->Connect();
85  m_TrackingDeviceSource->StartTracking();
86  // change button text
87  m_Controls->connectButton->setText("Disconnect");
88  m_Controls->connectButton->setChecked(true);
89  // disable configuration widget
90  m_Controls->trackingDeviceConfigurationWidget->setEnabled(false);
91  // emit connected signal
93  }
94  else
95  {
96  QString error(m_ErrorMessage.c_str());
97  QMessageBox::warning(NULL,"Warning",error);
98  // reset button to unchecked
99  m_Controls->connectButton->setChecked(false);
100  // remove tool nodes from DataStorage
101  this->RemoveToolNodes();
102  // reset NavigationToolStorage
104  }
105  }
106  else
107  {
108  // reset button to unchecked
109  m_Controls->connectButton->setChecked(false);
110  MITK_ERROR<<"Could not create TrackingDevice";
111  }
112  }
113  else // Disconnect tracking device
114  {
115  // disconnect TrackingDeviceSource
116  if (m_TrackingDeviceSource.IsNotNull())
117  {
118  m_TrackingDeviceSource->StopTracking();
119  m_TrackingDeviceSource->Disconnect();
120  }
121  // remove tool nodes from DataStorage
122  this->RemoveToolNodes();
123  // reset members
125  m_TrackingDevice = NULL;
126  m_TrackingDeviceSource = NULL;
127  // change button text
128  m_Controls->connectButton->setText("Connect");
129  // enable configuration widget
130  m_Controls->trackingDeviceConfigurationWidget->setEnabled(true);
131  // emit disconnected signal
133  }
134 }
135 
137 {
138  if (m_DataStorage.IsNotNull())
139  {
140  std::string filename = qFilename.toStdString();
142  mitk::NavigationToolStorage::Pointer tempStorage = myDeserializer->Deserialize(filename);
143  m_NavigationToolStorage = tempStorage;
144 
145  if (tempStorage.IsNull())
146  {
147  m_ErrorMessage = myDeserializer->GetErrorMessage();
148  return false;
149  }
150 
151  // check if there are tools in the storage
152  mitk::TrackingDeviceType lastDevice;
153  if (tempStorage->GetToolCount()>0)
154  {
155  lastDevice = tempStorage->GetTool(0)->GetTrackingDeviceType();
156  }
157  else
158  {
159  m_ErrorMessage = "Error: Didn't find a tool in the storage. Do you want to navigate without even an instrument?";
160  return false;
161  }
162  //check if all tools are from the same device
163  for (int i=1; i<tempStorage->GetToolCount(); i++)
164  {
165  if (lastDevice!=tempStorage->GetTool(i)->GetTrackingDeviceType())
166  {
167  m_ErrorMessage = "Error: Toolfile contains tools of different tracking devices which is not acceptable for this application.";
168  return false;
169  }
170  else lastDevice = tempStorage->GetTool(i)->GetTrackingDeviceType();
171  }
172  // check if tracking device typ of tools corresponds with chosen tracking device
173  if (m_TrackingDevice->GetType()!=tempStorage->GetTool(0)->GetTrackingDeviceType())
174  {
175  m_ErrorMessage = "Tools are not compliant with this tracking device. Please use correct toolfile for specified device.";
176  return false;
177  }
178  m_NavigationToolStorage = tempStorage;
179  return true;
180  }
181  else
182  {
183  m_ErrorMessage = "Error: No DataStorage available! Make sure the widget is initialized with a DataStorage";
184  return false;
185  }
186 }
187 
189 {
190  for (int i=0; i<m_NavigationToolStorage->GetToolCount(); i++)
191  {
192  mitk::DataNode::Pointer currentNode = m_NavigationToolStorage->GetTool(i)->GetDataNode();
193  if (currentNode.IsNotNull())
194  {
195  m_DataStorage->Remove(currentNode);
196  }
197  }
198 }
199 
201 {
202  return m_TrackingDeviceSource;
203 }
204 
206 {
207  m_DataStorage = dataStorage;
208 }
209 
211 {
213 }
itk::SmartPointer< Self > Pointer
Ui::QmitkIGTConnectionWidgetControls * m_Controls
virtual void CreateQtPartControl(QWidget *parent)
mitk::DataStorage::Pointer m_DataStorage
data storage to put navigation tools
#define MITK_ERROR
Definition: mitkLogMacros.h:24
void OnConnect()
Asks the user to specify a tool file and finally connects the TrackingDeviceSource.
void SetDataStorage(mitk::DataStorage::Pointer dataStorage)
set DataStorage that is used to put the navigation tools
std::string m_ErrorMessage
current problem description
static const std::string filename
void TrackingDeviceDisconnected()
signal emitted when TrackingDevice was successfully disconnected
QmitkIGTConnectionWidget(QWidget *parent=0, Qt::WindowFlags f=0)
mitk::TrackingDeviceSource::Pointer m_TrackingDeviceSource
holds the preconfigured source of the IGT pipeline which is provided by this widget for further proce...
mitk::TrackingDeviceSource::Pointer GetTrackingDeviceSource()
void TrackingDeviceConnected()
signal emitted when TrackingDevice was successfully connected
mitk::NavigationToolStorage::Pointer GetNavigationToolStorage()
Get the NavigationToolStorage holding all tools with corresponding surface objects.
mitk::NavigationToolStorage::Pointer m_NavigationToolStorage
holds all navigation tools currently loaded
std::string TrackingDeviceType
static const std::string VIEW_ID
void RemoveToolNodes()
Remove the tool nodes currently associated to the tools hold in the NavigationToolStorage from the Da...
bool LoadToolfile(QString qFilename)
Load NavigationToolStorage from given filename and set according member.
virtual void CreateConnections()
Creation of the connections.
mitk::TrackingDevice::Pointer m_TrackingDevice
tracking device currently connected
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.