Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
QmitkSimpleBarrierModel.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 <QColor>
14 #include <QSize>
15 
17 
20 const int INDEX_CONSTRAINT_TYPE = 1;
22 const int INDEX_CONSTRAINT_WIDTH = 3;
23 
25 QmitkSimpleBarrierModel(QObject* parent) :
26  QAbstractTableModel(parent), m_modified(false)
27 {
29 }
30 
31 void
35 {
36  if (pChecker != m_Checker)
37  {
38  emit beginResetModel();
39 
40  if (pChecker)
41  {
42  m_Checker = pChecker;
43  }
44  else
45  {
47  }
48 
49  m_ParameterNames = names;
50  m_modified = false;
51 
52  emit endResetModel();
53  }
54 };
55 
56 int
58 rowCount(const QModelIndex& parent) const
59 {
60  if (parent.isValid())
61  {
62  return 0;
63  }
64 
65  return m_Checker->GetNumberOfConstraints();
66 }
67 
68 int
70 columnCount(const QModelIndex& parent) const
71 {
72  if (parent.isValid())
73  {
74  return 0;
75  }
76 
78 }
79 
80 QVariant
82 data(const QModelIndex& index, int role) const
83 {
84  if (!index.isValid())
85  {
86  return QVariant();
87  }
88 
89  QVariant result;
90 
91  if (static_cast<unsigned int>(index.row()) < m_Checker->GetNumberOfConstraints())
92  {
93  const mitk::SimpleBarrierConstraintChecker::Constraint constraint = m_Checker->GetConstraint(
94  static_cast<unsigned int>(index.row()));
95 
96  switch (index.column())
97  {
99  if (role == Qt::DisplayRole)
100  {
101  QStringList names;
102 
103  for (mitk::SimpleBarrierConstraintChecker::ParameterIndexVectorType::const_iterator pos =
104  constraint.parameters.begin(); pos != constraint.parameters.end(); ++pos)
105  {
106  names.append(QString::fromStdString(this->m_ParameterNames[*pos]));
107  }
108 
109  result = QVariant(names);
110  }
111  else if (role == Qt::EditRole)
112  {
113  QStringList names;
114 
115  for (mitk::ModelTraitsInterface::ParameterNamesType::const_iterator pos =
116  this->m_ParameterNames.begin(); pos != this->m_ParameterNames.end(); ++pos)
117  {
118  names.append(QString::fromStdString(*pos));
119  }
120 
121  result = QVariant(names);
122  }
123  else if (role == Qt::ToolTipRole)
124  {
125  result = QVariant("Parameters that are relevant for this constraint. If more then one parameter is specified, it is the sum of all parameters.");
126  }
127 
128  break;
129 
131  if (role == Qt::DisplayRole)
132  {
133  if (constraint.upperBarrier)
134  {
135  result = QVariant(QString("upper"));
136  }
137  else
138  {
139  result = QVariant(QString("lower"));
140  }
141  }
142  else if (role == Qt::EditRole)
143  {
144  if (constraint.upperBarrier)
145  {
146  result = QVariant(1);
147  }
148  else
149  {
150  result = QVariant(0);
151  }
152  }
153  else if (role == Qt::ToolTipRole)
154  {
155  result = QVariant("Type of boundary constraint.");
156  }
157 
158  break;
159 
161  if (role == Qt::DisplayRole || role == Qt::EditRole)
162  {
163  result = QVariant(constraint.barrier);
164  }
165  else if (role == Qt::ToolTipRole)
166  {
167  result = QVariant("Barrier value for the constraint.");
168  }
169 
170  break;
171 
173  if (role == Qt::DisplayRole || role == Qt::EditRole)
174  {
175  result = QVariant(constraint.width);
176  }
177  else if (role == Qt::ToolTipRole)
178  {
179  result = QVariant("Width of the penalty zone before the barrier value. Must be >=0.0.");
180  }
181 
182  break;
183  }
184  }
185 
186  return result;
187 }
188 
189 Qt::ItemFlags
191 flags(const QModelIndex& index) const
192 {
193  Qt::ItemFlags flags = QAbstractItemModel::flags(index);
194 
195  if (static_cast<unsigned int>(index.row()) < m_Checker->GetNumberOfConstraints())
196  {
197  if (index.column() < NUMBER_OF_CONSTRAINT_ASPECTS && index.column() >= 0)
198  {
199  flags |= Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
200  }
201  }
202 
203  return flags;
204 }
205 
206 QVariant
208 headerData(int section, Qt::Orientation orientation, int role) const
209 {
210  if ((Qt::DisplayRole == role) &&
211  (Qt::Horizontal == orientation))
212  {
213  if (section == INDEX_CONSTRAINT_PARAMS)
214  {
215  return QVariant("Parameters");
216  }
217  else if (section == INDEX_CONSTRAINT_TYPE)
218  {
219  return QVariant("Type");
220  }
221  else if (section == INDEX_CONSTRAINT_THRESHOLD)
222  {
223  return QVariant("Value");
224  }
225  else if (section == INDEX_CONSTRAINT_WIDTH)
226  {
227  return QVariant("Width");
228  }
229  }
230 
231  return QVariant();
232 }
233 
234 bool
236 setData(const QModelIndex& index, const QVariant& value, int role)
237 {
238  if (!index.isValid() || (m_Checker->GetNumberOfConstraints() <= static_cast<unsigned int>(index.row()))
239  || (index.column() >= NUMBER_OF_CONSTRAINT_ASPECTS))
240  {
241  return false;
242  }
243 
244  if (Qt::EditRole == role)
245  {
246  mitk::SimpleBarrierConstraintChecker::Constraint constraint = m_Checker->GetConstraint(
247  static_cast<unsigned int>(index.row()));
248 
249  switch (index.column())
250  {
251  case 0:
252  {
253  QStringList selectedNames = value.toStringList();
254  constraint.parameters.clear();
255 
256  for (mitk::SimpleBarrierConstraintChecker::ParameterIndexVectorType::size_type pos = 0;
257  pos < this->m_ParameterNames.size(); ++pos)
258  {
259  if (selectedNames.contains(QString::fromStdString(this->m_ParameterNames[pos])))
260  {
261  constraint.parameters.push_back(pos);
262  }
263  }
264 
265  emit dataChanged(index, index);
266  break;
267  }
268 
269  case 1:
270  constraint.upperBarrier = value.toInt() == 1;
271  emit dataChanged(index, index);
272  break;
273 
274  case 2:
275  constraint.barrier = value.toDouble();
276  emit dataChanged(index, index);
277  break;
278 
279  case 3:
280  constraint.width = value.toDouble();
281  emit dataChanged(index, index);
282  break;
283  }
284 
285  emit beginResetModel();
286 
287  m_Checker->GetConstraint(static_cast<unsigned int>(index.row())) = constraint;
288 
289  m_modified = true;
290 
291  emit endResetModel();
292 
293  return true;
294  }
295 
296  return false;
297 };
298 
300 {
301  emit beginResetModel();
302  m_Checker->SetLowerBarrier(0, 0.0);
303  m_modified = true;
304  emit endResetModel();
305 }
306 
307 void QmitkSimpleBarrierModel::deleteConstraint(const QModelIndex& index)
308 {
309  if (!index.isValid() || (m_Checker->GetNumberOfConstraints() <= static_cast<unsigned int>(index.row()))
310  || (index.column() >= NUMBER_OF_CONSTRAINT_ASPECTS))
311  {
312  return;
313  }
314 
315  emit beginResetModel();
316  m_Checker->DeleteConstraint(static_cast<unsigned int>(index.row()));
317  m_modified = true;
318  emit endResetModel();
319 }
320 
322 {
323  return m_modified;
324 }
const int INDEX_CONSTRAINT_TYPE
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
int columnCount(const QModelIndex &parent=QModelIndex()) const override
This class implements constraints as simple barrier functions.
const int INDEX_CONSTRAINT_WIDTH
QVariant data(const QModelIndex &index, int role) const override
const int INDEX_CONSTRAINT_THRESHOLD
QVariant headerData(int section, Qt::Orientation orientation, int role) const override
const int INDEX_CONSTRAINT_PARAMS
void setChecker(mitk::SimpleBarrierConstraintChecker *pChecker, const mitk::ModelTraitsInterface::ParameterNamesType &names)
std::vector< ParameterNameType > ParameterNamesType
const int NUMBER_OF_CONSTRAINT_ASPECTS
void deleteConstraint(const QModelIndex &index)
QmitkSimpleBarrierModel(QObject *parent=nullptr)
Qt::ItemFlags flags(const QModelIndex &index) const override
int rowCount(const QModelIndex &parent=QModelIndex()) const override