QXRD  0.11.16
qxrdroicoordinateslistmodel.cpp
Go to the documentation of this file.
2 #include "qxrdroicoordinates.h"
3 #include "qcepmutexlocker.h"
4 #include <stdio.h>
5 
7  : QAbstractListModel(),
8  m_Saver(saver),
9  m_Experiment(exp),
11 {
12 }
13 
15 {
16 }
17 
18 void QxrdROICoordinatesListModel::readSettings(QSettings *settings, QString section)
19 {
20  QcepMutexLocker lock(__FILE__, __LINE__, &m_Mutex);
21 
22  beginResetModel();
23 
24  int n = settings->beginReadArray(section+"/roi");
25 
26  m_ROICoordinates.resize(0);
27 
28  for (int i=0; i<n; i++) {
29  settings->setArrayIndex(i);
30 
31  int roiType = settings->value("roiType", 0).toInt();
32 
33  QxrdROICoordinatesPtr roi = newROI(roiType);
34 
35  if (roi) {
36  roi->readSettings(settings, "");
37  m_ROICoordinates.append(roi);
38 
39  connect(roi.data(), &QxrdROICoordinates::roiChanged,
41  }
42  }
43 
44  settings->endArray();
45 
46  endResetModel();
47 }
48 
49 void QxrdROICoordinatesListModel::writeSettings(QSettings *settings, QString section)
50 {
51  QcepMutexLocker lock(__FILE__, __LINE__, &m_Mutex);
52 
53  settings->beginWriteArray(section+"/roi");
54 
55  for (int i=0; i<m_ROICoordinates.count(); i++) {
56  settings->setArrayIndex(i);
57 
59 
60  if (roi) {
61  roi->writeSettings(settings, "");
62  }
63  }
64 
65  settings->endArray();
66 }
67 
68 QScriptValue QxrdROICoordinatesListModel::toScriptValue(QScriptEngine *engine, const QxrdROICoordinatesListModelPtr &coords)
69 {
70  return engine->newQObject(coords.data());
71 }
72 
74 {
75  QObject *qobj = obj.toQObject();
76 
77  if (qobj) {
78  QxrdROICoordinatesListModel *qcoords = qobject_cast<QxrdROICoordinatesListModel*>(qobj);
79 
80  if (qcoords) {
81  coords = QxrdROICoordinatesListModelPtr(qcoords);
82  }
83  }
84 }
85 
87 {
88  return m_ROICoordinates.count();
89 }
90 
91 int QxrdROICoordinatesListModel::rowCount(const QModelIndex &parent) const
92 {
93  return m_ROICoordinates.count();
94 }
95 
96 int QxrdROICoordinatesListModel::columnCount(const QModelIndex &parent) const
97 {
98  return ColCount;
99 }
100 
101 QVariant QxrdROICoordinatesListModel::data(const QModelIndex &index, int role) const
102 {
103  int row = index.row();
104  int col = index.column();
105 
106  QxrdROICoordinatesPtr c = roi(row);
107 
108  if (c) {
109  if (role == Qt::DisplayRole || role == Qt::EditRole) {
110  if (col == NumCol) {
111  return row;
112  } else if (col == SumCol) {
113  return c->get_Sum();
114  } else if (col == AverageCol) {
115  return c->get_Average();
116  } else if (col == MinimumCol) {
117  return c->get_Minimum();
118  } else if (col == MaximumCol) {
119  return c->get_Maximum();
120  } else if (col == NPixelsCol) {
121  return c->get_NPixels();
122  } else if (col == BackgroundCol) {
123  return c->get_Background();
124  } else if (col == XGradientCol) {
125  return c->get_XGradient();
126  } else if (col == YGradientCol) {
127  return c->get_YGradient();
128  } else if (col == TypeCol) {
129  return c->get_RoiTypeName();
130  } else if (col == CenterXCol) {
131  return c->get_Coords().center().x();
132  } else if (col == CenterYCol) {
133  return c->get_Coords().center().y();
134  } else if (col == WidthCol) {
135  return c->get_Coords().width();
136  } else if (col == HeightCol) {
137  return c->get_Coords().height();
138  } else if (col == Width2Col) {
139  return c->get_Width2();
140  } else if (col == Height2Col) {
141  return c->get_Height2();
142  }
143  }
144  }
145 
146  return QVariant();
147 }
148 
149 QVariant QxrdROICoordinatesListModel::headerData(int section, Qt::Orientation orientation, int role) const
150 {
151  if (orientation == Qt::Horizontal) {
152  if (role == Qt::DisplayRole) {
153  if (section == NumCol) {
154  return "#";
155  } else if (section == SumCol) {
156  return "Sum";
157  } else if (section == AverageCol) {
158  return "Avg";
159  } else if (section == MinimumCol) {
160  return "Min";
161  } else if (section == MaximumCol) {
162  return "Max";
163  } else if (section == NPixelsCol) {
164  return "NPix";
165  } else if (section == BackgroundCol) {
166  return "Bkgd";
167  } else if (section == XGradientCol) {
168  return "XGrad";
169  } else if (section == YGradientCol) {
170  return "YGrad";
171  } else if (section == TypeCol) {
172  return "Type";
173  } else if (section == CenterXCol) {
174  return "CenterX";
175  } else if (section == CenterYCol) {
176  return "CenterY";
177  } else if (section == WidthCol) {
178  return "Width";
179  } else if (section == HeightCol) {
180  return "Height";
181  } else if (section == Width2Col) {
182  return "Wd 2";
183  } else if (section == Height2Col) {
184  return "Ht 2";
185  }
186  } else if (role == Qt::TextAlignmentRole) {
187  return Qt::AlignHCenter;
188  }
189  }
190 
191  return QVariant();
192 }
193 
194 Qt::ItemFlags QxrdROICoordinatesListModel::flags(const QModelIndex &index) const
195 {
196  int row = index.row();
197  int col = index.column();
198 
199  if (col == TypeCol ||
200  col == CenterXCol ||
201  col == CenterYCol ||
202  col == WidthCol ||
203  col == HeightCol ||
204  col == Width2Col ||
205  col == Height2Col) {
206  return QAbstractListModel::flags(index) | Qt::ItemIsEditable;
207  } else {
208  return QAbstractListModel::flags(index);
209  }
210 }
211 
212 bool QxrdROICoordinatesListModel::setData(const QModelIndex &index, const QVariant &value, int role)
213 {
214  int row = index.row();
215  int col = index.column();
216 
217  QxrdROICoordinatesPtr c = roi(row);
218 
219  if (c) {
220  if (role == Qt::EditRole || role == Qt::DisplayRole) {
221  if (col == SumCol) {
222  c->set_Sum(value.toDouble());
223  } else if (col == AverageCol) {
224  c->set_Average(value.toDouble());
225  } else if (col == MinimumCol) {
226  c->set_Minimum(value.toDouble());
227  } else if (col == MaximumCol) {
228  c->set_Maximum(value.toDouble());
229  } else if (col == NPixelsCol) {
230  c->set_NPixels(value.toDouble());
231  } else if (col == BackgroundCol) {
232  c->set_Background(value.toDouble());
233  } else if (col == XGradientCol) {
234  c->set_XGradient(value.toDouble());
235  } else if (col == YGradientCol) {
236  c->set_YGradient(value.toDouble());
237  } else if (col == TypeCol) {
238  c->selectNamedROIType(value.toString());
239  } else if (col == CenterXCol) {
240  c->setCenterX(value.toDouble());
241  } else if (col == CenterYCol) {
242  c->setCenterY(value.toDouble());
243  } else if (col == WidthCol) {
244  c->setWidth(value.toDouble());
245  } else if (col == HeightCol) {
246  c->setHeight(value.toDouble());
247  } else if (col == Width2Col) {
248  c->setWidth2(value.toDouble());
249  } else if (col == Height2Col) {
250  c->setHeight2(value.toDouble());
251  } else {
252  return false;
253  }
254 
255  setRoi(row, c);
256 
257  emit dataChanged(index, index);
258 
259  return true;
260  }
261  }
262  return false;
263 }
264 
266 {
268 }
269 
271 {
272  beginInsertRows(QModelIndex(), m_ROICoordinates.count(), m_ROICoordinates.count());
273 
274  m_ROICoordinates.append(coords);
275 
276  connect(coords.data(), &QxrdROICoordinates::roiChanged,
278 
279  endInsertRows();
280 }
281 
283 {
284  beginRemoveRows(QModelIndex(), row, row);
285 
286  m_ROICoordinates.remove(row);
287 
288  endRemoveRows();
289 }
290 
292 {
293  int nRows = m_ROICoordinates.count();
294 
295  if (row >= 0 && row < (nRows-1)) {
296  beginMoveRows(QModelIndex(), row+1, row+1, QModelIndex(), row);
297 
298  QxrdROICoordinatesPtr p1 = m_ROICoordinates.value(row);
299  QxrdROICoordinatesPtr p2 = m_ROICoordinates.value(row+1);
300 
301  m_ROICoordinates[row] = p2;
302  m_ROICoordinates[row+1] = p1;
303 
304  endMoveRows();
305  }
306 }
307 
309 {
310  int nRows = m_ROICoordinates.count();
311 
312  if (row >= 1 && row < nRows) {
313  beginMoveRows(QModelIndex(), row, row, QModelIndex(), row-1);
314 
315  QxrdROICoordinatesPtr p1 = m_ROICoordinates.value(row-1);
316  QxrdROICoordinatesPtr p2 = m_ROICoordinates.value(row);
317 
318  m_ROICoordinates[row-1] = p2;
319  m_ROICoordinates[row] = p1;
320 
321  endMoveRows();
322  }
323 }
324 
326 {
327 }
328 
330 {
331  return m_ROICoordinates.value(row);
332 }
333 
335 {
336  if (row >= 0 && row < m_ROICoordinates.count()) {
337  m_ROICoordinates[row] = c;
338 
339  emit dataChanged(index(row,0), index(row,ColCount));
340  }
341 }
342 
343 void QxrdROICoordinatesListModel::moveROICenter(int i, double x, double y)
344 {
345  QxrdROICoordinatesPtr roi = this->roi(i);
346 
347  if (roi) {
348  roi->setCenter(QPointF(x,y));
349 
350  emit dataChanged(index(i,0), index(i,ColCount));
351  }
352 }
353 
355 {
356 // printf("QxrdROICoordinatesListModel::onROIChanged()\n");
357 
358  QObject *s = sender();
359 
360  QxrdROICoordinates *c = qobject_cast<QxrdROICoordinates*>(s);
361 
362  if (c) {
363  for (int i=0; i<m_ROICoordinates.count(); i++) {
365 
366  if (r && r.data() == c) {
367 // printf("ROI %d changed\n", i);
368 
369  emit dataChanged(index(i,0), index(i,columnCount(QModelIndex())));
370  }
371  }
372  }
373 }
374 
376 {
377  emit dataChanged(index(0,0), index(rowCount(QModelIndex()), columnCount(QModelIndex())));
378 }
379 
381 {
382  for (int i=0; i<m_ROICoordinates.count(); i++) {
384 
385  if (r) {
386  r->recalculate(img, mask);
387  }
388  }
389 
390  emit dataChanged(index(0,SumCol), index(m_ROICoordinates.count(),NPixelsCol));
391 }
392 
394 {
396 
397  if (r) {
398  r->visualizeBackground(img, mask);
399  }
400 }
401 
403 {
405 
406  if (r) {
407  r->visualizePeak(img, mask);
408  }
409 }
void visualizePeak(int n, QcepImageDataBasePtr img, QcepMaskDataPtr mask)
int rowCount(const QModelIndex &parent) const
QSharedPointer< QxrdROICoordinates > QxrdROICoordinatesPtr
QVariant headerData(int section, Qt::Orientation orientation, int role) const
void recalculate(QcepImageDataBasePtr img, QcepMaskDataPtr mask)
Qt::ItemFlags flags(const QModelIndex &index) const
QWeakPointer< QxrdExperiment > QxrdExperimentWPtr
void append(QxrdROICoordinatesPtr coords)
QVariant data(const QModelIndex &index, int role) const
void readSettings(QSettings *settings, QString section)
void writeSettings(QSettings *settings, QString section)
int columnCount(const QModelIndex &parent) const
void visualizeBackground(int n, QcepImageDataBasePtr img, QcepMaskDataPtr mask)
QxrdROICoordinatesPtr newROI(int roiType)
QxrdROICoordinatesListModel(QcepSettingsSaverWPtr saver, QxrdExperimentWPtr exp)
static void fromScriptValue(const QScriptValue &obj, QxrdROICoordinatesListModelPtr &coords)
static QScriptValue toScriptValue(QScriptEngine *engine, const QxrdROICoordinatesListModelPtr &coords)
bool setData(const QModelIndex &index, const QVariant &value, int role)
void moveROICenter(int row, double x, double y)
QVector< QxrdROICoordinatesPtr > m_ROICoordinates
QSharedPointer< QcepImageDataBase > QcepImageDataBasePtr
QSharedPointer< QcepMaskData > QcepMaskDataPtr
QSharedPointer< QxrdROICoordinatesListModel > QxrdROICoordinatesListModelPtr
QxrdROICoordinatesPtr roi(int row) const
void setRoi(int row, QxrdROICoordinatesPtr c)
QWeakPointer< QcepSettingsSaver > QcepSettingsSaverWPtr