Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "QmitkIGTLoggerWidget.h"
00019
00020
00021 #include "mitkTrackingTypes.h"
00022 #include <mitkSTLFileReader.h>
00023 #include <mitkSurface.h>
00024 #include <mitkNavigationToolReader.h>
00025 #include <mitkNavigationToolWriter.h>
00026 #include <mitkNavigationToolStorage.h>
00027 #include <mitkNavigationToolStorageDeserializer.h>
00028 #include <mitkNavigationToolStorageSerializer.h>
00029 #include <mitkStatusBar.h>
00030
00031 #include <itksys/SystemTools.hxx>
00032
00033
00034 #include <qfiledialog.h>
00035 #include <qmessagebox.h>
00036 #include <qtimer.h>
00037
00038
00039 QmitkIGTLoggerWidget::QmitkIGTLoggerWidget(QWidget* parent, Qt::WindowFlags f)
00040 : QWidget(parent, f), m_Recorder(NULL), m_RecordingActivated(false)
00041 {
00042 m_Controls = NULL;
00043 CreateQtPartControl(this);
00044 CreateConnections();
00045
00046
00047 this->SetOutputFileName();
00048
00049
00050 this->SetDefaultRecordingSettings();
00051 }
00052
00053
00054 QmitkIGTLoggerWidget::~QmitkIGTLoggerWidget()
00055 {
00056 m_RecordingTimer->stop();
00057 m_Recorder = NULL;
00058 m_RecordingTimer = NULL;
00059 }
00060
00061 void QmitkIGTLoggerWidget::CreateQtPartControl(QWidget *parent)
00062 {
00063 if (!m_Controls)
00064 {
00065
00066 m_Controls = new Ui::QmitkIGTLoggerWidgetControls;
00067 m_Controls->setupUi(parent);
00068
00069 m_RecordingTimer = new QTimer(this);
00070 }
00071 }
00072
00073 void QmitkIGTLoggerWidget::CreateConnections()
00074 {
00075 if ( m_Controls )
00076 {
00077 connect( (QObject*)(m_Controls->m_pbLoadDir), SIGNAL(clicked()), this, SLOT(OnChangePressed()) );
00078 connect( (QObject*)(m_Controls->m_pbStartRecording), SIGNAL(clicked()), this, SLOT(OnStartRecording()) );
00079 connect( m_RecordingTimer, SIGNAL(timeout()), this, SLOT(OnRecording()) );
00080 connect( (QObject*)(m_Controls->m_leRecordingValue), SIGNAL(editingFinished()), this, SLOT(UpdateRecordingTime()) );
00081 connect( (QObject*)(m_Controls->m_cbRecordingType), SIGNAL(activated(int)), this, SLOT(UpdateRecordingTime()) );
00082 connect( (QObject*)(m_Controls->m_leOutputFile), SIGNAL(editingFinished()), this, SLOT(UpdateOutputFileName()) );
00083
00084 }
00085 }
00086
00087 void QmitkIGTLoggerWidget::SetDataStorage(mitk::DataStorage* dataStorage)
00088 {
00089 m_DataStorage = dataStorage;
00090 }
00091
00092 void QmitkIGTLoggerWidget::OnStartRecording()
00093 {
00094
00095 if (m_Recorder.IsNull())
00096 {
00097 QMessageBox::warning(NULL, "Warning", QString("Please start tracking before recording!"));
00098 return;
00099 }
00100 if (m_CmpFilename.isEmpty())
00101 {
00102 QMessageBox::warning(NULL, "Warning", QString("Please specify filename without extension!"));
00103 return;
00104 }
00105
00106 if (!m_RecordingActivated)
00107 {
00108 m_Recorder->SetFileName(m_CmpFilename.toStdString());
00109
00110 try
00111 {
00112 m_Recorder->StartRecording();
00113 m_RecordingTimer->start(50);
00114 mitk::StatusBar::GetInstance()->DisplayText("Recording tracking data now");
00115 }
00116 catch (std::exception& e)
00117 {
00118 QMessageBox::warning(NULL, "IGT-Tracking Logger: Error", QString("Error while recording tracking data: %1").arg(e.what()));
00119 mitk::StatusBar::GetInstance()->DisplayText("");
00120 }
00121 m_Controls->m_pbStartRecording->setText("Stop recording");
00122 m_Controls->m_leRecordingValue->setEnabled(false);
00123 m_Controls->m_cbRecordingType->setEnabled(false);
00124
00125 m_RecordingActivated = true;
00126
00127 if(m_Controls->m_cbRecordingType->currentIndex()==0)
00128 {
00129 bool success = false;
00130 QString str_ms = m_Controls->m_leRecordingValue->text();
00131 int int_ms = str_ms.toInt(&success);
00132 if (success)
00133 QTimer::singleShot(int_ms, this, SLOT(StopRecording()));
00134 }
00135 }
00136 else
00137 {
00138 this->StopRecording();
00139 }
00140
00141 }
00142
00143 void QmitkIGTLoggerWidget::StopRecording()
00144 {
00145 m_RecordingTimer->stop();
00146 m_Recorder->StopRecording();
00147 mitk::StatusBar::GetInstance()->DisplayText("Recording STOPPED", 2000);
00148 m_Controls->m_pbStartRecording->setText("Start recording");
00149 m_Controls->m_leRecordingValue->setEnabled(true);
00150 m_Controls->m_cbRecordingType->setEnabled(true);
00151 m_RecordingActivated = false;
00152 }
00153
00154 void QmitkIGTLoggerWidget::OnRecording()
00155 {
00156 static unsigned int sampleCounter = 0;
00157 unsigned int int_samples = m_Samples.toInt();
00158 if(sampleCounter >= int_samples)
00159 {
00160 this->StopRecording();
00161 sampleCounter=0;
00162 return;
00163 }
00164 m_Recorder->Update();
00165
00166 if (m_Controls->m_cbRecordingType->currentIndex()==1)
00167 sampleCounter++;
00168 }
00169
00170 void QmitkIGTLoggerWidget::OnChangePressed()
00171 {
00172 QString oldName = m_CmpFilename;
00173 m_CmpFilename.clear();
00174 m_CmpFilename = QFileDialog::getSaveFileName( QApplication::activeWindow()
00175 , "Save tracking data", "IGT_Tracking_Data.xml", "XML files (*.xml)" );
00176
00177 if (m_CmpFilename.isEmpty())
00178 {
00179 m_CmpFilename=oldName;
00180 }
00181 m_Controls->m_leOutputFile->setText(m_CmpFilename);
00182 }
00183
00184 void QmitkIGTLoggerWidget::UpdateOutputFileName()
00185 {
00186 QString oldName = m_CmpFilename;
00187 m_CmpFilename.clear();
00188 m_CmpFilename = m_Controls->m_leOutputFile->text();
00189 if (m_CmpFilename.isEmpty())
00190 {
00191 QMessageBox::warning(NULL, "Warning", QString("Please enter valid path! Using previous path again."));
00192 m_CmpFilename=oldName;
00193 m_Controls->m_leOutputFile->setText(m_CmpFilename);
00194 }
00195 }
00196
00197 void QmitkIGTLoggerWidget::SetRecorder( mitk::NavigationDataRecorder::Pointer recorder )
00198 {
00199 m_Recorder = recorder;
00200 }
00201
00202
00203 void QmitkIGTLoggerWidget::UpdateRecordingTime()
00204 {
00205
00206 if (m_Controls->m_cbRecordingType->currentIndex()==0)
00207 {
00208 m_MilliSeconds = m_Controls->m_leRecordingValue->text();
00209
00210 if(m_MilliSeconds.compare("infinite")==0)
00211 {
00212 this->SetDefaultRecordingSettings();
00213 }
00214
00215 bool success = false;
00216 m_MilliSeconds.toInt(&success);
00217 if (!success)
00218 {
00219 QMessageBox::warning(NULL, "Warning", QString("Please enter a number!"));
00220 this->SetDefaultRecordingSettings();
00221 return;
00222 }
00223 }
00224 else if(m_Controls->m_cbRecordingType->currentIndex()==1)
00225 {
00226 m_Samples = m_Controls->m_leRecordingValue->text();
00227
00228 if(m_Samples.compare("infinite")==0)
00229 {
00230 this->SetDefaultRecordingSettings();
00231 }
00232
00233 bool success = false;
00234 m_Samples.toInt(&success);
00235 if (!success)
00236 {
00237 QMessageBox::warning(NULL, "Warning", QString("Please enter a number!"));
00238 this->SetDefaultRecordingSettings();
00239 return;
00240 }
00241 }
00242 else if (m_Controls->m_cbRecordingType->currentIndex()==2)
00243 {
00244
00245 QString infinite("infinite");
00246 m_Controls->m_leRecordingValue->setText(infinite);
00247 }
00248
00249 }
00250
00251
00252 void QmitkIGTLoggerWidget::SetDefaultRecordingSettings()
00253 {
00254 m_Controls->m_leRecordingValue->setText("2000");
00255 m_Controls->m_cbRecordingType->setCurrentIndex(0);
00256 m_Samples="100";
00257 m_MilliSeconds="2000";
00258 }
00259
00260 void QmitkIGTLoggerWidget::SetOutputFileName()
00261 {
00262 std::string tmpDir = itksys::SystemTools::GetCurrentWorkingDirectory();
00263 QString dir = QString(tmpDir.c_str());
00264 QString filename = "IGT_Tracking_Data.xml";
00265 m_CmpFilename.append(dir);
00266
00267 if(dir.isEmpty())
00268 {
00269 QMessageBox::warning(NULL, "Warning", QString("Could not load current working directory"));
00270 return;
00271 }
00272 if(dir.endsWith("/")||dir.endsWith("\\"))
00273 {
00274 m_CmpFilename.append(filename);
00275 }
00276 else
00277 {
00278 m_CmpFilename.append("/");
00279 m_CmpFilename.append(filename);
00280 }
00281 m_Controls->m_leOutputFile->setText(m_CmpFilename);
00282 }