Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
QmitkMAPAlgorithmModel.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 "QmitkMAPAlgorithmModel.h"
14 
15 #include "mapMetaProperty.h"
16 
17 QmitkMAPAlgorithmModel::QmitkMAPAlgorithmModel(QObject *parent) : QAbstractTableModel(parent), m_pMetaInterface(nullptr)
18 {
19 }
20 
21 void QmitkMAPAlgorithmModel::SetAlgorithm(map::algorithm::RegistrationAlgorithmBase *pAlgorithm)
22 {
23  this->SetAlgorithm(dynamic_cast<map::algorithm::facet::MetaPropertyAlgorithmInterface *>(pAlgorithm));
24 };
25 
26 void QmitkMAPAlgorithmModel::SetAlgorithm(map::algorithm::facet::MetaPropertyAlgorithmInterface *pMetaInterface)
27 {
28  emit beginResetModel();
29 
30  m_pMetaInterface = pMetaInterface;
31  this->UpdateMetaProperties();
32 
33  emit endResetModel();
34 };
35 
36 int QmitkMAPAlgorithmModel::rowCount(const QModelIndex &parent) const
37 {
38  if (parent.isValid())
39  {
40  return 0;
41  }
42 
43  return m_MetaProperties.size();
44 }
45 
46 int QmitkMAPAlgorithmModel::columnCount(const QModelIndex &parent) const
47 {
48  if (parent.isValid())
49  return 0;
50 
51  return 2;
52 }
53 
54 QVariant QmitkMAPAlgorithmModel::data(const QModelIndex &index, int role) const
55 {
56  if (!index.isValid())
57  return QVariant();
58 
59  if (!m_pMetaInterface)
60  {
61  return QVariant();
62  }
63 
64  QVariant result;
65 
66  if (index.row() < static_cast<int>(m_MetaProperties.size()))
67  {
68  map::algorithm::MetaPropertyInfo *pInfo = m_MetaProperties[index.row()];
69 
70  switch (index.column())
71  {
72  case 0:
73  if (Qt::DisplayRole == role && index.row() < static_cast<int>(m_MetaProperties.size()))
74  {
75  result = QVariant(pInfo->getName().c_str());
76  }
77  break;
78  case 1:
79  if (Qt::DisplayRole == role && !pInfo->isReadable())
80  {
81  result = QVariant("value is not accessible");
82  }
83  else if (pInfo->isReadable() && (Qt::DisplayRole == role || (Qt::EditRole == role && pInfo->isWritable())))
84  { // should also be readable to be sensible editable in the GUI
85  result = GetPropertyValue(pInfo, role);
86  }
87  break;
88  }
89  }
90 
91  return result;
92 }
93 
94 Qt::ItemFlags QmitkMAPAlgorithmModel::flags(const QModelIndex &index) const
95 {
96  Qt::ItemFlags flags = QAbstractItemModel::flags(index);
97 
98  if (index.row() < static_cast<int>(m_MetaProperties.size()))
99  {
100  map::algorithm::MetaPropertyInfo *pInfo = m_MetaProperties[index.row()];
101  if (index.column() == 1)
102  {
103  if (index.data(Qt::EditRole).isValid() && pInfo->isWritable())
104  {
105  flags |= Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
106  }
107  }
108  }
109 
110  return flags;
111 }
112 
113 QVariant QmitkMAPAlgorithmModel::headerData(int section, Qt::Orientation orientation, int role) const
114 {
115  if ((Qt::DisplayRole == role) && (Qt::Horizontal == orientation))
116  {
117  if (section == 0)
118  {
119  return QVariant("Property");
120  }
121  else if (section == 1)
122  {
123  return QVariant("Value");
124  }
125  }
126  return QVariant();
127 }
128 
129 bool QmitkMAPAlgorithmModel::setData(const QModelIndex &index, const QVariant &value, int role)
130 {
131  if (!index.isValid() || (static_cast<int>(m_MetaProperties.size()) <= index.row()) || (1 != index.column()))
132  {
133  return false;
134  }
135 
136  if (Qt::EditRole == role)
137  {
138  map::algorithm::MetaPropertyInfo *pInfo = m_MetaProperties[index.row()];
139 
140  bool result = SetPropertyValue(pInfo, value);
141 
142  if (result)
143  {
144  emit beginResetModel();
145  this->UpdateMetaProperties();
146  emit endResetModel();
147  }
148 
149  return result;
150  }
151 
152  return false;
153 };
154 
155 void QmitkMAPAlgorithmModel::UpdateMetaProperties() const
156 {
157  if (m_pMetaInterface)
158  {
159  m_MetaProperties = m_pMetaInterface->getPropertyInfos();
160  }
161  else
162  {
163  m_MetaProperties.clear();
164  }
165 }
166 
167 QVariant QmitkMAPAlgorithmModel::GetPropertyValue(const map::algorithm::MetaPropertyInfo *pInfo, int role) const
168 {
169  if (!m_pMetaInterface)
170  {
171  return QVariant();
172  }
173 
174  map::algorithm::facet::MetaPropertyAlgorithmInterface::MetaPropertyPointer prop =
175  m_pMetaInterface->getProperty(pInfo);
176 
177  if (!prop)
178  {
179  return QVariant();
180  }
181 
182  QVariant result;
183 
184  if (role == Qt::DisplayRole)
185  {
186  result = QVariant(QString("Error. Cannot unwrap MetaProperty. Name: ") + QString::fromStdString(pInfo->getName()));
187  }
188 
189  if (prop->getMetaPropertyTypeInfo() == typeid(bool))
190  {
191  bool val;
192  if (map::core::unwrapCastedMetaProperty(prop, val))
193  {
194  result = QVariant(val);
195  }
196  }
197  else if (prop->getMetaPropertyTypeInfo() == typeid(int))
198  {
199  int val;
200  if (map::core::unwrapCastedMetaProperty(prop, val))
201  {
202  result = QVariant(val);
203  }
204  }
205  else if (prop->getMetaPropertyTypeInfo() == typeid(unsigned int))
206  {
207  unsigned int val;
208  if (map::core::unwrapCastedMetaProperty(prop, val))
209  {
210  result = QVariant(val);
211  }
212  }
213  else if (prop->getMetaPropertyTypeInfo() == typeid(long))
214  {
215  long val;
216  if (map::core::unwrapCastedMetaProperty(prop, val))
217  {
218  result = QVariant(qlonglong(val));
219  }
220  }
221  else if (prop->getMetaPropertyTypeInfo() == typeid(unsigned long))
222  {
223  unsigned long val;
224  if (map::core::unwrapCastedMetaProperty(prop, val))
225  {
226  result = QVariant(qulonglong(val));
227  }
228  }
229  else if (prop->getMetaPropertyTypeInfo() == typeid(float))
230  {
231  float val;
232  if (map::core::unwrapCastedMetaProperty(prop, val))
233  {
234  result = QVariant(val);
235  }
236  }
237  else if (prop->getMetaPropertyTypeInfo() == typeid(double))
238  {
239  double val;
240  if (map::core::unwrapCastedMetaProperty(prop, val))
241  {
242  result = QVariant(val);
243  }
244  }
245  else if (prop->getMetaPropertyTypeInfo() == typeid(map::core::String))
246  {
247  map::core::String val;
248  if (map::core::unwrapCastedMetaProperty(prop, val))
249  {
250  result = QVariant(QString::fromStdString(val));
251  }
252  }
253  else
254  {
255  if (role == Qt::DisplayRole)
256  {
257  result = QVariant(QString("Error. Cannot offer MetaProperty because of unsupported type. Property name: ") +
258  QString::fromStdString(pInfo->getName()) + QString("; type name: ") +
259  QString(prop->getMetaPropertyTypeName()));
260  }
261  }
262  return result;
263 };
264 
265 template <typename TValueType>
266 bool QmitkMAPAlgorithmModel::CheckCastAndSetProp(const map::algorithm::MetaPropertyInfo *pInfo, const QVariant &value)
267 {
268  bool result = false;
269  if (pInfo->getTypeInfo() == typeid(TValueType) && value.canConvert<TValueType>())
270  {
273  TValueType val = value.value<TValueType>();
274  map::core::MetaPropertyBase::Pointer spMetaProp = map::core::MetaProperty<TValueType>::New(val).GetPointer();
275 
276  result = m_pMetaInterface->setProperty(pInfo, spMetaProp);
277  }
278  return result;
279 };
280 
281 bool QmitkMAPAlgorithmModel::SetPropertyValue(const map::algorithm::MetaPropertyInfo *pInfo, const QVariant &value)
282 {
283  if (!m_pMetaInterface)
284  {
285  return false;
286  }
287 
288  bool result = CheckCastAndSetProp<bool>(pInfo, value);
289 
290  if (!result)
291  result = CheckCastAndSetProp<int>(pInfo, value);
292  if (!result)
293  result = CheckCastAndSetProp<unsigned int>(pInfo, value);
294  if (!result)
295  result = CheckCastAndSetProp<long>(pInfo, value);
296  if (!result)
297  result = CheckCastAndSetProp<unsigned long>(pInfo, value);
298  if (!result)
299  result = CheckCastAndSetProp<float>(pInfo, value);
300  if (!result)
301  result = CheckCastAndSetProp<double>(pInfo, value);
302  if (!result && pInfo->getTypeInfo() == typeid(map::core::String))
303  {
304  map::core::String val = value.toString().toStdString();
305  map::core::MetaPropertyBase::Pointer spMetaProp = map::core::MetaProperty<map::core::String>::New(val).GetPointer();
306 
307  result = m_pMetaInterface->setProperty(pInfo, spMetaProp);
308  };
309 
310  return result;
311 };
int columnCount(const QModelIndex &parent=QModelIndex()) const override
QVariant headerData(int section, Qt::Orientation orientation, int role) const override
Qt::ItemFlags flags(const QModelIndex &index) const override
QVariant data(const QModelIndex &index, int role) const override
int rowCount(const QModelIndex &parent=QModelIndex()) const override
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
QmitkMAPAlgorithmModel(QObject *parent=nullptr)
void SetAlgorithm(map::algorithm::RegistrationAlgorithmBase *pAlgorithm)