Medical Imaging Interaction Toolkit  2018.4.99-064ad45c
Medical Imaging Interaction Toolkit
QmitkNDIConfigurationWidget.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 (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #define MITK_NO_DEPRECATED_WARNINGS
14 
16 #include <QTableWidget>
17 #include <QMessageBox>
18 #include <QFileDialog>
19 #include <QInputDialog>
20 #include <QDir>
21 #include <QFileInfo>
22 
25 #include <mitkIGTException.h>
26 #include <mitkIOUtil.h>
27 
28 #include <QmitkIGTCommonHelper.h>
29 #include "QmitkCustomVariants.h"
30 
31 //#include <QtConcurrentMap>
32 #include "QmitkNDIToolDelegate.h"
33 
36 
37 /* VIEW MANAGEMENT */
39 : QWidget(parent), m_Controls(nullptr), m_Tracker(nullptr), m_Source(nullptr),
40 m_Delegate(nullptr), m_SROMCellDefaultText("<click to load SROM file>"), m_RepresentatonCellDefaultText("<click to select representation>")
41 {
42  this->CreateQtPartControl(this);
43 }
44 
45 
47 {
48  m_Controls = nullptr;
49  m_Tracker = nullptr;
50  m_Source = nullptr;
51 }
52 
53 
55 {
56  if (!m_Controls)
57  {
58  // create GUI widgets
59  m_Controls = new Ui::QmitkNDIConfigurationWidget;
60  m_Controls->setupUi(parent);
61  QStringList comPorts;
62 #ifdef WIN32
63  comPorts << "COM1" << "COM2" << "COM3" << "COM4" << "COM5" << "COM6" << "COM7" << "COM8" << "COM9";
64 #else
65  comPorts << "/dev/ttyS1" << "/dev/ttyS2" << "/dev/ttyS3" << "/dev/ttyS4" << "/dev/ttyS5" << "/dev/ttyUSB0" << "/dev/ttyUSB1" << "/dev/ttyUSB2" << "/dev/ttyUSB3";
66 #endif
67  m_Controls->m_ComPortSelector->addItems(comPorts);
68  m_Delegate = new QmitkNDIToolDelegate(m_Controls->m_ToolTable);
69  m_Delegate->SetDataStorage(nullptr); //needs to be set later using the setter methods
70  m_Delegate->SetPredicate(nullptr);
71  m_Delegate->SetTypes(QStringList());
72  m_Controls->m_ToolTable->setItemDelegate(m_Delegate);
73  this->CreateConnections();
74  this->HidePolarisOptionsGroupbox(true);
75  this->HideAuroraOptionsGroupbox(true);
76  }
77 }
78 
79 
81 {
82  connect(m_Controls->m_Connect, SIGNAL(clicked()), this, SLOT(OnConnect()));
83  connect(m_Controls->m_DiscoverToolsBtn, SIGNAL(clicked()), this, SLOT(OnDiscoverTools()));
84  connect(m_Controls->m_AddToolBtn, SIGNAL(clicked()), this, SLOT(OnAddPassiveTool()));
85  connect(m_Controls->m_DisoverDevicesBtn, SIGNAL(clicked()), this, SLOT(OnDiscoverDevices()));
86  connect(m_Controls->m_ToolTable->model(), SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &)), this, SLOT(UpdateTrackerFromToolTable(const QModelIndex &, const QModelIndex &)));
87  connect(m_Controls->m_DisoverDevicesBtnInfo, SIGNAL(clicked()), this, SLOT(OnDisoverDevicesBtnInfo()));
88 
89  connect(m_Controls->m_SaveToolPushButton, SIGNAL(clicked()), this, SLOT(OnSaveTool()) );
90  connect(m_Controls->m_LoadToolPushButton, SIGNAL(clicked()), this, SLOT(OnLoadTool()) );
91 
92  }
93 
94 
96 {
97  if (m_Tracker.IsNotNull())
98  {
99  m_Tracker->CloseConnection();
100  m_Tracker = nullptr;
101  }
102  this->CreateTracker();
103 
104  this->SetupTracker();
105  bool okay = false;
106  try
107  {
108  okay = m_Tracker->OpenConnection();
109  }
110  catch(mitk::IGTException &e)
111  {
112  QMessageBox::warning(nullptr, "Error", QString("Connection failed, error message: ") + e.GetDescription());
113  m_Tracker->CloseConnection();
114  this->m_Tracker = nullptr;
115 
116  }
117  if (okay)
118  {
119  // show/hide options according to connected device
121  {
122  this->HideAuroraOptionsGroupbox(true);
123  this->HidePolarisOptionsGroupbox(false);
124  }
126  {
127  this->HidePolarisOptionsGroupbox(true);
128  this->HideAuroraOptionsGroupbox(false);
129  }
130 
131  this->UpdateWidgets();
132  this->UpdateToolTable();
133 
134  connect(m_Controls->m_ToolTable, SIGNAL(cellChanged(int,int)), this, SLOT(OnTableCellChanged(int,int)));
135 
136  emit ToolsAdded(this->GetToolNamesList());
137  emit Connected();
138  }
139  else
140  {
141  QMessageBox::warning(nullptr, "Error", QString("Connection failed due to an unknown reason!"));
142  m_Tracker->CloseConnection();
143  this->m_Tracker = nullptr;
144  }
145 }
146 
147 
149 {
150  if (m_Tracker.IsNull())
151  return;
152  m_Tracker->CloseConnection();
153  m_Tracker = nullptr;
154 
155  disconnect(m_Controls->m_ToolTable, SIGNAL(cellChanged(int,int)), this, SLOT(OnTableCellChanged(int,int)));
156  m_Controls->m_ToolSelectionComboBox->clear();
157 
158  this->UpdateToolTable();
159  this->UpdateWidgets();
160  emit ToolsAdded(this->GetToolNamesList());
161  emit Disconnected();
162 
163  this->HidePolarisOptionsGroupbox(true);
164  this->HideAuroraOptionsGroupbox(true);
165 }
166 
167 
169 {
170  m_Controls->m_DeviceStatus->setText(this->GetStatusText());
171  if (m_Tracker.IsNull()) // not connected to tracker
172  {
173  m_Controls->m_Connect->setText("Connect");
174  m_Controls->m_lConnection->setText("III. Enable connection to device ");
175 
176  disconnect(m_Controls->m_Connect, SIGNAL(clicked()), this, SLOT(OnDisconnect()));
177  connect(m_Controls->m_Connect, SIGNAL(clicked()), this, SLOT(OnConnect()));
178  m_Controls->m_DiscoverToolsBtn->setDisabled(true);
179  m_Controls->m_AddToolBtn->setDisabled(true);
180  return;
181  }
182 
183  if (m_Tracker->GetState() == mitk::TrackingDevice::Setup)
184  {
185  m_Controls->m_Connect->setText("Connect");
186  m_Controls->m_lConnection->setText("III. Enable connection to device ");
187  disconnect(m_Controls->m_Connect, SIGNAL(clicked()), this, SLOT(OnDisconnect()));
188  connect(m_Controls->m_Connect, SIGNAL(clicked()), this, SLOT(OnConnect()));
189  m_Controls->m_DiscoverToolsBtn->setDisabled(true);
190  m_Controls->m_AddToolBtn->setDisabled(true);
191  return;
192  }
193  if ((m_Tracker->GetState() == mitk::TrackingDevice::Ready) || (m_Tracker->GetState() == mitk::TrackingDevice::Tracking))
194  {
195  m_Controls->m_Connect->setText("Disconnect");
196  m_Controls->m_lConnection->setText("III. Disable connection to device ");
197  disconnect(m_Controls->m_Connect, SIGNAL(clicked()), this, SLOT(OnConnect()));
198  connect(m_Controls->m_Connect, SIGNAL(clicked()), this, SLOT(OnDisconnect()));
199  m_Controls->m_DiscoverToolsBtn->setEnabled(true);
200  m_Controls->m_AddToolBtn->setEnabled(true);
201  }
202 }
203 
204 
206 {
207  if (m_Tracker.IsNull())
208  return QString("Not connected");
209 
210  QString devName = QString::fromStdString(m_Tracker->GetType());
211 
212  if (m_Tracker->GetState() == mitk::TrackingDevice::Ready)
213  return QString("Connected to %1 on %2. Device is ready.").arg(devName).arg(m_Tracker->GetDeviceName());
214  if (m_Tracker->GetState() == mitk::TrackingDevice::Tracking)
215  return QString("%1 is tracking.").arg(devName);
216  return QString("");
217 }
218 
219 
221 {
222  if (m_Tracker.IsNull())
223  {
224  QMessageBox::warning(nullptr, "Error", QString("Connection failed. No tracking device found."));
225  return;
226  }
227  m_Tracker->DiscoverWiredTools();
228  this->UpdateToolTable();
229  emit ToolsAdded(this->GetToolNamesList());
230 }
231 
232 
234 {
235  if (m_Tracker.IsNull())
236  this->CreateTracker();
237 
238  QStringList filenames = QFileDialog::getOpenFileNames(this, "Select NDI SROM file", QmitkIGTCommonHelper::GetLastFileLoadPath(),"NDI SROM files (*.rom)");
239  if (filenames.isEmpty())
240  {
241  this->m_Tracker = nullptr;
242  return;
243  }
244 
246 
247  foreach(QString fileName, filenames)
248  {
249  //QString toolName = QInputDialog::getText(this, "Enter a name for the tool", "Name of the tool: ", QLineEdit::Normal, QFileInfo(filename).baseName(), &ok);
250  //if (ok == false || toolName.isEmpty())
251  // return;
252  m_Tracker->AddTool(QFileInfo(fileName).baseName().toLatin1(), fileName.toLatin1());
253  m_Tracker->Modified();
254  }
255  emit ToolsAdded(this->GetToolNamesList());
256  this->UpdateToolTable();
257 }
258 
259 
261 {
263 }
264 
265 
267 {
268  if (m_Tracker.IsNull())
269  return;
270  m_Tracker->SetDeviceName(this->GetDeviceName());
272 }
273 
274 
276 {
277  if (m_Controls == nullptr)
278  return nullptr;
279  QString deviceName = m_Controls->m_ComPortSelector->currentText();
280 #if WIN32
281  deviceName.prepend("\\\\.\\"); // always prepend "\\.\ to all COM ports, to be able to connect to ports > 9"
282 #endif
283  return deviceName.toStdString();
284 }
285 
286 
288 {
289  if (m_Controls == nullptr)
290  return;
291  m_Controls->m_ComPortSelector->setCurrentIndex(m_Controls->m_ComPortSelector->findText(dev));
292 }
293 
294 
296 {
297  //disconnect(m_Controls->m_ToolTable, SIGNAL(itemChanged(QTableWidgetItem*)), this, SLOT(OnTableItemChanged(QTableWidgetItem*))); // stop listening to table changes
298  disconnect(m_Controls->m_ToolTable->model(), SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &)), this, SLOT(UpdateTrackerFromToolTable(const QModelIndex &, const QModelIndex &)));
299  disconnect(m_Controls->m_ToolTable, SIGNAL( clicked ( const QModelIndex & )), this, SLOT ( OnTableItemClicked( const QModelIndex & )));
300 
301  m_Controls->m_ToolTable->clearContents();
302  m_Controls->m_ToolTable->setRowCount(0);
303  if (m_Tracker.IsNull() || (m_Controls == nullptr))
304  return;
305 
306  m_Controls->m_ToolSelectionComboBox->clear();
307 
308  m_Controls->m_ToolTable->setRowCount(m_Tracker->GetToolCount());
309  for (unsigned int i = 0; i < m_Tracker->GetToolCount(); ++i)
310  {
311  mitk::TrackingTool* t = m_Tracker->GetTool(i);
312  if (t == nullptr)
313  {
314  m_Controls->m_ToolTable->setItem(i, QmitkNDIToolDelegate::IndexCol, new QTableWidgetItem("INVALID")); // Index
315  continue;
316  }
317 
318  m_Controls->m_ToolSelectionComboBox->addItem(m_Tracker->GetTool(i)->GetToolName());
319 
320  m_Controls->m_ToolTable->setItem(i, QmitkNDIToolDelegate::IndexCol, new QTableWidgetItem(QString::number(i))); // Index
321  m_Controls->m_ToolTable->setItem(i, QmitkNDIToolDelegate::NameCol, new QTableWidgetItem(t->GetToolName())); // Name
322  if (dynamic_cast<mitk::NDIPassiveTool*>(t)->GetSROMDataLength() > 0)
323  m_Controls->m_ToolTable->setItem(i, QmitkNDIToolDelegate::SROMCol, new QTableWidgetItem("SROM file loaded")); // SROM file
324  else
325  m_Controls->m_ToolTable->setItem(i, QmitkNDIToolDelegate::SROMCol, new QTableWidgetItem(m_SROMCellDefaultText)); // SROM file
326  m_Controls->m_ToolTable->setItem(i, QmitkNDIToolDelegate::TypeCol, new QTableWidgetItem("<click to set type>")); // Type
327  if (t->IsEnabled())
328  m_Controls->m_ToolTable->setItem(i, QmitkNDIToolDelegate::StatusCol, new QTableWidgetItem("Enabled")); // Status
329  else
330  m_Controls->m_ToolTable->setItem(i, QmitkNDIToolDelegate::StatusCol, new QTableWidgetItem("Disabled")); // Status
331  m_Controls->m_ToolTable->setItem(i, QmitkNDIToolDelegate::NodeCol, new QTableWidgetItem("<click to select node>")); // Node
332 
333  m_Controls->m_ToolTable->setItem(i, QmitkNDIToolDelegate::RepCol, new QTableWidgetItem(m_RepresentatonCellDefaultText)); // Representation
334 
335 
336  /* set read-only/editable flags */
337  m_Controls->m_ToolTable->item(i, QmitkNDIToolDelegate::IndexCol)->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled); // Index
338  m_Controls->m_ToolTable->item(i, QmitkNDIToolDelegate::NodeCol)->setFlags(Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled); // Name
339  m_Controls->m_ToolTable->item(i, QmitkNDIToolDelegate::SROMCol)->setFlags(Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled); // SROM file
340  m_Controls->m_ToolTable->item(i, QmitkNDIToolDelegate::TypeCol)->setFlags(Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled); // Type
341  m_Controls->m_ToolTable->item(i, QmitkNDIToolDelegate::StatusCol)->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled); // Status
342  m_Controls->m_ToolTable->item(i, QmitkNDIToolDelegate::NodeCol)->setFlags(Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled); // Node
343 
344  m_Controls->m_ToolTable->item(i, QmitkNDIToolDelegate::RepCol)->setFlags(Qt::NoItemFlags); // Representation surface file
345  }
346  m_Controls->m_ToolTable->resizeColumnsToContents();
347  //connect(m_Controls->m_ToolTable, SIGNAL(itemChanged(QTableWidgetItem*)), this, SLOT(OnTableItemChanged(QTableWidgetItem*))); // listen to table changes again
348  connect(m_Controls->m_ToolTable->model(), SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &)), this, SLOT(UpdateTrackerFromToolTable(const QModelIndex &, const QModelIndex &)));
349  connect(m_Controls->m_ToolTable, SIGNAL( clicked ( const QModelIndex & )), this, SLOT ( OnTableItemClicked( const QModelIndex & )));
350 
351 }
352 
353 
355 {
356  PortDeviceMap portsAndDevices;
357  QString status = "Scanning ";
358 #ifdef WIN32
359  QString devName;
360  for (unsigned int i = 1; i < 40; ++i)
361  {
362  if (i<10)
363  devName = QString("COM%1").arg(i);
364  else
365  devName = QString("\\\\.\\COM%1").arg(i); // prepend "\\.\ to COM ports >9, to be able to allow connection"
366  portsAndDevices[devName];
367  status += QString("COM%1").arg(i) + ", ";
368  }
369 #else //linux/posix systems
370  for(unsigned int i = 1; i < 6; ++i)
371  {
372  QString devName = QString("/dev/ttyS%1").arg(i);
373  portsAndDevices[devName];
374  status += devName + ", ";
375  }
376  for(unsigned int i = 0; i <7; ++i)
377  {
378  QString devName = QString("/dev/ttyUSB%1").arg(i);
379  portsAndDevices[devName];
380  status += devName + ", ";
381  }
382 #endif
383 
384  status.chop(2); // remove last ", "
385  status += " for NDI tracking devices...";
386  m_Controls->m_DeviceStatus->setText(status);
387  ScanPortsForNDITrackingDevices(portsAndDevices);
388  m_Controls->m_ComPortSelector->clear();
389  QString result = "The following tracking devices were found:<BR/>\n";
390  for (PortDeviceMap::const_iterator it = portsAndDevices.begin(); it != portsAndDevices.end(); ++it)
391  {
392  QString tmpComPort = it.key();
393  if (tmpComPort.startsWith("\\"))
394  {
395  tmpComPort.remove(0,4); // remove "\\.\" for nice ui visualisation
396  }
397  result += tmpComPort + ": ";
398 
400  {
401  result += QString::fromStdString(it.value());
402  result += "<BR/>\n";
403  m_Controls->m_ComPortSelector->addItem(tmpComPort);
404  }
405  else
406  {
407  result += "No NDI tracking device found<BR/>\n";
408  }
409  }
410  //QMessageBox::information(nullptr, "Tracking Device Discovery", result);
411  m_Controls->m_DeviceStatus->setText(result);
412 }
413 
414 
416 {
417  mitk::NDITrackingDevice::Pointer tracker = mitk::NDITrackingDevice::New();
418  tracker->SetDeviceName(port.toStdString());
419  return tracker->TestConnection();
420 }
421 
422 
424 {
425  // Iterative scanning:
426  for (PortDeviceMap::iterator it = portsAndDevices.begin(); it != portsAndDevices.end(); ++it)
427  it.value() = this->ScanPort(it.key());
428 
429  // \Todo: use parallel scanning
430  //QtConcurrent::blockingMap( portsAndDevices.begin(), portsAndDevices.end(), ScanPort );
431  //MITK_INFO << portsAndDevices;
432 }
433 
434 
436 {
437  QStringList toolNames;
438  if (m_Tracker.IsNull())
439  return toolNames;
440  for (unsigned int i = 0; i < m_Tracker->GetToolCount(); ++i)
441  {
442  mitk::TrackingTool* t = m_Tracker->GetTool(i);
443  if (t == nullptr)
444  continue;
445  toolNames << t->GetToolName();
446  }
447  return toolNames;
448 }
449 
450 
452 {
453  return m_Tracker.GetPointer();
454 }
455 
456 
457 void QmitkNDIConfigurationWidget::SetToolTypes(const QStringList& types)
458 {
459  m_Delegate->SetTypes(types);
460 }
461 
462 
464 {
466 }
467 
468 
470 {
472 }
473 
474 
475 void QmitkNDIConfigurationWidget::SetTagPropertyName( const std::string& name )
476 {
478 }
479 
480 
482 {
483  m_Delegate->SetTagProperty(prop);
484 }
485 
486 
487 void QmitkNDIConfigurationWidget::OnTableItemClicked(const QModelIndex & topLeft )
488 {
489  QString filename;
490  QTableWidgetItem* filenameItem;
491 
492  switch (topLeft.column())
493  {
495 
496  filename = QFileDialog::getOpenFileName(this, "Select Surface File", QmitkIGTCommonHelper::GetLastFileLoadPath(),"STL files (*.stl)");
498 
499  filenameItem = new QTableWidgetItem(filename);
500  m_Controls->m_ToolTable->setItem( topLeft.row(), topLeft.column(), filenameItem );
501 
502  if(QFileInfo(filename).exists())
503  {
504  mitk::Surface::Pointer surface = this->LoadSurfaceFromSTLFile(filename);
505 
506  if(surface.IsNotNull())
507  emit RepresentationChanged( topLeft.row(), surface);
508  }
509  break;
510  default:
511  break;
512  }
513 }
514 
515 
516 void QmitkNDIConfigurationWidget::UpdateTrackerFromToolTable(const QModelIndex & topLeft, const QModelIndex & /*bottomRight*/)
517 {
518  //Colums ID doesn't have to be processed.
519  if (topLeft.column()<1)
520  return;
521 
522  if (m_Tracker.IsNull())
523  return;
524 
525  if (topLeft.row() >= (int) m_Tracker->GetToolCount())
526  return;
527 
528  QAbstractItemModel* model = m_Controls->m_ToolTable->model();
529 
530  //define topleft contains row and column; row 0 is tool 0; column is index =0, Name =1, SROMFileName = 2; Type = 3; Status = 4; Node (?) = 5
531  //only update the changed item
532  mitk::NDIPassiveTool* tool = dynamic_cast<mitk::NDIPassiveTool*> (m_Tracker->GetTool(topLeft.row()));
533  if (tool == nullptr)
534  return;
535 
536  switch (topLeft.column())
537  {
538  case QmitkNDIToolDelegate::IndexCol: //index
539  break;
540  case QmitkNDIToolDelegate::NameCol: //name
541  tool->SetToolName(model->data(model->index(topLeft.row(), 1)).toString().toLatin1());
542  emit ToolsChanged();
543  break;
544  case QmitkNDIToolDelegate::SROMCol: //SROM File Name
545  {
546  QString romfile = model->data(model->index(topLeft.row(), QmitkNDIToolDelegate::SROMCol)).toString();
547  if (QFileInfo(romfile).exists())
548  tool->LoadSROMFile(romfile.toLatin1());
549  m_Tracker->UpdateTool(tool);
550  break;
551  }
552 
553  //TODO: Add Node Status and Type here as well
554  default:
555  break;
556  }
557 
558 }
559 
560 
561 const QString QmitkNDIConfigurationWidget::GetToolType( unsigned int index ) const
562 {
563  if (m_Controls == nullptr)
564  return QString("");
565 
566  QAbstractItemModel* model = m_Controls->m_ToolTable->model();
567  QModelIndex modelIndex = model->index(index, QmitkNDIToolDelegate::TypeCol);
568  if (modelIndex.isValid() == false)
569  return QString("");
570 
571  return model->data(modelIndex).toString();
572 }
573 
574 
575 const QString QmitkNDIConfigurationWidget::GetToolName( unsigned int index ) const
576 {
577  if (m_Controls == nullptr)
578  return QString("");
579 
580  QAbstractItemModel* model = m_Controls->m_ToolTable->model();
581  QModelIndex modelIndex = model->index(index, QmitkNDIToolDelegate::NameCol);
582  if (modelIndex.isValid() == false)
583  return QString("");
584 
585  return model->data(modelIndex).toString();
586 }
587 
588 
589 QMap<QString, unsigned int> QmitkNDIConfigurationWidget::GetToolAndTypes() const
590 {
591  QMap<QString, unsigned int> map;
592  if (m_Controls == nullptr)
593  return map;
594  QAbstractItemModel* model = m_Controls->m_ToolTable->model();
595  for (int i = 0; i < model->rowCount(); ++i)
596  {
597  QModelIndex indexIndex = model->index(i, QmitkNDIToolDelegate::IndexCol);
598  QModelIndex typeIndex = model->index(i, QmitkNDIToolDelegate::TypeCol);
599  if ((indexIndex.isValid() == false) || (typeIndex.isValid() == false))
600  continue;
601  map.insert(model->data(typeIndex).toString(), model->data(indexIndex).toUInt());
602  }
603  return map;
604 }
605 
606 
607 QList<unsigned int> QmitkNDIConfigurationWidget::GetToolsByToolType( QString toolType ) const
608 {
609  QList<unsigned int> list;
610  if (m_Controls == nullptr)
611  return list;
612  QAbstractItemModel* model = m_Controls->m_ToolTable->model();
613  for (int i = 0; i < model->rowCount(); ++i)
614  {
615  QModelIndex indexIndex = model->index(i, QmitkNDIToolDelegate::IndexCol);
616  QModelIndex typeIndex = model->index(i, QmitkNDIToolDelegate::TypeCol);
617  if ((indexIndex.isValid() == false) || (typeIndex.isValid() == false))
618  continue;
619  if (model->data(typeIndex).toString() == toolType)
620  list.append(model->data(indexIndex).toUInt());
621  }
622  return list;
623 }
624 
625 
627 {
628 
629  if (m_Controls == nullptr)
630  return nullptr;
631  QAbstractItemModel* model = m_Controls->m_ToolTable->model();
632  QVariant data = model->data(model->index(index, QmitkNDIToolDelegate::NodeCol), QmitkNDIToolDelegate::OrganNodeRole);
633  return data.value<mitk::DataNode*>();
634 }
635 
637 {
638  m_Controls->m_gbPolarisOptions->setHidden(on);
639 }
640 
642 {
643  m_Controls->m_gbAuroraOptions->setHidden(on);
644 }
645 
647 {
648  int cols = m_Controls->m_ToolTable->columnCount();
649 
650  //checking if representation column is inserted at right index
651  if(cols != QmitkNDIToolDelegate::RepCol)
652  {
653  //throw std::exception("Representation Column is not inserted at it's designated index!");
654  return;
655  }
656 
657 
658  m_Controls->m_ToolTable->insertColumn(cols); // insert new column at end of table
659 
660 
661  m_Controls->m_ToolTable->setHorizontalHeaderItem(QmitkNDIToolDelegate::RepCol, new QTableWidgetItem(QString("Representation"))); // inser column header for new colum
662  //m_Controls->m_ToolTable->setEditTriggers(QAbstractItemView::EditTrigger::NoEditTriggers);
663 
664  int rows = m_Controls->m_ToolTable->rowCount();
665 
666  // make all representation colum items not editable
667  for(int i=0; i < rows; ++i)
668  {
669  m_Controls->m_ToolTable->setItem(i, QmitkNDIToolDelegate::RepCol, new QTableWidgetItem("<click to select representation>")); // Representation
670  m_Controls->m_ToolTable->item(i,QmitkNDIToolDelegate::RepCol)->setFlags(Qt::NoItemFlags);
671  }
672 
673  //connect(m_Controls->m_ToolTable, SIGNAL( clicked ( const QModelIndex & )), this, SLOT ( OnTableItemClicked( const QModelIndex & )));
674 
675 }
676 
678 {
679  QMessageBox *infoBox = new QMessageBox(this);
680  infoBox->setText("Click \"Scan Ports\" to get a list of all connected NDI tracking devices. This will clear the selection menu below and add the ports for discovered NDI tracking devices. Use this function, if a port is not listed.");
681  infoBox->exec();
682  delete infoBox;
683 }
684 
685 
687 {
688 
689  if(m_Tracker.IsNull())
690  return;
691 
692  QString toolName;
693 
694  switch (column)
695  {
697  toolName = m_Controls->m_ToolTable->item(row,column)->text();
698  m_Controls->m_ToolSelectionComboBox->setItemText(row, toolName);
699 
700  emit SignalToolNameChanged(row, toolName);
701 
702  break;
703 
704  default:
705  break;
706  }
707 
708 }
709 
710 
712 {
713  if(m_Tracker.IsNull() || m_Tracker->GetToolCount() <= 0)
714  return;
715 
716  int currId = m_Controls->m_ToolSelectionComboBox->currentIndex();
717  QString filename = QFileDialog::getSaveFileName(nullptr, "Save NDI-Tool", QString(QString(m_Tracker->GetTool(currId)->GetToolName())),"NDI Tracking Tool file(*.ntf)");
718 
719  mitk::TrackingTool* selectedTool = m_Tracker->GetTool(currId);
720 
721  if(filename.isEmpty())
722  return;
723 
724  mitk::NavigationTool::Pointer navTool = mitk::NavigationTool::New();
725 
726 
727  mitk::NavigationToolWriter::Pointer toolWriter = mitk::NavigationToolWriter::New();
728  try {
729  toolWriter->DoWrite(filename.toStdString(), this->GenerateNavigationTool(selectedTool));
730  }
731  catch( ... )
732  {
733  QMessageBox::warning(nullptr, "Saving Tool Error", QString("An error occured! Could not save tool!\n\n"));
734  MBI_ERROR<<"Could not save tool surface!";
735  MBI_ERROR<< toolWriter->GetErrorMessage();
736 
737  QFile maybeCorruptFile(filename);
738 
739  if(maybeCorruptFile.exists())
740  maybeCorruptFile.remove();
741  }
742 
743  emit SignalSavedTool(currId, filename);
744 }
745 
746 
748 {
749  if(m_Tracker.IsNull() || m_Tracker->GetToolCount() <= 0)
750  return;
751 
752  QString filename = QFileDialog::getOpenFileName(nullptr, "Load NDI-Tools", QmitkIGTCommonHelper::GetLastFileLoadPath(),"NDI Tracking Tool file(*.ntf)");
753  int currId = m_Controls->m_ToolSelectionComboBox->currentIndex();
754 
755  if(filename.isEmpty())
756  return;
757 
759 
760  mitk::DataNode::Pointer toolNode;
761  mitk::NavigationToolReader::Pointer toolReader = mitk::NavigationToolReader::New();
762  mitk::NavigationTool::Pointer navTool;
763 
764  try {
765  navTool = toolReader->DoRead(filename.toStdString());
766  }
767  catch( ... )
768  {
769  QMessageBox::warning(nullptr, "Loading Tool Error", QString("An error occured! Could not load tool!\n\n"));
770  MBI_ERROR<<"Could not load tool surface!";
771  MBI_ERROR<< toolReader->GetErrorMessage();
772  }
773 
774  int currSelectedToolID = m_Controls->m_ToolSelectionComboBox->currentIndex();
775 
776  // name
777  m_Controls->m_ToolTable->item(currSelectedToolID,QmitkNDIToolDelegate::NameCol)->setText(navTool->GetToolName().c_str());
778  dynamic_cast<mitk::NDIPassiveTool*>(m_Tracker->GetTool(currSelectedToolID))->SetToolName(navTool->GetToolName().c_str()); // also setting name to tool directly
779 
780  //calibration file (.srom) filename
781  m_Controls->m_ToolTable->item(currSelectedToolID,QmitkNDIToolDelegate::SROMCol)->setText(navTool->GetCalibrationFile().c_str());
782 
783  //type
784  if(navTool->GetType() == mitk::NavigationTool::Instrument)
785  m_Controls->m_ToolTable->item(currSelectedToolID,QmitkNDIToolDelegate::TypeCol)->setText("Instrument");
786  else if(navTool->GetType() == mitk::NavigationTool::Fiducial)
787  m_Controls->m_ToolTable->item(currSelectedToolID,QmitkNDIToolDelegate::TypeCol)->setText("Fiducial");
788  else if(navTool->GetType() == mitk::NavigationTool::Skinmarker)
789  m_Controls->m_ToolTable->item(currSelectedToolID,QmitkNDIToolDelegate::TypeCol)->setText("Skinmarker");
790  else
791  m_Controls->m_ToolTable->item(currSelectedToolID,QmitkNDIToolDelegate::TypeCol)->setText("Unknown");
792 
793 
794  //representation
795  m_Controls->m_ToolTable->item(currSelectedToolID,QmitkNDIToolDelegate::SROMCol)->setText(m_RepresentatonCellDefaultText);
796 
797  emit SignalLoadTool(currId, navTool->GetDataNode());
798 }
799 
800 
802 {
803  mitk::NavigationTool::Pointer navTool = mitk::NavigationTool::New();
804  mitk::NDIPassiveTool::Pointer passiveTool = dynamic_cast<mitk::NDIPassiveTool*>(tool);
805 
806  if(passiveTool.IsNull())
807  throw std::runtime_error("Could not cast TrackingTool to PassiveTool");
808 
809  int currSelectedToolID = m_Controls->m_ToolSelectionComboBox->currentIndex();
810 
811  QString sromFileName = m_Controls->m_ToolTable->item(currSelectedToolID, QmitkNDIToolDelegate::SROMCol)->text();
812  QString surfaceFileName = m_Controls->m_ToolTable->item(currSelectedToolID, QmitkNDIToolDelegate::RepCol)->text();
813 
814  //calibration file (.srom) filename
815  QFile sromFile(sromFileName);
816  if(sromFile.exists())
817  navTool->SetCalibrationFile(sromFileName.toStdString());
818 
819  //serial number
820  navTool->SetSerialNumber(passiveTool->GetSerialNumber());
821 
822  // name and surface as dataNode
824 
825  mitk::Surface::Pointer toolSurface;
826 
827  try{
828  toolSurface = this->LoadSurfaceFromSTLFile(surfaceFileName);
829  }
830  catch( ... )
831  {
832  QMessageBox::warning(nullptr, "Loading Surface Error", QString("An error occured! Could not load surface from .stl file!\n\n"));
833  MBI_ERROR<<"Could not load .stl tool surface!";
834  }
835 
836 
837  if(toolSurface.IsNotNull())
838  {
839  node->SetData(toolSurface);
840  node->SetName(tool->GetToolName());
841  }
842 
843  navTool->SetDataNode(node);
844 
845  // type
847  QString currentToolType = m_Controls->m_ToolTable->item(currSelectedToolID,QmitkNDIToolDelegate::TypeCol)->text();
848 
849  if(currentToolType.compare("Instrument") == 0)
851  else if(currentToolType.compare("Fiducial") == 0)
853  else if(currentToolType.compare("Skinmarker") == 0)
855  else
857 
858  navTool->SetType(type);
859 
860  return navTool;
861 }
862 
863 
865 {
866  mitk::Surface::Pointer toolSurface;
867 
868  QFile surfaceFile(surfaceFilename);
869  if(surfaceFile.exists())
870  {
871  try{
872  toolSurface = mitk::IOUtil::Load<mitk::Surface>(surfaceFilename.toStdString().c_str());
873  }
874  catch(std::exception& e )
875  {
876  MBI_ERROR<<"Could not load surface for tool!";
877  MBI_ERROR<< e.what();
878  throw e;
879  }
880  }
881 
882  return toolSurface;
883 }
884 
886  m_Controls->m_AddToolBtn->setEnabled(enable);
887 }
888 
890  m_Controls->m_DiscoverToolsBtn->setEnabled(enable);
891 }
QStringList GetToolNamesList()
returns a string list with the names of all tools of the current tracking device
QString GetStatusText()
construct a status text depending on the current state of the tracking device object ...
void SetDataStorage(mitk::DataStorage *ds)
set datastorage for organ node editor
static const QString GetLastFileLoadPath()
static Pointer New()
Interface for all Tracking Tools.
Data management class that handles &#39;was created by&#39; relations.
QMap< QString, unsigned int > GetToolAndTypes() const
void SignalLoadTool(int id, mitk::DataNode::Pointer dn)
static void SetLastFileLoadPathByFileName(const QString &str)
An object of this class represents an exception of the MITK-IGT module.
void SetPredicate(mitk::NodePredicateBase::Pointer p)
set predicate for organ node editor
void OnTableCellChanged(int row, int column)
mitk::NavigationTool::Pointer GenerateNavigationTool(mitk::TrackingTool *tool)
virtual bool LoadSROMFile(const char *filename)
load a srom tool description file
virtual void CreateQtPartControl(QWidget *parent)
QMap< QString, mitk::TrackingDeviceType > PortDeviceMap
void SetTagPropertyName(const std::string &name)
set name of the property that is used to tag selected nodes
An item delegate for rendering and editing mitk::Properties in a QTableView.
Ui::QmitkNDIConfigurationWidget * m_Controls
gui widgets
void RepresentationChanged(int row, mitk::Surface::Pointer surface)
void SetTagProperty(mitk::BaseProperty::Pointer prop)
set the property that is used to tag selected nodes
void ToolsAdded(QStringList tools)
const QString GetToolType(unsigned int index) const
void SetDataStorage(mitk::DataStorage *ds)
set datastorage for organ node editor
virtual void CreateConnections()
Creation of the connections of main and control widget.
void SignalSavedTool(int id, QString surfaceFilename)
void SignalToolNameChanged(int id, QString name)
void SetPredicate(mitk::NodePredicateBase::Pointer p)
set predicate for organ node editor
mitk::DataNode * GetNode(unsigned int index) const
void OnTableItemClicked(const QModelIndex &topLeft)
for clicking on tooltable items
static Pointer New()
void UpdateTrackerFromToolTable(const QModelIndex &topLeft, const QModelIndex &)
const QString GetToolName(unsigned int index) const
void UpdateToolTable()
read all tools from the tracking device object and display them in the gui
QList< unsigned int > GetToolsByToolType(QString toolType) const
MITKMATCHPOINTREGISTRATION_EXPORT ResultImageType::Pointer map(const InputImageType *input, const RegistrationType *registration, bool throwOnOutOfInputAreaError=false, const double &paddingValue=0, const ResultImageGeometryType *resultGeometry=nullptr, bool throwOnMappingError=true, const double &errorValue=0, mitk::ImageMappingInterpolator::Type interpolatorType=mitk::ImageMappingInterpolator::Linear)
static Pointer New()
Implementation of a passive NDI optical tool.
mitk::NDITrackingDevice::Pointer m_Tracker
tracking device object
void OnConnect()
enables or disables the Discover new Tools button
std::string TrackingDeviceType
void SetDeviceName(const char *dev)
set the device name (e.g. "COM1", "/dev/ttyS0") that will be used to connect to the tracking device ...
void CreateTracker()
creates new NDITrackingDevice object
virtual bool IsEnabled() const
returns whether the tool is enabled or disabled
void EnableAddToolsButton(bool enable)
enables or disables the Add Tools button
mitk::Surface::Pointer LoadSurfaceFromSTLFile(QString surfaceFilename)
void SetTypes(const QStringList &types)
set types list for type editor combobox
superclass for specific NDI tracking Devices that use serial communication.
mitk::TrackingDeviceSource::Pointer m_Source
void SetToolTypes(const QStringList &types)
set types list for type editor combobox
void ScanPortsForNDITrackingDevices(PortDeviceMap &portsAndDevices)
scans the ports provided as key in the portsAndDevices and fills the respective value of portsAndDevi...
void ShowToolRepresentationColumn()
show or hide the tooltable column "Tool Representation". This SLOT should be called after SIGNAL "Con...
void SetupTracker()
sets the parameters from the gui to the tracking device object
void SetTagProperty(mitk::BaseProperty::Pointer prop)
set the property that is used to tag selected nodes
virtual void SetToolName(const std::string _arg)
Sets the name of the tool.
void SetTagPropertyName(const std::string &name)
set name of the property that is used to tag selected nodes
virtual const char * GetToolName() const
every tool has a name thatgit can be used to identify it.
void HidePolarisOptionsGroupbox(bool on)
show or hide polaris options in the UI
void HideAuroraOptionsGroupbox(bool on)
show or hide aurora options in the UI
Class for nodes of the DataTree.
Definition: mitkDataNode.h:57
#define MBI_ERROR
Definition: mbilog.h:210
mitk::NDITrackingDevice * GetTracker() const
mitk::TrackingDeviceType ScanPort(QString port)