Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
QmitkFitParameterModel.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 "mitkExceptionMacro.h"
15 
16 #include "QmitkFitParameterModel.h"
17 
18 
20 QmitkFitParameterModel(QObject* parent) :
21  QAbstractTableModel(parent)
22 {
23  m_CurrentPos.Fill(0.0);
24 }
25 
28 getFits() const
29 {
30  return m_Fits;
31 };
32 
36 {
37  return m_CurrentPos;
38 };
39 
40 const mitk::PointSet*
43 {
44  return m_Bookmarks;
45 };
46 
47 void
49 setFits(const FitVectorType& fits)
50 {
51  emit beginResetModel();
52  m_Fits = fits;
53  emit endResetModel();
54 };
55 
56 void
59 {
60  emit beginResetModel();
61  m_CurrentPos = currentPos;
62  emit endResetModel();
63 };
64 
65 void
68 {
69  emit beginResetModel();
70  m_Bookmarks = bookmarks;
71  emit endResetModel();
72 };
73 
74 bool
75 QmitkFitParameterModel::
76 hasSingleFit() const
77 {
78  return this->m_Fits.size() == 1;
79 };
80 
81 int
83 rowCount(const QModelIndex& parent) const
84 {
85  if (this->hasSingleFit())
86  {
87  if (parent.isValid())
88  {
89  return 0;
90  }
91  else
92  {
93  return this->m_Fits.front()->GetParameters().size() + this->m_Fits.front()->staticParamMap.Size();
94  }
95  }
96  else
97  {
98  if (parent.isValid())
99  {
100  auto row = static_cast<std::size_t>(parent.row());
101  assert(row < this->m_Fits.size());
102  return this->m_Fits[row]->GetParameters().size() + this->m_Fits[row]->staticParamMap.Size();
103  }
104  else
105  {
106  return this->m_Fits.size();
107  }
108  }
109 }
110 
111 std::size_t
114 {
115  if (m_Bookmarks.IsNotNull())
116  {
117  return m_Bookmarks->GetSize();
118  }
119  return 0;
120 }
121 
122 int
124 columnCount(const QModelIndex&) const
125 {
126  return 3 + this->getBookmarksCount();
127 }
128 
129 
132 std::string GetStaticParameterName(const mitk::modelFit::ModelFitInfo* currentFit, const QModelIndex& index)
133 {
134  const auto paramSize = static_cast<int>(currentFit->GetParameters().size());
135 
136  std::string staticParamName;
137 
138  if (index.row() >= paramSize)
139  {
140  int pos = paramSize;
141  for (const auto& iter : currentFit->staticParamMap)
142  {
143  if (pos == index.row())
144  {
145  staticParamName = iter.first;
146  break;
147  }
148  ++pos;
149  }
150  }
151 
152  return staticParamName;
153 }
154 
155 QVariant
157 data(const QModelIndex& index, int role) const
158 {
159  if (!index.isValid())
160  {
161  return QVariant();
162  }
163 
164  QVariant result;
165  if (!index.parent().isValid() && !this->hasSingleFit())
166  { //we need the fit names
167  if (index.row() < static_cast<int>(m_Fits.size()) && index.column() == 0)
168  {
169  if (role == Qt::DisplayRole || role == Qt::EditRole)
170  {
171  result = QVariant(QString::fromStdString(m_Fits[index.row()]->fitName)+QString("(") + QString::fromStdString(m_Fits[index.row()]->uid) + QString(")"));
172  }
173  else if (role == Qt::ToolTipRole)
174  {
175  result = QVariant("Name (UID) of the fit.");
176  }
177  }
178  }
179  else
180  { // realy want to get the values of the current fit
181  const mitk::modelFit::ModelFitInfo* currentFit = nullptr;
182 
183  if (this->hasSingleFit() && !index.parent().isValid())
184  {
185  currentFit = m_Fits.front();
186  }
187  else if (index.parent().isValid() && index.parent().row() < static_cast<int>(m_Fits.size()))
188  {
189  currentFit = m_Fits[index.parent().row()];
190  }
191 
192  if (currentFit)
193  {
194  const auto paramSize = static_cast<int>(currentFit->GetParameters().size());
195  const auto staticParamSize = static_cast<int>(currentFit->staticParamMap.Size());
196 
197  if (index.row() < paramSize + staticParamSize)
198  {
199  std::string staticParamName = GetStaticParameterName(currentFit, index);
200 
201  switch (index.column())
202  {
203  case 0:
204  if (role == Qt::DisplayRole || role == Qt::EditRole)
205  {
206  if (index.row() < paramSize)
207  {
208  const auto& param = currentFit->GetParameters()[index.row()];
209  result = QVariant(QString::fromStdString(param->name));
210  }
211  else
212  {
213  result = QVariant(QString::fromStdString(staticParamName));
214  }
215  }
216  else if (role == Qt::ToolTipRole)
217  {
218  result = QVariant("Name of the parameter.");
219  }
220  break;
221 
222  case 1:
223  if (role == Qt::DisplayRole || role == Qt::EditRole)
224  {
225  if (index.row() < paramSize)
226  {
227  const auto& param = currentFit->GetParameters()[index.row()];
229 
230  if (param->type == mitk::modelFit::Parameter::DerivedType)
231  {
233  }
234  else if (param->type == mitk::modelFit::Parameter::CriterionType)
235  {
237  }
238  else if (param->type == mitk::modelFit::Parameter::EvaluationType)
239  {
241  }
242 
243  result = QVariant(QString::fromStdString(paramType));
244  }
245  else
246  {
247  result = QVariant("static");
248  }
249  }
250  else if (role == Qt::ToolTipRole)
251  {
252  result = QVariant("Type of the parameter.");
253  }
254  break;
255 
256  default:
257  if (index.column() - 2 < static_cast<int>(this->getBookmarksCount()+1))
258  {
259  mitk::Point3D pos = m_CurrentPos;
260  if (index.column() > 2)
261  {
262  pos = m_Bookmarks->GetPoint(index.column() - 3);
263  }
264 
265  if (role == Qt::DisplayRole || role == Qt::EditRole)
266  {
267  if (index.row() < paramSize)
268  {
269  auto value = mitk::ReadVoxel(currentFit->GetParameters()[index.row()]->image, pos);
270  result = QVariant(QString::number(value));
271  }
272  else
273  {
274  auto value = currentFit->staticParamMap.Get(staticParamName).front();
275  result = QVariant(QString::number(value));
276  }
277  }
278  else if (role == Qt::ToolTipRole)
279  {
280  result = QVariant("Value of a (static) fit parameter");
281  }
282  }
283  break;
284  }
285  }
286  }
287  }
288 
289  return result;
290 }
291 
292 Qt::ItemFlags
294 flags(const QModelIndex& index) const
295 {
296  Qt::ItemFlags flags = QAbstractItemModel::flags(index);
297 
298  return flags;
299 }
300 
301 QVariant
303 headerData(int section, Qt::Orientation orientation, int role) const
304 {
305  if ((Qt::DisplayRole == role) &&
306  (Qt::Horizontal == orientation))
307  {
308  if (section == 0)
309  {
310  return QVariant("Name");
311  }
312  else if (section == 1)
313  {
314  return QVariant("Type");
315  }
316  else if (section == 2)
317  {
318  return QVariant("Value");
319  }
320  else if (section - 3 < static_cast<int>(this->getBookmarksCount()))
321  {
322  const auto & pos = m_Bookmarks->GetPoint(section - 3);
323  std::ostringstream strm;
324  strm.imbue(std::locale("C"));
325  strm << std::setprecision(3) << "Value @ Pos " << section -3 << " (" << pos[0] << "|" << pos[1] << "|" << pos[2] << ")";
326  return QVariant(QString::fromStdString(strm.str()));
327  }
328  }
329 
330  return QVariant();
331 }
332 
333 bool
335 setData(const QModelIndex&, const QVariant&, int)
336 {
337  return false;
338 };
static const std::string PARAMETER_TYPE_VALUE_CRITERION()
Qt::ItemFlags flags(const QModelIndex &index) const override
MITKMODELFIT_EXPORT ModelTraitsInterface::ParameterValueType ReadVoxel(const mitk::Image *image, const mitk::Point3D &position, unsigned int timestep=0, bool noThrow=true)
int columnCount(const QModelIndex &parent=QModelIndex()) const override
QmitkFitParameterModel(QObject *parent=nullptr)
int rowCount(const QModelIndex &parent=QModelIndex()) const override
const mitk::PointSet * getPositionBookmarks() const
const FitVectorType & getFits() const
void setFits(const FitVectorType &fits)
StaticParameterMap staticParamMap
QVariant headerData(int section, Qt::Orientation orientation, int role) const override
std::vector< mitk::modelFit::ModelFitInfo::ConstPointer > FitVectorType
Data structure which stores a set of points. Superclass of mitk::Mesh.
Definition: mitkPointSet.h:75
std::string GetStaticParameterName(const mitk::modelFit::ModelFitInfo *currentFit, const QModelIndex &index)
static const std::string PARAMETER_TYPE_VALUE_DERIVED_PARAMETER()
const ValueType & Get(const std::string &name) const
Returns the values of the given variable name.
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
void setCurrentPosition(const mitk::Point3D &currentPos)
mitk::Point3D getCurrentPosition() const
Data class that stores all information about a modelfit that is relevant to the visualization and sto...
std::vcl_size_t getBookmarksCount() const
static const std::string PARAMETER_TYPE_VALUE_PARAMETER()
void setPositionBookmarks(const mitk::PointSet *bookmarks)
const ParamListType & GetParameters() const
static const std::string PARAMETER_TYPE_VALUE_EVALUATION_PARAMETER()
QVariant data(const QModelIndex &index, int role) const override