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