Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
QmitkToFRecorderWidget.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
18 #include "QmitkToFRecorderWidget.h"
19 
20 //QT headers
21 #include <QMessageBox>
22 #include <QComboBox>
23 #include <QLabel>
24 #include <QLayout>
25 #include <QGridLayout>
26 #include <QCheckBox>
27 #include <QFileDialog>
28 
29 //mitk headers
30 #include <mitkToFImageWriter.h>
31 
32 //itk headers
33 #pragma GCC visibility push(default)
34 #include <itkEventObject.h>
35 #pragma GCC visibility pop
36 #include <itksys/SystemTools.hxx>
37 
38 struct QFileDialogArgs;
39 class QFileDialogPrivate;
40 
41 const std::string QmitkToFRecorderWidget::VIEW_ID = "org.mitk.views.qmitktofrecorderwidget";
42 
43 QmitkToFRecorderWidget::QmitkToFRecorderWidget(QWidget* parent, Qt::WindowFlags f): QWidget(parent, f)
44 {
45  this->m_ToFImageRecorder = NULL;
46  this->m_ToFImageGrabber = NULL;
48 
49  this-> m_Controls = NULL;
50  CreateQtPartControl(this);
51 }
52 
54 {
55 }
56 
58 {
59  if (!m_Controls)
60  {
61  // create GUI widgets
62  this->m_Controls = new Ui::QmitkToFRecorderWidgetControls;
63  this->m_Controls->setupUi(parent);
64  this->CreateConnections();
65  }
66 }
67 
69 {
70  if ( m_Controls )
71  {
72  connect( (QObject*)(m_Controls->m_PlayButton), SIGNAL(clicked()),(QObject*) this, SLOT(OnPlay()) );
73  connect( (QObject*)(m_Controls->m_StopButton), SIGNAL(clicked()),(QObject*) this, SLOT(OnStop()) );
74  connect( (QObject*)(m_Controls->m_StartRecordingButton), SIGNAL(clicked()),(QObject*) this, SLOT(OnStartRecorder()) );
75  connect( (QObject*)(m_Controls->m_RecordModeComboBox), SIGNAL(currentIndexChanged(int)),(QObject*) this, SLOT(OnChangeRecordModeComboBox(int)) );
76 
77  connect(this, SIGNAL(RecordingStopped()), this, SLOT(OnRecordingStopped()), Qt::BlockingQueuedConnection);
78  }
79 }
80 
82 {
83  this->m_ToFImageGrabber = toFImageGrabber;
84 
85  this->m_ToFImageRecorder = toFImageRecorder;
86 
89  this->m_ToFImageRecorder->RemoveAllObservers();
90  this->m_ToFImageRecorder->AddObserver(itk::AbortEvent(), this->m_StopRecordingCommand);
91 
92  m_Controls->m_PlayButton->setChecked(false);
93  m_Controls->m_PlayButton->setEnabled(true);
94  m_Controls->m_StartRecordingButton->setChecked(false);
95  m_Controls->m_RecorderGroupBox->setEnabled(true);
96 }
97 
99 {
100  emit RecordingStopped();
101 }
102 
104 {
105  m_Controls->m_PlayButton->setChecked(false);
106  m_Controls->m_PlayButton->setEnabled(true);
107  m_Controls->m_RecorderGroupBox->setEnabled(false);
108 }
109 
111 {
112  m_Controls->m_StartRecordingButton->setChecked(false);
113  m_Controls->m_RecorderGroupBox->setEnabled(true);
114 }
115 
117 {
118  StopCamera();
119  StopRecorder();
120 
122 
123  emit ToFCameraStopped();
124 }
125 
127 {
128  m_Controls->m_PlayButton->setChecked(true);
129  m_Controls->m_PlayButton->setEnabled(false);
130  m_Controls->m_RecorderGroupBox->setEnabled(true);
131  this->repaint();
132 
133  StartCamera();
134 
135  emit ToFCameraStarted();
136 }
137 
139 {
140  if (!m_ToFImageGrabber->IsCameraActive())
141  {
142  m_ToFImageGrabber->StartCamera();
143  }
144 }
145 
147 {
148  if( m_ToFImageGrabber.IsNotNull() )
149  m_ToFImageGrabber->StopCamera();
150 }
151 
153 {
154  if( m_ToFImageRecorder.IsNotNull() )
155  {
156  this->m_ToFImageRecorder->StopRecording();
157  }
158 }
159 
161 {
162  m_Controls->m_StartRecordingButton->setChecked(true);
163  m_Controls->m_RecorderGroupBox->setEnabled(false);
164  this->repaint();
165 
166  int numOfFrames = m_Controls->m_NumOfFramesSpinBox->value();
167  try
168  {
169  bool fileOK = true;
170  bool distanceImageSelected = true;
171  bool amplitudeImageSelected = false;
172  bool intensityImageSelected = false;
173  bool rgbImageSelected = false;
174  bool rawDataSelected = false;
175 
176  //Set check boxes in dialog according to device properties
177  m_ToFImageGrabber->GetCameraDevice()->GetBoolProperty("HasAmplitudeImage",amplitudeImageSelected);
178  m_ToFImageGrabber->GetCameraDevice()->GetBoolProperty("HasIntensityImage",intensityImageSelected);
179  m_ToFImageGrabber->GetCameraDevice()->GetBoolProperty("HasRGBImage",rgbImageSelected);
180 
181  QString tmpFileName("");
182  QString selectedFilter("");
183  QString imageFileName("");
185  tmpFileName = QmitkToFRecorderWidget::getSaveFileName(tofImageType,
186  distanceImageSelected, amplitudeImageSelected, intensityImageSelected, rgbImageSelected, rawDataSelected,
187  NULL, "Save Image To...", imageFileName, "NRRD Images (*.nrrd);;PIC Images - deprecated (*.pic);;Text (*.csv)", &selectedFilter);
188 
189  if (tmpFileName.isEmpty())
190  {
191  fileOK = false;
192  }
193  else
194  {
195  imageFileName = tmpFileName;
196  }
197 
198  if (fileOK)
199  {
200  std::string dir = itksys::SystemTools::GetFilenamePath( imageFileName.toStdString() );
201  std::string baseFilename = itksys::SystemTools::GetFilenameWithoutLastExtension( imageFileName.toStdString() );
202  std::string extension = itksys::SystemTools::GetFilenameLastExtension( imageFileName.toStdString() );
203 
204  int integrationTime = this->m_ToFImageGrabber->GetIntegrationTime();
205  int modulationFreq = this->m_ToFImageGrabber->GetModulationFrequency();
206 
207  QString integrationTimeStr;
208  integrationTimeStr.setNum(integrationTime);
209  QString modulationFreqStr;
210  modulationFreqStr.setNum(modulationFreq);
211  QString numOfFramesStr("");
213  {
214  numOfFramesStr.setNum(numOfFrames);
215  }
216 
217  std::string distImageFileName = prepareFilename(dir, baseFilename, modulationFreqStr.toStdString(),
218  integrationTimeStr.toStdString(), numOfFramesStr.toStdString(), extension, "_DistanceImage");
219  MITK_INFO << "Save distance data to: " << distImageFileName;
220 
221  std::string amplImageFileName = prepareFilename(dir, baseFilename, modulationFreqStr.toStdString(),
222  integrationTimeStr.toStdString(), numOfFramesStr.toStdString(), extension, "_AmplitudeImage");
223  MITK_INFO << "Save amplitude data to: " << amplImageFileName;
224 
225  std::string intenImageFileName = prepareFilename(dir, baseFilename, modulationFreqStr.toStdString(),
226  integrationTimeStr.toStdString(), numOfFramesStr.toStdString(), extension, "_IntensityImage");
227  MITK_INFO << "Save intensity data to: " << intenImageFileName;
228 
229  std::string rgbImageFileName = prepareFilename(dir, baseFilename, modulationFreqStr.toStdString(),
230  integrationTimeStr.toStdString(), numOfFramesStr.toStdString(), extension, "_RGBImage");
231  MITK_INFO << "Save intensity data to: " << rgbImageFileName;
232 
233  if (selectedFilter.compare("Text (*.csv)") == 0)
234  {
235  this->m_ToFImageRecorder->SetFileFormat(".csv");
236  }
237  else if (selectedFilter.compare("PIC Images - deprecated (*.pic)") == 0)
238  {
239  //default
240  this->m_ToFImageRecorder->SetFileFormat(".pic");
241 
242  QMessageBox::warning(NULL, "Deprecated File Format!",
243  "Please note that *.pic file format is deprecated and not longer supported! The suggested file format for images is *.nrrd!");
244  }
245  else if (selectedFilter.compare("NRRD Images (*.nrrd)") == 0)
246  {
247  this->m_ToFImageRecorder->SetFileFormat(".nrrd");
248  }
249  else
250  {
251  QMessageBox::warning(NULL, "Unsupported file format!", "Please specify one of the supported file formats *.nrrd, *.csv!");
252  return;
253  }
254 
255  numOfFrames = m_Controls->m_NumOfFramesSpinBox->value();
256  this->m_ToFImageRecorder->SetDistanceImageFileName(distImageFileName);
257  this->m_ToFImageRecorder->SetAmplitudeImageFileName(amplImageFileName);
258  this->m_ToFImageRecorder->SetIntensityImageFileName(intenImageFileName);
259  this->m_ToFImageRecorder->SetRGBImageFileName(rgbImageFileName);
260  this->m_ToFImageRecorder->SetToFImageType(tofImageType);
261  this->m_ToFImageRecorder->SetDistanceImageSelected(distanceImageSelected);
262  this->m_ToFImageRecorder->SetAmplitudeImageSelected(amplitudeImageSelected);
263  this->m_ToFImageRecorder->SetIntensityImageSelected(intensityImageSelected);
264  this->m_ToFImageRecorder->SetRGBImageSelected(rgbImageSelected);
265  this->m_ToFImageRecorder->SetRecordMode(this->m_RecordMode);
266  this->m_ToFImageRecorder->SetNumOfFrames(numOfFrames);
267 
268  emit RecordingStarted();
269  this->m_ToFImageRecorder->StartRecording();
270  }
271  else
272  {
273  this->OnRecordingStopped();
274  }
275  }
276  catch(std::exception& e)
277  {
278  QMessageBox::critical(NULL, "Error", QString(e.what()));
279  this->OnRecordingStopped();
280  }
281 }
282 
284  bool& distanceImageSelected,
285  bool& amplitudeImageSelected,
286  bool& intensityImageSelected,
287  bool& rgbImageSelected,
288  bool& rawDataSelected,
289  QWidget *parent,
290  const QString &caption,
291  const QString &dir,
292  const QString &filter,
293  QString *selectedFilter,
294  QFileDialog::Options options
295  )
296 {
297  QString selectedFileName = "";
298  QComboBox* combo = new QComboBox;
299  combo->addItem("3D");
300  combo->addItem("2D + t");
301 
302  QHBoxLayout* checkBoxGroup = new QHBoxLayout();
303 
304  QCheckBox* distanceImageCheckBox = new QCheckBox;
305  distanceImageCheckBox->setText("Distance image");
306  distanceImageCheckBox->setChecked(distanceImageSelected);
307 
308  QCheckBox* amplitudeImageCheckBox = new QCheckBox;
309  amplitudeImageCheckBox->setText("Amplitude image");
310  amplitudeImageCheckBox->setChecked(amplitudeImageSelected);
311  amplitudeImageCheckBox->setEnabled(amplitudeImageSelected);
312  if(!amplitudeImageSelected)
313  amplitudeImageCheckBox->setToolTip(QString("This device does not provide amplitude data."));
314 
315  QCheckBox* intensityImageCheckBox = new QCheckBox;
316  intensityImageCheckBox->setText("Intensity image");
317  intensityImageCheckBox->setChecked(intensityImageSelected);
318  intensityImageCheckBox->setEnabled(intensityImageSelected);
319  if(!intensityImageSelected)
320  intensityImageCheckBox->setToolTip(QString("This device does not provide intensity data."));
321 
322  QCheckBox* rgbImageCheckBox = new QCheckBox;
323  rgbImageCheckBox->setText("RGB image");
324  rgbImageCheckBox->setChecked(rgbImageSelected);
325  rgbImageCheckBox->setEnabled(rgbImageSelected);
326  if(!rgbImageSelected)
327  rgbImageCheckBox->setToolTip(QString("This device does not provide RGB data."));
328 
329  QCheckBox* rawDataCheckBox = new QCheckBox;
330  rawDataCheckBox->setText("Raw data");
331  rawDataCheckBox->setChecked(false);
332  rawDataCheckBox->setEnabled(false);
333 
334  checkBoxGroup->addWidget(distanceImageCheckBox);
335  checkBoxGroup->addWidget(amplitudeImageCheckBox);
336  checkBoxGroup->addWidget(intensityImageCheckBox);
337  checkBoxGroup->addWidget(rgbImageCheckBox);
338  checkBoxGroup->addWidget(rawDataCheckBox);
339 
340  QFileDialog* fileDialog = new QFileDialog(parent, caption, dir, filter);
341 
342  QLayout* layout = fileDialog->layout();
343  QGridLayout* gridbox = qobject_cast<QGridLayout*>(layout);
344 
345  if (gridbox)
346  {
347  gridbox->addWidget(new QLabel("ToF-Image type:"));
348  gridbox->addWidget(combo);
349  int lastRow = gridbox->rowCount();
350  gridbox->addLayout(checkBoxGroup, lastRow, 0, 1, -1);
351  }
352 
353  fileDialog->setLayout(gridbox);
354  fileDialog->setAcceptMode(QFileDialog::AcceptSave);
355 
356  if (selectedFilter)
357  {
358  fileDialog->selectNameFilter(*selectedFilter);
359  }
360 
361  if (fileDialog->exec() == QDialog::Accepted)
362  {
363  if (selectedFilter)
364  {
365  *selectedFilter = fileDialog->selectedNameFilter();
366  }
367 
368  if (combo->currentIndex() == 0)
369  {
371  }
372 
373  else
374  {
376  }
377 
378  distanceImageSelected = distanceImageCheckBox->isChecked();
379  amplitudeImageSelected = amplitudeImageCheckBox->isChecked();
380  intensityImageSelected = intensityImageCheckBox->isChecked();
381  rgbImageSelected = rgbImageCheckBox->isChecked();
382  rawDataSelected = rawDataCheckBox->isChecked();
383 
384  selectedFileName = fileDialog->selectedFiles().value(0);
385  }
386 
387  delete fileDialog;
388  return selectedFileName;
389 }
390 
391 std::string QmitkToFRecorderWidget::prepareFilename(std::string dir,
392  std::string baseFilename,
393  std::string modulationFreq,
394  std::string integrationTime,
395  std::string numOfFrames,
396  std::string extension,
397  std::string imageType)
398 {
399  std::string filenName("");
400  filenName.append(dir);
401  filenName.append("/");
402  filenName.append(baseFilename);
403  filenName.append("_MF");
404  filenName.append(modulationFreq);
405  filenName.append("_IT");
406  filenName.append(integrationTime);
407  filenName.append("_");
408  filenName.append(numOfFrames);
409  filenName.append("Images");
410  filenName.append(imageType);
411  filenName.append(extension);
412  return filenName;
413 }
414 
416 {
417  if (index == 0)
418  {
420  m_Controls->m_NumOfFramesSpinBox->setEnabled(true);
421  }
422  else
423  {
425  m_Controls->m_NumOfFramesSpinBox->setEnabled(false);
426  }
427 }
#define MITK_INFO
Definition: mitkLogMacros.h:22
void ToFCameraStarted()
signal emitted when "Play" button is pressed
void ToFCameraStopped()
signal emitted when "Stop" button is pressed
void OnStartRecorder()
slot invoking to start the recording After letting the user chose a file location for the record...
void StopRecorder()
stops the recording by calling ToFImageRecorder::StopRecording()
mitk::ToFImageRecorder::RecordMode m_RecordMode
member holding the RecordMode of the recorder (PerFrame / Infinite)
void OnRecordingStopped()
slot resetting the GUI elements of the recording box
void StopCamera()
stops the camera by calling ToFImageGrabber::StopCamera()
CommandType::Pointer m_StopRecordingCommand
itkCommand for abort of recording
QmitkToFRecorderWidget(QWidget *p=0, Qt::WindowFlags f1=0)
Recorder class for ToF images.
virtual void CreateQtPartControl(QWidget *parent)
void SetParameter(mitk::ToFImageGrabber *ToFImageGrabber, mitk::ToFImageRecorder *toFImageRecorder)
Set the parameters used for this widget.
void OnStop()
slot invoking to stop the camera and the recorder. Calls StopCamera() and StopRecorder and emits ToFC...
static QString getSaveFileName(mitk::ToFImageWriter::ToFImageType &tofImageType, bool &distanceImageSelected, bool &amplitudeImageSelected, bool &intensityImageSelected, bool &rgbImageSelected, bool &rawDataSelected, QWidget *parent=0, const QString &caption=QString(), const QString &dir=QString(), const QString &filter=QString(), QString *selectedFilter=0, QFileDialog::Options options=0)
adapted version of QFileDialog::getSaveFileName() The user is now asked to choose which images he wan...
Ui::QmitkToFRecorderWidgetControls * m_Controls
member holding the UI elements of this widget
void StopRecordingCallback()
emits RecordingStopped signal.
void StartCamera()
starts the camera by calling ToFImageGrabber::StartCamera()
void RecordingStopped()
signal emitted AbortEvent() in ToFImageRecorder is observed
void OnPlay()
slot invoking to start the camera. Calls StartCamera() and emits ToFCameraStarted signal ...
Image source providing ToF images. Interface for filters provided in ToFProcessing module...
std::string prepareFilename(std::string dir, std::string baseFilename, std::string modulationFreq, std::string integrationTime, std::string numOfFrames, std::string extension, std::string imageType)
method creating a filename from the given information
static const std::string VIEW_ID
void RecordingStarted()
signal emitted when recording is started
void OnChangeRecordModeComboBox(int index)
slot activating/deactivating "number of frames" spin box dependent on recording mode (PerFrame / Infi...
mitk::ToFImageRecorder::Pointer m_ToFImageRecorder
member holding the recorder for ToF images
mitk::ToFImageGrabber::Pointer m_ToFImageGrabber
member holding the ToFImageGrabber for acquiring ToF images
void ResetGUIToInitial()
resets the GUI elements to the initial state. Play button: enabled, Stop button: disabled, Recording box: disabled
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.