MITK-IGT
IGT Extension of MITK
Loading...
Searching...
No Matches
QmitkIGTLoggerWidget.cpp
Go to the documentation of this file.
1/*============================================================================
2
3The Medical Imaging Interaction Toolkit (MITK)
4
5Copyright (c) German Cancer Research Center (DKFZ)
6All rights reserved.
7
8Use of this source code is governed by a 3-clause BSD license that can be
9found in the LICENSE file.
10
11============================================================================*/
12
14
15//mitk headers
16#include "mitkTrackingTypes.h"
17#include <mitkSurface.h>
23#include <mitkStatusBar.h>
24#include <mitkIOUtil.h>
25
26//itk headers
27#include <itksys/SystemTools.hxx>
28
29//qt headers
30#include <qfiledialog.h>
31#include <qmessagebox.h>
32#include <qtimer.h>
33
34
35QmitkIGTLoggerWidget::QmitkIGTLoggerWidget(QWidget* parent, Qt::WindowFlags f)
36: QWidget(parent, f), m_Recorder(nullptr), m_RecordingActivated(false)
37{
38 m_Controls = nullptr;
41
42 //set output file
43 this->SetOutputFileName();
44
45 //update milliseconds and samples
47}
48
49
56
58{
59 if (!m_Controls)
60 {
61 // create GUI widgets
62 m_Controls = new Ui::QmitkIGTLoggerWidgetControls;
63 m_Controls->setupUi(parent);
64
65 m_RecordingTimer = new QTimer(this);
66 }
67}
68
70{
71 if ( m_Controls )
72 {
73 connect( (QObject*)(m_Controls->m_pbLoadDir), SIGNAL(clicked()), this, SLOT(OnChangePressed()) );
74 connect( (QObject*)(m_Controls->m_pbStartRecording), SIGNAL(clicked(bool)), this, SLOT(OnStartRecording(bool)) );
75 connect( m_RecordingTimer, SIGNAL(timeout()), this, SLOT(OnRecording()) );
76 connect( (QObject*)(m_Controls->m_leRecordingValue), SIGNAL(editingFinished()), this, SLOT(UpdateRecordingTime()) );
77 connect( (QObject*)(m_Controls->m_cbRecordingType), SIGNAL(activated(int)), this, SLOT(UpdateRecordingTime()) );
78 connect( (QObject*)(m_Controls->m_leOutputFile), SIGNAL(editingFinished()), this, SLOT(UpdateOutputFileName()) );
79
80 }
81}
82
83void QmitkIGTLoggerWidget::SetDataStorage(mitk::DataStorage* dataStorage)
84{
85 m_DataStorage = dataStorage;
86}
87
89{
90
91 if (m_Recorder.IsNull())
92 {
93 QMessageBox::warning(nullptr, "Warning", QString("Please start tracking before recording!"));
94 return;
95 }
96 if (m_CmpFilename.isEmpty())
97 {
98 QMessageBox::warning(nullptr, "Warning", QString("Please specify filename!"));
99 return;
100 }
101
102 if(recording)
103 {
104
106 {
107 //m_Recorder->SetFileName(m_CmpFilename.toStdString());
108
109 try
110 { /*start the recording mechanism */
111 m_Recorder->StartRecording();
112 m_RecordingTimer->start(50); //now every update of the recorder stores one line into the file for each added NavigationData
113 mitk::StatusBar::GetInstance()->DisplayText("Recording tracking data now"); // Display recording message for 75ms in status bar
114
116 }
117 catch (std::exception& e)
118 {
119 QMessageBox::warning(nullptr, "IGT-Tracking Logger: Error", QString("Error while recording tracking data: %1").arg(e.what()));
120 mitk::StatusBar::GetInstance()->DisplayText(""); // Display recording message for 75ms in status bar
121 }
122 m_Controls->m_pbStartRecording->setText("Stop recording");
123 m_Controls->m_leRecordingValue->setEnabled(false);
124 m_Controls->m_cbRecordingType->setEnabled(false);
125
127
128 if(m_Controls->m_cbRecordingType->currentIndex()==0)
129 {
130 bool success = false;
131 QString str_ms = m_Controls->m_leRecordingValue->text();
132 int int_ms = str_ms.toInt(&success);
133 if (success)
134 QTimer::singleShot(int_ms, this, SLOT(StopRecording()));
135 }
136 }
137 else
138 {
139 this->StopRecording();
140 }
141
142 }
143 else
144 {
145 this->StopRecording();
146 m_Controls->m_pbStartRecording->setChecked(false);
147 }
148
149}
150
152{
153 m_RecordingTimer->stop();
154 m_Recorder->StopRecording();
155 mitk::StatusBar::GetInstance()->DisplayText("Recording STOPPED", 2000); // Display message for 2s in status bar
156 m_Controls->m_pbStartRecording->setText("Start recording");
157 m_Controls->m_pbStartRecording->setChecked(false);
158 m_Controls->m_leRecordingValue->setEnabled(true);
159 m_Controls->m_cbRecordingType->setEnabled(true);
160 m_RecordingActivated = false;
161
162 try
163 {
164 // write NavigationDataSet on StopRecording
165 mitk::IOUtil::Save(m_Recorder->GetNavigationDataSet(), m_CmpFilename.toStdString());
166 }
167 catch(const std::exception &e)
168 {
169 // TODO: catch must be adapted when new file writer are merged to master
170 QMessageBox::warning(nullptr, "IGT-Tracking Logger: Error", QString("Error while writing tracking data: %1").arg(e.what()));
171 MITK_WARN << "File could not be written.";
172 }
173
175}
176
178{
179 static unsigned int sampleCounter = 0;
180 unsigned int int_samples = m_Samples.toInt();
181 if(sampleCounter >= int_samples)
182 {
183 this->StopRecording();
184 sampleCounter=0;
185 return;
186 }
187 m_Recorder->Update();
188
189 if (m_Controls->m_cbRecordingType->currentIndex()==1)
190 sampleCounter++;
191}
192
194{
195 QString oldName = m_CmpFilename;
196 m_CmpFilename.clear();
197 m_CmpFilename = QFileDialog::getSaveFileName( QApplication::activeWindow()
198 , "Save tracking data", "IGT_Tracking_Data.xml", "XML files (*.xml)" );
199
200 if (m_CmpFilename.isEmpty())//if something went wrong or user pressed cancel in the save dialog
201 {
202 m_CmpFilename=oldName;
203 }
204 m_Controls->m_leOutputFile->setText(m_CmpFilename);
205}
206
208{
209 QString oldName = m_CmpFilename;
210 m_CmpFilename.clear();
211 m_CmpFilename = m_Controls->m_leOutputFile->text();
212 if (m_CmpFilename.isEmpty())
213 {
214 QMessageBox::warning(nullptr, "Warning", QString("Please enter valid path! Using previous path again."));
215 m_CmpFilename=oldName;
216 m_Controls->m_leOutputFile->setText(m_CmpFilename);
217 }
218}
219
220void QmitkIGTLoggerWidget::SetRecorder( mitk::NavigationDataRecorder::Pointer recorder )
221{
222 m_Recorder = recorder;
223}
224
225
227{
228 // milliseconds selected in the combobox
229 if (m_Controls->m_cbRecordingType->currentIndex()==0)
230 {
231 m_MilliSeconds = m_Controls->m_leRecordingValue->text();
232
233 if(m_MilliSeconds.compare("infinite")==0)
234 {
236 }
237
238 bool success = false;
239 m_MilliSeconds.toInt(&success);
240 if (!success)
241 {
242 QMessageBox::warning(nullptr, "Warning", QString("Please enter a number!"));
244 return;
245 }
246 }
247 else if(m_Controls->m_cbRecordingType->currentIndex()==1) // #samples selected in the combobox
248 {
249 m_Samples = m_Controls->m_leRecordingValue->text();
250
251 if(m_Samples.compare("infinite")==0)
252 {
254 }
255
256 bool success = false;
257 m_Samples.toInt(&success);
258 if (!success)
259 {
260 QMessageBox::warning(nullptr, "Warning", QString("Please enter a number!"));
262 return;
263 }
264 }
265 else if (m_Controls->m_cbRecordingType->currentIndex()==2)// infinite selected in the combobox
266 {
267 // U+221E unicode symbole for infinite
268 QString infinite("infinite");
269 m_Controls->m_leRecordingValue->setText(infinite);
270 }
271 // m_Controls->m_leSamples->setText(QString::number(samples));
272}
273
274
276{
277 m_Controls->m_leRecordingValue->setText("2000");
278 m_Controls->m_cbRecordingType->setCurrentIndex(0);
279 m_Samples="100";
280 m_MilliSeconds="2000";
281}
282
284{
285 std::string tmpDir = itksys::SystemTools::GetCurrentWorkingDirectory();
286 QString dir = QString(tmpDir.c_str());
287 QString filename = "IGT_Tracking_Data.xml";
288 m_CmpFilename.append(dir);
289
290 if(dir.isEmpty())
291 {
292 QMessageBox::warning(nullptr, "Warning", QString("Could not load current working directory"));
293 return;
294 }
295 if(dir.endsWith("/")||dir.endsWith("\\"))
296 {
297 m_CmpFilename.append(filename);
298 }
299 else
300 {
301 m_CmpFilename.append("/");
302 m_CmpFilename.append(filename);
303 }
304 m_Controls->m_leOutputFile->setText(m_CmpFilename);
305}
void SetRecorder(mitk::NavigationDataRecorder::Pointer recorder)
mitk::DataStorage::Pointer m_DataStorage
holds the DataStorage
void OnStartRecording(bool recording)
void SetDataStorage(mitk::DataStorage *dataStorage)
virtual void CreateConnections()
Creation of the connections.
virtual void CreateQtPartControl(QWidget *parent)
QmitkIGTLoggerWidget(QWidget *parent=nullptr, Qt::WindowFlags f={})
mitk::NavigationDataRecorder::Pointer m_Recorder
records NDs to a XML file
Ui::QmitkIGTLoggerWidgetControls * m_Controls