Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
QmitkNDIToolDelegate.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 
17 
18 #include "QmitkNDIToolDelegate.h"
19 #include <QStringList>
20 #include <QComboBox>
21 #include <QLabel>
22 #include <QFileDialog>
23 
24 #include "QmitkEnums.h"
26 #include "QmitkCustomVariants.h"
27 #include "mitkDataStorage.h"
28 #include "mitkNodePredicateBase.h"
30 #include "mitkNDIPassiveTool.h"
31 
32 Q_DECLARE_METATYPE(mitk::NDIPassiveTool*)
33 
34 
35 QmitkNDIToolDelegate::QmitkNDIToolDelegate(QObject * parent) : QStyledItemDelegate(parent),
36 m_Types(), m_DataStorage(nullptr), m_Predicate(nullptr), m_TagProperty(nullptr), m_TagPropertyName()
37 {
38 }
39 
40 
41 QWidget* QmitkNDIToolDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
42 {
43  if (index.isValid() == false)
44  return QStyledItemDelegate::createEditor(parent, option, index);
45 
46  switch (index.column())
47  {
48  case SROMCol:
49  {
50  return new QLabel("", parent);
51  }
52  case TypeCol:
53  {
54  auto c = new QComboBox(parent);
55  c->addItems(m_Types);
56  return c;
57  }
58  case NodeCol:
59  {
60  return new QmitkDataStorageComboBox(m_DataStorage, m_Predicate, parent);
61  }
62  case IndexCol:
63  case NameCol:
64  case StatusCol:
65  default:
66  return QStyledItemDelegate::createEditor(parent, option, index);
67  }
68 }
69 
70 
71 void QmitkNDIToolDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
72 {
73  if (index.isValid() == false)
74  return;
75 
76  switch (index.column())
77  {
78  case SROMCol:
79  {
80  QLabel* l = qobject_cast<QLabel*>(editor);
81  if (l->text().isEmpty())
82  {
83  QString fileName = index.data(/*mitk::FileNameRole*/).value<QString>();
84  fileName = QFileDialog::getOpenFileName(editor, "Open SROM file", fileName, "SROM files (*.rom)");
85  QLabel* l = qobject_cast<QLabel*>(editor);
86  l->setText(fileName);
87  }
88  return;
89  }
90  case TypeCol:
91  {
92  QString type = index.data(/*mitk::TypeRole*/).value<QString>();
93  QComboBox* c = qobject_cast<QComboBox*>(editor);
94  c->setCurrentIndex(c->findText(type));
95  connect(c, SIGNAL(currentIndexChanged(int)), this, SLOT(ComboBoxCurrentIndexChanged(int)));
96  return;
97  }
98  case NodeCol:
99  {
100  mitk::DataNode::Pointer n = index.data(/*mitk::OrganNodeRole*/).value<mitk::DataNode::Pointer>();
101  if (n.IsNotNull())
102  {
103  QmitkDataStorageComboBox* dsc = qobject_cast<QmitkDataStorageComboBox*>(editor);
104  dsc->setCurrentIndex(dsc->findText(QString::fromStdString(n->GetName())));
105  connect(dsc, SIGNAL(currentIndexChanged(int)), this, SLOT(ComboBoxCurrentIndexChanged(int)));
106  }
107  return;
108  }
109  case IndexCol:
110  case NameCol:
111  case StatusCol:
112  default:
113  QStyledItemDelegate::setEditorData(editor, index);
114  }
115 }
116 
117 
118 void QmitkNDIToolDelegate::setModelData(QWidget *editor, QAbstractItemModel* model, const QModelIndex &index) const
119 {
120  if (index.isValid() == false)
121  return;
122 
123  switch (index.column())
124  {
125  case SROMCol:
126  {
127  QLabel* l = qobject_cast<QLabel*>(editor);
128  //model->setData(index, l->text(), mitk::FileNameRole);
129  //model->setData(index, l->text(), Qt::DisplayRole); // use for display too
130  model->setData(index, l->text()); // use for display too
131  return;
132  }
133  case TypeCol:
134  {
135  QComboBox* c = qobject_cast<QComboBox*>(editor);
136  //model->setData(index, c->currentText(), mitk::TypeRole);
137  model->setData(index, c->currentText(), Qt::DisplayRole);
138  return;
139  }
140  case NodeCol:
141  {
142  QmitkDataStorageComboBox* dsc = qobject_cast<QmitkDataStorageComboBox*>(editor);
143  if (dsc->GetSelectedNode().IsNotNull())
144  {
145  model->setData(index, qVariantFromValue(dsc->GetSelectedNode()), OrganNodeRole);
146  //model->setData(index, QString::fromStdString(dsc->GetSelectedNode()->GetName()), Qt::DisplayRole);
147  model->setData(index, QString::fromStdString(dsc->GetSelectedNode()->GetName()));
148  if ((m_TagProperty.IsNotNull()) && (m_TagPropertyName.empty() == false)) // tag this node as selected
149  dsc->GetSelectedNode()->SetProperty(m_TagPropertyName.c_str(), m_TagProperty);
150  }
151  }
152  return;
153  case IndexCol:
154  case NameCol:
155  case StatusCol:
156  default:
157  QStyledItemDelegate::setModelData(editor, model, index);
158  }
159 }
160 
161 void QmitkNDIToolDelegate::commitAndCloseEditor()
162 {
163  //QWidget* editor = 0;
164  //if(QPushButton *pushBtn = qobject_cast<QPushButton *>(sender()))
165  //{
166 
167  //}
168 
169  //if(editor)
170  //{
171  //emit commitData(editor);
172  //emit closeEditor(editor);
173  //}
174 
175 }
176 
177 
178 void QmitkNDIToolDelegate::ComboBoxCurrentIndexChanged( int /*index*/ )
179 {
180  if(QComboBox *comboBox = qobject_cast<QComboBox *>(sender()))
181  {
182  emit commitData(comboBox);
183  emit closeEditor(comboBox);
184  }
185 }
186 
187 
188 void QmitkNDIToolDelegate::SetTypes( const QStringList& types )
189 {
190  m_Types = types;
191 }
192 
193 
195 {
196  m_DataStorage = ds;
197 }
198 
199 
201 {
202  m_Predicate = p;
203 }
204 
205 
207 {
208  m_TagProperty = prop;
209 }
210 
211 void QmitkNDIToolDelegate::SetTagPropertyName( const std::string& name )
212 {
213  m_TagPropertyName = name;
214 }
void SetDataStorage(mitk::DataStorage *ds)
set datastorage for organ node editor
Data management class that handles 'was created by' relations.
Displays all or a subset (defined by a predicate) of nodes of the Data Storage.
void SetPredicate(mitk::NodePredicateBase::Pointer p)
set predicate for organ node editor
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override
An item delegate for rendering and editing mitk::Properties in a QTableView.
void SetTagProperty(mitk::BaseProperty::Pointer prop)
set the property that is used to tag selected nodes
mitk::DataStorage::Pointer m_DataStorage
void setEditorData(QWidget *editor, const QModelIndex &index) const override
virtual mitk::DataNode::Pointer GetSelectedNode() const
Returns the selected _DataNode or 0 if there is none.
Implementation of a passive NDI optical tool.
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override
void SetTypes(const QStringList &types)
set types list for type editor combobox
void SetTagPropertyName(const std::string &name)
set name of the property that is used to tag selected nodes