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