Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
QmitkFitPlotDataModel.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"
14 
15 #include "QmitkFitPlotDataModel.h"
16 
17 
19 QmitkFitPlotDataModel(QObject* parent) :
20  QAbstractTableModel(parent)
21 {
22 }
23 
26 GetPlotData() const
27 {
28  return &m_PlotData;
29 };
30 
31 void
34 {
35  emit beginResetModel();
36  m_PlotData = *data;
37  emit endResetModel();
38 };
39 
40 const std::string&
42 GetXName() const
43 {
44  return m_XName;
45 };
46 
47 void
49 SetXName(const std::string& xName)
50 {
51  emit beginResetModel();
52  m_XName = xName;
53  emit endResetModel();
54 };
55 
56 int
58 rowCount(const QModelIndex&) const
59 {
60  //we assume that all plots have the same nr of values (except the interpolated signal which will be ignored).
61  //Thus we have only to check one plot. Reason: One fit is always derived from one input data and therefore
62  //all derived data should have the same size.
63  auto plot = GetCurveByColumn(0);
64  if (plot.second)
65  {
66  return plot.second->GetValues().size();
67  }
68  return 0;
69 }
70 
71 std::pair<bool, mitk::ModelFitPlotData::PositionalCollectionMap::value_type> QmitkFitPlotDataModel::GetPositionalCurvePoint(const mitk::PlotDataCurve* curve) const
72 {
73  for (auto collection : this->m_PlotData.positionalPlots)
74  {
75  for (auto aCurve : collection.second.second->CastToSTLContainer())
76  {
77  if (curve == aCurve.second.GetPointer())
78  {
79  return std::make_pair(true, collection);
80  }
81  }
82  }
83  return std::make_pair(false, mitk::ModelFitPlotData::PositionalCollectionMap::value_type());
84 }
85 
86 
87 int
89 columnCount(const QModelIndex& parent) const
90 {
91  if (parent.isValid())
92  {
93  return 0;
94  }
95  else
96  {
97  auto size = this->m_PlotData.currentPositionPlots->size() + this->m_PlotData.staticPlots->size();
99  { //don't take the interpolated signal into account
100  size -= 1;
101  }
103  { //don't take the interpolated signal into account
104  size -= 1;
105  }
106 
107  for (const auto& coll : this->m_PlotData.positionalPlots)
108  {
109  size += coll.second.second->size();
111  { //don't take the interpolated signal into account
112  size -= 1;
113  }
114  }
115  return size+2;
116  }
117 }
118 
119 bool GetCurveFromCollection(const mitk::PlotDataCurveCollection* collection, std::size_t index, std::pair<std::string, const mitk::PlotDataCurve*>& finding, std::size_t& actualCount)
120 {
121  actualCount = 0;
122  for (const auto& iter : *(collection))
123  {
125  {
126  if (actualCount == index)
127  {
128  finding = std::make_pair(iter.first, iter.second.GetPointer());
129  return true;
130  }
131  ++actualCount;
132  }
133  }
134  return false;
135 }
136 
137 std::pair<std::string, const mitk::PlotDataCurve*>
139 GetCurveByColumn(int col) const
140 {
141  if (col == 1)
142  { /*the x axis is needed so just get the first signal.*/
143  col = 0;
144  }
145  else if (col > 1)
146  { /*a normal signal is requested, correct the column index to ignore the timestep and timepoint column*/
147  col -= 2;
148  }
149 
150  std::pair<std::string, const mitk::PlotDataCurve*> finding;
151  std::size_t actualCount;
152  if (GetCurveFromCollection(this->m_PlotData.currentPositionPlots.GetPointer(), col, finding, actualCount))
153  {
154  return finding;
155  }
156  col -= actualCount;
157 
158  for (auto collection : this->m_PlotData.positionalPlots)
159  {
160  if (GetCurveFromCollection(collection.second.second.GetPointer(), col, finding, actualCount))
161  {
162  return finding;
163  }
164  col -= actualCount;
165  }
166 
167  GetCurveFromCollection(this->m_PlotData.staticPlots.GetPointer(), col, finding, actualCount);
168  return finding;
169 };
170 
171 
172 QVariant
174 data(const QModelIndex& index, int role) const
175 {
176  if (!index.isValid())
177  {
178  return QVariant();
179  }
180 
181  QVariant result;
182 
183  if (index.column() == 0)
184  {
185  if (role == Qt::DisplayRole || role == Qt::EditRole)
186  {
187  result = QVariant(QString::number(index.row()));
188  }
189  else if (role == Qt::ToolTipRole)
190  {
191  result = QVariant("Index in plot.");
192  }
193  }
194  else if (index.column() == 1)
195  {
196  auto finding = GetCurveByColumn(index.column());
197  if (role == Qt::DisplayRole || role == Qt::EditRole)
198  {
199  if (finding.second && index.row() < static_cast<int>(finding.second->GetValues().size()))
200  {
201  result = QVariant(QString::number(finding.second->GetValues()[index.row()].first));
202  }
203  }
204  else if (role == Qt::ToolTipRole)
205  {
206  result = QVariant("x value in plot.");
207  }
208  }
209  else
210  {
211  auto finding = GetCurveByColumn(index.column());
212  if (role == Qt::DisplayRole || role == Qt::EditRole)
213  {
214  if (finding.second && index.row() < static_cast<int>(finding.second->GetValues().size()))
215  {
216  result = QVariant(QString::number(finding.second->GetValues()[index.row()].second));
217  }
218  }
219  else if (role == Qt::ToolTipRole)
220  {
221  result = QVariant("This is a value of the respective curve.");
222  }
223  }
224 
225  return result;
226 }
227 
228 Qt::ItemFlags
230 flags(const QModelIndex& index) const
231 {
232  Qt::ItemFlags flags = QAbstractItemModel::flags(index);
233 
234  return flags;
235 }
236 
237 QVariant
239 headerData(int section, Qt::Orientation orientation, int role) const
240 {
241  if ((Qt::DisplayRole == role) &&
242  (Qt::Horizontal == orientation))
243  {
244  if (section == 0)
245  {
246  return QVariant("#");
247  }
248  else if (section == 1)
249  {
250  return QVariant(QString::fromStdString(m_XName));
251  }
252  else
253  {
254  auto finding = GetCurveByColumn(section);
255 
256  auto pointFinding = GetPositionalCurvePoint(finding.second);
257 
258  std::ostringstream nameStrm;
259  nameStrm.imbue(std::locale("C"));
260  nameStrm << finding.first;
261 
262  if (pointFinding.first)
263  {
264  nameStrm << " @ " << mitk::ModelFitPlotData::GetPositionalCollectionName(pointFinding.second);
265  }
266 
267  return QVariant(QString::fromStdString(nameStrm.str()));
268 
269  }
270  }
271 
272  return QVariant();
273 }
274 
275 bool
277 setData(const QModelIndex&, const QVariant&, int)
278 {
279  return false;
280 };
QmitkFitPlotDataModel(QObject *parent=nullptr)
int rowCount(const QModelIndex &parent=QModelIndex()) const override
std::pair< std::string, const mitk::PlotDataCurve * > GetCurveByColumn(int col) const
QVariant headerData(int section, Qt::Orientation orientation, int role) const override
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
const mitk::ModelFitPlotData * GetPlotData() const
int columnCount(const QModelIndex &parent=QModelIndex()) const override
static std::string GetPositionalCollectionName(const PositionalCollectionMap::value_type &mapValue)
const std::string & GetXName() const
void SetPlotData(const mitk::ModelFitPlotData *data)
MITKMODELFIT_EXPORT const std::string MODEL_FIT_PLOT_INTERPOLATED_SIGNAL_NAME()
QVariant data(const QModelIndex &index, int role) const override
std::pair< bool, mitk::ModelFitPlotData::PositionalCollectionMap::value_type > GetPositionalCurvePoint(const mitk::PlotDataCurve *) const
bool GetCurveFromCollection(const mitk::PlotDataCurveCollection *collection, std::vcl_size_t index, std::pair< std::string, const mitk::PlotDataCurve *> &finding, std::vcl_size_t &actualCount)
itk::MapContainer< std::string, PlotDataCurve::Pointer > PlotDataCurveCollection
PositionalCollectionMap positionalPlots
void SetXName(const std::string &xName)
Qt::ItemFlags flags(const QModelIndex &index) const override
static const PlotDataCurve * GetInterpolatedSignalPlot(const PlotDataCurveCollection *coll)
PlotDataCurveCollection::Pointer currentPositionPlots
PlotDataCurveCollection::Pointer staticPlots