Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
QmitkInitialValuesModel.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 
13 #include <QColor>
14 #include <QSize>
15 
16 #include "mitkExceptionMacro.h"
17 #include "mitkImage.h"
19 
21 
22 
24 QmitkInitialValuesModel(QObject* parent) :
25  QAbstractTableModel(parent), m_modified(false)
26 {
27 }
28 
29 void
33 {
34  if (names.size() != values.size())
35  {
36  mitkThrow() <<
37  "Error. Cannot set initial value model. Passed parameter names vector and default values vector have different size.";
38  }
39 
40  emit beginResetModel();
41 
42  this->m_ParameterNames = names;
43  this->m_Values = values;
44 
45  this->m_modified = false;
46 
47  emit endResetModel();
48 };
49 
50 void
53 {
55  values.set_size(names.size());
56  values.fill(0.0);
57 
58  setInitialValues(names, values);
59 };
60 
61 void
63 addInitialParameterImage(const mitk::DataNode* node, mitk::ModelTraitsInterface::ParametersType::size_type paramIndex)
64 {
65  if (!node) mitkThrow() << "Try to set a null ptr as initial value source image.";
66 
67  if (!dynamic_cast<mitk::Image*>(node->GetData())) mitkThrow() << "Error. Passed node does not contain an image.";
68 
69  emit beginResetModel();
70 
71  this->m_ParameterImageMap[paramIndex] = node;
72 
73  emit endResetModel();
74 };
75 
77 {
78  emit beginResetModel();
79 
80  this->m_ParameterImageMap.clear();
81 
82  emit endResetModel();
83 };
84 
88 {
89  return this->m_Values;
90 };
91 
95 {
97  initDelegate->SetInitialParameterization(m_Values);
98 
99  for (const auto& pos : this->m_ParameterImageMap)
100  {
101  initDelegate->AddInitialParameterImage(dynamic_cast<mitk::Image*>(pos.second->GetData()), pos.first);
102  }
103 
104  return initDelegate.GetPointer();
105 };
106 
107 bool
110 {
111  for (const auto& pos : this->m_ParameterImageMap)
112  {
113  if (pos.second.IsNull()) return false;
114  }
115  return true;
116 };
117 
118 int
120 rowCount(const QModelIndex& parent) const
121 {
122  if (parent.isValid())
123  {
124  return 0;
125  }
126 
127  return m_Values.size();
128 }
129 
130 int
132 columnCount(const QModelIndex& parent) const
133 {
134  if (parent.isValid())
135  {
136  return 0;
137  }
138 
139  return 3;
140 }
141 
142 int
143 QmitkInitialValuesModel::
144 valueType(const QModelIndex& index) const
145 {
146  if (m_ParameterImageMap.find(index.row()) != m_ParameterImageMap.end())
147  { //image type
148  return 1;
149  }
150  else
151  { //simple scalar
152  return 0;
153  }
154 }
155 
156 QVariant
158 data(const QModelIndex& index, int role) const
159 {
160  if (!index.isValid())
161  {
162  return QVariant();
163  }
164 
165  QVariant result;
166 
167  if (index.row() < static_cast<int>(m_Values.size()))
168  {
169  switch (index.column())
170  {
171  case 0:
172  if (role == Qt::DisplayRole || role == Qt::EditRole)
173  {
174  result = QVariant(QString::fromStdString(m_ParameterNames[index.row()]));
175  }
176  else if (role == Qt::ToolTipRole)
177  {
178  result = QVariant("Name of the parameter.");
179  }
180 
181  break;
182 
183  case 1:
184  if (role == Qt::DisplayRole)
185  {
186  if (valueType(index) == 1)
187  { //image type
188  result = QVariant("image");
189  }
190  else
191  { //simple scalar
192  result = QVariant("scalar");
193  }
194  }
195  else if (role == Qt::EditRole)
196  {
197  result = QVariant(valueType(index));
198  }
199  else if (role == Qt::ToolTipRole)
200  {
201  result = QVariant("type of the inital value.");
202  }
203  break;
204 
205  case 2:
206  if (role == Qt::DisplayRole || role == Qt::EditRole)
207  {
208  const auto& finding = m_ParameterImageMap.find(index.row());
209  if (finding != m_ParameterImageMap.end())
210  { //image type -> return the name
211  if (finding->second.IsNotNull())
212  {
213  result = QVariant(finding->second->GetName().c_str());
214  }
215  else
216  {
217  result = QVariant("Invalid. Select image.");
218  }
219  }
220  else
221  { //simple scalar
222  result = QVariant(m_Values(index.row()));
223  }
224  }
225  else if (role == Qt::UserRole)
226  {
227  result = QVariant(valueType(index));
228  }
229  else if (role == Qt::ToolTipRole)
230  {
231  result = QVariant("Initial values for the parameter.");
232  }
233 
234  break;
235  }
236  }
237 
238  return result;
239 }
240 
241 Qt::ItemFlags
243 flags(const QModelIndex& index) const
244 {
245  Qt::ItemFlags flags = QAbstractItemModel::flags(index);
246 
247  if (index.row() < static_cast<int>(m_Values.size()))
248  {
249  if (index.column() > 0)
250  {
251  flags |= Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
252  }
253  else
254  {
255  flags |= Qt::ItemIsEnabled | Qt::ItemIsSelectable;
256  }
257  }
258 
259  return flags;
260 }
261 
262 QVariant
264 headerData(int section, Qt::Orientation orientation, int role) const
265 {
266  if ((Qt::DisplayRole == role) &&
267  (Qt::Horizontal == orientation))
268  {
269  if (section == 0)
270  {
271  return QVariant("Parameters");
272  }
273  else if (section == 1)
274  {
275  return QVariant("Type");
276  }
277  else if (section == 2)
278  {
279  return QVariant("Value");
280  }
281  }
282 
283  return QVariant();
284 }
285 
286 bool
288 setData(const QModelIndex& index, const QVariant& value, int role)
289 {
290  if (!index.isValid() || index.row() >= static_cast<int>(m_Values.size()) || (index.column() == 0))
291  {
292  return false;
293  }
294 
295  if (Qt::EditRole == role)
296  {
297  emit dataChanged(index, index);
298 
299  emit beginResetModel();
300 
301  bool result = false;
302  if (index.column() == 1)
303  {
304  if (value.toInt() == 0)
305  {
306  this->m_ParameterImageMap.erase(index.row());
307  m_modified = true;
308  result = true;
309  }
310  else
311  {
312  this->m_ParameterImageMap[index.row()] = nullptr;
313  m_modified = true;
314  result = true;
315  }
316  }
317  else
318  {
319  if (valueType(index) == 0)
320  {
321  m_Values[index.row()] = value.toDouble();
322  m_modified = true;
323  result = true;
324  }
325  else
326  {
327  mitk::DataNode *node = static_cast<mitk::DataNode*>(value.value<void*>());
328  if (node && dynamic_cast<mitk::Image*>(node->GetData()))
329  {
330  this->m_ParameterImageMap[index.row()] = node;
331  m_modified = true;
332  result = true;
333  }
334  }
335  }
336 
337  emit endResetModel();
338 
339  return result;
340  }
341 
342  return false;
343 };
344 
346 {
347  return m_modified;
348 }
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
mitk::InitialParameterizationDelegateBase::Pointer getInitialParametrizationDelegate() const
itk::Array< ParameterValueType > ParametersType
BaseData * GetData() const
Get the data object (instance of BaseData, e.g., an Image) managed by this DataNode.
std::vector< ParameterNameType > ParameterNamesType
int columnCount(const QModelIndex &parent=QModelIndex()) const override
#define mitkThrow()
Qt::ItemFlags flags(const QModelIndex &index) const override
QVariant data(const QModelIndex &index, int role) const override
mitk::ModelTraitsInterface::ParametersType getInitialValues() const
QVariant headerData(int section, Qt::Orientation orientation, int role) const override
QmitkInitialValuesModel(QObject *parent=nullptr)
void setInitialValues(const mitk::ModelTraitsInterface::ParameterNamesType &names, const mitk::ModelTraitsInterface::ParametersType values)
void addInitialParameterImage(const mitk::DataNode *node, mitk::ModelTraitsInterface::ParametersType::size_type paramIndex)
int rowCount(const QModelIndex &parent=QModelIndex()) const override
Class for nodes of the DataTree.
Definition: mitkDataNode.h:57