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