##// END OF EJS Templates
XYSeries model support refactored
Marek Rosa -
r1085:e4375f6dc43e
parent child
Show More
@@ -52,6 +52,7 void LineChartItem::setLayout(QVector<QPointF>& points)
52 {
52 {
53 if(points.size()==0)
53 if(points.size()==0)
54 {
54 {
55 m_path = QPainterPath();
55 XYChartItem::setLayout(points);
56 XYChartItem::setLayout(points);
56 return;
57 return;
57 }
58 }
@@ -105,6 +105,7 void ScatterChartItem::setLayout(QVector<QPointF>& points)
105 {
105 {
106 if(points.size()==0)
106 if(points.size()==0)
107 {
107 {
108 deletePoints(m_items.childItems().count());
108 XYChartItem::setLayout(points);
109 XYChartItem::setLayout(points);
109 return;
110 return;
110 }
111 }
@@ -220,16 +220,28 void QSplineSeriesPrivate::updateControlPoints()
220 }
220 }
221 }
221 }
222
222
223 void QSplineSeriesPrivate::modelDataAdded(QModelIndex parent, int start, int end)
223 void QSplineSeriesPrivate::modelRowsAdded(QModelIndex parent, int start, int end)
224 {
224 {
225 updateControlPoints();
225 updateControlPoints();
226 QXYSeriesPrivate::modelDataAdded(parent, start, end);
226 QXYSeriesPrivate::modelRowsAdded(parent, start, end);
227 }
227 }
228
228
229 void QSplineSeriesPrivate::modelDataRemoved(QModelIndex parent, int start, int end)
229 void QSplineSeriesPrivate::modelRowsRemoved(QModelIndex parent, int start, int end)
230 {
230 {
231 updateControlPoints();
231 updateControlPoints();
232 QXYSeriesPrivate::modelDataRemoved(parent, start, end);
232 QXYSeriesPrivate::modelRowsRemoved(parent, start, end);
233 }
234
235 void QSplineSeriesPrivate::modelColumnsAdded(QModelIndex parent, int start, int end)
236 {
237 updateControlPoints();
238 QXYSeriesPrivate::modelColumnsAdded(parent, start, end);
239 }
240
241 void QSplineSeriesPrivate::modelColumnsRemoved(QModelIndex parent, int start, int end)
242 {
243 updateControlPoints();
244 QXYSeriesPrivate::modelColumnsRemoved(parent, start, end);
233 }
245 }
234
246
235 Chart* QSplineSeriesPrivate::createGraphics(ChartPresenter* presenter)
247 Chart* QSplineSeriesPrivate::createGraphics(ChartPresenter* presenter)
@@ -46,8 +46,10 public Q_SLOTS:
46 void updateControlPoints();
46 void updateControlPoints();
47
47
48 protected Q_SLOTS:
48 protected Q_SLOTS:
49 void modelDataAdded(QModelIndex parent, int start, int end);
49 void modelRowsAdded(QModelIndex parent, int start, int end);
50 void modelDataRemoved(QModelIndex parent, int start, int end);
50 void modelRowsRemoved(QModelIndex parent, int start, int end);
51 void modelColumnsAdded(QModelIndex parent, int start, int end);
52 void modelColumnsRemoved(QModelIndex parent, int start, int end);
51
53
52 private:
54 private:
53 void calculateControlPoints();
55 void calculateControlPoints();
@@ -80,12 +80,16 QPointF SplineChartItem::calculateGeometryControlPoint(int index) const
80 void SplineChartItem::setLayout(QVector<QPointF> &points)
80 void SplineChartItem::setLayout(QVector<QPointF> &points)
81 {
81 {
82 // Dummy implementation because of a bug in Clang compiler
82 // Dummy implementation because of a bug in Clang compiler
83 if (points.size() < 2) {
84 m_path = QPainterPath();
85 }
83 XYChartItem::setLayout(points);
86 XYChartItem::setLayout(points);
84 }
87 }
85
88
86 void SplineChartItem::setLayout(QVector<QPointF> &points, QVector<QPointF> &controlPoints)
89 void SplineChartItem::setLayout(QVector<QPointF> &points, QVector<QPointF> &controlPoints)
87 {
90 {
88 if ((points.size()<2) || (controlPoints.size()<2)) {
91 if ((points.size()<2) || (controlPoints.size()<2)) {
92 m_path = QPainterPath();
89 XYChartItem::setLayout(points);
93 XYChartItem::setLayout(points);
90 m_controlPoints=controlPoints;
94 m_controlPoints=controlPoints;
91 return;
95 return;
@@ -174,6 +174,8 QList<QPointF> QXYSeries::points() const
174 QList<QPointF> result;
174 QList<QPointF> result;
175 if (d->m_mapOrientation == Qt::Vertical){
175 if (d->m_mapOrientation == Qt::Vertical){
176 // consecutive data is read from model's column
176 // consecutive data is read from model's column
177 if (d->m_mapX >= d->m_model->columnCount() || d->m_mapY >= d->m_model->columnCount())
178 return result; // mapped columns are not existing
177
179
178 for(int i = d->m_mapFirst; i< d->m_mapFirst + count(); ++i) {
180 for(int i = d->m_mapFirst; i< d->m_mapFirst + count(); ++i) {
179 qreal x = d->m_model->data(d->m_model->index(i, d->m_mapX), Qt::DisplayRole).toReal();
181 qreal x = d->m_model->data(d->m_model->index(i, d->m_mapX), Qt::DisplayRole).toReal();
@@ -184,6 +186,9 QList<QPointF> QXYSeries::points() const
184 }
186 }
185 else{
187 else{
186 // consecutive data is read from model's row
188 // consecutive data is read from model's row
189 if (d->m_mapX >= d->m_model->rowCount() || d->m_mapY >= d->m_model->rowCount())
190 return result; // mapped rows are not existing
191
187 for(int i = d->m_mapFirst; i< d->m_mapFirst + count(); ++i) {
192 for(int i = d->m_mapFirst; i< d->m_mapFirst + count(); ++i) {
188 qreal x = d->m_model->data(d->m_model->index(d->m_mapX, i), Qt::DisplayRole).toReal();
193 qreal x = d->m_model->data(d->m_model->index(d->m_mapX, i), Qt::DisplayRole).toReal();
189 qreal y = d->m_model->data(d->m_model->index(d->m_mapY, i), Qt::DisplayRole).toReal();
194 qreal y = d->m_model->data(d->m_model->index(d->m_mapY, i), Qt::DisplayRole).toReal();
@@ -208,14 +213,18 int QXYSeries::count() const
208 if (d->m_mapOrientation == Qt::Vertical) {
213 if (d->m_mapOrientation == Qt::Vertical) {
209 // data is in a column. Return the number of mapped items if the model's column have enough items
214 // data is in a column. Return the number of mapped items if the model's column have enough items
210 // or the number of items that can be mapped
215 // or the number of items that can be mapped
211 if (d->m_mapCount != -1)
216 if (d->m_mapX >= d->m_model->columnCount() || d->m_mapY >= d->m_model->columnCount())
217 return 0; // mapped columns are not existing
218 else if (d->m_mapCount != -1)
212 return qMin(d->m_mapCount, qMax(d->m_model->rowCount() - d->m_mapFirst, 0));
219 return qMin(d->m_mapCount, qMax(d->m_model->rowCount() - d->m_mapFirst, 0));
213 else
220 else
214 return qMax(d->m_model->rowCount() - d->m_mapFirst, 0);
221 return qMax(d->m_model->rowCount() - d->m_mapFirst, 0);
215 } else {
222 } else {
216 // data is in a row. Return the number of mapped items if the model's row have enough items
223 // data is in a row. Return the number of mapped items if the model's row have enough items
217 // or the number of items that can be mapped
224 // or the number of items that can be mapped
218 if (d->m_mapCount != -1)
225 if (d->m_mapX >= d->m_model->rowCount() || d->m_mapY >= d->m_model->rowCount())
226 return 0; // mapped rows are not existing
227 else if (d->m_mapCount != -1)
219 return qMin(d->m_mapCount, qMax(d->m_model->columnCount() - d->m_mapFirst, 0));
228 return qMin(d->m_mapCount, qMax(d->m_model->columnCount() - d->m_mapFirst, 0));
220 else
229 else
221 return qMax(d->m_model->columnCount() - d->m_mapFirst, 0);
230 return qMax(d->m_model->columnCount() - d->m_mapFirst, 0);
@@ -357,13 +366,13 void QXYSeries::setModelMapping(int modelX, int modelY, Qt::Orientation orientat
357
366
358 // connect the signals from the model
367 // connect the signals from the model
359 connect(d->m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex)));
368 connect(d->m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex)));
360 if (d->m_mapOrientation == Qt::Vertical) {
369 // if (d->m_mapOrientation == Qt::Vertical) {
361 connect(d->m_model,SIGNAL(rowsInserted(QModelIndex,int,int)), d, SLOT(modelDataAdded(QModelIndex,int,int)));
370 connect(d->m_model,SIGNAL(rowsInserted(QModelIndex,int,int)), d, SLOT(modelRowsAdded(QModelIndex,int,int)));
362 connect(d->m_model,SIGNAL(rowsRemoved(QModelIndex,int,int)), d, SLOT(modelDataRemoved(QModelIndex,int,int)));
371 connect(d->m_model,SIGNAL(rowsRemoved(QModelIndex,int,int)), d, SLOT(modelRowsRemoved(QModelIndex,int,int)));
363 } else {
372 // } else {
364 connect(d->m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), d, SLOT(modelDataAdded(QModelIndex,int,int)));
373 connect(d->m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), d, SLOT(modelColumnsAdded(QModelIndex,int,int)));
365 connect(d->m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), d, SLOT(modelDataRemoved(QModelIndex,int,int)));
374 connect(d->m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), d, SLOT(modelColumnsRemoved(QModelIndex,int,int)));
366 }
375 // }
367 }
376 }
368
377
369 void QXYSeries::setModelMappingRange(int first, int count)
378 void QXYSeries::setModelMappingRange(int first, int count)
@@ -426,7 +435,7 void QXYSeriesPrivate::scaleDomain(Domain& domain)
426 maxY = qMax(maxY, y);
435 maxY = qMax(maxY, y);
427 }
436 }
428
437
429 domain.setRange(minX,maxX,minY,maxY,tickXCount,tickYCount);
438 domain.setRange(minX,maxX,minY,maxY,tickXCount,tickYCount);
430 }
439 }
431
440
432 QList<LegendMarker*> QXYSeriesPrivate::createLegendMarker(QLegend* legend)
441 QList<LegendMarker*> QXYSeriesPrivate::createLegendMarker(QLegend* legend)
@@ -456,16 +465,40 void QXYSeriesPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight
456 }
465 }
457
466
458
467
459 void QXYSeriesPrivate::modelDataAdded(QModelIndex parent, int start, int end)
468 void QXYSeriesPrivate::modelRowsAdded(QModelIndex parent, int start, int end)
469 {
470 Q_UNUSED(parent);
471 if (m_mapOrientation == Qt::Vertical)
472 emit pointsAdded(start, end);
473 else if (start <= m_mapX || start <= m_mapY)
474 emit reinitialized();
475 }
476
477 void QXYSeriesPrivate::modelRowsRemoved(QModelIndex parent, int start, int end)
478 {
479 Q_UNUSED(parent);
480 if (m_mapOrientation == Qt::Vertical)
481 emit pointsRemoved(start, end);
482 else if (start <= m_mapX || start <= m_mapY)
483 emit reinitialized();
484 }
485
486 void QXYSeriesPrivate::modelColumnsAdded(QModelIndex parent, int start, int end)
460 {
487 {
461 Q_UNUSED(parent);
488 Q_UNUSED(parent);
462 emit pointsAdded(start, end);
489 if (m_mapOrientation == Qt::Horizontal)
490 emit pointsAdded(start, end);
491 else if (start <= m_mapX || start <= m_mapY)
492 emit reinitialized();
463 }
493 }
464
494
465 void QXYSeriesPrivate::modelDataRemoved(QModelIndex parent, int start, int end)
495 void QXYSeriesPrivate::modelColumnsRemoved(QModelIndex parent, int start, int end)
466 {
496 {
467 Q_UNUSED(parent);
497 Q_UNUSED(parent);
468 emit pointsRemoved(start, end);
498 if (m_mapOrientation == Qt::Horizontal)
499 emit pointsRemoved(start, end);
500 else if (start <= m_mapX || start <= m_mapY)
501 emit reinitialized();
469 }
502 }
470
503
471 #include "moc_qxyseries.cpp"
504 #include "moc_qxyseries.cpp"
@@ -48,8 +48,14 public:
48
48
49 protected Q_SLOTS:
49 protected Q_SLOTS:
50 void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight);
50 void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight);
51 virtual void modelDataAdded(QModelIndex parent, int start, int end);
51 virtual void modelRowsAdded(QModelIndex parent, int start, int end);
52 virtual void modelDataRemoved(QModelIndex parent, int start, int end);
52 virtual void modelRowsRemoved(QModelIndex parent, int start, int end);
53 virtual void modelColumnsAdded(QModelIndex parent, int start, int end);
54 virtual void modelColumnsRemoved(QModelIndex parent, int start, int end);
55
56 private:
57 void insertData(int start, int end);
58 void removeData(int start, int end);
53
59
54 Q_SIGNALS:
60 Q_SIGNALS:
55 void updated();
61 void updated();
@@ -58,8 +64,7 Q_SIGNALS:
58 void pointsRemoved(int start, int end);
64 void pointsRemoved(int start, int end);
59 void pointAdded(int index);
65 void pointAdded(int index);
60 void pointsAdded(int start, int end);
66 void pointsAdded(int start, int end);
61
67 void reinitialized();
62
63
68
64 protected:
69 protected:
65 QVector<QPointF> m_points;
70 QVector<QPointF> m_points;
@@ -44,6 +44,7 XYChartItem::XYChartItem(QXYSeries *series, ChartPresenter *presenter):ChartItem
44 connect(series->d_func(),SIGNAL(pointsAdded(int, int)),this,SLOT(handlePointsAdded(int, int)));
44 connect(series->d_func(),SIGNAL(pointsAdded(int, int)),this,SLOT(handlePointsAdded(int, int)));
45 connect(series->d_func(),SIGNAL(pointRemoved(int)),this,SLOT(handlePointRemoved(int)));
45 connect(series->d_func(),SIGNAL(pointRemoved(int)),this,SLOT(handlePointRemoved(int)));
46 connect(series->d_func(),SIGNAL(pointsRemoved(int, int)),this,SLOT(handlePointsRemoved(int, int)));
46 connect(series->d_func(),SIGNAL(pointsRemoved(int, int)),this,SLOT(handlePointsRemoved(int, int)));
47 connect(series->d_func(),SIGNAL(reinitialized()),this,SLOT(handleReinitialized()));
47 connect(this,SIGNAL(clicked(QPointF)),series,SIGNAL(clicked(QPointF)));
48 connect(this,SIGNAL(clicked(QPointF)),series,SIGNAL(clicked(QPointF)));
48 }
49 }
49
50
@@ -211,6 +212,16 void XYChartItem::handlePointReplaced(int index)
211 update();
212 update();
212 }
213 }
213
214
215 void XYChartItem::handleReinitialized()
216 {
217 QVector<QPointF> points = calculateGeometryPoints();
218 if (points.isEmpty())
219 setLayout(points);
220 else
221 updateLayout(m_points,points);
222 update();
223 }
224
214 void XYChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
225 void XYChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
215 {
226 {
216 m_minX=minX;
227 m_minX=minX;
@@ -47,6 +47,7 public Q_SLOTS:
47 void handlePointRemoved(int index);
47 void handlePointRemoved(int index);
48 void handlePointsRemoved(int start, int end);
48 void handlePointsRemoved(int start, int end);
49 void handlePointReplaced(int index);
49 void handlePointReplaced(int index);
50 void handleReinitialized();
50 void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY);
51 void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY);
51 void handleGeometryChanged(const QRectF &size);
52 void handleGeometryChanged(const QRectF &size);
52
53
@@ -368,9 +368,10 void TableWidget::updateChartType(bool toggle)
368 }
368 }
369
369
370
370
371 if (!m_barRadioButton->isChecked())
371 if (!m_barRadioButton->isChecked()) {
372 m_chart->axisX()->setRange(0, 500);
372 m_chart->axisX()->setRange(0, 500);
373 // m_chart->axisY()->setRange(0, 120);
373 m_chart->axisY()->setRange(0, 220);
374 }
374 m_chart->legend()->setVisible(true);
375 m_chart->legend()->setVisible(true);
375
376
376 // repaint table view colors
377 // repaint table view colors
General Comments 0
You need to be logged in to leave comments. Login now