Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
QmitkIGTLoggerWidget.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 #include "QmitkIGTLoggerWidget.h"
18 
19 //mitk headers
20 #include "mitkTrackingTypes.h"
21 #include <mitkSurface.h>
27 #include <mitkStatusBar.h>
28 #include <mitkIOUtil.h>
29 
30 //itk headers
31 #include <itksys/SystemTools.hxx>
32 
33 //qt headers
34 #include <qfiledialog.h>
35 #include <qmessagebox.h>
36 #include <qtimer.h>
37 
38 
39 QmitkIGTLoggerWidget::QmitkIGTLoggerWidget(QWidget* parent, Qt::WindowFlags f)
40 : QWidget(parent, f), m_Recorder(NULL), m_RecordingActivated(false)
41 {
42  m_Controls = NULL;
43  CreateQtPartControl(this);
45 
46  //set output file
47  this->SetOutputFileName();
48 
49  //update milliseconds and samples
51 }
52 
53 
55 {
56  m_RecordingTimer->stop();
57  m_Recorder = NULL;
58  m_RecordingTimer = NULL;
59 }
60 
62 {
63  if (!m_Controls)
64  {
65  // create GUI widgets
66  m_Controls = new Ui::QmitkIGTLoggerWidgetControls;
67  m_Controls->setupUi(parent);
68 
69  m_RecordingTimer = new QTimer(this);
70  }
71 }
72 
74 {
75  if ( m_Controls )
76  {
77  connect( (QObject*)(m_Controls->m_pbLoadDir), SIGNAL(clicked()), this, SLOT(OnChangePressed()) );
78  connect( (QObject*)(m_Controls->m_pbStartRecording), SIGNAL(clicked(bool)), this, SLOT(OnStartRecording(bool)) );
79  connect( m_RecordingTimer, SIGNAL(timeout()), this, SLOT(OnRecording()) );
80  connect( (QObject*)(m_Controls->m_leRecordingValue), SIGNAL(editingFinished()), this, SLOT(UpdateRecordingTime()) );
81  connect( (QObject*)(m_Controls->m_cbRecordingType), SIGNAL(activated(int)), this, SLOT(UpdateRecordingTime()) );
82  connect( (QObject*)(m_Controls->m_leOutputFile), SIGNAL(editingFinished()), this, SLOT(UpdateOutputFileName()) );
83 
84  }
85 }
86 
88 {
89  m_DataStorage = dataStorage;
90 }
91 
93 {
94 
95  if (m_Recorder.IsNull())
96  {
97  QMessageBox::warning(NULL, "Warning", QString("Please start tracking before recording!"));
98  return;
99  }
100  if (m_CmpFilename.isEmpty())
101  {
102  QMessageBox::warning(NULL, "Warning", QString("Please specify filename!"));
103  return;
104  }
105 
106  if(recording)
107  {
108 
110  {
111  //m_Recorder->SetFileName(m_CmpFilename.toStdString());
112 
113  try
114  { /*start the recording mechanism */
115  m_Recorder->StartRecording();
116  m_RecordingTimer->start(50); //now every update of the recorder stores one line into the file for each added NavigationData
117  mitk::StatusBar::GetInstance()->DisplayText("Recording tracking data now"); // Display recording message for 75ms in status bar
118 
119  emit SignalRecordingStarted();
120  }
121  catch (std::exception& e)
122  {
123  QMessageBox::warning(NULL, "IGT-Tracking Logger: Error", QString("Error while recording tracking data: %1").arg(e.what()));
124  mitk::StatusBar::GetInstance()->DisplayText(""); // Display recording message for 75ms in status bar
125  }
126  m_Controls->m_pbStartRecording->setText("Stop recording");
127  m_Controls->m_leRecordingValue->setEnabled(false);
128  m_Controls->m_cbRecordingType->setEnabled(false);
129 
130  m_RecordingActivated = true;
131 
132  if(m_Controls->m_cbRecordingType->currentIndex()==0)
133  {
134  bool success = false;
135  QString str_ms = m_Controls->m_leRecordingValue->text();
136  int int_ms = str_ms.toInt(&success);
137  if (success)
138  QTimer::singleShot(int_ms, this, SLOT(StopRecording()));
139  }
140  }
141  else
142  {
143  this->StopRecording();
144  }
145 
146  }
147  else
148  {
149  this->StopRecording();
150  m_Controls->m_pbStartRecording->setChecked(false);
151  }
152 
153 }
154 
156 {
157  m_RecordingTimer->stop();
158  m_Recorder->StopRecording();
159  mitk::StatusBar::GetInstance()->DisplayText("Recording STOPPED", 2000); // Display message for 2s in status bar
160  m_Controls->m_pbStartRecording->setText("Start recording");
161  m_Controls->m_pbStartRecording->setChecked(false);
162  m_Controls->m_leRecordingValue->setEnabled(true);
163  m_Controls->m_cbRecordingType->setEnabled(true);
164  m_RecordingActivated = false;
165 
166  try
167  {
168  // write NavigationDataSet on StopRecording
169  mitk::IOUtil::SaveBaseData(m_Recorder->GetNavigationDataSet(), m_CmpFilename.toStdString());
170  }
171  catch(const std::exception &e)
172  {
173  // TODO: catch must be adapted when new file writer are merged to master
174  QMessageBox::warning(NULL, "IGT-Tracking Logger: Error", QString("Error while writing tracking data: %1").arg(e.what()));
175  MITK_WARN << "File could not be written.";
176  }
177 
178  emit SignalRecordingStopped();
179 }
180 
182 {
183  static unsigned int sampleCounter = 0;
184  unsigned int int_samples = m_Samples.toInt();
185  if(sampleCounter >= int_samples)
186  {
187  this->StopRecording();
188  sampleCounter=0;
189  return;
190  }
191  m_Recorder->Update();
192 
193  if (m_Controls->m_cbRecordingType->currentIndex()==1)
194  sampleCounter++;
195 }
196 
198 {
199  QString oldName = m_CmpFilename;
200  m_CmpFilename.clear();
201  m_CmpFilename = QFileDialog::getSaveFileName( QApplication::activeWindow()
202  , "Save tracking data", "IGT_Tracking_Data.xml", "XML files (*.xml)" );
203 
204  if (m_CmpFilename.isEmpty())//if something went wrong or user pressed cancel in the save dialog
205  {
206  m_CmpFilename=oldName;
207  }
208  m_Controls->m_leOutputFile->setText(m_CmpFilename);
209 }
210 
212 {
213  QString oldName = m_CmpFilename;
214  m_CmpFilename.clear();
215  m_CmpFilename = m_Controls->m_leOutputFile->text();
216  if (m_CmpFilename.isEmpty())
217  {
218  QMessageBox::warning(NULL, "Warning", QString("Please enter valid path! Using previous path again."));
219  m_CmpFilename=oldName;
220  m_Controls->m_leOutputFile->setText(m_CmpFilename);
221  }
222 }
223 
225 {
226  m_Recorder = recorder;
227 }
228 
229 
231 {
232  // milliseconds selected in the combobox
233  if (m_Controls->m_cbRecordingType->currentIndex()==0)
234  {
235  m_MilliSeconds = m_Controls->m_leRecordingValue->text();
236 
237  if(m_MilliSeconds.compare("infinite")==0)
238  {
240  }
241 
242  bool success = false;
243  m_MilliSeconds.toInt(&success);
244  if (!success)
245  {
246  QMessageBox::warning(NULL, "Warning", QString("Please enter a number!"));
248  return;
249  }
250  }
251  else if(m_Controls->m_cbRecordingType->currentIndex()==1) // #samples selected in the combobox
252  {
253  m_Samples = m_Controls->m_leRecordingValue->text();
254 
255  if(m_Samples.compare("infinite")==0)
256  {
258  }
259 
260  bool success = false;
261  m_Samples.toInt(&success);
262  if (!success)
263  {
264  QMessageBox::warning(NULL, "Warning", QString("Please enter a number!"));
266  return;
267  }
268  }
269  else if (m_Controls->m_cbRecordingType->currentIndex()==2)// infinite selected in the combobox
270  {
271  // U+221E unicode symbole for infinite
272  QString infinite("infinite");
273  m_Controls->m_leRecordingValue->setText(infinite);
274  }
275  // m_Controls->m_leSamples->setText(QString::number(samples));
276 }
277 
278 
280 {
281  m_Controls->m_leRecordingValue->setText("2000");
282  m_Controls->m_cbRecordingType->setCurrentIndex(0);
283  m_Samples="100";
284  m_MilliSeconds="2000";
285 }
286 
288 {
289  std::string tmpDir = itksys::SystemTools::GetCurrentWorkingDirectory();
290  QString dir = QString(tmpDir.c_str());
291  QString filename = "IGT_Tracking_Data.xml";
292  m_CmpFilename.append(dir);
293 
294  if(dir.isEmpty())
295  {
296  QMessageBox::warning(NULL, "Warning", QString("Could not load current working directory"));
297  return;
298  }
299  if(dir.endsWith("/")||dir.endsWith("\\"))
300  {
301  m_CmpFilename.append(filename);
302  }
303  else
304  {
305  m_CmpFilename.append("/");
306  m_CmpFilename.append(filename);
307  }
308  m_Controls->m_leOutputFile->setText(m_CmpFilename);
309 }
virtual void CreateConnections()
Creation of the connections.
void SetDataStorage(mitk::DataStorage *dataStorage)
Data management class that handles 'was created by' relations.
itk::SmartPointer< Self > Pointer
void SetRecorder(mitk::NavigationDataRecorder::Pointer recorder)
QmitkIGTLoggerWidget(QWidget *parent=0, Qt::WindowFlags f=0)
static bool SaveBaseData(mitk::BaseData *data, const std::string &path)
SaveBaseData Convenience method to save arbitrary baseData.
Definition: mitkIOUtil.cpp:888
mitk::DataStorage::Pointer m_DataStorage
holds the DataStorage
#define MITK_WARN
Definition: mitkLogMacros.h:23
void DisplayText(const char *t)
Send a string to the applications StatusBar.
static const std::string filename
void OnStartRecording(bool recording)
static StatusBar * GetInstance()
static method to get the GUI dependent StatusBar-instance so the methods DisplayText, etc. can be called No reference counting, cause of decentral static use!
virtual void CreateQtPartControl(QWidget *parent)
mitk::NavigationDataRecorder::Pointer m_Recorder
records NDs to a XML file
Ui::QmitkIGTLoggerWidgetControls * m_Controls