Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
QmitkToFConnectionWidget.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 
13 //#define _USE_MATH_DEFINES
15 
16 //QT headers
17 #include <qmessagebox.h>
18 #include <qfiledialog.h>
19 #include <qcombobox.h>
20 
21 //mitk headers
22 #include "mitkToFConfig.h"
23 #include "mitkCameraIntrinsics.h"
25 
26 //itk headers
27 #include <itksys/SystemTools.hxx>
28 
29 //Setting the View_ID
30 const std::string QmitkToFConnectionWidget::VIEW_ID = "org.mitk.views.qmitktofconnectionwidget2";
31 
32 //Constructor of QmitkToFConnectionWidget
33 QmitkToFConnectionWidget::QmitkToFConnectionWidget(QWidget* parent, Qt::WindowFlags f): QWidget(parent, f)
34 , m_Controls(nullptr)
35 , m_IntegrationTime(0)
36 , m_ModulationFrequency(0)
37 , m_SelectedCameraName("")
38 {
40  //Calling CreateQtPartControl
41  CreateQtPartControl(this);
42 }
43 
44 //Destructor of QmitkToFConnectionWidget
46 {
47  //MitkServiceListWidget must not be deinizialized here. Qmitk methods destroy their children automatically before self-destruction
48 }
49 
50 void QmitkToFConnectionWidget::CreateQtPartControl(QWidget *parent) //Definition of CreateQtPartControll-Methode in QmitkToFConnectionWidget; Input= Pointer
51 {
52  if (!m_Controls) //Define if not alreaddy exists
53  {
54  // create GUI widgets
55  m_Controls = new Ui::QmitkToFConnectionWidgetControls2;
56  m_Controls->setupUi(parent);
57  //and hide them on startup
58  this->HideAllParameterWidgets();
59 
60  // initzializing MitkServiceListWidget here
61  std::string empty= "";
62  m_Controls->m_DeviceList->Initialize<mitk::ToFCameraDevice>("ToFDeviceName", empty);// the empty could just be any kind of filter
63 
64  this->CreateConnections();
65  }
66 }
67 //Creating the SIGNAL-SLOT-Connectuions
69 {
70  if ( m_Controls )
71  {
72  //ConnectCameraButton as a trigger for OnConnectCamera()
73  connect( (QObject*)(m_Controls->m_ConnectCameraButton), SIGNAL(clicked()),(QObject*) this, SLOT(OnConnectCamera()) );
74 
75  //QmitkServiceListWidget::ServiceSelectionChanged as a Signal for the OnSlectCamera() slot
76  connect( m_Controls->m_DeviceList, SIGNAL(ServiceSelectionChanged(us::ServiceReferenceU)), this, SLOT(OnSelectCamera()));
77 
78  /*Creating an other Datanode structur for Kinect is done here: As soon as a Kinect is connected, the KinectParameterWidget is enabled,
79  which can be used to trigger the KinectAcqusitionModeChanged-Method, to create a working Data-Node-structure*/
80  connect( m_Controls->m_KinectParameterWidget, SIGNAL(AcquisitionModeChanged()), this, SIGNAL(KinectAcquisitionModeChanged()) );
81  }
82 }
83 
85 {
86  return m_ToFImageGrabber;
87 }
88 
89 //The OnSelectCamer-Method is in charge of activating the appropiate ParameterWidgets
91 {
92  //Here we are getting our decvie through the QmitkServiceListWidget-Instance m_DeviceList through the GetSelectedService-Method
93  mitk::ToFCameraDevice* device = m_Controls->m_DeviceList->GetSelectedService<mitk::ToFCameraDevice>();
94 
95  //getting the selectedCamera through a static Method used to transform the device->GetNameOfClass
96  QString selectedCamera = QString::fromStdString(device->GetNameOfClass());
97 
98  this->HideAllParameterWidgets();
99  //reactivating the Widgets on slecting a device
100  if (selectedCamera.contains("PMD")) //Check if selectedCamera string contains ".." for each device
101  {
102  this->m_Controls->m_PMDParameterWidget->show(); //and activate the correct widget
103  }
104  else if (selectedCamera.contains("MESA"))
105  {
106  this->m_Controls->m_MESAParameterWidget->show();
107  }
108  else if (selectedCamera.contains("Kinect"))
109  {
110  this->m_Controls->m_KinectParameterWidget->show();
111  }
112  else if (selectedCamera.contains("Structure"))
113  {
114  this->m_Controls->m_StructureParameterWidget->show();
115  }
116  m_Controls->m_ConnectCameraButton->setEnabled(true); //ConnectCameraButton gets enabled
117  m_SelectedCameraName = selectedCamera;
118 }
119 //This Methods hides all Widgets (later each widget is activated on its own)
120 void QmitkToFConnectionWidget::HideAllParameterWidgets()
121 {
122  this->m_Controls->m_PMDParameterWidget->hide();
123  this->m_Controls->m_MESAParameterWidget->hide();
124  this->m_Controls->m_KinectParameterWidget->hide();
125  this->m_Controls->m_StructureParameterWidget->hide();
126 }
127 
128 //OnConnectCamera-Method; represents one of the main parts of ToFConnectionWidget2.
130 {
131  //After connecting a device
132  if (m_Controls->m_ConnectCameraButton->text()=="Connect")
133  {
134  //Getting the device- and the slectedCamera-variables using the ServiceListWidget as we did it in the CameraSelect-Method
135  mitk::ToFCameraDevice* device = m_Controls->m_DeviceList->GetSelectedService<mitk::ToFCameraDevice>();
136  if (device)
137  {
138  QString tmpFileName("");
139  QString fileFilter("");
140  QString selectedCamera = QString::fromStdString(device->GetNameOfClass());
141 
142  emit ToFCameraSelected(selectedCamera);
143 
144  //Feeding it with the Info from ServiceListWidget
145  this->m_ToFImageGrabber->SetCameraDevice(device);
146 
147  if (selectedCamera.contains("Kinect") )
148  {
149  //If the particular property is selected, the suitable data-node will be generated
150  this->m_ToFImageGrabber->SetBoolProperty("RGB", m_Controls->m_KinectParameterWidget->IsAcquisitionModeRGB());
151  this->m_ToFImageGrabber->SetBoolProperty("IR", m_Controls->m_KinectParameterWidget->IsAcquisitionModeIR());
152  }
153 
154  if (selectedCamera.contains("Structure") )
155  {
156  this->m_ToFImageGrabber->SetIntProperty("RGBResolution", m_Controls->m_StructureParameterWidget->GetSelectedResolution());
157  this->m_ToFImageGrabber->SetIntProperty("DepthResolution", m_Controls->m_StructureParameterWidget->GetSelectedResolution());
158  }
159 
160  //Activation of "PlayerMode". If the selectedCamera String contains "Player", we start the Player Mode
161  if (selectedCamera.contains("Player"))
162  {
163  //IF PMD-Player selected
164  if (selectedCamera.contains("PMD"))
165  {
166  fileFilter.append("PMD Files (*.pmd)"); //And seting the corresponding fileFilter
167  }
168  else
169  {
170  fileFilter.append("NRRD Images (*.nrrd);;PIC Images - deprecated (*.pic)");
171  }
172 
173  //open a QFileDialog to chose the corresponding file from the disc
174  tmpFileName = QFileDialog::getOpenFileName(nullptr, "Play Image From...", "", fileFilter);
175 
176  //If no fileName is returned by the Dialog,Button and Widget have to return to default(disconnected) + Opening a MessageBox
177  if (tmpFileName.isEmpty())
178  {
179  m_Controls->m_ConnectCameraButton->setChecked(false);
180  m_Controls->m_ConnectCameraButton->setEnabled(true); //re-enabling the ConnectCameraButton
181  m_Controls->m_DeviceList->setEnabled(true); //Reactivating ServiceListWidget
182 
183  this->OnSelectCamera(); //Calling the OnSelctCamera-Method -> Hides all Widget and just activates the needed ones
184  QMessageBox::information( this, "Template functionality", "Please select a valid image before starting some action.");
185  return;
186  }
187 
188  if(selectedCamera.contains("PMDPlayer")) //If PMD-Player is selected, set ToFImageGrabberProperty correspondingly
189  {
190  this->m_ToFImageGrabber->SetStringProperty("PMDFileName", tmpFileName.toStdString().c_str() );
191  }
192  else //Default action
193  {
194  std::string msg = "";
195  try
196  {
197  //get 3 corresponding file names
198  std::string dir = itksys::SystemTools::GetFilenamePath( tmpFileName.toStdString() );
199  std::string baseFilename = itksys::SystemTools::GetFilenameWithoutLastExtension( tmpFileName.toStdString() );
200  std::string extension = itksys::SystemTools::GetFilenameLastExtension( tmpFileName.toStdString() );
201 
202  //"Incorrect format"-warning while using .nrrd or .pic files
203  if (extension != ".pic" && extension != ".nrrd")
204  {
205  msg = msg + "Invalid file format, please select a \".nrrd\"-file";
206  throw std::logic_error(msg.c_str());
207  }
208 
209  //Checking for npos. If available, check for the Amplitude-, Intensity- and RGBImage
210 
211  int found = baseFilename.rfind("_DistanceImage"); //Defining "found" variable+checking if baseFilname contains "_DistanceImage". If not, found == npos(0)
212 
213  if (found == static_cast<int>(std::string::npos)) //If found =0
214  {
215  found = baseFilename.rfind("_AmplitudeImage"); //If "_AmplitudeImage" is found, the found variable is 1-> the next if statment is false
216  }
217 
218  if (found == static_cast<int>(std::string::npos))
219  {
220  found = baseFilename.rfind("_IntensityImage"); //found = true if baseFilename cotains "_IntesityImage"
221  }
222 
223  if (found == static_cast<int>(std::string::npos))
224  {
225  found = baseFilename.rfind("_RGBImage");
226  }
227 
228  if (found == static_cast<int>(std::string::npos)) //If none of the Nodes is found, display an error
229  {
230  msg = msg + "Input file name must end with \"_DistanceImage\", \"_AmplitudeImage\", \"_IntensityImage\" or \"_RGBImage\"!";
231  throw std::logic_error(msg.c_str());
232  }
233 
234  std::string baseFilenamePrefix = baseFilename.substr(0,found);//Set the baseFilenamePrefix as a substring from baseFilname
235 
236  //Set corresponding FileNames
237  std::string distanceImageFileName = dir + "/" + baseFilenamePrefix + "_DistanceImage" + extension; //Set the name as: directory+FilenamePrefix+""+extension
238  std::string amplitudeImageFileName = dir + "/" + baseFilenamePrefix + "_AmplitudeImage" + extension;
239  std::string intensityImageFileName = dir + "/" + baseFilenamePrefix + "_IntensityImage" + extension;
240  std::string rgbImageFileName = dir + "/" + baseFilenamePrefix + "_RGBImage" + extension;
241 
242 
243  if (!itksys::SystemTools::FileExists(distanceImageFileName.c_str(), true))
244  {
245  this->m_ToFImageGrabber->SetStringProperty("DistanceImageFileName", "");
246  }
247  else
248  {
249  this->m_ToFImageGrabber->SetStringProperty("DistanceImageFileName", distanceImageFileName.c_str());
250  }
251  if (!itksys::SystemTools::FileExists(amplitudeImageFileName.c_str(), true))
252  {
253  }
254  else
255  {
256  this->m_ToFImageGrabber->SetStringProperty("AmplitudeImageFileName", amplitudeImageFileName.c_str());
257  }
258  if (!itksys::SystemTools::FileExists(intensityImageFileName.c_str(), true))
259  {
260  this->m_ToFImageGrabber->SetStringProperty("IntensityImageFileName", "");
261  }
262  else
263  {
264  this->m_ToFImageGrabber->SetStringProperty("IntensityImageFileName", intensityImageFileName.c_str());
265  }
266  if (!itksys::SystemTools::FileExists(rgbImageFileName.c_str(), true))
267  {
268  this->m_ToFImageGrabber->SetStringProperty("RGBImageFileName", "");
269  }
270  else
271  {
272  this->m_ToFImageGrabber->SetStringProperty("RGBImageFileName", rgbImageFileName.c_str());
273  }
274  }
275 
276  catch (std::exception &e)
277  {
278  MITK_ERROR << e.what();
279  QMessageBox::critical( this, "Error", e.what() );
280  m_Controls->m_ConnectCameraButton->setChecked(false);
281  m_Controls->m_ConnectCameraButton->setEnabled(true);
282  m_Controls->m_DeviceList->setEnabled(true);
283  this->OnSelectCamera();
284  return;
285  }
286  }
287  }
288  //End "PlayerMode"
289 
290  //Reset the ConnectCameraButton to disconnected
291  m_Controls->m_ConnectCameraButton->setText("Disconnect");
292 
293  //if a connection could be established
294  try
295  {
296 
297  if (this->m_ToFImageGrabber->ConnectCamera())
298  {
299  this->m_Controls->m_PMDParameterWidget->SetToFImageGrabber(this->m_ToFImageGrabber);
300  this->m_Controls->m_MESAParameterWidget->SetToFImageGrabber(this->m_ToFImageGrabber);
301  this->m_Controls->m_KinectParameterWidget->SetToFImageGrabber(this->m_ToFImageGrabber);
302  this->m_Controls->m_StructureParameterWidget->SetToFImageGrabber(this->m_ToFImageGrabber);
303 
304  //Activating the respective widgets
305  if (selectedCamera.contains("PMD"))
306  {
307  this->m_Controls->m_PMDParameterWidget->ActivateAllParameters();
308  }
309  else if (selectedCamera.contains("MESA"))
310  {
311  this->m_Controls->m_MESAParameterWidget->ActivateAllParameters();
312  }
313  else if (selectedCamera.contains("Kinect"))
314  {
315  this->m_Controls->m_KinectParameterWidget->ActivateAllParameters();
316  }
317  else if (selectedCamera.contains("Structure"))
318  {
319  this->m_Controls->m_StructureParameterWidget->ActivateAllParameters();
320  }
321  else
322  {
323  this->HideAllParameterWidgets();
324  }
325  // send connect signal to the caller functionality
326  emit ToFCameraConnected();
327  }
328  else
329  //##### TODO: Remove this else part once all controllers are throwing exceptions
330  //if they cannot to any device!
331  {
332  //Throw an error if the Connection failed and reset the Widgets <- better catch an exception!
333  QMessageBox::critical( this, "Error", "Connection failed. Check if you have installed the latest driver for your system." );
334  m_Controls->m_ConnectCameraButton->setChecked(false);
335  m_Controls->m_ConnectCameraButton->setEnabled(true);
336  m_Controls->m_ConnectCameraButton->setText("Connect");
337  m_Controls->m_DeviceList->setEnabled(true); //Reactivating ServiceListWidget
338  this->OnSelectCamera();
339  return;
340  }
341  }catch(std::exception &e)
342  {
343  //catch exceptions of camera which cannot connect give a better reason
344  QMessageBox::critical( this, "Connection failed.", e.what() );
345  m_Controls->m_ConnectCameraButton->setChecked(false);
346  m_Controls->m_ConnectCameraButton->setEnabled(true);
347  m_Controls->m_ConnectCameraButton->setText("Connect");
348  m_Controls->m_DeviceList->setEnabled(true); //Reactivating ServiceListWidget
349  this->OnSelectCamera();
350  return;
351  }
352  m_Controls->m_ConnectCameraButton->setEnabled(true);
353 
354  // ask wether camera parameters (intrinsics, ...) should be loaded
355  if (QMessageBox::question(this,"Camera parameters","Do you want to specify your own camera intrinsics?",QMessageBox::Yes,QMessageBox::No)==QMessageBox::Yes)
356  {
357  try {
358  QString fileName = QFileDialog::getOpenFileName(this,"Open camera intrinsics","/","*.xml");
359  mitk::CameraIntrinsics::Pointer cameraIntrinsics = mitk::CameraIntrinsics::New();
360  cameraIntrinsics->FromXMLFile(fileName.toStdString());
361  this->m_ToFImageGrabber->SetProperty("CameraIntrinsics",mitk::CameraIntrinsicsProperty::New(cameraIntrinsics));
362  } catch ( std::exception &e ) {
363  MITK_WARN << "Error loading camera intrinsics: " << e.what();
364  }
365  }
367  m_Controls->m_DeviceList->setEnabled(false); //Deactivating the Instance of QmitkServiceListWidget
368  //repaint the widget
369  this->repaint();
370  }
371  else
372  {
373  QMessageBox::information(this,"Camera connection","No camera selected, please select a range camera");
374  m_Controls->m_ConnectCameraButton->setChecked(false);
375  }
376  }
377  else if (m_Controls->m_ConnectCameraButton->text()=="Disconnect")
378  {
379  this->m_ToFImageGrabber->StopCamera();
380  this->m_ToFImageGrabber->DisconnectCamera();
381  m_Controls->m_ConnectCameraButton->setText("Connect");
382  m_Controls->m_DeviceList->setEnabled(true); //Reactivating ServiceListWidget
383  this->OnSelectCamera();
384 
385  // send disconnect signal to the caller functionality
386  emit ToFCameraDisconnected();
387  }
388 }
389 
391 {
392  this->m_Controls->m_ConnectCameraButton->animateClick();
393 }
#define MITK_ERROR
Definition: mitkLogMacros.h:20
Virtual interface and base class for all Time-of-Flight devices.
mitk::ToFImageGrabber::Pointer GetToFImageGrabber()
returns the ToFImageGrabber which was configured after selecting a camera / player ...
void OnConnectCamera()
slot called when the "Connect Camera" button was pressed According to the selection in the camera com...
virtual void CreateQtPartControl(QWidget *parent)
Ui::QmitkToFConnectionWidgetControls2 * m_Controls
member holding the UI elements of this widget
void OnSelectCamera()
slot updating the GUI elements after the selection of the camera combo box has changed ...
void ConnectCamera()
invokes the call of OnConnectCamera()
#define MITK_WARN
Definition: mitkLogMacros.h:19
void ToFCameraConnected()
This signal is sent if the user has connected the TOF camera. The ToFImageGrabber is now availiable i...
QString m_SelectedCameraName
member holding the name of the currently selected camera
mitk::ToFImageGrabber::Pointer m_ToFImageGrabber
member holding the current ToFImageGrabber
static const std::string VIEW_ID
static Pointer New()
QmitkToFConnectionWidget(QWidget *p=nullptr, Qt::WindowFlags f1=nullptr)
static Pointer New()
void ToFCameraSelected(const QString selectedText)
signal that is emitted when a ToF camera is selected
void ToFCameraDisconnected()
This signal is sent if the user has disconnect the TOF camera.