##// END OF EJS Templates
XYModelMapper: added support for models with QDate and QDateTime cells
Marek Rosa -
r2107:9895ee3a7ecb
parent child
Show More
@@ -22,6 +22,7
22 #include "qxymodelmapper_p.h"
22 #include "qxymodelmapper_p.h"
23 #include "qxyseries.h"
23 #include "qxyseries.h"
24 #include <QAbstractItemModel>
24 #include <QAbstractItemModel>
25 #include <QDateTime>
25
26
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27
28
@@ -246,6 +247,34 QModelIndex QXYModelMapperPrivate::yModelIndex(int yPos)
246 return m_model->index(m_ySection, yPos + m_first);
247 return m_model->index(m_ySection, yPos + m_first);
247 }
248 }
248
249
250 qreal QXYModelMapperPrivate::valueFromModel(QModelIndex index)
251 {
252 QVariant value = m_model->data(index, Qt::DisplayRole);
253 switch (value.type()) {
254 case QVariant::DateTime:
255 return value.toDateTime().toMSecsSinceEpoch();
256 case QVariant::Date:
257 return QDateTime(value.toDate()).toMSecsSinceEpoch();
258 default:
259 return value.toReal();
260 }
261 }
262
263 void QXYModelMapperPrivate::setValueToModel(QModelIndex index, qreal value)
264 {
265 QVariant oldValue = m_model->data(index, Qt::DisplayRole);
266 switch (oldValue.type()) {
267 case QVariant::DateTime:
268 m_model->setData(index, QDateTime::fromMSecsSinceEpoch(value));
269 break;
270 case QVariant::Date:
271 m_model->setData(index, QDateTime::fromMSecsSinceEpoch(value).date());
272 break;
273 default:
274 m_model->setData(index, value);
275 }
276 }
277
249 void QXYModelMapperPrivate::handlePointAdded(int pointPos)
278 void QXYModelMapperPrivate::handlePointAdded(int pointPos)
250 {
279 {
251 if (m_seriesSignalsBlock)
280 if (m_seriesSignalsBlock)
@@ -260,8 +289,8 void QXYModelMapperPrivate::handlePointAdded(int pointPos)
260 else
289 else
261 m_model->insertColumns(pointPos + m_first, 1);
290 m_model->insertColumns(pointPos + m_first, 1);
262
291
263 m_model->setData(xModelIndex(pointPos), m_series->points().at(pointPos).x());
292 setValueToModel(xModelIndex(pointPos), m_series->points().at(pointPos).x());
264 m_model->setData(yModelIndex(pointPos), m_series->points().at(pointPos).y());
293 setValueToModel(yModelIndex(pointPos), m_series->points().at(pointPos).y());
265 blockModelSignals(false);
294 blockModelSignals(false);
266 }
295 }
267
296
@@ -287,8 +316,8 void QXYModelMapperPrivate::handlePointReplaced(int pointPos)
287 return;
316 return;
288
317
289 blockModelSignals();
318 blockModelSignals();
290 m_model->setData(xModelIndex(pointPos), m_series->points().at(pointPos).x());
319 setValueToModel(xModelIndex(pointPos), m_series->points().at(pointPos).x());
291 m_model->setData(yModelIndex(pointPos), m_series->points().at(pointPos).y());
320 setValueToModel(yModelIndex(pointPos), m_series->points().at(pointPos).y());
292 blockModelSignals(false);
321 blockModelSignals(false);
293 }
322 }
294
323
@@ -318,8 +347,8 void QXYModelMapperPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottom
318 QModelIndex yIndex = yModelIndex(index.row() - m_first);
347 QModelIndex yIndex = yModelIndex(index.row() - m_first);
319 if (xIndex.isValid() && yIndex.isValid()) {
348 if (xIndex.isValid() && yIndex.isValid()) {
320 oldPoint = m_series->points().at(index.row() - m_first);
349 oldPoint = m_series->points().at(index.row() - m_first);
321 newPoint.setX(m_model->data(xIndex).toReal());
350 newPoint.setX(valueFromModel(xIndex));
322 newPoint.setY(m_model->data(yIndex).toReal());
351 newPoint.setY(valueFromModel(yIndex));
323 }
352 }
324 }
353 }
325 } else if (m_orientation == Qt::Horizontal && (index.row() == m_xSection || index.row() == m_ySection)) {
354 } else if (m_orientation == Qt::Horizontal && (index.row() == m_xSection || index.row() == m_ySection)) {
@@ -328,8 +357,8 void QXYModelMapperPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottom
328 QModelIndex yIndex = yModelIndex(index.column() - m_first);
357 QModelIndex yIndex = yModelIndex(index.column() - m_first);
329 if (xIndex.isValid() && yIndex.isValid()) {
358 if (xIndex.isValid() && yIndex.isValid()) {
330 oldPoint = m_series->points().at(index.column() - m_first);
359 oldPoint = m_series->points().at(index.column() - m_first);
331 newPoint.setX(m_model->data(xIndex).toReal());
360 newPoint.setX(valueFromModel(xIndex));
332 newPoint.setY(m_model->data(yIndex).toReal());
361 newPoint.setY(valueFromModel(yIndex));
333 }
362 }
334 }
363 }
335 } else {
364 } else {
@@ -420,8 +449,8 void QXYModelMapperPrivate::insertData(int start, int end)
420 QModelIndex xIndex = xModelIndex(i - m_first);
449 QModelIndex xIndex = xModelIndex(i - m_first);
421 QModelIndex yIndex = yModelIndex(i - m_first);
450 QModelIndex yIndex = yModelIndex(i - m_first);
422 if (xIndex.isValid() && yIndex.isValid()) {
451 if (xIndex.isValid() && yIndex.isValid()) {
423 point.setX(m_model->data(xIndex, Qt::DisplayRole).toDouble());
452 point.setX(valueFromModel(xIndex));
424 point.setY(m_model->data(yIndex, Qt::DisplayRole).toDouble());
453 point.setY(valueFromModel(yIndex));
425 m_series->insert(i - m_first, point);
454 m_series->insert(i - m_first, point);
426 }
455 }
427 }
456 }
@@ -464,8 +493,8 void QXYModelMapperPrivate::removeData(int start, int end)
464 QModelIndex xIndex = xModelIndex(i);
493 QModelIndex xIndex = xModelIndex(i);
465 QModelIndex yIndex = yModelIndex(i);
494 QModelIndex yIndex = yModelIndex(i);
466 if (xIndex.isValid() && yIndex.isValid()) {
495 if (xIndex.isValid() && yIndex.isValid()) {
467 point.setX(m_model->data(xIndex, Qt::DisplayRole).toDouble());
496 point.setX(valueFromModel(xIndex));
468 point.setY(m_model->data(yIndex, Qt::DisplayRole).toDouble());
497 point.setY(valueFromModel(yIndex));
469 m_series->insert(i, point);
498 m_series->insert(i, point);
470 }
499 }
471 }
500 }
@@ -488,8 +517,8 void QXYModelMapperPrivate::initializeXYFromModel()
488 QModelIndex yIndex = yModelIndex(pointPos);
517 QModelIndex yIndex = yModelIndex(pointPos);
489 while (xIndex.isValid() && yIndex.isValid()) {
518 while (xIndex.isValid() && yIndex.isValid()) {
490 QPointF point;
519 QPointF point;
491 point.setX(m_model->data(xIndex, Qt::DisplayRole).toDouble());
520 point.setX(valueFromModel(xIndex));
492 point.setY(m_model->data(yIndex, Qt::DisplayRole).toDouble());
521 point.setY(valueFromModel(yIndex));
493 m_series->append(point);
522 m_series->append(point);
494 pointPos++;
523 pointPos++;
495 xIndex = xModelIndex(pointPos);
524 xIndex = xModelIndex(pointPos);
@@ -73,6 +73,8 private:
73 void removeData(int start, int end);
73 void removeData(int start, int end);
74 void blockModelSignals(bool block = true);
74 void blockModelSignals(bool block = true);
75 void blockSeriesSignals(bool block = true);
75 void blockSeriesSignals(bool block = true);
76 qreal valueFromModel(QModelIndex index);
77 void setValueToModel(QModelIndex index, qreal value);
76
78
77 private:
79 private:
78 QXYSeries *m_series;
80 QXYSeries *m_series;
General Comments 0
You need to be logged in to leave comments. Login now