From 8e3983e15cdce34051c5ea0e89bcef454e105c7c 2012-06-01 12:20:44 From: Tero Ahola Date: 2012-06-01 12:20:44 Subject: [PATCH] Visible property to abstract series --- diff --git a/demos/qmlcustommodel/qml/qmlcustommodel/main.qml b/demos/qmlcustommodel/qml/qmlcustommodel/main.qml index 9a94807..5b1f8bb 100644 --- a/demos/qmlcustommodel/qml/qmlcustommodel/main.qml +++ b/demos/qmlcustommodel/qml/qmlcustommodel/main.qml @@ -27,7 +27,7 @@ Rectangle { height: parent.height ChartView { - id: chart + id: chartView title: "Top-5 car brand shares in Finland" anchors.fill: parent theme: ChartView.ChartThemeLight @@ -53,6 +53,7 @@ Rectangle { BarSeries { name: "Others" barMargin: 0 + visible: false HBarModelMapper { model: customModel firstBarSetRow: 6 @@ -62,10 +63,9 @@ Rectangle { } LineSeries { - id: lineSeries name: "Volkswagen" + visible: false HXYModelMapper { - id: lineSeriesMapper model: customModel xRow: 0 yRow: 1 @@ -73,6 +73,50 @@ Rectangle { } } + LineSeries { + name: "Toyota" + visible: false + HXYModelMapper { + model: customModel + xRow: 0 + yRow: 2 + first: 2 + } + } + + LineSeries { + name: "Ford" + visible: false + HXYModelMapper { + model: customModel + xRow: 0 + yRow: 2 + first: 2 + } + } + + LineSeries { + name: "Skoda" + visible: false + HXYModelMapper { + model: customModel + xRow: 0 + yRow: 3 + first: 2 + } + } + + LineSeries { + name: "Volvo" + visible: false + HXYModelMapper { + model: customModel + xRow: 0 + yRow: 4 + first: 2 + } + } + PieSeries { id: pieSeries size: 0.4 @@ -80,15 +124,12 @@ Rectangle { verticalPosition: 0.4 onClicked: { // Show the selection by exploding the slice - for (var i = 0; i < pieSeries.count; i++) - pieSeries.at(i).exploded = false; - slice.exploded = true; + slice.exploded = !slice.exploded; // Update the line series to show the yearly data for this slice - lineSeries.name = slice.label; - for (var j = 0; j < customModel.rowCount; j++) { - if (customModel.at(j, 1) == slice.label) { - lineSeriesMapper.yRow = j; + for (var i = 0; i < chartView.count; i++) { + if (chartView.series(i).name == slice.label) { + chartView.series(i).visible = slice.exploded; } } } diff --git a/src/areachart/areachartitem.cpp b/src/areachart/areachartitem.cpp index 6735f67..282437f 100644 --- a/src/areachart/areachartitem.cpp +++ b/src/areachart/areachartitem.cpp @@ -45,6 +45,7 @@ AreaChartItem::AreaChartItem(QAreaSeries *areaSeries, ChartPresenter *presenter) m_lower = new AreaBoundItem(this,m_series->lowerSeries(),presenter); QObject::connect(m_series->d_func(),SIGNAL(updated()),this,SLOT(handleUpdated())); + QObject::connect(m_series, SIGNAL(visibleChanged()), this, SLOT(handleUpdated())); QObject::connect(this,SIGNAL(clicked(QPointF)),areaSeries,SIGNAL(clicked(QPointF))); handleUpdated(); @@ -119,18 +120,20 @@ void AreaChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *opt Q_UNUSED(widget) Q_UNUSED(option) - painter->save(); - painter->setPen(m_linePen); - painter->setBrush(m_brush); - painter->setClipRect(m_clipRect); - painter->drawPath(m_path); - if (m_pointsVisible) { - painter->setPen(m_pointPen); - painter->drawPoints(m_upper->geometryPoints()); - if (m_lower) - painter->drawPoints(m_lower->geometryPoints()); + if (m_series->isVisible()) { + painter->save(); + painter->setPen(m_linePen); + painter->setBrush(m_brush); + painter->setClipRect(m_clipRect); + painter->drawPath(m_path); + if (m_pointsVisible) { + painter->setPen(m_pointPen); + painter->drawPoints(m_upper->geometryPoints()); + if (m_lower) + painter->drawPoints(m_lower->geometryPoints()); + } + painter->restore(); } - painter->restore(); } void AreaChartItem::mousePressEvent(QGraphicsSceneMouseEvent *event) diff --git a/src/barchart/barchartitem.cpp b/src/barchart/barchartitem.cpp index 0ca718a..3784b1b 100644 --- a/src/barchart/barchartitem.cpp +++ b/src/barchart/barchartitem.cpp @@ -40,6 +40,7 @@ BarChartItem::BarChartItem(QBarSeries *series, ChartPresenter *presenter) : connect(series->d_func(), SIGNAL(updatedBars()), this, SLOT(handleLayoutChanged())); connect(series->d_func(), SIGNAL(labelsVisibleChanged(bool)), this, SLOT(handleLabelsVisibleChanged(bool))); connect(series->d_func(), SIGNAL(restructuredBars()), this, SLOT(handleDataStructureChanged())); + connect(series, SIGNAL(visibleChanged()), this, SLOT(handleLayoutChanged())); setZValue(ChartPresenter::BarSeriesZValue); handleDataStructureChanged(); } diff --git a/src/barchart/qbarseries.cpp b/src/barchart/qbarseries.cpp index 9633235..cf42bdf 100644 --- a/src/barchart/qbarseries.cpp +++ b/src/barchart/qbarseries.cpp @@ -183,24 +183,6 @@ QList QBarSeries::barSets() const } /*! - Sets the visibility of series to \a visible -*/ -void QBarSeries::setVisible(bool visible) -{ - Q_D(QBarSeries); - d->setVisible(visible); -} - -/*! - Returns the visibility of series -*/ -bool QBarSeries::isVisible() const -{ - Q_D(const QBarSeries); - return d->isVisible(); -} - -/*! Sets the visibility of labels in series to \a visible */ void QBarSeries::setLabelsVisible(bool visible) diff --git a/src/barchart/qbarseries.h b/src/barchart/qbarseries.h index 9f28bb1..afad19e 100644 --- a/src/barchart/qbarseries.h +++ b/src/barchart/qbarseries.h @@ -34,8 +34,7 @@ class QTCOMMERCIALCHART_EXPORT QBarSeries : public QAbstractSeries { Q_OBJECT Q_PROPERTY(qreal barMargin READ barMargin WRITE setBarMargin) - Q_PROPERTY(int count READ barsetCount) // TODO: categoryCount to be removed - Q_PROPERTY(bool visible READ isVisible WRITE setVisible) + Q_PROPERTY(int count READ barsetCount) Q_PROPERTY(bool labelsVisible READ isLabelsVisible WRITE setLabelsVisible) public: @@ -55,8 +54,6 @@ public: QList barSets() const; void clear(); - void setVisible(bool visible = true); - bool isVisible() const; void setLabelsVisible(bool visible = true); bool isLabelsVisible() const; diff --git a/src/linechart/linechartitem.cpp b/src/linechart/linechartitem.cpp index 1243fff..677cd25 100644 --- a/src/linechart/linechartitem.cpp +++ b/src/linechart/linechartitem.cpp @@ -37,6 +37,7 @@ m_pointsVisible(false) { setZValue(ChartPresenter::LineChartZValue); QObject::connect(series->d_func(),SIGNAL(updated()),this,SLOT(handleUpdated())); + QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleUpdated())); handleUpdated(); } @@ -90,15 +91,17 @@ void LineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *opt Q_UNUSED(widget) Q_UNUSED(option) - painter->save(); - painter->setPen(m_linePen); - painter->setClipRect(clipRect()); - painter->drawPath(m_path); - if(m_pointsVisible){ - painter->setPen(m_pointPen); - painter->drawPoints(geometryPoints()); + if (m_series->isVisible()) { + painter->save(); + painter->setPen(m_linePen); + painter->setClipRect(clipRect()); + painter->drawPath(m_path); + if(m_pointsVisible){ + painter->setPen(m_pointPen); + painter->drawPoints(geometryPoints()); + } + painter->restore(); } - painter->restore(); } void LineChartItem::mousePressEvent(QGraphicsSceneMouseEvent *event) diff --git a/src/qabstractseries.cpp b/src/qabstractseries.cpp index bfdb6ce..d7fcbe9 100644 --- a/src/qabstractseries.cpp +++ b/src/qabstractseries.cpp @@ -100,7 +100,7 @@ void QAbstractSeries::setName(const QString& name) { if (name != d_ptr->m_name) { d_ptr->m_name = name; - nameChanged(); + emit nameChanged(); } } @@ -114,6 +114,25 @@ QString QAbstractSeries::name() const } /*! + Sets the visibility of series to \a visible +*/ +void QAbstractSeries::setVisible(bool visible) +{ + if (visible != d_ptr->m_visible) { + d_ptr->m_visible = visible; + emit visibleChanged(); + } +} + +/*! + Returns the visibility of series +*/ +bool QAbstractSeries::isVisible() const +{ + return d_ptr->m_visible; +} + +/*! \brief Returns the chart where series belongs to. Set automatically when the series is added to the chart @@ -129,7 +148,8 @@ QChart* QAbstractSeries::chart() const QAbstractSeriesPrivate::QAbstractSeriesPrivate(QAbstractSeries* q): q_ptr(q), m_chart(0), - m_dataset(0) + m_dataset(0), + m_visible(true) { } diff --git a/src/qabstractseries.h b/src/qabstractseries.h index 3668351..9f9dbe8 100644 --- a/src/qabstractseries.h +++ b/src/qabstractseries.h @@ -34,6 +34,7 @@ class QTCOMMERCIALCHART_EXPORT QAbstractSeries : public QObject { Q_OBJECT Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged) + Q_PROPERTY(bool visible READ isVisible WRITE setVisible NOTIFY visibleChanged) Q_ENUMS(SeriesType) public: @@ -57,10 +58,13 @@ public: virtual SeriesType type() const = 0; void setName(const QString& name); QString name() const; + void setVisible(bool visible = true); + bool isVisible() const; QChart* chart() const; Q_SIGNALS: void nameChanged(); + void visibleChanged(); protected: QScopedPointer d_ptr; diff --git a/src/qabstractseries_p.h b/src/qabstractseries_p.h index 1296e71..97a10b0 100644 --- a/src/qabstractseries_p.h +++ b/src/qabstractseries_p.h @@ -57,6 +57,7 @@ protected: QChart *m_chart; ChartDataSet *m_dataset; QString m_name; + bool m_visible; friend class QAbstractSeries; friend class ChartDataSet; diff --git a/src/scatterchart/scatterchartitem.cpp b/src/scatterchart/scatterchartitem.cpp index 559f4b8..6dd6e96 100644 --- a/src/scatterchart/scatterchartitem.cpp +++ b/src/scatterchart/scatterchartitem.cpp @@ -30,14 +30,15 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE ScatterChartItem::ScatterChartItem(QScatterSeries *series, ChartPresenter *presenter) : -XYChart(series,presenter), -QGraphicsItem(presenter ? presenter->rootItem() : 0), -m_series(series), -m_items(this), -m_shape(QScatterSeries::MarkerShapeRectangle), -m_size(15) + XYChart(series,presenter), + QGraphicsItem(presenter ? presenter->rootItem() : 0), + m_series(series), + m_items(this), + m_shape(QScatterSeries::MarkerShapeRectangle), + m_size(15) { QObject::connect(m_series->d_func(),SIGNAL(updated()), this, SLOT(handleUpdated())); + QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleUpdated())); setZValue(ChartPresenter::ScatterSeriesZValue); setFlags(QGraphicsItem::ItemClipsChildrenToShape); diff --git a/src/splinechart/splinechartitem.cpp b/src/splinechart/splinechartitem.cpp index ca6183c..5c95737 100644 --- a/src/splinechart/splinechartitem.cpp +++ b/src/splinechart/splinechartitem.cpp @@ -28,14 +28,15 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE SplineChartItem::SplineChartItem(QSplineSeries *series, ChartPresenter *presenter) : -XYChart(series, presenter), -QGraphicsItem(presenter ? presenter->rootItem() : 0), -m_series(series), -m_pointsVisible(false), -m_animation(0) + XYChart(series, presenter), + QGraphicsItem(presenter ? presenter->rootItem() : 0), + m_series(series), + m_pointsVisible(false), + m_animation(0) { setZValue(ChartPresenter::LineChartZValue); QObject::connect(m_series->d_func(),SIGNAL(updated()),this,SLOT(handleUpdated())); + QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleUpdated())); handleUpdated(); } @@ -147,15 +148,17 @@ void SplineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *o { Q_UNUSED(widget) Q_UNUSED(option) - painter->save(); - painter->setClipRect(clipRect()); - painter->setPen(m_linePen); - painter->drawPath(m_path); - if (m_pointsVisible) { - painter->setPen(m_pointPen); - painter->drawPoints(geometryPoints()); + if (m_series->isVisible()) { + painter->save(); + painter->setClipRect(clipRect()); + painter->setPen(m_linePen); + painter->drawPath(m_path); + if (m_pointsVisible) { + painter->setPen(m_pointPen); + painter->drawPoints(geometryPoints()); + } + painter->restore(); } - painter->restore(); } void SplineChartItem::mousePressEvent(QGraphicsSceneMouseEvent *event) diff --git a/src/xychart/qxyseries.cpp b/src/xychart/qxyseries.cpp index b947d8c..c03c6de 100644 --- a/src/xychart/qxyseries.cpp +++ b/src/xychart/qxyseries.cpp @@ -90,8 +90,8 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE */ QXYSeries::QXYSeries(QXYSeriesPrivate &d,QObject *parent) : QAbstractSeries(d, parent) { - } + /*! Destroys the object. Series added to QChartView or QChart instances are owned by those, and are deleted when mentioned object are destroyed. diff --git a/tests/auto/qbarset/tst_qbarset.cpp b/tests/auto/qbarset/tst_qbarset.cpp index fdb980f..46b2ae5 100644 --- a/tests/auto/qbarset/tst_qbarset.cpp +++ b/tests/auto/qbarset/tst_qbarset.cpp @@ -401,7 +401,6 @@ void tst_QBarSet::customize() // Test adding data to the sets *set1 << 1 << 2 << 1 << 3; *set2 << 2 << 1 << 3 << 1; - QTest::qWait(3000); } QTEST_MAIN(tst_QBarSet) diff --git a/tests/auto/qxyseries/tst_qxyseries.cpp b/tests/auto/qxyseries/tst_qxyseries.cpp index 03fdf30..bcaa56a 100644 --- a/tests/auto/qxyseries/tst_qxyseries.cpp +++ b/tests/auto/qxyseries/tst_qxyseries.cpp @@ -348,3 +348,15 @@ void tst_QXYSeries::pointsVisible_raw() TRY_COMPARE(spy0.count(), 0); QCOMPARE(m_series->pointsVisible(), pointsVisible); } + +void tst_QXYSeries::changedSignals() +{ + QSignalSpy visibleSpy(m_series, SIGNAL(visibleChanged())); + + m_series->setVisible(false); + m_series->setVisible(false); + TRY_COMPARE(visibleSpy.count(), 1); + m_series->setVisible(true); + TRY_COMPARE(visibleSpy.count(), 2); +} + diff --git a/tests/auto/qxyseries/tst_qxyseries.h b/tests/auto/qxyseries/tst_qxyseries.h index b5f59ec..6d408f3 100644 --- a/tests/auto/qxyseries/tst_qxyseries.h +++ b/tests/auto/qxyseries/tst_qxyseries.h @@ -74,6 +74,7 @@ private slots: void replace_chart(); void replace_chart_animation_data(); void replace_chart_animation(); + void changedSignals(); protected: void append_data(); void count_data(); diff --git a/tests/qmlchartproperties/qml/qmlchartproperties/AreaEditor.qml b/tests/qmlchartproperties/qml/qmlchartproperties/AreaEditor.qml index cbd29be..43ad78f 100644 --- a/tests/qmlchartproperties/qml/qmlchartproperties/AreaEditor.qml +++ b/tests/qmlchartproperties/qml/qmlchartproperties/AreaEditor.qml @@ -29,6 +29,10 @@ Flow { property variant series Button { + text: "visible" + onClicked: series.visible = !series.visible; + } + Button { text: "color" onClicked: series.color = main.nextColor(); } diff --git a/tests/qmlchartproperties/qml/qmlchartproperties/BarEditor.qml b/tests/qmlchartproperties/qml/qmlchartproperties/BarEditor.qml index 424af4b..38543ad 100644 --- a/tests/qmlchartproperties/qml/qmlchartproperties/BarEditor.qml +++ b/tests/qmlchartproperties/qml/qmlchartproperties/BarEditor.qml @@ -28,18 +28,6 @@ Flow { property variant series Button { - text: "set 1 color" - onClicked: series.at(0).color = main.nextColor(); - } - Button { - text: "set 1 border color" - onClicked: series.at(0).borderColor = main.nextColor(); - } - Button { - text: "set 1 label color" - onClicked: series.at(0).labelColor = main.nextColor(); - } - Button { text: "visible" onClicked: series.visible = !series.visible; } @@ -56,6 +44,18 @@ Flow { onClicked: series.barMargin -= 0.1; } Button { + text: "set 1 color" + onClicked: series.at(0).color = main.nextColor(); + } + Button { + text: "set 1 border color" + onClicked: series.at(0).borderColor = main.nextColor(); + } + Button { + text: "set 1 label color" + onClicked: series.at(0).labelColor = main.nextColor(); + } + Button { text: "set 1 font size +" onClicked: series.at(0).labelFont.pixelSize += 1; } diff --git a/tests/qmlchartproperties/qml/qmlchartproperties/LineEditor.qml b/tests/qmlchartproperties/qml/qmlchartproperties/LineEditor.qml index 425a31d..f4243f4 100644 --- a/tests/qmlchartproperties/qml/qmlchartproperties/LineEditor.qml +++ b/tests/qmlchartproperties/qml/qmlchartproperties/LineEditor.qml @@ -29,6 +29,10 @@ Flow { property variant series Button { + text: "visible" + onClicked: series.visible = !series.visible; + } + Button { text: "color" onClicked: series.color = main.nextColor(); } diff --git a/tests/qmlchartproperties/qml/qmlchartproperties/PieEditor.qml b/tests/qmlchartproperties/qml/qmlchartproperties/PieEditor.qml index 42a3af8..4309ced 100644 --- a/tests/qmlchartproperties/qml/qmlchartproperties/PieEditor.qml +++ b/tests/qmlchartproperties/qml/qmlchartproperties/PieEditor.qml @@ -28,6 +28,50 @@ Flow { property variant series Button { + text: "visible" + onClicked: series.visible = !series.visible; + } + Button { + text: "series hpos +" + onClicked: series.horizontalPosition += 0.1; + } + Button { + text: "series hpos -" + onClicked: series.horizontalPosition -= 0.1; + } + Button { + text: "series vpos +" + onClicked: series.verticalPosition += 0.1; + } + Button { + text: "series vpos -" + onClicked: series.verticalPosition -= 0.1; + } + Button { + text: "series size +" + onClicked: series.size += 0.1; + } + Button { + text: "series size -" + onClicked: series.size -= 0.1; + } + Button { + text: "series start angle +" + onClicked: series.startAngle += 0.1; + } + Button { + text: "series start angle -" + onClicked: series.startAngle -= 0.1; + } + Button { + text: "series end angle +" + onClicked: series.endAngle += 0.1; + } + Button { + text: "series end angle -" + onClicked: series.endAngle -= 0.1; + } + Button { text: "slice color" onClicked: series.at(0).color = main.nextColor(); } @@ -71,44 +115,4 @@ Flow { text: "slice explode dist -" onClicked: series.at(0).explodeDistanceFactor -= 0.1; } - Button { - text: "series hpos +" - onClicked: series.horizontalPosition += 0.1; - } - Button { - text: "series hpos -" - onClicked: series.horizontalPosition -= 0.1; - } - Button { - text: "series vpos +" - onClicked: series.verticalPosition += 0.1; - } - Button { - text: "series vpos -" - onClicked: series.verticalPosition -= 0.1; - } - Button { - text: "series size +" - onClicked: series.size += 0.1; - } - Button { - text: "series size -" - onClicked: series.size -= 0.1; - } - Button { - text: "series start angle +" - onClicked: series.startAngle += 0.1; - } - Button { - text: "series start angle -" - onClicked: series.startAngle -= 0.1; - } - Button { - text: "series end angle +" - onClicked: series.endAngle += 0.1; - } - Button { - text: "series end angle -" - onClicked: series.endAngle -= 0.1; - } } diff --git a/tests/qmlchartproperties/qml/qmlchartproperties/ScatterEditor.qml b/tests/qmlchartproperties/qml/qmlchartproperties/ScatterEditor.qml index e1d5ebc..05d8341 100644 --- a/tests/qmlchartproperties/qml/qmlchartproperties/ScatterEditor.qml +++ b/tests/qmlchartproperties/qml/qmlchartproperties/ScatterEditor.qml @@ -29,6 +29,10 @@ Flow { property variant series Button { + text: "visible" + onClicked: series.visible = !series.visible; + } + Button { text: "color" onClicked: series.color = main.nextColor(); }