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