##// END OF EJS Templates
Added declarative model for bar series
Tero Ahola -
r1162:e5feb9c12a84
parent child
Show More
@@ -1,111 +1,138
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 import QtQuick 1.0
22 22 import QtCommercial.Chart 1.0
23 23
24 24 Rectangle {
25 25 anchors.fill: parent
26 property int __explodedIndex: -1
26 27
27 28 ChartView {
28 29 id: chart
29 30 title: "Top-5 car brand shares in Finland"
30 31 anchors.top: parent.top
31 32 anchors.bottom: button.top
32 33 anchors.left: parent.left
33 34 anchors.right: parent.right
34 35 theme: ChartView.ChartThemeLight
35 36 legend: ChartView.LegendBottom
36 37 animationOptions: ChartView.SeriesAnimations
37 38
38 39 PieSeries {
40 id: pieSeries
39 41 model: PieModel {
40 42 id: pieModel
41 PieSlice { label: "Volkswagen"; value: 13.5 }
43 // TODO: initializing properties does not work at the moment, see DeclarativePieModel::append
44 // TODO: explode range, color, border color, border thickness, font, ..
45 PieSlice { exploded: true; label: "Volkswagen"; value: 13.5 }
42 46 PieSlice { label: "Toyota"; value: 10.9 }
43 47 PieSlice { label: "Ford"; value: 8.6 }
44 48 PieSlice { label: "Skoda"; value: 8.2 }
45 49 PieSlice { label: "Volvo"; value: 6.8 }
46 50 }
47 51 }
48 52 }
49 53
54 Timer {
55 repeat: true
56 interval: 2000
57 running: true
58 onTriggered: {
59 changeSliceExploded(__explodedIndex);
60 __explodedIndex = (__explodedIndex + 1) % pieModel.count;
61 changeSliceExploded(__explodedIndex);
62 }
63 }
64
65 function changeSliceExploded(index) {
66 if (index >= 0 && index < pieModel.count) {
67 pieSeries.slice(index).exploded = !pieSeries.slice(index).exploded;
68 }
69 }
70
50 71 Rectangle {
51 72 id: button
52 73 anchors.bottom: parent.bottom
53 74 anchors.bottomMargin: 10
54 75 anchors.horizontalCenter: parent.horizontalCenter
55 76 height: 40
56 77 width: 100
57 78 color: "orange"
58 79 radius: 5
59 80 Text {
60 81 id: buttonText
61 82 anchors.centerIn: parent
62 83 text: button.state == "" ? "Show others" : "Hide others"
63 84 }
64 85 MouseArea {
65 86 anchors.fill: parent
66 87 onClicked: {
67 88 if (button.state == "") {
68 89 // The share of "others" was enabled -> append the data into the model
69 90 // TODO: this should also be doable by redefining the range inside the model
70 91 button.state = "show";
71 92 pieModel.append(["Others", 52.0]);
72 93 } else {
73 94 // The share of "others" was disabled -> remove the data from the model
74 95 // TODO: this should also be doable by redefining the range inside the model
75 96 button.state = "";
76 97 pieModel.removeRow(pieModel.count - 1);
98 // TODO: removeAll("label") ?
77 99 }
78 100 }
79 101 }
80 102 }
81 103
82 104
83 105 // TODO: Optional syntax for defining models for different series. Is this really needed?
84 106 // ChartModel {
85 107 // id: chartModel
86 108 // ChartElement { column1: "Volkswagen"; column2: 13.5; column3: 1.2 }
87 109 // ChartElement { column1: "Toyota"; column2: 10.9; column3: 2.5 }
88 110 // }
89 111 // // column3 not used by pie series
90 112 // PieSeries {
91 113 // model: chartModel
92 // mappings: [ {"column1":"label"}, {"column2":"value"} ]
114 // modelMapping: PieMapping {
115 // labels: 0 // undefined by default
116 // values: 1 // undefined by default
117 // first: 0 // 0 by default
118 // count: 10 // "Undefined" by default
119 // orientation: PieMapping.Vertical // Vertical by default
120 // }
93 121 // }
94 122
95
96 123 // TODO: show how to use data from a list model in a chart view
97 124 // i.e. copy the data into a chart model
98 125 // ListModel {
99 126 // id: listModel
100 127 // ListElement {
101 128 // label: "Volkswagen"
102 129 // value: 13.5
103 130 // }
104 131 // ListElement {
105 132 // label: "Toyota"
106 133 // value: 10.9
107 134 // }
108 135 // // and so on...
109 136 // }
110 137
111 138 }
@@ -1,115 +1,128
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 import QtQuick 1.0
22 22 import QtCommercial.Chart 1.0
23 23
24 24 Rectangle {
25 25 anchors.fill: parent
26 26
27 27 ChartView {
28 28 title: "NHL All-Star Team Players"
29 29 anchors.fill: parent
30 30 theme: ChartView.ChartThemeHighContrast
31 31 legend: ChartView.LegendTop
32 32 axisXLabels: ["0", "2000", "1", "2001", "2", "2002", "3", "2003", "4", "2004", "5", "2005",
33 33 "6", "2006", "7", "2007", "8", "2008", "9", "2009", "10", "2010", "11", "2011"]
34 34
35 35 AreaSeries {
36 36 name: "Russian"
37 37 upperModel: russianModel
38 38 lowerModel: zerosModel
39 39 }
40 40 AreaSeries {
41 41 name: "Swedish"
42 42 upperModel: swedishModel
43 43 lowerModel: zerosModel
44 44 }
45 45 AreaSeries {
46 46 name: "Finnish"
47 47 upperModel: finnishModel
48 48 lowerModel: zerosModel
49 49 }
50 50 }
51 51
52 // TODO: optional implementation with generic ChartModel
53 // AreaSeries {
54 // model: chartModel
55 // modelMapping: XyMapping {
56 // xValues: 0 // undefined by default
57 // yValues: 1 // undefined by default
58 // first: 0 // 0 by default
59 // count: 10 // "Undefined" by default
60 // orientation: XyMapping.Vertical // Vertical by default
61 // }
62 // }
63
64
52 65 XYModel {
53 66 id: zerosModel
54 67 XyPoint { x: 0; y: 0 }
55 68 XyPoint { x: 1; y: 0 }
56 69 XyPoint { x: 2; y: 0 }
57 70 XyPoint { x: 3; y: 0 }
58 71 XyPoint { x: 4; y: 0 }
59 72 XyPoint { x: 5; y: 0 }
60 73 XyPoint { x: 6; y: 0 }
61 74 XyPoint { x: 7; y: 0 }
62 75 XyPoint { x: 8; y: 0 }
63 76 XyPoint { x: 9; y: 0 }
64 77 XyPoint { x: 10; y: 0 }
65 78 XyPoint { x: 11; y: 0 }
66 79 }
67 80
68 81 XYModel {
69 82 id: russianModel
70 83 XyPoint { x: 0; y: 1 }
71 84 XyPoint { x: 1; y: 1 }
72 85 XyPoint { x: 2; y: 1 }
73 86 XyPoint { x: 3; y: 1 }
74 87 XyPoint { x: 4; y: 1 }
75 88 XyPoint { x: 5; y: 0 }
76 89 XyPoint { x: 6; y: 1 }
77 90 XyPoint { x: 7; y: 1 }
78 91 XyPoint { x: 8; y: 4 }
79 92 XyPoint { x: 9; y: 3 }
80 93 XyPoint { x: 10; y: 2 }
81 94 XyPoint { x: 11; y: 1 }
82 95 }
83 96
84 97 XYModel {
85 98 id: swedishModel
86 99 XyPoint { x: 0; y: 1 }
87 100 XyPoint { x: 1; y: 1 }
88 101 XyPoint { x: 2; y: 3 }
89 102 XyPoint { x: 3; y: 3 }
90 103 XyPoint { x: 4; y: 2 }
91 104 XyPoint { x: 5; y: 0 }
92 105 XyPoint { x: 6; y: 2 }
93 106 XyPoint { x: 7; y: 1 }
94 107 XyPoint { x: 8; y: 2 }
95 108 XyPoint { x: 9; y: 1 }
96 109 XyPoint { x: 10; y: 3 }
97 110 XyPoint { x: 11; y: 3 }
98 111 }
99 112
100 113 XYModel {
101 114 id: finnishModel
102 115 XyPoint { x: 0; y: 0 }
103 116 XyPoint { x: 1; y: 0 }
104 117 XyPoint { x: 2; y: 0 }
105 118 XyPoint { x: 3; y: 0 }
106 119 XyPoint { x: 4; y: 0 }
107 120 XyPoint { x: 5; y: 0 }
108 121 XyPoint { x: 6; y: 1 }
109 122 XyPoint { x: 7; y: 0 }
110 123 XyPoint { x: 8; y: 0 }
111 124 XyPoint { x: 9; y: 0 }
112 125 XyPoint { x: 10; y: 0 }
113 126 XyPoint { x: 11; y: 1 }
114 127 }
115 128 }
@@ -1,38 +1,78
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 import QtQuick 1.0
22 22 import QtCommercial.Chart 1.0
23 23
24 24 Rectangle {
25 25 anchors.fill: parent
26 26
27 27 ChartView {
28 28 title: "Bar series"
29 29 anchors.fill: parent
30 30 theme: ChartView.ChartThemeLight
31 31 legend: ChartView.LegendBottom
32 // axisXLabels: ["0", "2008", "1", "2009", "2", "2010", "3", "2012"]
33 axisX.max: 10
32 34
33 35 BarSeries {
34 36 barCategories: [ "2008", "2009", "2010", "2011", "2012" ]
35 // data implementation missing
37 model: barModel
36 38 }
39
40
41 // // TODO: optional syntax with ChartModel base model API
42 // BarSeries {
43 // model: chartModel
44 // modelMapping: BarSeriesMapping {
45 // // Giving "undefined" x mapping value means that the indexes are used as x-values
46 // setIndexes: [BarSeriesMapping.Undefined, 0,
47 // BarSeriesMapping.Undefined, 1,
48 // BarSeriesMapping.Undefined, 2] // defaults to []
49 //// setValues: [
50 //// BarSetMapping {x: BarSetMapping.Undefined; y: 0},
51 //// BarSetMapping {x: BarSetMapping.Undefined; y: 1},
52 //// BarSetMapping {x: BarSetMapping.Undefined; y: 2}
53 //// ]
54 // orientation: BarSeriesMapping.Vertical // defaults to Vertical
55 // startIndex: 0 // defaults to 0
56 // count: BarSeriesMapping.Undefined // defaults to "Undefined"
57 // }
58 // }
59 }
60
61 // ChartModel {
62 // id: chartModel
63 // }
64
65 BarModel {
66 id: barModel
67 BarSet { name: "Bob"; values: [2, 2, 3, 4, 5, 6] }
68 BarSet { name: "Bub"; values: [5, 1, 2, 4, 1, 7] }
69 BarSet { name: "Bib"; values: [3, 5, 8, 13, 5, 8] }
37 70 }
71
72 // TODO
73 // Component.onCompleted: {
74 // bobBars.append(1.2);
75 // bobBars.append(1.5);
76 // bobBars.append([1.5, 1.4, 1.9]);
77 // }
38 78 }
@@ -1,81 +1,97
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "declarativebarseries.h"
22 22 #include "declarativechart.h"
23 #include "qchart.h"
24 #include "qbarseries.h"
25 #include "qbarset.h"
23 #include <QBarSet>
26 24
27 25 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 26
29 DeclarativeBarSeries::DeclarativeBarSeries(QDeclarativeItem *parent) :
30 QDeclarativeItem(parent)
27 DeclarativeBarSet::DeclarativeBarSet(QObject *parent) :
28 QBarSet("", parent)
31 29 {
32 setFlag(QGraphicsItem::ItemHasNoContents, false);
33 30 }
34 31
35 void DeclarativeBarSeries::componentComplete()
32 QVariantList DeclarativeBarSet::values()
36 33 {
34 QVariantList values;
35 for (int i(0); i < count(); i++)
36 values.append(QVariant(at(i)));
37 return values;
37 38 }
38 39
39 void DeclarativeBarSeries::setBarCategories(QStringList categories)
40 void DeclarativeBarSet::setValues(QVariantList values)
40 41 {
41 m_categories = categories;
42 while (count())
43 remove(count() - 1);
42 44
43 if (m_series) {
44 delete m_series;
45 m_series = 0;
45 for (int i(0); i < values.count(); i++) {
46 if (values.at(i).canConvert(QVariant::Double))
47 append(values[i].toDouble());
46 48 }
49 }
47 50
48 DeclarativeChart *declarativeChart = qobject_cast<DeclarativeChart *>(parent());
49 if (declarativeChart) {
50 QChart *chart = qobject_cast<QChart *>(declarativeChart->m_chart);
51 Q_ASSERT(chart);
51 DeclarativeBarSeries::DeclarativeBarSeries(QDeclarativeItem *parent) :
52 QBarSeries(parent)
53 {
54 }
52 55
53 // m_series = new QBarSeries(m_categories);
54 m_series = new QBarSeries();
55 m_series->setCategories(m_categories);
56 void DeclarativeBarSeries::classBegin()
57 {
58 }
56 59
57 // TODO: use data from model
58 QBarSet *set0 = new QBarSet("Bub");
59 QBarSet *set1 = new QBarSet("Bob");
60 QBarSet *set2 = new QBarSet("Guybrush");
60 void DeclarativeBarSeries::componentComplete()
61 {
62 if (model())
63 setModelMapping(0, 1, 1, Qt::Vertical);
64 }
61 65
62 *set0 << 1 << 2 << 3 << 4 << 5 << 6;
63 *set1 << 5 << 1 << 2 << 4 << 1 << 7;
64 *set2 << 3 << 5 << 8 << 13 << 8 << 5;
66 bool DeclarativeBarSeries::setDeclarativeModel(DeclarativeBarModel *model)
67 {
68 QAbstractItemModel *m = qobject_cast<QAbstractItemModel *>(model);
69 bool value(false);
70 if (m) {
71 value = setModel(m);
72 //setModelMapping(int categories, int bottomBoundary, int topBoundary, Qt::Orientation orientation = Qt::Vertical);
73 setModelMapping(0, 1, 1, Qt::Vertical);
74 } else {
75 qWarning("DeclarativeBarSeries: Illegal model");
76 }
77 return value;
78 }
65 79
66 m_series->appendBarSet(set0);
67 m_series->appendBarSet(set1);
68 m_series->appendBarSet(set2);
80 DeclarativeBarModel *DeclarativeBarSeries::declarativeModel()
81 {
82 return qobject_cast<DeclarativeBarModel *>(model());
83 }
69 84
70 chart->addSeries(m_series);
71 }
85 void DeclarativeBarSeries::setBarCategories(QStringList categories)
86 {
87 setCategories(categories);
72 88 }
73 89
74 90 QStringList DeclarativeBarSeries::barCategories()
75 91 {
76 return m_categories;
92 return categories();
77 93 }
78 94
79 95 #include "moc_declarativebarseries.cpp"
80 96
81 97 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,59 +1,76
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #ifndef DECLARATIVEBARSERIES_H
22 22 #define DECLARATIVEBARSERIES_H
23 23
24 24 #include "qchartglobal.h"
25 #include "declarativemodel.h"
25 26 #include <QDeclarativeItem>
27 #include <QDeclarativeParserStatus>
28 #include <QBarSeries>
26 29
27 30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 31
29 32 class QChart;
30 33 class QBarSeries;
31 34
32 class DeclarativeBarSeries : public QDeclarativeItem
35 class DeclarativeBarSet : public QBarSet
33 36 {
34 37 Q_OBJECT
38 Q_PROPERTY(QVariantList values READ values WRITE setValues)
39 Q_PROPERTY(QString name READ name WRITE setName)
40
41 public:
42 explicit DeclarativeBarSet(QObject *parent = 0);
43 QVariantList values();
44 void setValues(QVariantList values);
45 };
46
47 class DeclarativeBarSeries : public QBarSeries, public QDeclarativeParserStatus
48 {
49 Q_OBJECT
50 Q_INTERFACES(QDeclarativeParserStatus)
51 Q_PROPERTY(DeclarativeBarModel *model READ declarativeModel WRITE setDeclarativeModel)
35 52 Q_PROPERTY(QStringList barCategories READ barCategories WRITE setBarCategories)
36 53
37 54 public:
38 55 explicit DeclarativeBarSeries(QDeclarativeItem *parent = 0);
39 56
40 57 public: // from QDeclarativeParserStatus
58 void classBegin();
41 59 void componentComplete();
42 60
43 61 public:
62 bool setDeclarativeModel(DeclarativeBarModel *model);
63 DeclarativeBarModel *declarativeModel();
44 64 void setBarCategories(QStringList categories);
45 65 QStringList barCategories();
46 66
47 67 Q_SIGNALS:
48 68
49 69 public Q_SLOTS:
50 70
51 71 public:
52 QChart *m_chart;
53 QBarSeries *m_series;
54 QStringList m_categories;
55 72 };
56 73
57 74 QTCOMMERCIALCHART_END_NAMESPACE
58 75
59 76 #endif // DECLARATIVEBARSERIES_H
@@ -1,138 +1,177
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "declarativemodel.h"
22 22 #include <qdeclarativelist.h>
23 23 #include <QDebug>
24 24
25 25 QTCOMMERCIALCHART_BEGIN_NAMESPACE
26 26
27 ////////////// Table model (base) ///////////////////
28
29 DeclarativeTableModel::DeclarativeTableModel(QObject *parent) :
30 ChartTableModel(parent)
31 {
32 }
33
34 void DeclarativeTableModel::classBegin()
35 {
36 }
37
38 void DeclarativeTableModel::componentComplete()
39 {
40 foreach (QObject *child, children())
41 appendToModel(child);
42 }
43
44 QDeclarativeListProperty<QObject> DeclarativeTableModel::modelChildren()
45 {
46 return QDeclarativeListProperty<QObject>(this, 0, &DeclarativeTableModel::appendModelChild);
47 }
48
49 void DeclarativeTableModel::appendModelChild(QDeclarativeListProperty<QObject> *list,
50 QObject *child)
51 {
52 // childs are added in componentComplete instead
53 Q_UNUSED(list)
54 Q_UNUSED(child)
55 }
56
57 void DeclarativeTableModel::appendToModel(QObject *object)
58 {
59 if (qobject_cast<DeclarativeBarModel *>(this)) {
60 DeclarativeBarModel *model = qobject_cast<DeclarativeBarModel *>(this);
61 model->append(qobject_cast<QBarSet *>(object));
62 } else if (qobject_cast<DeclarativePieModel *>(this)) {
63 DeclarativePieModel *model = qobject_cast<DeclarativePieModel *>(this);
64 model->append(qobject_cast<QPieSlice *>(object));
65 } else if (qobject_cast<DeclarativeXyModel *>(this)) {
66 DeclarativeXyModel *model = qobject_cast<DeclarativeXyModel *>(this);
67 model->append(qobject_cast<DeclarativeXyPoint *>(object));
68 }
69 }
70
27 71 ////////////// XY model ///////////////////////
28 72
29 73 DeclarativeXyModel::DeclarativeXyModel(QObject *parent) :
30 ChartTableModel(parent)
74 DeclarativeTableModel(parent)
31 75 {
32 76 }
33 77
34 78 void DeclarativeXyModel::append(DeclarativeXyPoint* point)
35 79 {
36 80 // qDebug() << "DeclarativeXyModel::append:" << point->x() << " " << point->y();
37 81 insertRow(rowCount());
38 82 QModelIndex xModelIndex = createIndex(rowCount() - 1, 0);
39 83 QModelIndex yModelIndex = createIndex(rowCount() - 1, 1);
40 84 setData(xModelIndex, point->x());
41 85 setData(yModelIndex, point->y());
42 86 dataChanged(xModelIndex, yModelIndex);
43 87 }
44 88
45 89 void DeclarativeXyModel::append(QVariantList points)
46 90 {
47 91 qreal x = 0.0;
48 92 for (int i(0); i < points.count(); i++) {
49 93 if (i % 2) {
50 94 bool ok(false);
51 95 qreal y = points.at(i).toReal(&ok);
52 96 if (ok) {
53 97 DeclarativeXyPoint *point= new DeclarativeXyPoint();
54 98 point->setX(x);
55 99 point->setY(y);
56 100 append(point);
57 101 } else {
58 102 qWarning() << "Illegal y value";
59 103 }
60 104 } else {
61 105 bool ok(false);
62 106 x = points.at(i).toReal(&ok);
63 107 if (!ok) {
64 108 qWarning() << "Illegal x value";
65 109 }
66 110 }
67 111 }
68 112 }
69 113
70 QDeclarativeListProperty<DeclarativeXyPoint> DeclarativeXyModel::points()
71 {
72 return QDeclarativeListProperty<DeclarativeXyPoint>(this, 0, &DeclarativeXyModel::appendPoint);
73 }
74
75 void DeclarativeXyModel::appendPoint(QDeclarativeListProperty<DeclarativeXyPoint> *list,
76 DeclarativeXyPoint *point)
77 {
78 DeclarativeXyModel *model = qobject_cast<DeclarativeXyModel *>(list->object);
79 if (model)
80 model->append(point);
81 else
82 qWarning() << "Illegal point item";
83 }
84
85 114 ////////////// Pie model ///////////////////////
86 115
87 116 DeclarativePieModel::DeclarativePieModel(QObject *parent) :
88 ChartTableModel(parent)
117 DeclarativeTableModel(parent)
89 118 {
90 119 }
91 120
92 121 void DeclarativePieModel::append(QPieSlice* slice)
93 122 {
94 123 // qDebug() << "DeclarativePieModel::append:" << slice->label() << " " << slice->value();
95 124 insertRow(rowCount());
96 125
97 126 setData(createIndex(rowCount() - 1, 0), slice->value());
98 127 setData(createIndex(rowCount() - 1, 1), slice->label());
99 128 }
100 129
101 130 void DeclarativePieModel::append(QVariantList slices)
102 131 {
103 132 // qDebug() << "append:" << slices;
104 133 QString label = "";
105 134 for (int i(0); i < slices.count(); i++) {
106 135 if (i % 2) {
107 136 bool ok(false);
108 137 qreal value = slices.at(i).toReal(&ok);
109 138 if (ok) {
110 139 QPieSlice *slice = new QPieSlice(value, label);
111 140 append(slice);
141 // TODO: how to copy the properties to the newly added slice?
142 // (DeclarativePieModel::append only copies the label and value to the model)
143 // QPieSlice *addedSlice = append(slice);
144 // addedSlice->setExploded(slice->isExploded());
112 145 } else {
113 146 qWarning() << "Illegal slice item";
114 147 }
115 148 } else {
116 149 label = slices.at(i).toString();
117 150 }
118 151 }
119 152 }
120 153
121 QDeclarativeListProperty<QPieSlice> DeclarativePieModel::slices()
154 ////////////// Bar model ///////////////////////
155
156 DeclarativeBarModel::DeclarativeBarModel(QObject *parent) :
157 DeclarativeTableModel(parent)
122 158 {
123 return QDeclarativeListProperty<QPieSlice>(this, 0, &DeclarativePieModel::appendSlice);
124 159 }
125 160
126 void DeclarativePieModel::appendSlice(QDeclarativeListProperty<QPieSlice> *list,
127 QPieSlice *slice)
161 void DeclarativeBarModel::append(QBarSet* barSet)
128 162 {
129 DeclarativePieModel *pieModel = qobject_cast<DeclarativePieModel *>(list->object);
130 if (pieModel)
131 pieModel->append(slice);
132 else
133 qWarning() << "Illegal slice item";
163 insertColumn(columnCount());
164 for (int i(0); i < barSet->count(); i++) {
165 if (rowCount() < (i + 1))
166 insertRow(rowCount());
167 setData(createIndex(i, columnCount() - 1), barSet->at(i));
168 // insertRow(rowCount());
169 // setData(createIndex(rowCount() - 1, 0), );
170 // setData(createIndex(rowCount() - 1, 1), barSet->at(i));
171 }
172 // TODO: setModelMapping(0, 1, columnCount(), Qt::Vertical);
134 173 }
135 174
136 175 #include "moc_declarativemodel.cpp"
137 176
138 177 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,70 +1,94
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #ifndef DECLARATIVEMODEL_H
22 22 #define DECLARATIVEMODEL_H
23 23
24 24 #include "qchartglobal.h"
25 25 #include "declarativexypoint.h"
26 #include "qpieslice.h"
26 #include <QPieSlice>
27 27 #include "../src/charttablemodel.h" // TODO
28 #include <QBarSet>
28 29 #include <QDeclarativeListProperty>
29 30 #include <QVariant>
31 #include <QDeclarativeParserStatus>
30 32
31 33 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32 34
33 class DeclarativeXyModel : public ChartTableModel
35 class DeclarativeTableModel : public ChartTableModel, public QDeclarativeParserStatus
36 {
37 Q_OBJECT
38 Q_INTERFACES(QDeclarativeParserStatus)
39 Q_PROPERTY(QDeclarativeListProperty<QObject> modelChildren READ modelChildren)
40 Q_CLASSINFO("DefaultProperty", "modelChildren")
41
42 public:
43 explicit DeclarativeTableModel(QObject *parent = 0);
44 QDeclarativeListProperty<QObject> modelChildren();
45
46 public: // from QDeclarativeParserStatus
47 void classBegin();
48 void componentComplete();
49
50 public Q_SLOTS:
51 static void appendModelChild(QDeclarativeListProperty<QObject> *list,
52 QObject *element);
53 private:
54 void appendToModel(QObject *object);
55 };
56
57 class DeclarativeXyModel : public DeclarativeTableModel
34 58 {
35 59 Q_OBJECT
36 Q_PROPERTY(QDeclarativeListProperty<DeclarativeXyPoint> points READ points)
37 Q_CLASSINFO("DefaultProperty", "points")
38 60
39 61 public:
40 62 explicit DeclarativeXyModel(QObject *parent = 0);
41 QDeclarativeListProperty<DeclarativeXyPoint> points();
42 63
43 64 public Q_SLOTS:
44 65 void append(DeclarativeXyPoint* point);
45 66 void append(QVariantList points);
46 static void appendPoint(QDeclarativeListProperty<DeclarativeXyPoint> *list,
47 DeclarativeXyPoint *element);
48 67 };
49 68
50
51 class DeclarativePieModel : public ChartTableModel
69 class DeclarativePieModel : public DeclarativeTableModel
52 70 {
53 71 Q_OBJECT
54 Q_PROPERTY(QDeclarativeListProperty<QPieSlice> slices READ slices)
55 Q_CLASSINFO("DefaultProperty", "slices")
56 72
57 73 public:
58 74 explicit DeclarativePieModel(QObject *parent = 0);
59 QDeclarativeListProperty<QPieSlice> slices();
60 75
61 76 public Q_SLOTS:
62 77 void append(QPieSlice* slice);
63 78 void append(QVariantList slices);
64 static void appendSlice(QDeclarativeListProperty<QPieSlice> *list,
65 QPieSlice *element);
79 };
80
81 class DeclarativeBarModel : public DeclarativeTableModel
82 {
83 Q_OBJECT
84
85 public:
86 explicit DeclarativeBarModel(QObject *parent = 0);
87
88 public Q_SLOTS:
89 void append(QBarSet* barSet);
66 90 };
67 91
68 92 QTCOMMERCIALCHART_END_NAMESPACE
69 93
70 94 #endif // DECLARATIVEMODEL_H
@@ -1,53 +1,62
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "declarativepieseries.h"
22 22 #include "declarativechart.h"
23 23 #include "qchart.h"
24 24 #include <qdeclarativelist.h>
25 25
26 26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 27
28 28 DeclarativePieSeries::DeclarativePieSeries(QObject *parent) :
29 29 QPieSeries(parent)
30 30 {
31 31 }
32 32
33 QPieSlice *DeclarativePieSeries::slice(int index)
34 {
35 QList<QPieSlice*> sliceList = slices();
36 if (index < sliceList.count())
37 return sliceList[index];
38
39 return 0;
40 }
41
33 42 bool DeclarativePieSeries::setPieModel(DeclarativePieModel *model)
34 43 {
35 44 QAbstractItemModel *m = qobject_cast<QAbstractItemModel *>(model);
36 45 bool value(false);
37 46 if (m) {
38 47 value = QPieSeries::setModel(m);
39 48 setModelMapping(0, 1, Qt::Vertical);
40 49 } else {
41 50 qWarning("DeclarativePieSeries: Illegal model");
42 51 }
43 52 return value;
44 53 }
45 54
46 55 DeclarativePieModel *DeclarativePieSeries::pieModel()
47 56 {
48 57 return qobject_cast<DeclarativePieModel *>(model());
49 58 }
50 59
51 60 #include "moc_declarativepieseries.cpp"
52 61
53 62 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,53 +1,56
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #ifndef DECLARATIVEPIESERIES_H
22 22 #define DECLARATIVEPIESERIES_H
23 23
24 24 #include "qchartglobal.h"
25 25 #include "qpieslice.h"
26 26 #include "qpieseries.h"
27 27 #include <QDeclarativeListProperty>
28 28 #include <QAbstractItemModel>
29 29 #include <QVariant>
30 30 #include "declarativemodel.h"
31 31
32 32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33 33
34 34 class QChart;
35 35
36 36 class DeclarativePieSeries : public QPieSeries
37 37 {
38 38 Q_OBJECT
39 39 Q_PROPERTY(DeclarativePieModel *model READ pieModel WRITE setPieModel)
40 40
41 41 public:
42 42 explicit DeclarativePieSeries(QObject *parent = 0);
43 43
44 public:
45 Q_INVOKABLE QPieSlice *slice(int index);
46
44 47 public Q_SLOTS:
45 48
46 49 public:
47 50 bool setPieModel(DeclarativePieModel *model);
48 51 DeclarativePieModel *pieModel();
49 52 };
50 53
51 54 QTCOMMERCIALCHART_END_NAMESPACE
52 55
53 56 #endif // DECLARATIVEPIESERIES_H
@@ -1,59 +1,59
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 //#include "DeclarativeXySeries.h"
22 22 #include "declarativexyseries.h"
23 23 #include "qxyseries.h"
24 24 #include "declarativechart.h"
25 25
26 26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 27
28 28 DeclarativeXySeries::DeclarativeXySeries()
29 29 {
30 30 }
31 31
32 32 DeclarativeXySeries::~DeclarativeXySeries()
33 33 {
34 34 }
35 35
36 36 bool DeclarativeXySeries::setDeclarativeModel(DeclarativeXyModel *model)
37 37 {
38 38 QAbstractItemModel *m = qobject_cast<QAbstractItemModel *>(model);
39 39 bool value(false);
40 40 if (m) {
41 41 // All the inherited objects must be of type QXYSeries, so it is safe to cast
42 42 QXYSeries *series = reinterpret_cast<QXYSeries *>(this);
43 series->setModel(m);
43 value = series->setModel(m);
44 44 series->setModelMapping(0, 1, Qt::Vertical);
45 45 } else {
46 46 qWarning("DeclarativeXySeries: Illegal model");
47 47 }
48 48 return value;
49 49 }
50 50
51 51 DeclarativeXyModel *DeclarativeXySeries::declarativeModel()
52 52 {
53 53 // All the inherited objects must be of type QXYSeries, so it is safe to cast
54 54 QXYSeries *series = reinterpret_cast<QXYSeries *>(this);
55 55 Q_ASSERT(series);
56 56 return qobject_cast<DeclarativeXyModel *>(series->model());
57 57 }
58 58
59 59 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,69 +1,69
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include <QtDeclarative/qdeclarativeextensionplugin.h>
22 22 #include <QtDeclarative/qdeclarative.h>
23 23 #include "qchart.h"
24 24 #include "qaxiscategories.h"
25 25 #include "declarativechart.h"
26 26 #include "declarativexypoint.h"
27 27 #include "declarativelineseries.h"
28 28 #include "declarativesplineseries.h"
29 29 #include "declarativeareaseries.h"
30 30 #include "declarativescatterseries.h"
31 31 #include "declarativebarseries.h"
32 32 #include "declarativepieseries.h"
33 //#include "declarativepiemodel.h"
34 33
35 34 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36 35
37 36 class ChartQmlPlugin : public QDeclarativeExtensionPlugin
38 37 {
39 38 Q_OBJECT
40 39 public:
41 40 virtual void registerTypes(const char *uri)
42 41 {
43 42 Q_ASSERT(QLatin1String(uri) == QLatin1String("QtCommercial.Chart"));
44 43
45 44 qmlRegisterType<DeclarativeChart>(uri, 1, 0, "ChartView");
46 45 qmlRegisterUncreatableType<QAxis>(uri, 1, 0, "Axis",
47 46 QLatin1String("Trying to create uncreatable type: Axis."));
48 47 //qmlRegisterType<DeclarativeAxisCategory>(uri, 1, 0, "AxisCategory");
49 48 qmlRegisterType<DeclarativeXyPoint>(uri, 1, 0, "XyPoint");
50 49 qmlRegisterType<DeclarativeScatterSeries>(uri, 1, 0, "ScatterSeries");
51 50 qmlRegisterType<DeclarativeLineSeries>(uri, 1, 0, "LineSeries");
52 51 qmlRegisterType<DeclarativeSplineSeries>(uri, 1, 0, "SplineSeries");
53 52 qmlRegisterType<DeclarativeAreaSeries>(uri, 1, 0, "AreaSeries");
54 53 qmlRegisterType<DeclarativeBarSeries>(uri, 1, 0, "BarSeries");
55 54 qmlRegisterType<DeclarativePieSeries>(uri, 1, 0, "PieSeries");
56 55 qmlRegisterType<QPieSlice>(uri, 1, 0, "PieSlice");
57 // TODO: a declarative model for each type
58 56 qmlRegisterType<DeclarativePieModel>(uri, 1, 0, "PieModel");
59 57 qmlRegisterType<DeclarativeXyModel>(uri, 1, 0, "XYModel");
58 qmlRegisterType<DeclarativeBarModel>(uri, 1, 0, "BarModel");
59 qmlRegisterType<DeclarativeBarSet>(uri, 1, 0, "BarSet");
60 60 }
61 61 };
62 62
63 63 #include "plugin.moc"
64 64
65 65 QTCOMMERCIALCHART_END_NAMESPACE
66 66
67 67 QTCOMMERCIALCHART_USE_NAMESPACE
68 68
69 69 Q_EXPORT_PLUGIN2(qtcommercialchartqml, QT_PREPEND_NAMESPACE(ChartQmlPlugin))
@@ -1,303 +1,306
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "qbarset.h"
22 22 #include "qbarset_p.h"
23 23
24 24 QTCOMMERCIALCHART_BEGIN_NAMESPACE
25 25
26 26 /*!
27 27 \class QBarSet
28 28 \brief part of QtCommercial chart API.
29 29
30 30 QBarSet represents one set of bars. Set of bars contains one data value for each category.
31 31 First value of set is assumed to belong to first category, second to second category and so on.
32 32 If set has fewer values than there are categories, then the missing values are assumed to be
33 33 at the end of set. For missing values in middle of a set, numerical value of zero is used.
34 34
35 35 \mainclass
36 36
37 37 \sa QBarSeries, QStackedBarSeries, QPercentBarSeries
38 38 */
39 39
40 40 /*!
41 41 \fn void QBarSet::clicked(QString category)
42 42 \brief signals that set has been clicked
43 43 Parameter \a category describes on which category was clicked
44 44 */
45 45
46 46 /*!
47 47 \fn void QBarSet::hovered(bool status)
48 48 \brief signals that mouse has hovered over the set. If \a status is true, then mouse was entered. If \a status is false, then mouse was left.
49 49
50 50 The signal is emitted if mouse is hovered on top of set
51 51 Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series.
52 52 */
53 53
54 54 /*!
55 55 Constructs QBarSet with a name of \a name and with parent of \a parent
56 56 */
57 57 QBarSet::QBarSet(const QString name, QObject *parent)
58 58 : QObject(parent)
59 59 ,d_ptr(new QBarSetPrivate(name,this))
60 60 {
61 61 }
62 62
63 63 /*!
64 64 Destroys the barset
65 65 */
66 66 QBarSet::~QBarSet()
67 67 {
68 68 // NOTE: d_ptr destroyed by QObject
69 69 }
70 70
71 71 /*!
72 72 Sets new \a name for set.
73 73 */
74 74 void QBarSet::setName(const QString name)
75 75 {
76 76 d_ptr->m_name = name;
77 77 }
78 78
79 79 /*!
80 80 Returns name of the set.
81 81 */
82 82 QString QBarSet::name() const
83 83 {
84 84 return d_ptr->m_name;
85 85 }
86 86
87 87 /*!
88 88 Appends new value \a value to the end of set.
89 89 */
90 90 void QBarSet::append(const qreal value)
91 91 {
92 92 d_ptr->m_values.append(value);
93 93 emit d_ptr->restructuredBars();
94 94 }
95 95
96 96 /*!
97 97 Appends new value \a value to the end of set.
98 98 */
99 99 QBarSet& QBarSet::operator << (const qreal &value)
100 100 {
101 101 append(value);
102 102 return *this;
103 103 }
104 104
105 105 /*!
106 106 Inserts new \a value on the \a index position.
107 107 The value that is currently at this postion is moved to postion index + 1
108 108 \sa remove()
109 109 */
110 110 void QBarSet::insert(const int index, const qreal value)
111 111 {
112 112 d_ptr->m_values.insert(index, value);
113 113 // emit d_ptr->updatedBars();
114 114 }
115 115
116 116 /*!
117 117 Removes the value specified by \a index
118 118 \sa insert()
119 119 */
120 120 void QBarSet::remove(const int index)
121 121 {
122 122 d_ptr->m_values.removeAt(index);
123 123 // emit d_ptr->updatedBars();
124 124 }
125 125
126 126 /*!
127 127 Sets a new value \a value to set, indexed by \a index
128 128 */
129 129 void QBarSet::replace(const int index, const qreal value)
130 130 {
131 131 d_ptr->m_values.replace(index,value);
132 132 emit d_ptr->updatedBars();
133 133 }
134 134
135 135 /*!
136 136 Returns value of set indexed by \a index
137 137 */
138 138 qreal QBarSet::at(const int index) const
139 139 {
140 if (index < 0 || index >= d_ptr->m_values.count())
141 return 0.0;
142
140 143 return d_ptr->m_values.at(index);
141 144 }
142 145
143 146 /*!
144 147 Returns value of set indexed by \a index
145 148 */
146 149 qreal QBarSet::operator [] (int index) const
147 150 {
148 151 return d_ptr->m_values.at(index);
149 152 }
150 153
151 154 /*!
152 155 Returns count of values in set.
153 156 */
154 157 int QBarSet::count() const
155 158 {
156 159 return d_ptr->m_values.count();
157 160 }
158 161
159 162 /*!
160 163 Returns sum of all values in barset.
161 164 */
162 165 qreal QBarSet::sum() const
163 166 {
164 167 qreal total(0);
165 168 for (int i=0; i < d_ptr->m_values.count(); i++) {
166 169 total += d_ptr->m_values.at(i);
167 170 }
168 171 return total;
169 172 }
170 173
171 174 /*!
172 175 Sets pen for set. Bars of this set are drawn using \a pen
173 176 */
174 177 void QBarSet::setPen(const QPen &pen)
175 178 {
176 179 if(d_ptr->m_pen!=pen){
177 180 d_ptr->m_pen = pen;
178 181 emit d_ptr->updatedBars();
179 182 }
180 183 }
181 184
182 185 /*!
183 186 Returns pen of the set.
184 187 */
185 188 QPen QBarSet::pen() const
186 189 {
187 190 return d_ptr->m_pen;
188 191 }
189 192
190 193 /*!
191 194 Sets brush for the set. Bars of this set are drawn using \a brush
192 195 */
193 196 void QBarSet::setBrush(const QBrush &brush)
194 197 {
195 198 if(d_ptr->m_brush!=brush){
196 199 d_ptr->m_brush = brush;
197 200 emit d_ptr->updatedBars();
198 201 }
199 202 }
200 203
201 204 /*!
202 205 Returns brush of the set.
203 206 */
204 207 QBrush QBarSet::brush() const
205 208 {
206 209 return d_ptr->m_brush;
207 210 }
208 211
209 212 /*!
210 213 Sets \a pen of the values that are drawn on top of this barset
211 214 */
212 215 void QBarSet::setLabelPen(const QPen &pen)
213 216 {
214 217 if(d_ptr->m_labelPen!=pen){
215 218 d_ptr->m_labelPen = pen;
216 219 emit d_ptr->updatedBars();
217 220 }
218 221 }
219 222
220 223 /*!
221 224 Returns pen of the values that are drawn on top of this barset
222 225 */
223 226 QPen QBarSet::labelPen() const
224 227 {
225 228 return d_ptr->m_labelPen;
226 229 }
227 230
228 231 /*!
229 232 Sets \a brush of the values that are drawn on top of this barset
230 233 */
231 234 void QBarSet::setLabelBrush(const QBrush &brush)
232 235 {
233 236 if(d_ptr->m_labelBrush!=brush){
234 237 d_ptr->m_labelBrush = brush;
235 238 emit d_ptr->updatedBars();
236 239 }
237 240 }
238 241
239 242 /*!
240 243 Returns brush of the values that are drawn on top of this barset
241 244 */
242 245 QBrush QBarSet::labelBrush() const
243 246 {
244 247 return d_ptr->m_labelBrush;
245 248 }
246 249
247 250 /*!
248 251 Sets the \a font for values that are drawn on top of this barset
249 252 */
250 253 void QBarSet::setLabelFont(const QFont &font)
251 254 {
252 255 if(d_ptr->m_labelFont!=font) {
253 256 d_ptr->m_labelFont = font;
254 257 emit d_ptr->updatedBars();
255 258 }
256 259
257 260 }
258 261
259 262 /*!
260 263 Returns the pen for values that are drawn on top of this set
261 264 */
262 265 QFont QBarSet::labelFont() const
263 266 {
264 267 return d_ptr->m_labelFont;
265 268 }
266 269
267 270 /*!
268 271 Sets visibility of bar labels. If \a visible is true, labels are drawn on top of barsets.
269 272 */
270 273
271 274 void QBarSet::setLabelsVisible(bool visible)
272 275 {
273 276 if(d_ptr->m_labelsVisible!=visible) {
274 277 d_ptr->m_labelsVisible = visible;
275 278 emit d_ptr->labelsVisibleChanged(visible);
276 279 }
277 280 }
278 281
279 282 /*!
280 283 Returns the visibility of values
281 284 */
282 285 bool QBarSet::labelsVisible() const
283 286 {
284 287 return d_ptr->m_labelsVisible;
285 288 }
286 289
287 290 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
288 291
289 292 QBarSetPrivate::QBarSetPrivate(const QString name, QBarSet *parent) : QObject(parent),
290 293 q_ptr(parent),
291 294 m_name(name),
292 295 m_labelsVisible(false)
293 296 {
294 297 }
295 298
296 299 QBarSetPrivate::~QBarSetPrivate()
297 300 {
298 301 }
299 302
300 303 #include "moc_qbarset.cpp"
301 304 #include "moc_qbarset_p.cpp"
302 305
303 306 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,96 +1,97
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #ifndef QBARSET_H
22 22 #define QBARSET_H
23 23
24 24 #include <qchartglobal.h>
25 25 #include <QPen>
26 26 #include <QBrush>
27 27 #include <QFont>
28 28
29 29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 30 class QBarSetPrivate;
31 31
32 32 class QTCOMMERCIALCHART_EXPORT QBarSet : public QObject
33 33 {
34 34 Q_OBJECT
35 Q_PROPERTY(QString name READ name WRITE setName)
35 36
36 37 public:
37 38 explicit QBarSet(const QString name, QObject *parent = 0);
38 39 virtual ~QBarSet();
39 40
40 41 void setName(const QString name);
41 42 QString name() const;
42 43
43 44 // TODO:
44 45 // void append(const QPointF value); // Appends bar with x-value
45 46 // void append(const QList<QPointF> value); // Same with list
46 47 void append(const qreal value); // TODO: change so, that count becomes x-value
47 48
48 49 // TODO:
49 50 // void append(const QList<qreal> values); // Append list of values. Using index as x-value
50 51
51 52 QBarSet& operator << (const qreal &value); // TODO: change implementations so, that count becomes x-value
52 53 // TODO:
53 54 // QBarSet& operator << (const QPointF &value); // Appends bar with x-value
54 55
55 56 void insert(const int index, const qreal value); // TODO: internal reindexing (what happens, if there are points with real x values?)
56 57 void remove(const int index); // TODO: internal reindexing (what happens, if there are points with real x values?)
57 58 void replace(const int index, const qreal value);
58 59 qreal at(const int index) const;
59 60 qreal operator [] (int index) const;
60 61 int count() const;
61 62 qreal sum() const;
62 63
63 64 void setPen(const QPen &pen);
64 65 QPen pen() const;
65 66
66 67 void setBrush(const QBrush &brush);
67 68 QBrush brush() const;
68 69
69 70 void setLabelPen(const QPen &pen);
70 71 QPen labelPen() const;
71 72
72 73 void setLabelBrush(const QBrush &brush);
73 74 QBrush labelBrush() const;
74 75
75 76 void setLabelFont(const QFont &font);
76 77 QFont labelFont() const;
77 78
78 79 void setLabelsVisible(bool visible = true);
79 80 bool labelsVisible() const;
80 81
81 82 Q_SIGNALS:
82 83 void clicked(QString category);
83 84 void hovered(bool status);
84 85
85 86 private:
86 87 QScopedPointer<QBarSetPrivate> d_ptr;
87 88 Q_DISABLE_COPY(QBarSet)
88 89 friend class QBarSeries;
89 90 friend class BarLegendMarker;
90 91 friend class BarChartItem;
91 92 friend class QBarSeriesPrivate;
92 93 };
93 94
94 95 QTCOMMERCIALCHART_END_NAMESPACE
95 96
96 97 #endif // QBARSET_H
@@ -1,141 +1,140
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "charttablemodel.h"
22 22 #include <QVector>
23 23 #include <QTime>
24 24 #include <QRect>
25 25 #include <QColor>
26 26
27 27 QTCOMMERCIALCHART_USE_NAMESPACE
28 28
29 29 ChartTableModel::ChartTableModel(QObject *parent) :
30 30 QAbstractTableModel(parent)
31 31 {
32 32 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
33 33
34 34 m_columnCount = 2;
35 35 m_rowCount = 0;
36 36
37 37 // m_data
38 38 for (int i = 0; i < m_rowCount; i++) {
39 39 QVector<QVariant>* dataVec = new QVector<QVariant>(m_columnCount);
40 40 for (int k = 0; k < dataVec->size(); k++) {
41 41 if (k%2 == 0)
42 42 dataVec->replace(k, i * 50 + qrand()%20);
43 43 else
44 44 dataVec->replace(k, qrand()%100);
45 45 }
46 46 m_data.append(dataVec);
47 47 }
48 48 }
49 49
50 50 int ChartTableModel::rowCount(const QModelIndex & parent) const
51 51 {
52 52 Q_UNUSED(parent)
53 53 return m_data.count();
54 54 }
55 55
56 56 int ChartTableModel::columnCount(const QModelIndex & parent) const
57 57 {
58 58 Q_UNUSED(parent)
59 59 return m_columnCount;
60 60 }
61 61
62 62 QVariant ChartTableModel::headerData (int section, Qt::Orientation orientation, int role ) const
63 63 {
64 64 if (role != Qt::DisplayRole)
65 65 return QVariant();
66 66
67 if (orientation == Qt::Horizontal)
68 {
69 if (section%2 == 0)
67 if (orientation == Qt::Horizontal) {
68 if (section % 2 == 0)
70 69 return "x";
71 70 else
72 71 return "y";
73 }
74 else
72 } else {
75 73 return QString("%1").arg(section + 1);
74 }
76 75 }
77 76
78 77 QVariant ChartTableModel::data(const QModelIndex &index, int role) const
79 78 {
80 79 if (role == Qt::DisplayRole) {
81 80 return m_data[index.row()]->at(index.column());
82 81 } else if (role == Qt::EditRole) {
83 82 return m_data[index.row()]->at(index.column());
84 83 }
85 84 return QVariant();
86 85 }
87 86
88 87 bool ChartTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
89 88 {
90 89 if (index.isValid() && role == Qt::EditRole) {
91 90 m_data[index.row()]->replace(index.column(), value);
92 91 emit dataChanged(index, index);
93 92 return true;
94 93 }
95 94 return false;
96 95 }
97 96
98 97 void ChartTableModel::insertRow(int row, const QModelIndex &parent)
99 98 {
100 99 Q_UNUSED(parent)
101 100
102 101 beginInsertRows(QModelIndex(), row, row);
103 102 QVector<QVariant>* dataVec = new QVector<QVariant>(m_columnCount);
104 103 m_data.insert(row, dataVec);
105 104 endInsertRows();
106 105 }
107 106
108 107 //bool ChartTableModel::removeRow(int row, const QModelIndex &parent)
109 108 //{
110 109 // Q_UNUSED(parent)
111 110 // Q_ASSERT(row >= 0 && row < rowCount);
112 111
113 112 // beginRemoveRows(parent, row, row);
114 113 // m_data.removeAt(row);
115 114 // endRemoveRows();
116 115 // return true;
117 116 //}
118 117
119 118 bool ChartTableModel::removeRow(int row, const QModelIndex &parent)
120 119 {
121 120 return QAbstractTableModel::removeRow(row, parent);
122 121 }
123 122
124 123 bool ChartTableModel::removeRows(int row, int count, const QModelIndex &parent)
125 124 {
126 125 beginRemoveRows(parent, row, row + count - 1);
127 126 bool removed(false);
128 127 for (int i(row); i < (row + count); i++) {
129 128 m_data.removeAt(i);
130 129 removed = true;
131 130 }
132 131 endRemoveRows();
133 132 return removed;
134 133 }
135 134
136 135 Qt::ItemFlags ChartTableModel::flags ( const QModelIndex & index ) const
137 136 {
138 137 return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
139 138 }
140 139
141 140 #include "moc_charttablemodel.cpp"
@@ -1,84 +1,85
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #ifndef QPIESLICE_H
22 22 #define QPIESLICE_H
23 23
24 24 #include <qchartglobal.h>
25 25 #include <QObject>
26 26 #include <QPen>
27 27 #include <QBrush>
28 28 #include <QFont>
29 29
30 30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31 31 class PieSliceData;
32 32
33 33 class QTCOMMERCIALCHART_EXPORT QPieSlice : public QObject
34 34 {
35 35 Q_OBJECT
36 Q_PROPERTY(QString label READ label WRITE setLabel NOTIFY changed)
37 Q_PROPERTY(qreal value READ value WRITE setValue NOTIFY changed)
36 Q_PROPERTY(QString label READ label WRITE setLabel)
37 Q_PROPERTY(qreal value READ value WRITE setValue)
38 Q_PROPERTY(bool exploded READ isExploded WRITE setExploded)
38 39
39 40 public:
40 41 explicit QPieSlice(QObject *parent = 0);
41 42 QPieSlice(qreal value, QString label, QObject *parent = 0);
42 43 virtual ~QPieSlice();
43 44
44 45 void setValue(qreal value);
45 46 qreal value() const;
46 47 void setLabel(QString label);
47 48 QString label() const;
48 49 void setLabelVisible(bool visible = true);
49 50 bool isLabelVisible() const;
50 51 void setExploded(bool exploded = true);
51 52 bool isExploded() const;
52 53
53 54 void setPen(const QPen &pen);
54 55 QPen pen() const;
55 56 void setBrush(const QBrush &brush);
56 57 QBrush brush() const;
57 58 void setLabelPen(const QPen &pen);
58 59 QPen labelPen() const;
59 60 void setLabelFont(const QFont &font);
60 61 QFont labelFont() const;
61 62
62 63 void setLabelArmLengthFactor(qreal factor);
63 64 qreal labelArmLengthFactor() const;
64 65 void setExplodeDistanceFactor(qreal factor);
65 66 qreal explodeDistanceFactor() const;
66 67
67 68 qreal percentage() const;
68 69 qreal startAngle() const;
69 70 qreal endAngle() const;
70 71
71 72 Q_SIGNALS:
72 73 void clicked();
73 74 void hovered(bool state);
74 75 void changed();
75 76
76 77 private:
77 78 friend class PieSliceData;
78 79 PieSliceData * const d;
79 80 Q_DISABLE_COPY(QPieSlice)
80 81 };
81 82
82 83 QTCOMMERCIALCHART_END_NAMESPACE
83 84
84 85 #endif // QPIESLICE_H
General Comments 0
You need to be logged in to leave comments. Login now