##// END OF EJS Templates
QML: VPieModelMapper as a child for PieSeries
Tero Ahola -
r1254:7867f67d21c3
parent child
Show More
@@ -1,118 +1,108
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 import QtQuick 1.0
21 import QtQuick 1.0
22 import QtCommercial.Chart 1.0
22 import QtCommercial.Chart 1.0
23
23
24 Rectangle {
24 Rectangle {
25 width: parent.width
25 width: parent.width
26 height: parent.height
26 height: parent.height
27
27
28 ChartView {
28 ChartView {
29 id: chart
29 id: chart
30 title: "Custom model example"
30 title: "Custom model example"
31 anchors.fill: parent
31 anchors.fill: parent
32 theme: ChartView.ChartThemeLight
32 theme: ChartView.ChartThemeLight
33
33
34 // For dynamic data you can use the ChartModel API.
34 // For dynamic data you can use the ChartModel API.
35 ChartModel {
35 ChartModel {
36 id: chartModel
36 id: chartModel
37 ChartModelElement { values: [0, "Volkswagen", 13.5, 4.4] }
37 ChartModelElement { values: ["Volkswagen", 13.5, 4.4] }
38 ChartModelElement { values: [0, "Toyota", 10.9, 4.2] }
38 ChartModelElement { values: ["Toyota", 10.9, 4.2] }
39 ChartModelElement { values: [0, "Ford", 8.6, 3.0] }
39 ChartModelElement { values: ["Ford", 8.6, 3.0] }
40 ChartModelElement { values: [0, "Skoda", 8.2, 1.9] }
40 ChartModelElement { values: ["Skoda", 8.2, 1.9] }
41 ChartModelElement { values: [0, "Volvo", 6.8, 1.5] }
41 ChartModelElement { values: ["Volvo", 6.8, 1.5] }
42 }
42 }
43
43
44 LineSeries {
44 LineSeries {
45 name: "line"
45 name: "line"
46
46
47 // TODO: the new mapper api
47 // TODO: the new mapper api
48 // XYModelMapperVertical {
48 // VXYModelMapper {
49 // model: chartModel
49 // model: chartModel
50 // xColumn: 0
50 // xColumn: 0
51 // yColumn: 1
51 // yColumn: 1
52 // }
52 // }
53 }
53 }
54
54
55
55
56 PieSeries {
56 PieSeries {
57 id: pieSeries
57 id: pieSeries
58 size: 0.4
58 size: 0.4
59 horizontalPosition: 0.2
59 horizontalPosition: 0.2
60 verticalPosition: 0.3
60 verticalPosition: 0.3
61
61
62 VPieModelMapper {
62 VPieModelMapper {
63 series: pieSeries // TODO
64 model: chartModel
63 model: chartModel
65 labelsColumn: 1
64 labelsColumn: 0
66 valuesColumn: 2
65 valuesColumn: 1
67 }
66 }
68 }
67 }
69
68
70 // TODO: the new mapper api
69 // VPieModelMapper {
71 // PieModelMapperVertical {
72 // series: pieSeries
70 // series: pieSeries
73 // model: chartModel
71 // model: chartModel
74 // labelsColumn: 0
72 // labelsColumn: 1
75 // valuesColumn: 1
73 // valuesColumn: 2
76 // }
74 // }
77
75
78 AreaSeries {
76 // AreaSeries {
79 name: "area"
77 // name: "area"
80 upperSeries: LineSeries {
78 // upperSeries: LineSeries {}
81 // model: chartModel
79 // lowerSeries: LineSeries {}
82 // modelMapper.mapX: 2
80 // }
83 // modelMapper.mapY: 2
84 }
85 lowerSeries: LineSeries {
86 // model: chartModel
87 // modelMapper.mapX: 2
88 // modelMapper.mapY: 0
89 }
90 }
91
81
92 // TODO: BarSeries with ChartModel base model API
82 // TODO: BarSeries with ChartModel base model API
93 // BarSeries {
83 // BarSeries {
94 // model: chartModel
84 // model: chartModel
95 // modelMapper.first: 0
85 // modelMapper.first: 0
96 // }
86 // }
97 }
87 }
98
88
99
89
100 // TODO: you could also implement appending to your model, for example:
90 // TODO: you could also implement appending to your model, for example:
101 // pieSeries.model.append(["Others", 52.0]);
91 // pieSeries.model.append(["Others", 52.0]);
102
92
103 // TODO: show how to use data from a list model in a chart view
93 // TODO: show how to use data from a list model in a chart view
104 // i.e. copy the data into a chart model
94 // i.e. copy the data into a chart model
105 // ListModel {
95 // ListModel {
106 // id: listModel
96 // id: listModel
107 // ListElement {
97 // ListElement {
108 // label: "Volkswagen"
98 // label: "Volkswagen"
109 // value: 13.5
99 // value: 13.5
110 // }
100 // }
111 // ListElement {
101 // ListElement {
112 // label: "Toyota"
102 // label: "Toyota"
113 // value: 10.9
103 // value: 10.9
114 // }
104 // }
115 // // and so on...
105 // // and so on...
116 // }
106 // }
117
107
118 }
108 }
@@ -1,104 +1,107
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "declarativepieseries.h"
21 #include "declarativepieseries.h"
22 #include "declarativechart.h"
22 #include "declarativechart.h"
23 #include "qchart.h"
23 #include "qchart.h"
24 #include <qdeclarativelist.h>
24 #include <qdeclarativelist.h>
25 #include "qpiemodelmapper.h"
25 #include <QVPieModelMapper>
26 #include <QHPieModelMapper>
26
27
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28
29
29 DeclarativePieSeries::DeclarativePieSeries(QObject *parent) :
30 DeclarativePieSeries::DeclarativePieSeries(QObject *parent) :
30 QPieSeries(parent)
31 QPieSeries(parent)
31 {
32 {
32 // TODO: set default model on init?
33 // setModel(new DeclarativeTableModel());
34
35 // TODO: Set default mapper parameters to allow easy to use PieSeries api?
36 // QPieModelMapper *mapper = modelMapper();//new QPieModelMapper();
37 // mapper->setMapLabels(0);
38 // mapper->setMapValues(1);
39 // mapper->setOrientation(Qt::Vertical);
40 // mapper->setFirst(0);
41 // mapper->setCount(-1);
42 // setModelMapper(mapper);
43 }
33 }
44
34
45 void DeclarativePieSeries::classBegin()
35 void DeclarativePieSeries::classBegin()
46 {
36 {
47 }
37 }
48
38
49 void DeclarativePieSeries::componentComplete()
39 void DeclarativePieSeries::componentComplete()
50 {
40 {
51 foreach(QObject *child, children()) {
41 foreach(QObject *child, children()) {
52 if (qobject_cast<QPieSlice *>(child)) {
42 if (qobject_cast<QPieSlice *>(child)) {
53 QPieSeries::append(qobject_cast<QPieSlice *>(child));
43 QPieSeries::append(qobject_cast<QPieSlice *>(child));
44 } else if(qobject_cast<QVPieModelMapper *>(child)) {
45 QVPieModelMapper *mapper = qobject_cast<QVPieModelMapper *>(child);
46 mapper->setSeries(this);
47 } else if(qobject_cast<QHPieModelMapper *>(child)) {
48 QHPieModelMapper *mapper = qobject_cast<QHPieModelMapper *>(child);
49 mapper->setSeries(this);
54 }
50 }
55 }
51 }
56 }
52 }
57
53
58 QDeclarativeListProperty<QPieSlice> DeclarativePieSeries::initialSlices()
54 QDeclarativeListProperty<QObject> DeclarativePieSeries::seriesChildren()
55 {
56 return QDeclarativeListProperty<QObject>(this, 0, &DeclarativePieSeries::appendSeriesChildren);
57 }
58
59 void DeclarativePieSeries::appendSeriesChildren(QDeclarativeListProperty<QObject> * list, QObject *element)
59 {
60 {
60 return QDeclarativeListProperty<QPieSlice>(this, 0, &DeclarativePieSeries::appendInitialSlices);
61 // Empty implementation; the children are parsed in componentComplete instead
62 Q_UNUSED(list);
63 Q_UNUSED(element);
61 }
64 }
62
65
63 QPieSlice *DeclarativePieSeries::at(int index)
66 QPieSlice *DeclarativePieSeries::at(int index)
64 {
67 {
65 QList<QPieSlice*> sliceList = slices();
68 QList<QPieSlice*> sliceList = slices();
66 if (index < sliceList.count())
69 if (index < sliceList.count())
67 return sliceList[index];
70 return sliceList[index];
68
71
69 return 0;
72 return 0;
70 }
73 }
71
74
72 QPieSlice* DeclarativePieSeries::find(QString label)
75 QPieSlice* DeclarativePieSeries::find(QString label)
73 {
76 {
74 foreach (QPieSlice *slice, slices()) {
77 foreach (QPieSlice *slice, slices()) {
75 if (slice->label() == label)
78 if (slice->label() == label)
76 return slice;
79 return slice;
77 }
80 }
78 return 0;
81 return 0;
79 }
82 }
80
83
81 QPieSlice* DeclarativePieSeries::append(QString name, qreal value)
84 QPieSlice* DeclarativePieSeries::append(QString name, qreal value)
82 {
85 {
83 // TODO: parameter order is wrong, switch it:
86 // TODO: parameter order is wrong, switch it:
84 return QPieSeries::append(name, value);
87 return QPieSeries::append(name, value);
85 }
88 }
86
89
87 void DeclarativePieSeries::setPieModel(DeclarativeTableModel *model)
90 void DeclarativePieSeries::setPieModel(DeclarativeTableModel *model)
88 {
91 {
89 QAbstractItemModel *m = qobject_cast<QAbstractItemModel *>(model);
92 QAbstractItemModel *m = qobject_cast<QAbstractItemModel *>(model);
90 if (m) {
93 if (m) {
91 // QPieSeries::setModel(m);
94 // QPieSeries::setModel(m);
92 } else {
95 } else {
93 qWarning("DeclarativePieSeries: Illegal model");
96 qWarning("DeclarativePieSeries: Illegal model");
94 }
97 }
95 }
98 }
96
99
97 DeclarativeTableModel *DeclarativePieSeries::pieModel()
100 DeclarativeTableModel *DeclarativePieSeries::pieModel()
98 {
101 {
99 return 0;//qobject_cast<DeclarativeTableModel *>(model());
102 return 0;//qobject_cast<DeclarativeTableModel *>(model());
100 }
103 }
101
104
102 #include "moc_declarativepieseries.cpp"
105 #include "moc_declarativepieseries.cpp"
103
106
104 QTCOMMERCIALCHART_END_NAMESPACE
107 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,64 +1,63
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #ifndef DECLARATIVEPIESERIES_H
21 #ifndef DECLARATIVEPIESERIES_H
22 #define DECLARATIVEPIESERIES_H
22 #define DECLARATIVEPIESERIES_H
23
23
24 #include "qchartglobal.h"
24 #include "qchartglobal.h"
25 #include <QPieSlice>
25 #include <QPieSlice>
26 #include <QPieSeries>
26 #include <QPieSeries>
27 #include <QDeclarativeParserStatus>
27 #include <QDeclarativeParserStatus>
28 #include <QDeclarativeListProperty>
28 #include <QDeclarativeListProperty>
29 #include <QAbstractItemModel>
29 #include <QAbstractItemModel>
30 #include <QVariant>
31 #include "declarativemodel.h"
30 #include "declarativemodel.h"
32
31
33 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
34
33
35 class QChart;
34 class QChart;
36
35
37 class DeclarativePieSeries : public QPieSeries, public QDeclarativeParserStatus
36 class DeclarativePieSeries : public QPieSeries, public QDeclarativeParserStatus
38 {
37 {
39 Q_OBJECT
38 Q_OBJECT
40 Q_INTERFACES(QDeclarativeParserStatus)
39 Q_INTERFACES(QDeclarativeParserStatus)
41 Q_PROPERTY(DeclarativeTableModel *model READ pieModel WRITE setPieModel)
40 Q_PROPERTY(DeclarativeTableModel *model READ pieModel WRITE setPieModel)
42 Q_PROPERTY(QDeclarativeListProperty<QPieSlice> initialSlices READ initialSlices)
41 Q_PROPERTY(QDeclarativeListProperty<QObject> seriesChildren READ seriesChildren)
43 Q_CLASSINFO("DefaultProperty", "initialSlices")
42 Q_CLASSINFO("DefaultProperty", "seriesChildren")
44
43
45 public:
44 public:
46 explicit DeclarativePieSeries(QObject *parent = 0);
45 explicit DeclarativePieSeries(QObject *parent = 0);
47 QDeclarativeListProperty<QPieSlice> initialSlices();
46 QDeclarativeListProperty<QObject> seriesChildren();
48 DeclarativeTableModel *pieModel();
47 DeclarativeTableModel *pieModel();
49 void setPieModel(DeclarativeTableModel *model);
48 void setPieModel(DeclarativeTableModel *model);
50 Q_INVOKABLE QPieSlice *at(int index);
49 Q_INVOKABLE QPieSlice *at(int index);
51 Q_INVOKABLE QPieSlice* find(QString label);
50 Q_INVOKABLE QPieSlice* find(QString label);
52 Q_INVOKABLE QPieSlice* append(QString label, qreal value);
51 Q_INVOKABLE QPieSlice* append(QString label, qreal value);
53
52
54 public:
53 public:
55 void classBegin();
54 void classBegin();
56 void componentComplete();
55 void componentComplete();
57
56
58 public Q_SLOTS:
57 public Q_SLOTS:
59 static void appendInitialSlices(QDeclarativeListProperty<QPieSlice> * /*list*/, QPieSlice * /*element*/) {}
58 static void appendSeriesChildren(QDeclarativeListProperty<QObject> *list, QObject *element);
60 };
59 };
61
60
62 QTCOMMERCIALCHART_END_NAMESPACE
61 QTCOMMERCIALCHART_END_NAMESPACE
63
62
64 #endif // DECLARATIVEPIESERIES_H
63 #endif // DECLARATIVEPIESERIES_H
General Comments 0
You need to be logged in to leave comments. Login now