Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
QmitkInitialValuesDelegate.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 
17 
18 #include <QPainter>
19 #include <QApplication>
20 #include <QLabel>
21 #include <QMouseEvent>
22 #include <QListWidget>
23 
24 QmitkInitialValuesDelegate::QmitkInitialValuesDelegate(QObject* /*parent*/) : m_Storage(nullptr), m_Predicate(nullptr)
25 {
26 }
27 
29  const QStyleOptionViewItem& option
30  , const QModelIndex& index) const
31 {
32  QVariant data = index.data(Qt::EditRole);
33 
34  if (data.isValid())
35  {
36  if (valueType(index)==0)
37  {
38  return QStyledItemDelegate::createEditor(parent, option, index);
39  }
40  else
41  {
43 
44  return box;
45  }
46 
47  }
48  else
49  {
50  return new QLabel(data.toString(), parent);
51  }
52 }
53 
54 int
56 valueType(const QModelIndex& index) const
57 {
58  QVariant data = index.data(Qt::UserRole);
59 
60  return data.toInt();
61 }
62 
64  const QModelIndex& index) const
65 {
66  QVariant data = index.data(Qt::EditRole);
67 
68  if (data.isValid())
69  {
70  if (valueType(index) == 0)
71  {
72  return QStyledItemDelegate::setEditorData(editor, index);
73  }
74  else
75  {
76  QmitkDataStorageComboBox* box = qobject_cast<QmitkDataStorageComboBox*>(editor);
77 
78  mitk::DataNode *node = static_cast<mitk::DataNode*>(data.value<void*>());
79 
80  auto index = box->Find(node);
81  box->setCurrentIndex(index);
82  }
83  }
84 }
85 
86 void QmitkInitialValuesDelegate::setModelData(QWidget* editor, QAbstractItemModel* model
87  , const QModelIndex& index) const
88 {
89  QVariant data = index.data(Qt::EditRole);
90 
91  if (data.isValid() && valueType(index) == 1)
92  {
93  QmitkDataStorageComboBox* box = qobject_cast<QmitkDataStorageComboBox*>(editor);
94  QVariant newNode = qVariantFromValue<void*>(static_cast<void*>(box->GetSelectedNode().GetPointer()));
95  model->setData(index, newNode);
96  }
97  else
98  {
99  QStyledItemDelegate::setModelData(editor, model, index);
100  }
101 }
102 
105 {
106  this->m_Storage = storage;
107 };
108 
111 {
112  this->m_Predicate = predicate;
113 };
virtual mitk::DataNode::Pointer GetSelectedNode() const
Returns the selected dataNode or 0 if there is none.
Data management class that handles &#39;was created by&#39; relations.
Displays all or a subset (defined by a predicate) of nodes of the Data Storage.
void setEditorData(QWidget *editor, const QModelIndex &index) const override
mitk::DataStorage::Pointer m_Storage
int valueType(const QModelIndex &index) const
QmitkInitialValuesDelegate(QObject *parent=nullptr)
void setDataStorage(mitk::DataStorage *storage)
virtual int Find(const mitk::DataNode *dataNode) const
Searches for a given node and returns a valid index or -1 if the node was not found.
void setNodePredicate(mitk::NodePredicateBase *predicate)
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override
mitk::NodePredicateBase::Pointer m_Predicate
Interface for evaluation conditions used in the DataStorage class GetSubset() method.
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override
Class for nodes of the DataTree.
Definition: mitkDataNode.h:57