From 264c9c6f234944c28b0f6cd36043e87d89a46de9 2012-05-31 10:29:43 From: Tero Ahola Date: 2012-05-31 10:29:43 Subject: [PATCH] Added stacked and percent bar series to QML api --- diff --git a/plugins/declarative/declarativebarseries.cpp b/plugins/declarative/declarativebarseries.cpp index 17194fb..ac994ae 100644 --- a/plugins/declarative/declarativebarseries.cpp +++ b/plugins/declarative/declarativebarseries.cpp @@ -196,6 +196,96 @@ DeclarativeBarSet *DeclarativeGroupedBarSeries::at(int index) return 0; } +DeclarativeStackedBarSeries::DeclarativeStackedBarSeries(QDeclarativeItem *parent) : + QStackedBarSeries(parent) +{ +} + +void DeclarativeStackedBarSeries::classBegin() +{ +} + +void DeclarativeStackedBarSeries::componentComplete() +{ + foreach(QObject *child, children()) { + if (qobject_cast(child)) { + QBarSeries::append(qobject_cast(child)); + } else if(qobject_cast(child)) { + QVBarModelMapper *mapper = qobject_cast(child); + mapper->setSeries(this); + } else if(qobject_cast(child)) { + QHBarModelMapper *mapper = qobject_cast(child); + mapper->setSeries(this); + } + } +} + +QDeclarativeListProperty DeclarativeStackedBarSeries::seriesChildren() +{ + return QDeclarativeListProperty(this, 0, &DeclarativeBarSeries::appendSeriesChildren); +} + +void DeclarativeStackedBarSeries::appendSeriesChildren(QDeclarativeListProperty * list, QObject *element) +{ + // Empty implementation; the children are parsed in componentComplete instead + Q_UNUSED(list); + Q_UNUSED(element); +} + +DeclarativeBarSet *DeclarativeStackedBarSeries::at(int index) +{ + QList setList = barSets(); + if (index < setList.count()) + return qobject_cast(setList[index]); + + return 0; +} + +DeclarativePercentBarSeries::DeclarativePercentBarSeries(QDeclarativeItem *parent) : + QPercentBarSeries(parent) +{ +} + +void DeclarativePercentBarSeries::classBegin() +{ +} + +void DeclarativePercentBarSeries::componentComplete() +{ + foreach(QObject *child, children()) { + if (qobject_cast(child)) { + QBarSeries::append(qobject_cast(child)); + } else if(qobject_cast(child)) { + QVBarModelMapper *mapper = qobject_cast(child); + mapper->setSeries(this); + } else if(qobject_cast(child)) { + QHBarModelMapper *mapper = qobject_cast(child); + mapper->setSeries(this); + } + } +} + +QDeclarativeListProperty DeclarativePercentBarSeries::seriesChildren() +{ + return QDeclarativeListProperty(this, 0, &DeclarativeBarSeries::appendSeriesChildren); +} + +void DeclarativePercentBarSeries::appendSeriesChildren(QDeclarativeListProperty * list, QObject *element) +{ + // Empty implementation; the children are parsed in componentComplete instead + Q_UNUSED(list); + Q_UNUSED(element); +} + +DeclarativeBarSet *DeclarativePercentBarSeries::at(int index) +{ + QList setList = barSets(); + if (index < setList.count()) + return qobject_cast(setList[index]); + + return 0; +} + #include "moc_declarativebarseries.cpp" QTCOMMERCIALCHART_END_NAMESPACE diff --git a/plugins/declarative/declarativebarseries.h b/plugins/declarative/declarativebarseries.h index 011b05c..e3a038f 100644 --- a/plugins/declarative/declarativebarseries.h +++ b/plugins/declarative/declarativebarseries.h @@ -22,10 +22,12 @@ #define DECLARATIVEBARSERIES_H #include "qchartglobal.h" -#include -#include #include +#include +#include #include +#include +#include QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -105,6 +107,46 @@ public Q_SLOTS: static void appendSeriesChildren(QDeclarativeListProperty *list, QObject *element); }; +class DeclarativeStackedBarSeries : public QStackedBarSeries, public QDeclarativeParserStatus +{ + Q_OBJECT + Q_INTERFACES(QDeclarativeParserStatus) + Q_PROPERTY(QDeclarativeListProperty seriesChildren READ seriesChildren) + Q_CLASSINFO("DefaultProperty", "seriesChildren") + +public: + explicit DeclarativeStackedBarSeries(QDeclarativeItem *parent = 0); + QDeclarativeListProperty seriesChildren(); + Q_INVOKABLE DeclarativeBarSet *at(int index); + +public: // from QDeclarativeParserStatus + void classBegin(); + void componentComplete(); + +public Q_SLOTS: + static void appendSeriesChildren(QDeclarativeListProperty *list, QObject *element); +}; + +class DeclarativePercentBarSeries : public QPercentBarSeries, public QDeclarativeParserStatus +{ + Q_OBJECT + Q_INTERFACES(QDeclarativeParserStatus) + Q_PROPERTY(QDeclarativeListProperty seriesChildren READ seriesChildren) + Q_CLASSINFO("DefaultProperty", "seriesChildren") + +public: + explicit DeclarativePercentBarSeries(QDeclarativeItem *parent = 0); + QDeclarativeListProperty seriesChildren(); + Q_INVOKABLE DeclarativeBarSet *at(int index); + +public: // from QDeclarativeParserStatus + void classBegin(); + void componentComplete(); + +public Q_SLOTS: + static void appendSeriesChildren(QDeclarativeListProperty *list, QObject *element); +}; + QTCOMMERCIALCHART_END_NAMESPACE #endif // DECLARATIVEBARSERIES_H diff --git a/plugins/declarative/plugin.cpp b/plugins/declarative/plugin.cpp index f1c8081..2788967 100644 --- a/plugins/declarative/plugin.cpp +++ b/plugins/declarative/plugin.cpp @@ -55,6 +55,8 @@ public: qmlRegisterType(uri, 1, 0, "AreaSeries"); qmlRegisterType(uri, 1, 0, "BarSeries"); qmlRegisterType(uri, 1, 0, "GroupedBarSeries"); + qmlRegisterType(uri, 1, 0, "StackedBarSeries"); + qmlRegisterType(uri, 1, 0, "PercentBarSeries"); qmlRegisterType(uri, 1, 0, "PieSeries"); qmlRegisterType(uri, 1, 0, "PieSlice"); qmlRegisterType(uri, 1, 0, "BarSet"); diff --git a/tests/qmlchartproperties/qml/qmlchartproperties/BarChart.qml b/tests/qmlchartproperties/qml/qmlchartproperties/BarChart.qml index 4e57e78..8d8435c 100644 --- a/tests/qmlchartproperties/qml/qmlchartproperties/BarChart.qml +++ b/tests/qmlchartproperties/qml/qmlchartproperties/BarChart.qml @@ -31,7 +31,7 @@ ChartView { property variant series: daSeries - GroupedBarSeries { + BarSeries { id: daSeries barCategories: [ "2008", "2009", "2010", "2011", "2012" ] BarSet { name: "Bob"; values: [2, 2, 3, 4, 5, 6] } diff --git a/tests/qmlchartproperties/qml/qmlchartproperties/Button.qml b/tests/qmlchartproperties/qml/qmlchartproperties/Button.qml index 5f07e0c..e5325c1 100644 --- a/tests/qmlchartproperties/qml/qmlchartproperties/Button.qml +++ b/tests/qmlchartproperties/qml/qmlchartproperties/Button.qml @@ -23,7 +23,7 @@ import QtQuick 1.0 Rectangle { id: button height: 25 - width: 110 + width: 140 color: "#afafaf" radius: 5 diff --git a/tests/qmlchartproperties/qml/qmlchartproperties/GroupedBarChart.qml b/tests/qmlchartproperties/qml/qmlchartproperties/GroupedBarChart.qml new file mode 100644 index 0000000..d81e395 --- /dev/null +++ b/tests/qmlchartproperties/qml/qmlchartproperties/GroupedBarChart.qml @@ -0,0 +1,41 @@ +/**************************************************************************** +** +** 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.0 + +ChartView { + title: "Grouped bar series" + anchors.fill: parent + theme: ChartView.ChartThemeLight + legend: ChartView.LegendBottom + // TODO: labels defined by x-axis, not by bar series + axisXLabels: ["0", "2008", "1", "2009", "2", "2010", "3", "2012"] + + property variant series: daSeries + + GroupedBarSeries { + id: daSeries + barCategories: [ "2008", "2009", "2010", "2011", "2012" ] + BarSet { name: "Bob"; values: [2, 2, 3, 4, 5, 6] } + BarSet { name: "Susan"; values: [5, 1, 2, 4, 1, 7] } + BarSet { name: "James"; values: [3, 5, 8, 13, 5, 8] } + } +} diff --git a/tests/qmlchartproperties/qml/qmlchartproperties/PercentBarChart.qml b/tests/qmlchartproperties/qml/qmlchartproperties/PercentBarChart.qml new file mode 100644 index 0000000..58f580d --- /dev/null +++ b/tests/qmlchartproperties/qml/qmlchartproperties/PercentBarChart.qml @@ -0,0 +1,40 @@ +/**************************************************************************** +** +** 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.0 + +ChartView { + title: "Percent bar series" + anchors.fill: parent + theme: ChartView.ChartThemeLight + legend: ChartView.LegendBottom + // TODO: labels defined by x-axis, not by bar series + axisXLabels: ["0", "2008", "1", "2009", "2", "2010", "3", "2012"] + + property variant series: daSeries + + PercentBarSeries { + id: daSeries + BarSet { name: "Bob"; values: [2, 2, 3, 4, 5, 6] } + BarSet { name: "Susan"; values: [5, 1, 2, 4, 1, 7] } + BarSet { name: "James"; values: [3, 5, 8, 13, 5, 8] } + } +} diff --git a/tests/qmlchartproperties/qml/qmlchartproperties/StackedBarChart.qml b/tests/qmlchartproperties/qml/qmlchartproperties/StackedBarChart.qml new file mode 100644 index 0000000..8a98fef --- /dev/null +++ b/tests/qmlchartproperties/qml/qmlchartproperties/StackedBarChart.qml @@ -0,0 +1,39 @@ +/**************************************************************************** +** +** 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.0 + +ChartView { + title: "Stacked bar series" + anchors.fill: parent + theme: ChartView.ChartThemeLight + legend: ChartView.LegendBottom + axisXLabels: ["0", "2008", "1", "2009", "2", "2010", "3", "2012"] + + property variant series: daSeries + + StackedBarSeries { + id: daSeries + BarSet { name: "Bob"; values: [2, 2, 3, 4, 5, 6] } + BarSet { name: "Susan"; values: [5, 1, 2, 4, 1, 7] } + BarSet { name: "James"; values: [3, 5, 8, 13, 5, 8] } + } +} diff --git a/tests/qmlchartproperties/qml/qmlchartproperties/main.qml b/tests/qmlchartproperties/qml/qmlchartproperties/main.qml index 7c02877..c419082 100644 --- a/tests/qmlchartproperties/qml/qmlchartproperties/main.qml +++ b/tests/qmlchartproperties/qml/qmlchartproperties/main.qml @@ -26,7 +26,7 @@ Rectangle { width: parent.width height: parent.height property int viewNumber: 0 - property int viewCount: 6 + property int viewCount: 9 property variant colors: ["#637D74", "#403D3A", "#8C3B3B", "#AB6937", "#D4A960"] property int colorIndex: 0 @@ -55,6 +55,15 @@ Rectangle { } else if (viewNumber == 5) { chartLoader.source = "BarChart.qml"; editorLoader.source = "BarEditor.qml"; + } else if (viewNumber == 6) { + chartLoader.source = "GroupedBarChart.qml"; + editorLoader.source = "BarEditor.qml"; + } else if (viewNumber == 7) { + chartLoader.source = "StackedBarChart.qml"; + editorLoader.source = "BarEditor.qml"; + } else if (viewNumber == 8) { + chartLoader.source = "PercentBarChart.qml"; + editorLoader.source = "BarEditor.qml"; } else { console.log("Illegal view number"); } @@ -63,6 +72,7 @@ Rectangle { Row { anchors.top: parent.top anchors.bottom: buttonRow.top + anchors.bottomMargin: 10 anchors.left: parent.left anchors.right: parent.right @@ -80,7 +90,7 @@ Rectangle { Loader { id: editorLoader - width: 230 + width: 280 height: parent.height source: "PieEditor.qml" onStatusChanged: { @@ -93,43 +103,22 @@ Rectangle { Row { id: buttonRow - height: 100 + height: 40 anchors.bottom: parent.bottom - anchors.bottomMargin: 10 anchors.horizontalCenter: parent.horizontalCenter spacing: 10 - Rectangle { - height: buttonRow.height - width: 100 - color: "#afafaf" - radius: 5 - Text { - anchors.centerIn: parent - text: "previous" - } - MouseArea { - anchors.fill: parent - onClicked: { - viewNumber = (viewNumber + viewCount - 1) % viewCount; - } + Button { + text: "previous" + onClicked: { + viewNumber = (viewNumber + viewCount - 1) % viewCount; } } - Rectangle { - height: buttonRow.height - width: 100 - color: "#afafaf" - radius: 5 - Text { - anchors.centerIn: parent - text: "next" - } - MouseArea { - anchors.fill: parent - onClicked: { - viewNumber = (viewNumber + 1) % viewCount; - } + Button { + text: "next" + onClicked: { + viewNumber = (viewNumber + 1) % viewCount; } } } diff --git a/tests/qmlchartproperties/resources.qrc b/tests/qmlchartproperties/resources.qrc index f2ba921..0452afa 100644 --- a/tests/qmlchartproperties/resources.qrc +++ b/tests/qmlchartproperties/resources.qrc @@ -14,5 +14,8 @@ qml/qmlchartproperties/BarEditor.qml qml/qmlchartproperties/ScatterEditor.qml qml/qmlchartproperties/AreaEditor.qml + qml/qmlchartproperties/GroupedBarChart.qml + qml/qmlchartproperties/StackedBarChart.qml + qml/qmlchartproperties/PercentBarChart.qml