Medical Imaging Interaction Toolkit  2018.4.99-36d69b77
Medical Imaging Interaction Toolkit
QmitkNavigationToolManagementWidget.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 
14 
15 //mitk headers
16 #include "mitkTrackingTypes.h"
17 #include <mitkSurface.h>
23 #include <mitkIGTIOException.h>
24 #include <QmitkIGTCommonHelper.h>
25 
26 //qt headers
27 #include <qfiledialog.h>
28 #include <qinputdialog.h>
29 #include <qmessagebox.h>
30 #include <qsettings.h>
31 
32 //poco headers
33 #include <Poco/Path.h>
34 
35 const std::string QmitkNavigationToolManagementWidget::VIEW_ID = "org.mitk.views.navigationtoolmanagementwidget";
36 
38  : QWidget(parent, f)
39 {
40  m_Controls = nullptr;
41  CreateQtPartControl(this);
43 }
44 
46 {
47 }
48 
50 {
51  if (!m_Controls)
52  {
53  // create GUI widgets
54  m_Controls = new Ui::QmitkNavigationToolManagementWidgetControls;
55  m_Controls->setupUi(parent);
56  }
57 
58  //Disable StorageControls in the beginning, because there is no storage to edit
60 }
61 
63 {
64  if (m_NavigationToolStorage->isLocked())
65  {
66  MessageBox("Storage is locked, cannot modify it. Maybe the tracking device which uses this storage is connected. If you want to modify the storage please disconnect the device first.");
67  return;
68  }
69  mitk::NavigationToolReader::Pointer myReader = mitk::NavigationToolReader::New();
70  std::string filename = QFileDialog::getOpenFileName(nullptr,tr("Add Navigation Tool"), QmitkIGTCommonHelper::GetLastFileLoadPath(), "*.IGTTool").toLatin1().data();
71  QmitkIGTCommonHelper::SetLastFileLoadPathByFileName(QString::fromStdString(filename));
72  if (filename == "") return;
73  mitk::NavigationTool::Pointer readTool = myReader->DoRead(filename);
74  if (readTool.IsNull()) MessageBox("Error: " + myReader->GetErrorMessage());
75  else
76  {
77  if (!m_NavigationToolStorage->AddTool(readTool))
78  {
79  MessageBox("Error: Can't add tool!");
80  m_DataStorage->Remove(readTool->GetDataNode());
81  }
84  }
85 }
86 
88 {
89  //if no item is selected, show error message:
90  if (m_Controls->m_ToolList->currentItem() == nullptr) { MessageBox("Error: Please select tool first!"); return; }
91 
92  mitk::NavigationToolWriter::Pointer myWriter = mitk::NavigationToolWriter::New();
93  std::string filename = QFileDialog::getSaveFileName(nullptr,tr("Save Navigation Tool"), QmitkIGTCommonHelper::GetLastFileSavePath(), "*.IGTTool").toLatin1().data();
94  QmitkIGTCommonHelper::SetLastFileSavePathByFileName(QString::fromStdString(filename));
95  if (filename == "") return;
96  std::string fileExt = Poco::Path(filename).getExtension();
97  if (fileExt == "") { filename.append(".IGTTool"); }
98  if (!myWriter->DoWrite(filename,m_NavigationToolStorage->GetTool(m_Controls->m_ToolList->currentIndex().row())))
99  MessageBox("Error: "+ myWriter->GetErrorMessage());
100 }
101 
103 {
104  if (m_Controls)
105  {
106  //main widget page:
107  connect((QObject*)(m_Controls->m_AddTool), SIGNAL(clicked()), this, SLOT(OnAddTool()));
108  connect((QObject*)(m_Controls->m_DeleteTool), SIGNAL(clicked()), this, SLOT(OnDeleteTool()));
109  connect((QObject*)(m_Controls->m_EditTool), SIGNAL(clicked()), this, SLOT(OnEditTool()));
110  connect((QObject*)(m_Controls->m_MoveToolUp), SIGNAL(clicked()), this, SLOT(OnMoveToolUp()));
111  connect((QObject*)(m_Controls->m_MoveToolDown), SIGNAL(clicked()), this, SLOT(OnMoveToolDown()));
112  connect((QObject*)(m_Controls->m_LoadStorage), SIGNAL(clicked()), this, SLOT(OnLoadStorage()));
113  connect((QObject*)(m_Controls->m_SaveStorage), SIGNAL(clicked()), this, SLOT(OnSaveStorage()));
114  connect((QObject*)(m_Controls->m_LoadTool), SIGNAL(clicked()), this, SLOT(OnLoadTool()));
115  connect((QObject*)(m_Controls->m_SaveTool), SIGNAL(clicked()), this, SLOT(OnSaveTool()));
116  connect((QObject*)(m_Controls->m_CreateNewStorage), SIGNAL(clicked()), this, SLOT(OnCreateStorage()));
117 
118  connect((QObject*)(m_Controls->m_ToolList), SIGNAL(itemClicked(QListWidgetItem *)), this, SLOT(OnToolSelected()));
119 
120  //widget page "add tool":
121  connect((QObject*)(m_Controls->m_ToolCreationWidget), SIGNAL(Canceled()), this, SLOT(OnAddToolCancel()));
122  connect((QObject*)(m_Controls->m_ToolCreationWidget), SIGNAL(NavigationToolFinished()), this, SLOT(OnAddToolSave()));
123  }
124 }
125 
127 {
128  m_DataStorage = dataStorage;
129  m_Controls->m_ToolCreationWidget->Initialize(m_DataStorage, "Tool0");
130 }
131 
132 void QmitkNavigationToolManagementWidget::LoadStorage(mitk::NavigationToolStorage::Pointer storageToLoad)
133 {
134  if (storageToLoad.IsNotNull())
135  {
136  m_NavigationToolStorage = storageToLoad;
137  m_Controls->m_StorageName->setText(m_NavigationToolStorage->GetName().c_str());
139  }
140  else
141  {
142  m_NavigationToolStorage = nullptr;
144  }
145  UpdateToolTable();
146 }
147 
148 //##################################################################################
149 //############################## slots: main widget ################################
150 //##################################################################################
151 
153 {
154  if (m_NavigationToolStorage.IsNotNull())
155  {
156  //Proof, if the NavigationToolStorage is locked. If it is locked, show MessageBox to user.
157  if( m_NavigationToolStorage->isLocked() )
158  {
159  MessageBox("The storage is locked and it cannot be modified. Maybe the tracking device which " \
160  "uses this storage is connected. If you want to modify the storage please " \
161  "disconnect the device first.");
162  return;
163  }
164  int toolIndex = m_Controls->m_ToolList->currentIndex().row();
165  if (toolIndex >= 0)
166  {
167  mitk::NavigationTool::Pointer currentNavigationTool = m_NavigationToolStorage->GetTool(toolIndex);
168  if (currentNavigationTool.IsNotNull())
169  {
170  std::string currentIdentifier = currentNavigationTool->GetIdentifier();
171  int NewNumber = m_Controls->m_ToolList->currentIndex().row() - 1;
172  if (NewNumber < 0) { MITK_WARN << "Cannot move tool up because it is on the top!"; }
173  else { m_NavigationToolStorage->AssignToolNumber(currentIdentifier, NewNumber); }
174  UpdateToolTable();
175  }
176  }
177  }
178 }
179 
181 {
182  if (m_NavigationToolStorage.IsNotNull())
183  {
184  //Proof, if the NavigationToolStorage is locked. If it is locked, show MessageBox to user.
185  if( m_NavigationToolStorage->isLocked() )
186  {
187  MessageBox("The storage is locked and it cannot be modified. Maybe the tracking device which " \
188  "uses this storage is connected. If you want to modify the storage please " \
189  "disconnect the device first.");
190  return;
191  }
192  int toolIndex = m_Controls->m_ToolList->currentIndex().row();
193  if (toolIndex >= 0)
194  {
195  mitk::NavigationTool::Pointer currentNavigationTool = m_NavigationToolStorage->GetTool(toolIndex);
196  if (currentNavigationTool.IsNotNull())
197  {
198  std::string currentIdentifier = currentNavigationTool->GetIdentifier();
199  unsigned int NewNumber = m_Controls->m_ToolList->currentIndex().row() + 1;
200  if (NewNumber >= m_NavigationToolStorage->GetToolCount()) { MITK_WARN << "Cannot move tool down because it is the last tool in this storage!"; }
201  else { m_NavigationToolStorage->AssignToolNumber(currentIdentifier, NewNumber); }
202  UpdateToolTable();
203  }
204  }
205  }
206 }
207 
209 {
210  if (m_NavigationToolStorage->isLocked())
211  {
212  MessageBox("Storage is locked, cannot modify it. Maybe the tracking device which uses this storage is connected. If you want to modify the storage please disconnect the device first.");
213  return;
214  }
215  QString defaultIdentifier = "NavigationTool#" + QString::number(m_NavigationToolStorage->GetToolCount());
216  QString defaultName = "NavigationTool" + QString::number(m_NavigationToolStorage->GetToolCount());
217  m_Controls->m_ToolCreationWidget->Initialize(m_DataStorage, defaultIdentifier.toStdString(), defaultName.toStdString());
218  m_Controls->m_ToolCreationWidget->ShowToolPreview("Tool preview");
219  m_edit = false;
220  m_Controls->m_MainWidgets->setCurrentIndex(1);
221 }
222 
224 {
225  //first: some checks
226  if (m_NavigationToolStorage->isLocked())
227  {
228  MessageBox("Storage is locked, cannot modify it. Maybe the tracking device which uses this storage is connected. If you want to modify the storage please disconnect the device first.");
229  return;
230  }
231  else if (m_Controls->m_ToolList->currentItem() == nullptr) //if no item is selected, show error message:
232  {
233  MessageBox("Error: Please select tool first!");
234  return;
235  }
236 
237  m_DataStorage->Remove(m_NavigationToolStorage->GetTool(m_Controls->m_ToolList->currentIndex().row())->GetDataNode());
238  m_NavigationToolStorage->DeleteTool(m_Controls->m_ToolList->currentIndex().row());
239  UpdateToolTable();
240 }
241 
243 {
244  if (m_NavigationToolStorage->isLocked())
245  {
246  MessageBox("Storage is locked, cannot modify it. Maybe the tracking device which uses this storage is connected. If you want to modify the storage please disconnect the device first.");
247  return;
248  }
249  else if (m_Controls->m_ToolList->currentItem() == nullptr) //if no item is selected, show error message:
250  {
251  MessageBox("Error: Please select tool first!");
252  return;
253  }
254  mitk::NavigationTool::Pointer selectedTool = m_NavigationToolStorage->GetTool(m_Controls->m_ToolList->currentIndex().row());
255  m_Controls->m_ToolCreationWidget->Initialize(m_DataStorage, "", ""); //Initialize once again, might be called here for the first time after autodetection
256  m_Controls->m_ToolCreationWidget->SetDefaultData(selectedTool);
257  m_Controls->m_ToolCreationWidget->ShowToolPreview("Tool preview");
258  m_edit = true;
259  m_Controls->m_MainWidgets->setCurrentIndex(1);
260 }
261 
263 {
264  QString storageName = QInputDialog::getText(nullptr, "Storage Name", "Name of the new tool storage:");
265  if (storageName.isNull()) return;
267  m_NavigationToolStorage->SetName(storageName.toStdString());
268  m_Controls->m_StorageName->setText(m_NavigationToolStorage->GetName().c_str());
270  emit NewStorageAdded(m_NavigationToolStorage, storageName.toStdString());
271 }
272 
274 {
275  mitk::NavigationToolStorageDeserializer::Pointer myDeserializer = mitk::NavigationToolStorageDeserializer::New(m_DataStorage);
276  std::string filename = QFileDialog::getOpenFileName(nullptr, tr("Open Navigation Tool Storage"), QmitkIGTCommonHelper::GetLastFileLoadPath(), tr("IGT Tool Storage (*.IGTToolStorage)")).toStdString();
277  if (filename == "") return;
278 
279  QmitkIGTCommonHelper::SetLastFileLoadPathByFileName(QString::fromStdString(filename));
280 
281  mitk::NavigationToolStorage::Pointer tempStorage = nullptr;
282 
283  try
284  {
285  tempStorage = myDeserializer->Deserialize(filename);
286  }
287  catch (const mitk::Exception& exception)
288  {
289  MessageBox(exception.GetDescription());
290  }
291 
292  if (tempStorage.IsNotNull())
293  {
294  Poco::Path myPath = Poco::Path(filename.c_str());
295  tempStorage->SetName(myPath.getFileName()); //set the filename as name for the storage, so the user can identify it
296  this->LoadStorage(tempStorage);
297  emit NewStorageAdded(m_NavigationToolStorage,myPath.getFileName());
299  }
300 }
301 
303 {
304  QFileDialog *fileDialog = new QFileDialog;
305  fileDialog->setDefaultSuffix("IGTToolStorage");
306  QString suffix = "IGT Tool Storage (*.IGTToolStorage)";
307  // Set default file name to LastFileSavePath + storage name
308  QString defaultFileName = QmitkIGTCommonHelper::GetLastFileSavePath() + "/" + QString::fromStdString(m_NavigationToolStorage->GetName());
309  QString filename = fileDialog->getSaveFileName(nullptr, tr("Save Navigation Tool Storage"), defaultFileName, suffix, &suffix);
310 
311  if (filename.isEmpty()) return; //canceled by the user
312 
313  // check file suffix
314  QFileInfo file(filename);
315  if(file.suffix().isEmpty()) filename += ".IGTToolStorage";
316 
317  //serialize tool storage
318  mitk::NavigationToolStorageSerializer::Pointer mySerializer = mitk::NavigationToolStorageSerializer::New();
319 
320  try
321  {
322  mySerializer->Serialize(filename.toStdString(), m_NavigationToolStorage);
323  }
324  catch (const mitk::IGTIOException& e)
325  {
326  MessageBox("Error: " + std::string(e.GetDescription()));
327  return;
328  }
329 
330  QmitkIGTCommonHelper::SetLastFileSavePath(file.absolutePath());
331  Poco::Path myPath = Poco::Path(filename.toStdString());
332  m_Controls->m_StorageName->setText(QString::fromStdString(myPath.getFileName()));
333 }
334 
335 //##################################################################################
336 //############################## slots: add tool widget ############################
337 //##################################################################################
338 
340 {
341  //Proof, if the NavigationToolStorage is locked. If it is locked, show MessageBox to user.
342  if( m_NavigationToolStorage->isLocked() )
343  {
344  MessageBox( "The storage is locked and it cannot be modified. Maybe the tracking device which " \
345  "uses this storage is connected. If you want to modify the storage please " \
346  "disconnect the device first.");
347  return;
348  }
349 
350  mitk::NavigationTool::Pointer newTool = m_Controls->m_ToolCreationWidget->GetCreatedTool();
351 
352  if (m_edit) //here we edit a existing tool
353  {
354  mitk::NavigationTool::Pointer editedTool = m_NavigationToolStorage->GetTool(m_Controls->m_ToolList->currentIndex().row());
355  editedTool->Graft(newTool);
356  //Keep this line, 'cause otherwise, the NavigationToolStorage wouldn't notice, that the toolStorage changed.
357  m_NavigationToolStorage->UpdateMicroservice();
358  }
359  else //here we create a new tool
360  {
361  m_NavigationToolStorage->AddTool(newTool);
362  }
363 
364  //Remove tool preview
365  m_DataStorage->Remove(m_DataStorage->GetNamedNode("Tool preview"));
366 
367  UpdateToolTable();
368 
369  m_Controls->m_MainWidgets->setCurrentIndex(0);
370 
371  m_Controls->m_ToolInformation->setText("");
372 }
373 
375 {
376  m_Controls->m_MainWidgets->setCurrentIndex(0);
377  //Remove tool preview
378  m_DataStorage->Remove(m_DataStorage->GetNamedNode("Tool preview"));
379 }
380 
382 {
383  QString _label = "Information for tool " + m_Controls->m_ToolList->currentItem()->text() + "\n";
384  _label.append(QString(m_NavigationToolStorage->GetTool(m_Controls->m_ToolList->currentIndex().row())->GetStringWithAllToolInformation().c_str()));
385  m_Controls->m_ToolInformation->setText(_label);
386 }
387 
388 //##################################################################################
389 //############################## private help methods ##############################
390 //##################################################################################
392 {
393  m_Controls->m_ToolList->clear();
394  if (m_NavigationToolStorage.IsNull()) return;
395  for (unsigned int i = 0; i < m_NavigationToolStorage->GetToolCount(); i++)
396  {
397  QString currentTool = "Tool" + QString::number(i) + ": " + QString(m_NavigationToolStorage->GetTool(i)->GetToolName().c_str()) + " ";
398 
399  currentTool += "(" + QString::fromStdString(m_NavigationToolStorage->GetTool(i)->GetTrackingDeviceType()) + "/";
400 
401  switch (m_NavigationToolStorage->GetTool(i)->GetType())
402  {
404  currentTool += "Instrument)"; break;
406  currentTool += "Fiducial)"; break;
408  currentTool += "Skinmarker)"; break;
409  default:
410  currentTool += "Unknown)";
411  }
412  m_Controls->m_ToolList->addItem(currentTool);
413  }
414 }
415 
417 {
418  QMessageBox msgBox;
419  msgBox.setText(s.c_str());
420  msgBox.exec();
421 }
422 
424 {
425  m_Controls->m_StorageName->setText("<none>");
426  m_Controls->m_AddTool->setEnabled(false);
427  m_Controls->m_LoadTool->setEnabled(false);
428  m_Controls->m_MoveToolUp->setEnabled(false);
429  m_Controls->m_MoveToolDown->setEnabled(false);
430  m_Controls->m_selectedLabel->setEnabled(false);
431  m_Controls->m_DeleteTool->setEnabled(false);
432  m_Controls->m_EditTool->setEnabled(false);
433  m_Controls->m_SaveTool->setEnabled(false);
434  m_Controls->m_ToolList->setEnabled(false);
435  m_Controls->m_SaveStorage->setEnabled(false);
436  m_Controls->m_ToolLabel->setEnabled(false);
437 }
438 
440 {
441  m_Controls->m_AddTool->setEnabled(true);
442  m_Controls->m_LoadTool->setEnabled(true);
443  m_Controls->m_MoveToolUp->setEnabled(true);
444  m_Controls->m_MoveToolDown->setEnabled(true);
445  m_Controls->m_selectedLabel->setEnabled(true);
446  m_Controls->m_DeleteTool->setEnabled(true);
447  m_Controls->m_EditTool->setEnabled(true);
448  m_Controls->m_SaveTool->setEnabled(true);
449  m_Controls->m_ToolList->setEnabled(true);
450  m_Controls->m_SaveStorage->setEnabled(true);
451  m_Controls->m_ToolLabel->setEnabled(true);
452 }
static const QString GetLastFileLoadPath()
Data management class that handles &#39;was created by&#39; relations.
virtual void CreateConnections()
Creation of the connections.
static void SetLastFileSavePath(const QString &str)
static void SetLastFileLoadPathByFileName(const QString &str)
mitk::NavigationToolStorage::Pointer m_NavigationToolStorage
holds the NavigationToolStorage we are working with.
An object of this class represents an exception of the MITK-IGT module which are releated to the inpu...
bool m_edit
shows if we are in edit mode, if not we create new navigation tool objects.
mitk::DataStorage * m_DataStorage
holds the DataStorage
#define MITK_WARN
Definition: mitkLogMacros.h:19
An object of this class represents an exception of MITK. Please don&#39;t instantiate exceptions manually...
Definition: mitkException.h:45
static RenderingManager * GetInstance()
void LoadStorage(mitk::NavigationToolStorage::Pointer storageToLoad)
void NewStorageAdded(mitk::NavigationToolStorage::Pointer newStorage, std::string storageName)
static void SetLastFileSavePathByFileName(const QString &str)
static Pointer New()
Constructs a NavigationToolStorage without reference to a DataStorage. The Data Nodes of tools have t...
virtual void Remove(const DataNode *node)=0
Removes node from the DataStorage.
Ui::QmitkNavigationToolManagementWidgetControls * m_Controls
QmitkNavigationToolManagementWidget(QWidget *parent=nullptr, Qt::WindowFlags f=nullptr)
virtual void InitializeViewsByBoundingObjects(const DataStorage *)
Initializes the renderwindows by the aggregated geometry of all objects that are held in the data sto...
static const QString GetLastFileSavePath()
DataNode * GetNamedNode(const char *name) const
Convenience method to get the first node with a given name.