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