Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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,
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 
19 //mitk headers
20 #include "mitkTrackingTypes.h"
21 #include <mitkSurface.h>
27 
28 //qt headers
29 #include <qfiledialog.h>
30 #include <qinputdialog.h>
31 #include <qmessagebox.h>
32 
33 //poco headers
34 #include <Poco/Path.h>
35 
36 const std::string QmitkNavigationToolManagementWidget::VIEW_ID = "org.mitk.views.navigationtoolmanagementwidget";
37 
39  : QWidget(parent, f)
40 {
41  m_Controls = NULL;
42  CreateQtPartControl(this);
44 }
45 
47 {
48 }
49 
51 {
52  if (!m_Controls)
53  {
54  // create GUI widgets
55  m_Controls = new Ui::QmitkNavigationToolManagementWidgetControls;
56  m_Controls->setupUi(parent);
57  }
58 
59  //Disable StorageControls in the beginning, because there is no storage to edit
61 }
62 
64 {
65  if (m_NavigationToolStorage->isLocked())
66  {
67  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.");
68  return;
69  }
71  std::string filename = QFileDialog::getOpenFileName(NULL, tr("Add Navigation Tool"), "/", "*.IGTTool").toLatin1().data();
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  }
83  m_NavigationToolStorage->UpdateMicroservice();
84  }
85 }
86 
88 {
89  //if no item is selected, show error message:
90  if (m_Controls->m_ToolList->currentItem() == NULL) { MessageBox("Error: Please select tool first!"); return; }
91 
93  std::string filename = QFileDialog::getSaveFileName(NULL, tr("Save Navigation Tool"), "/", "*.IGTTool").toLatin1().data();
94  filename.append(".IGTTool");
95  if (filename == "") return;
96  if (!myWriter->DoWrite(filename, m_NavigationToolStorage->GetTool(m_Controls->m_ToolList->currentIndex().row())))
97  MessageBox("Error: " + myWriter->GetErrorMessage());
98 }
99 
101 {
102  if (m_Controls)
103  {
104  //main widget page:
105  connect((QObject*)(m_Controls->m_AddTool), SIGNAL(clicked()), this, SLOT(OnAddTool()));
106  connect((QObject*)(m_Controls->m_DeleteTool), SIGNAL(clicked()), this, SLOT(OnDeleteTool()));
107  connect((QObject*)(m_Controls->m_EditTool), SIGNAL(clicked()), this, SLOT(OnEditTool()));
108  connect((QObject*)(m_Controls->m_MoveToolUp), SIGNAL(clicked()), this, SLOT(OnMoveToolUp()));
109  connect((QObject*)(m_Controls->m_MoveToolDown), SIGNAL(clicked()), this, SLOT(OnMoveToolDown()));
110  connect((QObject*)(m_Controls->m_LoadStorage), SIGNAL(clicked()), this, SLOT(OnLoadStorage()));
111  connect((QObject*)(m_Controls->m_SaveStorage), SIGNAL(clicked()), this, SLOT(OnSaveStorage()));
112  connect((QObject*)(m_Controls->m_LoadTool), SIGNAL(clicked()), this, SLOT(OnLoadTool()));
113  connect((QObject*)(m_Controls->m_SaveTool), SIGNAL(clicked()), this, SLOT(OnSaveTool()));
114  connect((QObject*)(m_Controls->m_CreateNewStorage), SIGNAL(clicked()), this, SLOT(OnCreateStorage()));
115 
116  //widget page "add tool":
117  connect((QObject*)(m_Controls->m_ToolCreationWidget), SIGNAL(Canceled()), this, SLOT(OnAddToolCancel()));
118  connect((QObject*)(m_Controls->m_ToolCreationWidget), SIGNAL(NavigationToolFinished()), this, SLOT(OnAddToolSave()));
119  }
120 }
121 
123 {
124  m_DataStorage = dataStorage;
125  m_Controls->m_ToolCreationWidget->Initialize(m_DataStorage, "Tool0");
126 }
127 
129 {
130  if (storageToLoad.IsNotNull())
131  {
132  m_NavigationToolStorage = storageToLoad;
133  m_Controls->m_StorageName->setText(m_NavigationToolStorage->GetName().c_str());
135  }
136  else
137  {
140  }
141  UpdateToolTable();
142  m_NavigationToolStorage->UpdateMicroservice();
143 }
144 
145 //##################################################################################
146 //############################## slots: main widget ################################
147 //##################################################################################
148 
150 {
151  if (m_NavigationToolStorage.IsNotNull())
152  {
153  int toolIndex = m_Controls->m_ToolList->currentIndex().row();
154  if (toolIndex >= 0)
155  {
156  mitk::NavigationTool::Pointer currentNavigationTool = m_NavigationToolStorage->GetTool(toolIndex);
157  if (currentNavigationTool.IsNotNull())
158  {
159  std::string currentIdentifier = currentNavigationTool->GetIdentifier();
160  int NewNumber = m_Controls->m_ToolList->currentIndex().row() - 1;
161  if (NewNumber < 0) { MITK_WARN << "Cannot move tool up because it is on the top!"; }
162  else { m_NavigationToolStorage->AssignToolNumber(currentIdentifier, NewNumber); }
163  UpdateToolTable();
164  m_NavigationToolStorage->UpdateMicroservice();
165  }
166  }
167  }
168 }
169 
171 {
172  if (m_NavigationToolStorage.IsNotNull())
173  {
174  int toolIndex = m_Controls->m_ToolList->currentIndex().row();
175  if (toolIndex >= 0)
176  {
177  mitk::NavigationTool::Pointer currentNavigationTool = m_NavigationToolStorage->GetTool(toolIndex);
178  if (currentNavigationTool.IsNotNull())
179  {
180  std::string currentIdentifier = currentNavigationTool->GetIdentifier();
181  int NewNumber = m_Controls->m_ToolList->currentIndex().row() + 1;
182  if (NewNumber >= m_NavigationToolStorage->GetToolCount()) { MITK_WARN << "Cannot move tool down because it is the last tool in this storage!"; }
183  else { m_NavigationToolStorage->AssignToolNumber(currentIdentifier, NewNumber); }
184  UpdateToolTable();
185  m_NavigationToolStorage->UpdateMicroservice();
186  }
187  }
188  }
189 }
190 
192 {
193  if (m_NavigationToolStorage->isLocked())
194  {
195  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.");
196  return;
197  }
198  QString defaultIdentifier = "NavigationTool#" + QString::number(m_NavigationToolStorage->GetToolCount());
199  QString defaultName = "NavigationTool" + QString::number(m_NavigationToolStorage->GetToolCount());
200  m_Controls->m_ToolCreationWidget->Initialize(m_DataStorage, defaultIdentifier.toStdString(), defaultName.toStdString());
201  m_edit = false;
202  m_Controls->m_MainWidgets->setCurrentIndex(1);
203  m_NavigationToolStorage->UpdateMicroservice();
204 }
205 
207 {
208  //first: some checks
209  if (m_NavigationToolStorage->isLocked())
210  {
211  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.");
212  return;
213  }
214  else if (m_Controls->m_ToolList->currentItem() == NULL) //if no item is selected, show error message:
215  {
216  MessageBox("Error: Please select tool first!");
217  return;
218  }
219 
220  m_DataStorage->Remove(m_NavigationToolStorage->GetTool(m_Controls->m_ToolList->currentIndex().row())->GetDataNode());
221  m_NavigationToolStorage->DeleteTool(m_Controls->m_ToolList->currentIndex().row());
222  UpdateToolTable();
223  m_NavigationToolStorage->UpdateMicroservice();
224 }
225 
227 {
228  if (m_NavigationToolStorage->isLocked())
229  {
230  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.");
231  return;
232  }
233  else if (m_Controls->m_ToolList->currentItem() == NULL) //if no item is selected, show error message:
234  {
235  MessageBox("Error: Please select tool first!");
236  return;
237  }
238  mitk::NavigationTool::Pointer selectedTool = m_NavigationToolStorage->GetTool(m_Controls->m_ToolList->currentIndex().row());
239  m_Controls->m_ToolCreationWidget->SetDefaultData(selectedTool);
240  m_NavigationToolStorage->SetName("test");
241  m_edit = true;
242  m_Controls->m_MainWidgets->setCurrentIndex(1);
243  m_NavigationToolStorage->UpdateMicroservice();
244 }
245 
247 {
248  QString storageName = QInputDialog::getText(NULL, "Storage Name", "Name of the new tool storage:");
249  if (storageName.isNull()) return;
251  m_NavigationToolStorage->SetName(storageName.toStdString());
252  m_Controls->m_StorageName->setText(m_NavigationToolStorage->GetName().c_str());
254  emit NewStorageAdded(m_NavigationToolStorage, storageName.toStdString());
255 }
256 
258 {
260  std::string filename = QFileDialog::getOpenFileName(NULL, tr("Open Navigation Tool Storage"), "/", tr("IGT Tool Storage (*.IGTToolStorage)")).toStdString();
261  if (filename == "") return;
262 
263  try
264  {
265  mitk::NavigationToolStorage::Pointer tempStorage = myDeserializer->Deserialize(filename);
266 
267  if (tempStorage.IsNull()) MessageBox("Error" + myDeserializer->GetErrorMessage());
268  else
269  {
270  Poco::Path myPath = Poco::Path(filename.c_str());
271  tempStorage->SetName(myPath.getFileName()); //set the filename as name for the storage, so the user can identify it
272  this->LoadStorage(tempStorage);
273  emit NewStorageAdded(m_NavigationToolStorage, myPath.getFileName());
274  }
275  }
276  catch (const mitk::Exception& exception)
277  {
278  MessageBox(exception.GetDescription());
279  }
280  m_NavigationToolStorage->UpdateMicroservice();
281 }
282 
284 {
285  //read in filename
286  QString filename = QFileDialog::getSaveFileName(NULL, tr("Save Navigation Tool Storage"), "/", tr("IGT Tool Storage (*.IGTToolStorage)"));
287  if (filename.isEmpty()) return; //canceled by the user
288 
289  // add file extension if it wasn't added by the file dialog
290  if (filename.right(15) != ".IGTToolStorage") { filename += ".IGTToolStorage"; }
291 
292  //serialize tool storage
294  if (!mySerializer->Serialize(filename.toStdString(), m_NavigationToolStorage))
295  {
296  MessageBox("Error: " + mySerializer->GetErrorMessage());
297  return;
298  }
299  Poco::Path myPath = Poco::Path(filename.toStdString());
300  m_Controls->m_StorageName->setText(QString::fromStdString(myPath.getFileName()));
301 }
302 
303 //##################################################################################
304 //############################## slots: add tool widget ############################
305 //##################################################################################
306 
308 {
309  mitk::NavigationTool::Pointer newTool = m_Controls->m_ToolCreationWidget->GetCreatedTool();
310 
311  if (m_edit) //here we edit a existing tool
312  {
313  mitk::NavigationTool::Pointer editedTool = m_NavigationToolStorage->GetTool(m_Controls->m_ToolList->currentIndex().row());
314  editedTool->Graft(newTool);
315  }
316  else //here we create a new tool
317  {
318  m_NavigationToolStorage->AddTool(newTool);
319  }
320 
321  UpdateToolTable();
322 
323  m_Controls->m_MainWidgets->setCurrentIndex(0);
324  m_NavigationToolStorage->UpdateMicroservice();
325 }
326 
328 {
329  m_Controls->m_MainWidgets->setCurrentIndex(0);
330 }
331 
332 //##################################################################################
333 //############################## private help methods ##############################
334 //##################################################################################
336 {
337  m_Controls->m_ToolList->clear();
338  if (m_NavigationToolStorage.IsNull()) return;
339  for (int i = 0; i < m_NavigationToolStorage->GetToolCount(); i++)
340  {
341  QString currentTool = "Tool" + QString::number(i) + ": " + QString(m_NavigationToolStorage->GetTool(i)->GetDataNode()->GetName().c_str()) + " ";
342 
343  currentTool += "(" + QString::fromStdString(m_NavigationToolStorage->GetTool(i)->GetTrackingDeviceType()) + "/";
344 
345  switch (m_NavigationToolStorage->GetTool(i)->GetType())
346  {
348  currentTool += "Instrument)"; break;
350  currentTool += "Fiducial)"; break;
352  currentTool += "Skinmarker)"; break;
353  default:
354  currentTool += "Unknown)";
355  }
356  m_Controls->m_ToolList->addItem(currentTool);
357  }
358 }
359 
361 {
362  QMessageBox msgBox;
363  msgBox.setText(s.c_str());
364  msgBox.exec();
365 }
366 
368 {
369  m_Controls->m_StorageName->setText("<none>");
370  m_Controls->m_AddTool->setEnabled(false);
371  m_Controls->m_LoadTool->setEnabled(false);
372  m_Controls->m_MoveToolUp->setEnabled(false);
373  m_Controls->m_MoveToolDown->setEnabled(false);
374  m_Controls->m_selectedLabel->setEnabled(false);
375  m_Controls->m_DeleteTool->setEnabled(false);
376  m_Controls->m_EditTool->setEnabled(false);
377  m_Controls->m_SaveTool->setEnabled(false);
378  m_Controls->m_ToolList->setEnabled(false);
379  m_Controls->m_SaveStorage->setEnabled(false);
380  m_Controls->m_ToolLabel->setEnabled(false);
381 }
382 
384 {
385  m_Controls->m_AddTool->setEnabled(true);
386  m_Controls->m_LoadTool->setEnabled(true);
387  m_Controls->m_MoveToolUp->setEnabled(true);
388  m_Controls->m_MoveToolDown->setEnabled(true);
389  m_Controls->m_selectedLabel->setEnabled(true);
390  m_Controls->m_DeleteTool->setEnabled(true);
391  m_Controls->m_EditTool->setEnabled(true);
392  m_Controls->m_SaveTool->setEnabled(true);
393  m_Controls->m_ToolList->setEnabled(true);
394  m_Controls->m_SaveStorage->setEnabled(true);
395  m_Controls->m_ToolLabel->setEnabled(true);
396 }
Data management class that handles 'was created by' relations.
itk::SmartPointer< Self > Pointer
virtual void CreateConnections()
Creation of the connections.
mitk::NavigationToolStorage::Pointer m_NavigationToolStorage
holds the NavigationToolStorage we are working with.
QmitkNavigationToolManagementWidget(QWidget *parent=0, Qt::WindowFlags f=0)
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:23
An object of this class represents an exception of MITK. Please don't instantiate exceptions manually...
Definition: mitkException.h:49
static const std::string filename
void LoadStorage(mitk::NavigationToolStorage::Pointer storageToLoad)
void NewStorageAdded(mitk::NavigationToolStorage::Pointer newStorage, std::string storageName)
static Pointer New()
Constructs a NavigationToolStorage without reference to a DataStorage. The Data Nodes of tools have t...
Ui::QmitkNavigationToolManagementWidgetControls * m_Controls
virtual void Remove(const mitk::DataNode *node)=0
Removes node from the DataStorage.
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.