Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
QmitkDataStorageDefaultListModel.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 // qt widgets module
16 #include "QmitkCustomVariants.h"
17 #include "QmitkEnums.h"
19 
21 {
22 }
23 
25 {
27 }
28 
30 {
32 }
33 
35 {
37 }
38 
40 {
41  // since the "NodeChanged" event is sent quite often, we check here, if it is relevant for this model
42  if (m_NodePredicate.IsNull() || m_NodePredicate->CheckNode(node))
43  {
45  return;
46  }
47 
48  // not relevant - need to check if we have to remove it
49  if (std::find(m_DataNodes.begin(), m_DataNodes.end(), node) != m_DataNodes.end())
50  {
52  }
53 }
54 
56 {
58 }
59 
60 QModelIndex QmitkDataStorageDefaultListModel::index(int row, int column, const QModelIndex &parent) const
61 {
62  bool hasIndex = this->hasIndex(row, column, parent);
63  if (hasIndex)
64  {
65  return this->createIndex(row, column);
66  }
67 
68  return QModelIndex();
69 }
70 
71 QModelIndex QmitkDataStorageDefaultListModel::parent(const QModelIndex &/*child*/) const
72 {
73  return QModelIndex();
74 }
75 
77 {
78  if (parent.isValid())
79  {
80  return 0;
81  }
82 
83  return m_DataNodes.size();
84 }
85 
87 {
88  if (parent.isValid())
89  {
90  return 0;
91  }
92 
93  return 1;
94 }
95 
96 QVariant QmitkDataStorageDefaultListModel::data(const QModelIndex &index, int role) const
97 {
98  if (!index.isValid() || index.model() != this)
99  {
100  return QVariant();
101  }
102 
103  if(index.row() < 0 || index.row() >= static_cast<int>(m_DataNodes.size()))
104  {
105  return QVariant();
106  }
107 
108  mitk::DataNode::Pointer dataNode = m_DataNodes.at(index.row());
109  QString nodeName = QString::fromStdString(dataNode->GetName());
110  if (nodeName.isEmpty())
111  nodeName = "unnamed";
112 
113  if (role == Qt::DisplayRole)
114  return nodeName;
115  else if (role == Qt::ToolTipRole)
116  return nodeName;
117  else if (role == Qt::DecorationRole)
118  {
120  return nodeDescriptor->GetIcon(dataNode);
121  }
122  else if (role == QmitkDataNodeRole)
123  {
124  return QVariant::fromValue<mitk::DataNode::Pointer>(mitk::DataNode::Pointer(dataNode));
125  }
126  else if (role == QmitkDataNodeRawPointerRole)
127  {
128  return QVariant::fromValue<mitk::DataNode *>(dataNode);
129  }
130 
131  return QVariant();
132 }
133 
134 QVariant QmitkDataStorageDefaultListModel::headerData(int /*section*/, Qt::Orientation /*orientation*/, int /*role*/) const
135 {
136  return QVariant(tr("Nodes"));
137 }
138 
139 Qt::ItemFlags QmitkDataStorageDefaultListModel::flags(const QModelIndex &index) const
140 {
141  if (index.isValid() && index.model() == this)
142  {
143  return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
144  }
145 
146  return Qt::NoItemFlags;
147 }
148 
150 {
151  mitk::DataStorage::SetOfObjects::ConstPointer dataNodes;
152  if (!m_DataStorage.IsExpired())
153  {
154  auto dataStorage = m_DataStorage.Lock();
155  if (dataStorage.IsNotNull() && m_NodePredicate.IsNotNull())
156  {
157  dataNodes = dataStorage->GetSubset(m_NodePredicate);
158  }
159  else
160  {
161  dataNodes = dataStorage->GetAll();
162  }
163  }
164 
165  // update the model, so that it will be filled with the nodes of the new data storage
166  beginResetModel();
167  m_DataNodes.clear();
168 
169  // add all (filtered) nodes to the vector of nodes
170  if (dataNodes != nullptr)
171  {
172  for (auto& node : *dataNodes)
173  {
174  m_DataNodes.push_back(node);
175  }
176  }
177  endResetModel();
178 }
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
itk::SmartPointer< T > Lock() const
Decorator class for mitk::DataNode.
int columnCount(const QModelIndex &parent=QModelIndex()) const override
int rowCount(const QModelIndex &parent=QModelIndex()) const override
std::vector< mitk::DataNode::Pointer > m_DataNodes
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
itk::SmartPointer< Self > Pointer
Definition: mitkDataNode.h:71
QmitkNodeDescriptor * GetDescriptor(const mitk::DataNode *node) const
mitk::NodePredicateBase::ConstPointer m_NodePredicate
void NodeChanged(const mitk::DataNode *node) override
bool IsExpired() const noexcept
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
Qt::ItemFlags flags(const QModelIndex &index) const override
QModelIndex parent(const QModelIndex &child) const override
void NodeRemoved(const mitk::DataNode *node) override
virtual QIcon GetIcon(const mitk::DataNode *node) const
static QmitkNodeDescriptorManager * GetInstance()
Class for nodes of the DataTree.
Definition: mitkDataNode.h:57
void NodeAdded(const mitk::DataNode *node) override
mitk::WeakPointer< mitk::DataStorage > m_DataStorage