From 1370cf739b4fd6c22258817d9613c69f619230c6 2012-09-17 10:23:17 From: Tero Ahola Date: 2012-09-17 10:23:17 Subject: [PATCH] QML ChartView signals seriesAdded and seriesRemoved --- diff --git a/plugins/declarative/declarativechart.cpp b/plugins/declarative/declarativechart.cpp index 6ee43be..bc71308 100644 --- a/plugins/declarative/declarativechart.cpp +++ b/plugins/declarative/declarativechart.cpp @@ -32,6 +32,7 @@ #include "qcategoryaxis.h" #include "qabstractseries_p.h" #include "declarativemargins.h" +#include "qchart_p.h" #ifndef QT_ON_ARM #include "qdatetimeaxis.h" @@ -186,12 +187,12 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE */ /*! - \qmlmethod Axis ChartView::axisX(QAbstractSeries *series) + \qmlmethod Axis ChartView::axisX(AbstractSeries series) The x-axis of the series. */ /*! - \qmlmethod Axis ChartView::axisY(QAbstractSeries *series) + \qmlmethod Axis ChartView::axisY(AbstractSeries series) The y-axis of the series. */ @@ -231,6 +232,17 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE title. */ +/*! + \qmlsignal ChartView::seriesAdded(AbstractSeries series) + The \a series has been added to the chart. +*/ + +/*! + \qmlsignal ChartView::seriesRemoved(AbstractSeries series) + The \a series has been removed from the chart. Please note that \a series is no longer a valid + object after the signal handler has completed. +*/ + DeclarativeChart::DeclarativeChart(QDeclarativeItem *parent) : QDeclarativeItem(parent), m_chart(new QChart(this)) @@ -241,6 +253,14 @@ DeclarativeChart::DeclarativeChart(QDeclarativeItem *parent) connect(m_minMargins, SIGNAL(bottomChanged(int, int, int, int)), this, SLOT(changeMinimumMargins(int, int, int, int))); connect(m_minMargins, SIGNAL(leftChanged(int, int, int, int)), this, SLOT(changeMinimumMargins(int, int, int, int))); connect(m_minMargins, SIGNAL(rightChanged(int, int, int, int)), this, SLOT(changeMinimumMargins(int, int, int, int))); + connect(m_chart->d_ptr->m_dataset, SIGNAL(seriesAdded(QAbstractSeries *, Domain *)), this, SLOT(handleSeriesAdded(QAbstractSeries *, Domain *))); + connect(m_chart->d_ptr->m_dataset, SIGNAL(seriesRemoved(QAbstractSeries *)), this, SIGNAL(seriesRemoved(QAbstractSeries *))); +} + +void DeclarativeChart::handleSeriesAdded(QAbstractSeries *series, Domain *domain) +{ + Q_UNUSED(domain) + emit seriesAdded(series); } void DeclarativeChart::changeMinimumMargins(int top, int bottom, int left, int right) @@ -620,6 +640,14 @@ QAbstractSeries *DeclarativeChart::createSeries(DeclarativeChart::SeriesType typ return series; } +void DeclarativeChart::removeSeries(QAbstractSeries *series) +{ + if (series) + m_chart->removeSeries(series); + else + qWarning("removeSeries: cannot remove null"); +} + void DeclarativeChart::setAxisX(QAbstractAxis *axis, QAbstractSeries *series) { if (axis) diff --git a/plugins/declarative/declarativechart.h b/plugins/declarative/declarativechart.h index f0076ff..6587ef4 100644 --- a/plugins/declarative/declarativechart.h +++ b/plugins/declarative/declarativechart.h @@ -32,6 +32,7 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE class DeclarativeMargins; +class Domain; class DeclarativeChart : public QDeclarativeItem { @@ -127,7 +128,7 @@ public: Q_INVOKABLE QAbstractSeries *series(QString seriesName); Q_INVOKABLE QAbstractSeries *createSeries(DeclarativeChart::SeriesType type, QString name = ""); Q_INVOKABLE QAbstractSeries *createSeries(DeclarativeChart::SeriesType type, QString name, QAbstractAxis *axisX, QAbstractAxis *axisY); - Q_INVOKABLE void removeSeries(QAbstractSeries *series) { m_chart->removeSeries(series); } + Q_INVOKABLE void removeSeries(QAbstractSeries *series); Q_INVOKABLE void removeAllSeries() { m_chart->removeAllSeries(); } Q_INVOKABLE void setAxisX(QAbstractAxis *axis, QAbstractSeries *series = 0); Q_INVOKABLE void setAxisY(QAbstractAxis *axis, QAbstractSeries *series = 0); @@ -147,11 +148,14 @@ Q_SIGNALS: void dropShadowEnabledChanged(bool enabled); void minimumMarginsChanged(); void plotAreaChanged(QRectF plotArea); + void seriesAdded(QAbstractSeries* series); + void seriesRemoved(QAbstractSeries* series); public Q_SLOTS: void changeMinimumMargins(int top, int bottom, int left, int right); void handleAxisXSet(QAbstractAxis *axis); void handleAxisYSet(QAbstractAxis *axis); + void handleSeriesAdded(QAbstractSeries *series, Domain *domain); private: // Extending QChart with DeclarativeChart is not possible because QObject does not support diff --git a/src/qabstractseries.cpp b/src/qabstractseries.cpp index bc4fc8f..50c9a9b 100644 --- a/src/qabstractseries.cpp +++ b/src/qabstractseries.cpp @@ -89,7 +89,7 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE \brief whether the series is visible or not; true by default. */ /*! - \qmlproperty void AbstractSeries::visible + \qmlproperty bool AbstractSeries::visible Visibility of the series. True by default. */ @@ -101,6 +101,27 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE \qmlsignal AbstractSeries::onVisibleChanged() Emitted when the series visibility changes. */ + +/*! + \property QAbstractSeries::opacity + \brief The opacity of the series. + By default the opacity is 1.0. The valid values range from 0.0 (transparent) to 1.0 (opaque). +*/ +/*! + \qmlproperty real AbstractSeries::opacity + The opacity of the series. By default the opacity is 1.0. + The valid values range from 0.0 (transparent) to 1.0 (opaque). +*/ + +/*! + \fn void QAbstractSeries::opacityChanged() + Emitted when the opacity of the series changes. +*/ +/*! + \qmlsignal AbstractSeries::onOpacityChanged() + Emitted when the opacity of the series changes. +*/ + /*! \internal \brief Constructs ChartSeries object with \a parent. diff --git a/src/qchart.h b/src/qchart.h index 9eb8416..0fe6bff 100644 --- a/src/qchart.h +++ b/src/qchart.h @@ -122,7 +122,7 @@ public: protected: QScopedPointer d_ptr; friend class QLegend; - friend class ChartPresenter; + friend class DeclarativeChart; Q_DISABLE_COPY(QChart) }; diff --git a/tests/qmlchartproperties/qml/qmlchartproperties/Chart.qml b/tests/qmlchartproperties/qml/qmlchartproperties/Chart.qml index 6e69175..c5abec4 100644 --- a/tests/qmlchartproperties/qml/qmlchartproperties/Chart.qml +++ b/tests/qmlchartproperties/qml/qmlchartproperties/Chart.qml @@ -25,7 +25,7 @@ ChartView { id: chartView title: "Chart Title" anchors.fill: parent - property variant series: chartView + property variant chart: chartView LineSeries { name: "line" @@ -40,10 +40,12 @@ ChartView { onVisibleChanged: console.log("chart.onVisibleChanged: " + visible); onTitleColorChanged: console.log("chart.onTitleColorChanged: " + color); - onBackgroundColorChanged: console.log("chart.onBackgroundColorChanged: " + series.backgroundColor); + onBackgroundColorChanged: console.log("chart.onBackgroundColorChanged: " + chart.backgroundColor); onDropShadowEnabledChanged: console.log("chart.onDropShadowEnabledChanged: " + enabled); + onSeriesAdded: console.log("chart.onSeriesAdded: " + series.name); + onSeriesRemoved: console.log("chart.onSeriesRemoved: " + series.name); - legend.onVisibleChanged: console.log("legend.onVisibleChanged: " + series.legend.visible); + legend.onVisibleChanged: console.log("legend.onVisibleChanged: " + chart.legend.visible); legend.onBackgroundVisibleChanged: console.log("legend.onBackgroundVisibleChanged: " + visible); legend.onColorChanged: console.log("legend.onColorChanged: " + color); legend.onBorderColorChanged: console.log("legend.onBorderColorChanged: " + color); diff --git a/tests/qmlchartproperties/qml/qmlchartproperties/ChartEditor.qml b/tests/qmlchartproperties/qml/qmlchartproperties/ChartEditor.qml index 84f8e6b..c7923b5 100644 --- a/tests/qmlchartproperties/qml/qmlchartproperties/ChartEditor.qml +++ b/tests/qmlchartproperties/qml/qmlchartproperties/ChartEditor.qml @@ -22,10 +22,10 @@ import QtQuick 1.0 Item { id: chartEditor - property variant series // TODO: rename to chart - onSeriesChanged: { + property variant chart + onChartChanged: { if (loader.item != undefined) - loader.item.chart = series; + loader.item.chart = chart; } function selectButton(button) { @@ -34,6 +34,7 @@ Item { legendButton.color = "#79bd8f"; axisXButton.color = "#79bd8f"; axisYButton.color = "#79bd8f"; + seriesButton.color = "#79bd8f"; button.color = "#00a388"; } @@ -53,7 +54,7 @@ Item { onClicked: { selectButton(chartButton); loader.source = "ChartEditorProperties.qml"; - loader.item.chart = series; + loader.item.chart = chart; } } Button { @@ -63,7 +64,7 @@ Item { onClicked: { selectButton(titleButton); loader.source = "ChartEditorTitle.qml"; - loader.item.chart = series; + loader.item.chart = chart; } } Button { @@ -73,7 +74,7 @@ Item { onClicked: { selectButton(legendButton); loader.source = "ChartEditorLegend.qml"; - loader.item.chartLegend = series.legend; + loader.item.chartLegend = chart.legend; } } Button { @@ -83,7 +84,7 @@ Item { onClicked: { selectButton(axisXButton); loader.source = "ChartEditorAxis.qml"; - loader.item.axis = series.axisX; + loader.item.axis = chart.axisX; } } Button { @@ -93,7 +94,17 @@ Item { onClicked: { selectButton(axisYButton); loader.source = "ChartEditorAxis.qml"; - loader.item.axis = series.axisY; + loader.item.axis = chart.axisY; + } + } + Button { + id: seriesButton + text: "Series" + unpressedColor: "#79bd8f" + onClicked: { + selectButton(seriesButton); + loader.source = "ChartEditorSeries.qml"; + loader.item.chart = chart; } } } diff --git a/tests/qmlchartproperties/qml/qmlchartproperties/ChartEditorProperties.qml b/tests/qmlchartproperties/qml/qmlchartproperties/ChartEditorProperties.qml index 69df56d..3efc0a3 100644 --- a/tests/qmlchartproperties/qml/qmlchartproperties/ChartEditorProperties.qml +++ b/tests/qmlchartproperties/qml/qmlchartproperties/ChartEditorProperties.qml @@ -82,10 +82,6 @@ Flow { onClicked: chart.titleColor = main.nextColor(); } Button { - text: "zoom -" - onClicked: chart.zoom(0.5); - } - Button { text: "top min margin +" onClicked: chart.minimumMargins.top += 5; } diff --git a/tests/qmlchartproperties/qml/qmlchartproperties/ChartEditorSeries.qml b/tests/qmlchartproperties/qml/qmlchartproperties/ChartEditorSeries.qml new file mode 100644 index 0000000..7242b79 --- /dev/null +++ b/tests/qmlchartproperties/qml/qmlchartproperties/ChartEditorSeries.qml @@ -0,0 +1,64 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc +** All rights reserved. +** For any questions to Digia, please use contact form at http://qt.digia.com +** +** This file is part of the Qt Commercial Charts Add-on. +** +** $QT_BEGIN_LICENSE$ +** Licensees holding valid Qt Commercial licenses may use this file in +** accordance with the Qt Commercial License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. +** +** If you have questions regarding the use of this file, please use +** contact form at http://qt.digia.com +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import QtQuick 1.0 +import QtCommercial.Chart 1.1 + +Flow { + anchors.fill: parent + property variant chart + flow: Flow.TopToBottom + spacing: 5 + + Button { + text: "add line" + onClicked: addXYSeries(ChartView.SeriesTypeLine, "line"); + } + Button { + text: "add spline" + onClicked: addXYSeries(ChartView.SeriesTypeSpline, "spline"); + } + Button { + text: "add scatter" + onClicked: addXYSeries(ChartView.SeriesTypeScatter, "scatter"); + } + Button { + text: "remove last" + onClicked: { + if (chart.count > 0) + chart.removeSeries(chart.series(chart.count - 1)); + else + chart.removeSeries(0); + } + } + Button { + text: "remove all" + onClicked: chart.removeAllSeries(); + } + + function addXYSeries(type, name) { + var series = chart.createSeries(type, name + " " + chart.count); + for (var i = chart.axisX().min; i < chart.axisX().max; i++) { + var y = Math.random() * (chart.axisY().max - chart.axisY().min) + chart.axisY().min; + var x = Math.random() + i; + series.append(x, y); + } + } +} diff --git a/tests/qmlchartproperties/qml/qmlchartproperties/main.qml b/tests/qmlchartproperties/qml/qmlchartproperties/main.qml index 8ba55a3..7e7fca1 100644 --- a/tests/qmlchartproperties/qml/qmlchartproperties/main.qml +++ b/tests/qmlchartproperties/qml/qmlchartproperties/main.qml @@ -47,8 +47,12 @@ Rectangle { height: parent.height source: "Chart.qml" onStatusChanged: { - if (status == Loader.Ready && editorLoader.status == Loader.Ready && chartLoader.item) - editorLoader.item.series = chartLoader.item.series; + if (status == Loader.Ready && editorLoader.status == Loader.Ready && chartLoader.item) { + if (source.toString().search("Chart.qml") > 0) + editorLoader.item.chart = chartLoader.item.chart; + else + editorLoader.item.series = chartLoader.item.series; + } } } @@ -58,8 +62,12 @@ Rectangle { height: parent.height source: "ChartEditor.qml" onStatusChanged: { - if (status == Loader.Ready && chartLoader.status == Loader.Ready && chartLoader.item) - editorLoader.item.series = chartLoader.item.series; + if (status == Loader.Ready && chartLoader.status == Loader.Ready && chartLoader.item) { + if (source.toString().search("ChartEditor.qml") > 0) + editorLoader.item.chart = chartLoader.item.chart; + else + editorLoader.item.series = chartLoader.item.series; + } } } } diff --git a/tests/qmlchartproperties/resources.qrc b/tests/qmlchartproperties/resources.qrc index fdf8a01..a95923d 100644 --- a/tests/qmlchartproperties/resources.qrc +++ b/tests/qmlchartproperties/resources.qrc @@ -26,5 +26,6 @@ qml/qmlchartproperties/ChartEditorLegend.qml qml/qmlchartproperties/ChartEditorProperties.qml qml/qmlchartproperties/ChartEditorTitle.qml + qml/qmlchartproperties/ChartEditorSeries.qml