##// 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 22 #include "qxymodelmapper_p.h"
23 23 #include "qxyseries.h"
24 24 #include <QAbstractItemModel>
25 #include <QDateTime>
25 26
26 27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 28
@@ -246,6 +247,34 QModelIndex QXYModelMapperPrivate::yModelIndex(int yPos)
246 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 278 void QXYModelMapperPrivate::handlePointAdded(int pointPos)
250 279 {
251 280 if (m_seriesSignalsBlock)
@@ -260,8 +289,8 void QXYModelMapperPrivate::handlePointAdded(int pointPos)
260 289 else
261 290 m_model->insertColumns(pointPos + m_first, 1);
262 291
263 m_model->setData(xModelIndex(pointPos), m_series->points().at(pointPos).x());
264 m_model->setData(yModelIndex(pointPos), m_series->points().at(pointPos).y());
292 setValueToModel(xModelIndex(pointPos), m_series->points().at(pointPos).x());
293 setValueToModel(yModelIndex(pointPos), m_series->points().at(pointPos).y());
265 294 blockModelSignals(false);
266 295 }
267 296
@@ -287,8 +316,8 void QXYModelMapperPrivate::handlePointReplaced(int pointPos)
287 316 return;
288 317
289 318 blockModelSignals();
290 m_model->setData(xModelIndex(pointPos), m_series->points().at(pointPos).x());
291 m_model->setData(yModelIndex(pointPos), m_series->points().at(pointPos).y());
319 setValueToModel(xModelIndex(pointPos), m_series->points().at(pointPos).x());
320 setValueToModel(yModelIndex(pointPos), m_series->points().at(pointPos).y());
292 321 blockModelSignals(false);
293 322 }
294 323
@@ -318,8 +347,8 void QXYModelMapperPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottom
318 347 QModelIndex yIndex = yModelIndex(index.row() - m_first);
319 348 if (xIndex.isValid() && yIndex.isValid()) {
320 349 oldPoint = m_series->points().at(index.row() - m_first);
321 newPoint.setX(m_model->data(xIndex).toReal());
322 newPoint.setY(m_model->data(yIndex).toReal());
350 newPoint.setX(valueFromModel(xIndex));
351 newPoint.setY(valueFromModel(yIndex));
323 352 }
324 353 }
325 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 357 QModelIndex yIndex = yModelIndex(index.column() - m_first);
329 358 if (xIndex.isValid() && yIndex.isValid()) {
330 359 oldPoint = m_series->points().at(index.column() - m_first);
331 newPoint.setX(m_model->data(xIndex).toReal());
332 newPoint.setY(m_model->data(yIndex).toReal());
360 newPoint.setX(valueFromModel(xIndex));
361 newPoint.setY(valueFromModel(yIndex));
333 362 }
334 363 }
335 364 } else {
@@ -420,8 +449,8 void QXYModelMapperPrivate::insertData(int start, int end)
420 449 QModelIndex xIndex = xModelIndex(i - m_first);
421 450 QModelIndex yIndex = yModelIndex(i - m_first);
422 451 if (xIndex.isValid() && yIndex.isValid()) {
423 point.setX(m_model->data(xIndex, Qt::DisplayRole).toDouble());
424 point.setY(m_model->data(yIndex, Qt::DisplayRole).toDouble());
452 point.setX(valueFromModel(xIndex));
453 point.setY(valueFromModel(yIndex));
425 454 m_series->insert(i - m_first, point);
426 455 }
427 456 }
@@ -464,8 +493,8 void QXYModelMapperPrivate::removeData(int start, int end)
464 493 QModelIndex xIndex = xModelIndex(i);
465 494 QModelIndex yIndex = yModelIndex(i);
466 495 if (xIndex.isValid() && yIndex.isValid()) {
467 point.setX(m_model->data(xIndex, Qt::DisplayRole).toDouble());
468 point.setY(m_model->data(yIndex, Qt::DisplayRole).toDouble());
496 point.setX(valueFromModel(xIndex));
497 point.setY(valueFromModel(yIndex));
469 498 m_series->insert(i, point);
470 499 }
471 500 }
@@ -488,8 +517,8 void QXYModelMapperPrivate::initializeXYFromModel()
488 517 QModelIndex yIndex = yModelIndex(pointPos);
489 518 while (xIndex.isValid() && yIndex.isValid()) {
490 519 QPointF point;
491 point.setX(m_model->data(xIndex, Qt::DisplayRole).toDouble());
492 point.setY(m_model->data(yIndex, Qt::DisplayRole).toDouble());
520 point.setX(valueFromModel(xIndex));
521 point.setY(valueFromModel(yIndex));
493 522 m_series->append(point);
494 523 pointPos++;
495 524 xIndex = xModelIndex(pointPos);
@@ -73,6 +73,8 private:
73 73 void removeData(int start, int end);
74 74 void blockModelSignals(bool block = true);
75 75 void blockSeriesSignals(bool block = true);
76 qreal valueFromModel(QModelIndex index);
77 void setValueToModel(QModelIndex index, qreal value);
76 78
77 79 private:
78 80 QXYSeries *m_series;
General Comments 0
You need to be logged in to leave comments. Login now