Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
QmitkDataStorageLayerStackModel.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"
18 
21 {
22  // nothing here
23 }
24 
26 {
27  UpdateModelData();
28 }
29 
31 {
32  UpdateModelData();
33 }
34 
36 {
37  // nothing here; layers (nodes) are only added after button click
38 }
39 
41 {
42  UpdateModelData();
43 }
44 
46 {
47  UpdateModelData();
48 }
49 
50 void QmitkDataStorageLayerStackModel::SetCurrentRenderer(const std::string& renderWindowName)
51 {
52  if (!m_DataStorage.IsExpired())
53  {
54  m_BaseRenderer = mitk::BaseRenderer::GetByName(renderWindowName);
55  UpdateModelData();
56  }
57 }
58 
60 {
61  return m_BaseRenderer.Lock();
62 }
63 
64 QModelIndex QmitkDataStorageLayerStackModel::index(int row, int column, const QModelIndex& parent) const
65 {
66  bool hasIndex = this->hasIndex(row, column, parent);
67  if (hasIndex)
68  {
69  return this->createIndex(row, column);
70  }
71 
72  return QModelIndex();
73 }
74 
75 QModelIndex QmitkDataStorageLayerStackModel::parent(const QModelIndex& /*child*/) const
76 {
77  return QModelIndex();
78 }
79 
80 Qt::ItemFlags QmitkDataStorageLayerStackModel::flags(const QModelIndex& index) const
81 {
82  Qt::ItemFlags flags = Qt::NoItemFlags;
83  if (index.isValid() && index.model() == this)
84  {
85  flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
86  if (0 == index.column())
87  {
88  flags |= Qt::ItemIsUserCheckable;
89  }
90  }
91 
92  return flags;
93 }
94 
95 QVariant QmitkDataStorageLayerStackModel::headerData(int section, Qt::Orientation orientation, int role) const
96 {
97  if (Qt::Horizontal == orientation && Qt::DisplayRole == role)
98  {
99  if (0 == section)
100  {
101  return QVariant("Visibility");
102  }
103  else if (1 == section)
104  {
105  return QVariant("Data node");
106  }
107  }
108  return QVariant();
109 }
110 
111 int QmitkDataStorageLayerStackModel::rowCount(const QModelIndex& parent/* = QModelIndex()*/) const
112 {
113  if (parent.isValid())
114  {
115  return 0;
116  }
117 
118  return static_cast<int>(m_TempLayerStack.size());
119 }
120 
121 int QmitkDataStorageLayerStackModel::columnCount(const QModelIndex& parent/* = QModelIndex()*/) const
122 {
123  if (parent.isValid())
124  {
125  return 0;
126  }
127 
128  return 2;
129 }
130 
131 QVariant QmitkDataStorageLayerStackModel::data(const QModelIndex& index, int role) const
132 {
133  if (!index.isValid() || index.model() != this)
134  {
135  return QVariant();
136  }
137 
138  if ((index.row()) < 0 || index.row() >= static_cast<int>(m_TempLayerStack.size()))
139  {
140  return QVariant();
141  }
142 
143  RenderWindowLayerUtilities::LayerStack::const_iterator layerStackIt = m_TempLayerStack.begin();
144  std::advance(layerStackIt, index.row());
145  mitk::DataNode* dataNode = layerStackIt->second;
146  if (Qt::CheckStateRole == role && 0 == index.column())
147  {
148  bool visibility = false;
149  dataNode->GetVisibility(visibility, m_BaseRenderer.Lock());
150  if (visibility)
151  {
152  return Qt::Checked;
153  }
154  else
155  {
156  return Qt::Unchecked;
157  }
158  }
159  else if (Qt::DisplayRole == role && 1 == index.column())
160  {
161  return QVariant(QString::fromStdString(dataNode->GetName()));
162  }
163  else if (Qt::ToolTipRole == role)
164  {
165  if (0 == index.column())
166  {
167  return QVariant("Show/hide data node.");
168  }
169  else if (1 == index.column())
170  {
171  return QVariant("Name of the data node.");
172  }
173  }
174  else if (QmitkDataNodeRole == role)
175  {
176  return QVariant::fromValue<mitk::DataNode::Pointer>(mitk::DataNode::Pointer(dataNode));
177  }
178  else if (QmitkDataNodeRawPointerRole == role)
179  {
180  return QVariant::fromValue<mitk::DataNode*>(dataNode);
181  }
182 
183  return QVariant();
184 }
185 
186 bool QmitkDataStorageLayerStackModel::setData(const QModelIndex &index, const QVariant &value, int role /*= Qt::EditRole*/)
187 {
188  if (!index.isValid() || index.model() != this)
189  {
190  return false;
191  }
192 
193  if ((index.row()) < 0 || index.row() >= static_cast<int>(m_TempLayerStack.size()))
194  {
195  return false;
196  }
197 
198  if (!m_BaseRenderer.IsExpired())
199  {
200  auto baseRenderer = m_BaseRenderer.Lock();
201 
202  RenderWindowLayerUtilities::LayerStack::const_iterator layerStackIt = m_TempLayerStack.begin();
203  std::advance(layerStackIt, index.row());
204  mitk::DataNode* dataNode = layerStackIt->second;
205  if (Qt::CheckStateRole == role)
206  {
207  Qt::CheckState newCheckState = static_cast<Qt::CheckState>(value.toInt());
208 
209  if (Qt::PartiallyChecked == newCheckState || Qt::Checked == newCheckState)
210  {
211  dataNode->SetVisibility(true, baseRenderer);
212  }
213  else
214  {
215  dataNode->SetVisibility(false, baseRenderer);
216  }
217  emit dataChanged(index, index);
218  mitk::RenderingManager::GetInstance()->RequestUpdate(baseRenderer->GetRenderWindow());
219  return true;
220  }
221  }
222 
223  return false;
224 }
225 
226 void QmitkDataStorageLayerStackModel::UpdateModelData()
227 {
228  // update the model, so that the table will be filled with the nodes according to the current
229  // data storage and base renderer
230  beginResetModel();
231  // get the current layer stack of the given base renderer
232  m_TempLayerStack = RenderWindowLayerUtilities::GetLayerStack(m_DataStorage.Lock(), m_BaseRenderer.Lock(), true);
233  endResetModel();
234 }
void SetCurrentRenderer(const std::string &rendererName)
QModelIndex index(int row, int column, const QModelIndex &parent) const
overridden functions from QAbstractItemModel
itk::SmartPointer< T > Lock() const
void SetVisibility(bool visible, const mitk::BaseRenderer *renderer=nullptr, const char *propertyKey="visible")
Convenience method for setting visibility properties (instances of BoolProperty)
int columnCount(const QModelIndex &parent=QModelIndex()) const override
void NodeRemoved(const mitk::DataNode *node) override
Organizes the rendering process.
QModelIndex parent(const QModelIndex &child) const
void NodeChanged(const mitk::DataNode *node) override
QVariant data(const QModelIndex &index, int role) const override
QmitkDataStorageLayerStackModel(QObject *parent=nullptr)
itk::SmartPointer< Self > Pointer
Definition: mitkDataNode.h:71
Qt::ItemFlags flags(const QModelIndex &index) const override
static RenderingManager * GetInstance()
bool IsExpired() const noexcept
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
int rowCount(const QModelIndex &parent=QModelIndex()) const override
bool GetVisibility(bool &visible, const mitk::BaseRenderer *renderer, const char *propertyKey="visible") const
Convenience access method for visibility properties (instances of BoolProperty with property-key "vis...
Definition: mitkDataNode.h:422
QVariant headerData(int section, Qt::Orientation orientation, int role) const override
MITKRENDERWINDOWMANAGER_EXPORT LayerStack GetLayerStack(const DataStorage *dataStorage, const BaseRenderer *renderer, bool withBaseNode)
Return the stack of layers of the given renderer as std::map<int, DataNode::Pointer>, which guarantees ordering of the layers. Stacked layers are only included if they have their "fixedLayer" property set to true and their "layer" property set.
void NodeAdded(const mitk::DataNode *node) override
void RequestUpdate(vtkRenderWindow *renderWindow)
static BaseRenderer * GetByName(const std::string &name)
Class for nodes of the DataTree.
Definition: mitkDataNode.h:57
mitk::WeakPointer< mitk::DataStorage > m_DataStorage