##// END OF EJS Templates
Added new model mappers for pie to QML api
Tero Ahola -
r1249:94a47eb3a2ef
parent child
Show More
@@ -1,43 +1,42
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 anchors.fill: parent
25 anchors.fill: parent
26
26
27 ChartView {
27 ChartView {
28 title: "Bar series"
28 title: "Bar series"
29 anchors.fill: parent
29 anchors.fill: parent
30 theme: ChartView.ChartThemeLight
30 theme: ChartView.ChartThemeLight
31 legend: ChartView.LegendBottom
31 legend: ChartView.LegendBottom
32 // TODO: labels defined by x-axis, not by bar series
32 // TODO: labels defined by x-axis, not by bar series
33 axisXLabels: ["0", "2008", "1", "2009", "2", "2010", "3", "2012"]
33 axisXLabels: ["0", "2008", "1", "2009", "2", "2010", "3", "2012"]
34 axisX.max: 10
35
34
36 GroupedBarSeries {
35 GroupedBarSeries {
37 barCategories: [ "2008", "2009", "2010", "2011", "2012" ]
36 barCategories: [ "2008", "2009", "2010", "2011", "2012" ]
38 BarSet { name: "Bob"; values: [2, 2, 3, 4, 5, 6] }
37 BarSet { name: "Bob"; values: [2, 2, 3, 4, 5, 6] }
39 BarSet { name: "Susan"; values: [5, 1, 2, 4, 1, 7] }
38 BarSet { name: "Susan"; values: [5, 1, 2, 4, 1, 7] }
40 BarSet { name: "James"; values: [3, 5, 8, 13, 5, 8] }
39 BarSet { name: "James"; values: [3, 5, 8, 13, 5, 8] }
41 }
40 }
42 }
41 }
43 }
42 }
@@ -1,130 +1,118
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: [0, "Volkswagen", 13.5, 4.4] }
38 ChartModelElement { values: [0, "Toyota", 10.9, 4.2] }
38 ChartModelElement { values: [0, "Toyota", 10.9, 4.2] }
39 ChartModelElement { values: [0, "Ford", 8.6, 3.0] }
39 ChartModelElement { values: [0, "Ford", 8.6, 3.0] }
40 ChartModelElement { values: [0, "Skoda", 8.2, 1.9] }
40 ChartModelElement { values: [0, "Skoda", 8.2, 1.9] }
41 ChartModelElement { values: [0, "Volvo", 6.8, 1.5] }
41 ChartModelElement { values: [0, "Volvo", 6.8, 1.5] }
42 }
42 }
43
43
44 LineSeries {
44 LineSeries {
45 name: "line"
45 name: "line"
46 // TODO: the current mapper api
47 model: chartModel
48 modelMapper.mapX: 3
49 modelMapper.mapY: 3
50
46
51 // TODO: the new mapper api
47 // TODO: the new mapper api
52 // XYModelMapperVertical {
48 // XYModelMapperVertical {
53 // model: chartModel
49 // model: chartModel
54 // xColumn: 0
50 // xColumn: 0
55 // yColumn: 1
51 // yColumn: 1
56 // }
52 // }
57 }
53 }
58
54
59
55
60 PieSeries {
56 PieSeries {
61 id: pieSeries
57 id: pieSeries
62 size: 0.4
58 size: 0.4
63 horizontalPosition: 0.2
59 horizontalPosition: 0.2
64 verticalPosition: 0.3
60 verticalPosition: 0.3
65
61
66 // model: chartModel
62 VPieModelMapper {
67 // // TODO: the current mapper api
63 series: pieSeries // TODO
68 // modelMapper.mapLabels: 1
64 model: chartModel
69 // modelMapper.mapValues: 2
65 labelsColumn: 1
70 // modelMapper.first: 0
66 valuesColumn: 2
71 // modelMapper.count: -1 // "Undefined" = -1 by default
67 }
72 // modelMapper.orientation: PieModelMapper.Vertical
73
74 // TODO: the new mapper api
75 // PieModelMapperVertical {
76 // model: chartModel
77 // labelsColumn: 0
78 // valuesColumn: 1
79 // }
80 }
68 }
81
69
82 // TODO: the new mapper api
70 // TODO: the new mapper api
83 // PieModelMapperVertical {
71 // PieModelMapperVertical {
84 // series: pieSeries
72 // series: pieSeries
85 // model: chartModel
73 // model: chartModel
86 // labelsColumn: 0
74 // labelsColumn: 0
87 // valuesColumn: 1
75 // valuesColumn: 1
88 // }
76 // }
89
77
90 AreaSeries {
78 AreaSeries {
91 name: "area"
79 name: "area"
92 upperSeries: LineSeries {
80 upperSeries: LineSeries {
93 model: chartModel
81 // model: chartModel
94 modelMapper.mapX: 2
82 // modelMapper.mapX: 2
95 modelMapper.mapY: 2
83 // modelMapper.mapY: 2
96 }
84 }
97 lowerSeries: LineSeries {
85 lowerSeries: LineSeries {
98 model: chartModel
86 // model: chartModel
99 modelMapper.mapX: 2
87 // modelMapper.mapX: 2
100 modelMapper.mapY: 0
88 // modelMapper.mapY: 0
101 }
89 }
102 }
90 }
103
91
104 // TODO: BarSeries with ChartModel base model API
92 // TODO: BarSeries with ChartModel base model API
105 // BarSeries {
93 // BarSeries {
106 // model: chartModel
94 // model: chartModel
107 // modelMapper.first: 0
95 // modelMapper.first: 0
108 // }
96 // }
109 }
97 }
110
98
111
99
112 // TODO: you could also implement appending to your model, for example:
100 // TODO: you could also implement appending to your model, for example:
113 // pieSeries.model.append(["Others", 52.0]);
101 // pieSeries.model.append(["Others", 52.0]);
114
102
115 // TODO: show how to use data from a list model in a chart view
103 // TODO: show how to use data from a list model in a chart view
116 // i.e. copy the data into a chart model
104 // i.e. copy the data into a chart model
117 // ListModel {
105 // ListModel {
118 // id: listModel
106 // id: listModel
119 // ListElement {
107 // ListElement {
120 // label: "Volkswagen"
108 // label: "Volkswagen"
121 // value: 13.5
109 // value: 13.5
122 // }
110 // }
123 // ListElement {
111 // ListElement {
124 // label: "Toyota"
112 // label: "Toyota"
125 // value: 10.9
113 // value: 10.9
126 // }
114 // }
127 // // and so on...
115 // // and so on...
128 // }
116 // }
129
117
130 }
118 }
@@ -1,73 +1,76
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 DECLARATIVEMODEL_H
21 #ifndef DECLARATIVEMODEL_H
22 #define DECLARATIVEMODEL_H
22 #define DECLARATIVEMODEL_H
23
23
24 #include "qchartglobal.h"
24 #include "qchartglobal.h"
25 #include "declarativexypoint.h"
25 #include "declarativexypoint.h"
26 #include <QPieSlice>
26 #include <QPieSlice>
27 #include "../src/charttablemodel.h" // TODO
27 #include "../src/charttablemodel.h" // TODO
28 #include <QBarSet>
28 #include <QBarSet>
29 #include <QXYModelMapper>
29 #include <QXYModelMapper>
30 #include <QDeclarativeListProperty>
30 #include <QDeclarativeListProperty>
31 #include <QVariant>
31 #include <QVariant>
32 #include <QDeclarativeParserStatus>
32 #include <QDeclarativeParserStatus>
33
33
34 QTCOMMERCIALCHART_BEGIN_NAMESPACE
34 QTCOMMERCIALCHART_BEGIN_NAMESPACE
35
35
36 // TODO: move model into demo app,
37 // the ChartModel API will not be implemented by charts declarative plugin
38
36 class DeclarativeTableModelElement : public QObject
39 class DeclarativeTableModelElement : public QObject
37 {
40 {
38 Q_OBJECT
41 Q_OBJECT
39 Q_PROPERTY(QVariantList values READ values WRITE setValues)
42 Q_PROPERTY(QVariantList values READ values WRITE setValues)
40
43
41 public:
44 public:
42 explicit DeclarativeTableModelElement(QObject *parent = 0);
45 explicit DeclarativeTableModelElement(QObject *parent = 0);
43 QVariantList values();
46 QVariantList values();
44 void setValues(QVariantList values);
47 void setValues(QVariantList values);
45 private:
48 private:
46 QVariantList m_values;
49 QVariantList m_values;
47 };
50 };
48
51
49 class DeclarativeTableModel : public ChartTableModel, public QDeclarativeParserStatus
52 class DeclarativeTableModel : public ChartTableModel, public QDeclarativeParserStatus
50 {
53 {
51 Q_OBJECT
54 Q_OBJECT
52 Q_INTERFACES(QDeclarativeParserStatus)
55 Q_INTERFACES(QDeclarativeParserStatus)
53 Q_PROPERTY(QDeclarativeListProperty<QObject> modelChildren READ modelChildren)
56 Q_PROPERTY(QDeclarativeListProperty<QObject> modelChildren READ modelChildren)
54 Q_CLASSINFO("DefaultProperty", "modelChildren")
57 Q_CLASSINFO("DefaultProperty", "modelChildren")
55
58
56 public:
59 public:
57 explicit DeclarativeTableModel(QObject *parent = 0);
60 explicit DeclarativeTableModel(QObject *parent = 0);
58 QDeclarativeListProperty<QObject> modelChildren();
61 QDeclarativeListProperty<QObject> modelChildren();
59 void appendPoint(QXYModelMapper *mapper, DeclarativeXyPoint *point);
62 void appendPoint(QXYModelMapper *mapper, DeclarativeXyPoint *point);
60
63
61 public: // from QDeclarativeParserStatus
64 public: // from QDeclarativeParserStatus
62 void classBegin();
65 void classBegin();
63 void componentComplete();
66 void componentComplete();
64
67
65 public Q_SLOTS:
68 public Q_SLOTS:
66 void append(QVariantList slices);
69 void append(QVariantList slices);
67 static void appendModelChild(QDeclarativeListProperty<QObject> *list,
70 static void appendModelChild(QDeclarativeListProperty<QObject> *list,
68 QObject *element);
71 QObject *element);
69 };
72 };
70
73
71 QTCOMMERCIALCHART_END_NAMESPACE
74 QTCOMMERCIALCHART_END_NAMESPACE
72
75
73 #endif // DECLARATIVEMODEL_H
76 #endif // DECLARATIVEMODEL_H
@@ -1,79 +1,86
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 <QtDeclarative/qdeclarativeextensionplugin.h>
21 #include <QtDeclarative/qdeclarativeextensionplugin.h>
22 #include <QtDeclarative/qdeclarative.h>
22 #include <QtDeclarative/qdeclarative.h>
23 #include "qchart.h"
23 #include "qchart.h"
24 #include "qaxiscategories.h"
24 #include "qaxiscategories.h"
25 #include "declarativechart.h"
25 #include "declarativechart.h"
26 #include "declarativexypoint.h"
26 #include "declarativexypoint.h"
27 #include "declarativelineseries.h"
27 #include "declarativelineseries.h"
28 #include "declarativesplineseries.h"
28 #include "declarativesplineseries.h"
29 #include "declarativeareaseries.h"
29 #include "declarativeareaseries.h"
30 #include "declarativescatterseries.h"
30 #include "declarativescatterseries.h"
31 #include "declarativebarseries.h"
31 #include "declarativebarseries.h"
32 #include "declarativepieseries.h"
32 #include "declarativepieseries.h"
33 #include "declarativemodel.h"
33 #include "declarativemodel.h"
34 #include <QPieModelMapper>
34 #include <QHPieModelMapper>
35 #include <QVPieModelMapper>
35 #include <QXYModelMapper>
36 #include <QXYModelMapper>
36
37
37 QTCOMMERCIALCHART_BEGIN_NAMESPACE
38 QTCOMMERCIALCHART_BEGIN_NAMESPACE
38
39
39 class ChartQmlPlugin : public QDeclarativeExtensionPlugin
40 class ChartQmlPlugin : public QDeclarativeExtensionPlugin
40 {
41 {
41 Q_OBJECT
42 Q_OBJECT
42 public:
43 public:
43 virtual void registerTypes(const char *uri)
44 virtual void registerTypes(const char *uri)
44 {
45 {
45 Q_ASSERT(QLatin1String(uri) == QLatin1String("QtCommercial.Chart"));
46 Q_ASSERT(QLatin1String(uri) == QLatin1String("QtCommercial.Chart"));
46
47
47 qmlRegisterType<DeclarativeChart>(uri, 1, 0, "ChartView");
48 qmlRegisterType<DeclarativeChart>(uri, 1, 0, "ChartView");
48 qmlRegisterType<DeclarativeXyPoint>(uri, 1, 0, "XyPoint");
49 qmlRegisterType<DeclarativeXyPoint>(uri, 1, 0, "XyPoint");
49 qmlRegisterType<DeclarativeScatterSeries>(uri, 1, 0, "ScatterSeries");
50 qmlRegisterType<DeclarativeScatterSeries>(uri, 1, 0, "ScatterSeries");
50 qmlRegisterType<DeclarativeLineSeries>(uri, 1, 0, "LineSeries");
51 qmlRegisterType<DeclarativeLineSeries>(uri, 1, 0, "LineSeries");
51 qmlRegisterType<DeclarativeSplineSeries>(uri, 1, 0, "SplineSeries");
52 qmlRegisterType<DeclarativeSplineSeries>(uri, 1, 0, "SplineSeries");
52 qmlRegisterType<DeclarativeAreaSeries>(uri, 1, 0, "AreaSeries");
53 qmlRegisterType<DeclarativeAreaSeries>(uri, 1, 0, "AreaSeries");
53 qmlRegisterType<DeclarativeBarSeries>(uri, 1, 0, "BarSeries");
54 qmlRegisterType<DeclarativeBarSeries>(uri, 1, 0, "BarSeries");
54 qmlRegisterType<DeclarativeGroupedBarSeries>(uri, 1, 0, "GroupedBarSeries");
55 qmlRegisterType<DeclarativeGroupedBarSeries>(uri, 1, 0, "GroupedBarSeries");
55 qmlRegisterType<DeclarativePieSeries>(uri, 1, 0, "PieSeries");
56 qmlRegisterType<DeclarativePieSeries>(uri, 1, 0, "PieSeries");
56 qmlRegisterType<QPieSlice>(uri, 1, 0, "PieSlice");
57 qmlRegisterType<QPieSlice>(uri, 1, 0, "PieSlice");
57 qmlRegisterType<DeclarativeTableModel>(uri, 1, 0, "ChartModel");
58 qmlRegisterType<DeclarativeTableModel>(uri, 1, 0, "ChartModel");
58 qmlRegisterType<DeclarativeTableModelElement>(uri, 1, 0, "ChartModelElement");
59 qmlRegisterType<DeclarativeTableModelElement>(uri, 1, 0, "ChartModelElement");
59 qmlRegisterType<DeclarativeBarSet>(uri, 1, 0, "BarSet");
60 qmlRegisterType<DeclarativeBarSet>(uri, 1, 0, "BarSet");
60
61
62 qmlRegisterUncreatableType<QAbstractItemModel>(uri, 1, 0, "AbstractItemModel",
63 QLatin1String("Trying to create uncreatable: AbstractItemModel."));
64 qmlRegisterUncreatableType<QPieModelMapper>(uri, 1, 0, "PieModelMapper",
65 QLatin1String("Trying to create uncreatable: PieModelMapper."));
66 qmlRegisterType<QHPieModelMapper>(uri, 1, 0, "HPieModelMapper");
67 qmlRegisterType<QVPieModelMapper>(uri, 1, 0, "VPieModelMapper");
61
68
62 qmlRegisterUncreatableType<QAbstractSeries>(uri, 1, 0, "AbstractSeries",
69 qmlRegisterUncreatableType<QAbstractSeries>(uri, 1, 0, "AbstractSeries",
63 QLatin1String("Trying to create uncreatable: AbstractSeries."));
70 QLatin1String("Trying to create uncreatable: AbstractSeries."));
64 qmlRegisterUncreatableType<QAxis>(uri, 1, 0, "Axis",
71 qmlRegisterUncreatableType<QAxis>(uri, 1, 0, "Axis",
65 QLatin1String("Trying to create uncreatable: Axis."));
72 QLatin1String("Trying to create uncreatable: Axis."));
66 qmlRegisterUncreatableType<QPieModelMapper>(uri, 1, 0, "PieModelMapper",
73 qmlRegisterUncreatableType<QPieModelMapper>(uri, 1, 0, "PieModelMapper",
67 QLatin1String("Trying to create uncreatable: PieModelMapper."));
74 QLatin1String("Trying to create uncreatable: PieModelMapper."));
68 qmlRegisterUncreatableType<QXYModelMapper>(uri, 1, 0, "XYModelMapper",
75 qmlRegisterUncreatableType<QXYModelMapper>(uri, 1, 0, "XYModelMapper",
69 QLatin1String("Trying to create uncreatable: XYModelMapper."));
76 QLatin1String("Trying to create uncreatable: XYModelMapper."));
70 }
77 }
71 };
78 };
72
79
73 #include "plugin.moc"
80 #include "plugin.moc"
74
81
75 QTCOMMERCIALCHART_END_NAMESPACE
82 QTCOMMERCIALCHART_END_NAMESPACE
76
83
77 QTCOMMERCIALCHART_USE_NAMESPACE
84 QTCOMMERCIALCHART_USE_NAMESPACE
78
85
79 Q_EXPORT_PLUGIN2(qtcommercialchartqml, QT_PREPEND_NAMESPACE(ChartQmlPlugin))
86 Q_EXPORT_PLUGIN2(qtcommercialchartqml, QT_PREPEND_NAMESPACE(ChartQmlPlugin))
@@ -1,465 +1,465
1 #include "qpiemodelmapper_p.h"
1 #include "qpiemodelmapper_p.h"
2 #include "qpiemodelmapper.h"
2 #include "qpiemodelmapper.h"
3 #include "qpieseries.h"
3 #include "qpieseries.h"
4 #include "qpieslice.h"
4 #include "qpieslice.h"
5 #include <QAbstractItemModel>
5 #include <QAbstractItemModel>
6
6
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8
8
9 QPieModelMapper::QPieModelMapper(QObject *parent) :
9 QPieModelMapper::QPieModelMapper(QObject *parent) :
10 QObject(parent),
10 QObject(parent),
11 d_ptr(new QPieModelMapperPrivate(this))
11 d_ptr(new QPieModelMapperPrivate(this))
12 {
12 {
13 }
13 }
14
14
15 QAbstractItemModel* QPieModelMapper::model() const
15 QAbstractItemModel* QPieModelMapper::model() const
16 {
16 {
17 Q_D(const QPieModelMapper);
17 Q_D(const QPieModelMapper);
18 return d->m_model;
18 return d->m_model;
19 }
19 }
20
20
21 void QPieModelMapper::setModel(QAbstractItemModel *model)
21 void QPieModelMapper::setModel(QAbstractItemModel *model)
22 {
22 {
23 if (model == 0)
23 if (model == 0)
24 return;
24 return;
25
25
26 Q_D(QPieModelMapper);
26 Q_D(QPieModelMapper);
27 if (d->m_model) {
27 if (d->m_model) {
28 disconnect(d->m_model, 0, d, 0);
28 disconnect(d->m_model, 0, d, 0);
29 }
29 }
30
30
31 d->m_model = model;
31 d->m_model = model;
32 d->initializePieFromModel();
32 d->initializePieFromModel();
33 // connect signals from the model
33 // connect signals from the model
34 connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex)));
34 connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex)));
35 connect(d->m_model, SIGNAL(rowsInserted(QModelIndex,int,int)), d, SLOT(modelRowsAdded(QModelIndex,int,int)));
35 connect(d->m_model, SIGNAL(rowsInserted(QModelIndex,int,int)), d, SLOT(modelRowsAdded(QModelIndex,int,int)));
36 connect(d->m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)), d, SLOT(modelRowsRemoved(QModelIndex,int,int)));
36 connect(d->m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)), d, SLOT(modelRowsRemoved(QModelIndex,int,int)));
37 connect(d->m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), d, SLOT(modelColumnsAdded(QModelIndex,int,int)));
37 connect(d->m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), d, SLOT(modelColumnsAdded(QModelIndex,int,int)));
38 connect(d->m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), d, SLOT(modelColumnsRemoved(QModelIndex,int,int)));
38 connect(d->m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), d, SLOT(modelColumnsRemoved(QModelIndex,int,int)));
39 }
39 }
40
40
41 QPieSeries* QPieModelMapper::series() const
41 QPieSeries* QPieModelMapper::series() const
42 {
42 {
43 Q_D(const QPieModelMapper);
43 Q_D(const QPieModelMapper);
44 return d->m_series;
44 return d->m_series;
45 }
45 }
46
46
47 void QPieModelMapper::setSeries(QPieSeries *series)
47 void QPieModelMapper::setSeries(QPieSeries *series)
48 {
48 {
49 if (series == 0)
49 if (series == 0)
50 return;
50 return;
51
51
52 Q_D(QPieModelMapper);
52 Q_D(QPieModelMapper);
53 if (d->m_series) {
53 if (d->m_series) {
54 disconnect(d->m_series, 0, d, 0);
54 disconnect(d->m_series, 0, d, 0);
55 }
55 }
56
56
57 d->m_series = series;
57 d->m_series = series;
58 d->initializePieFromModel();
58 d->initializePieFromModel();
59 // connect the signals from the series
59 // connect the signals from the series
60 connect(d->m_series, SIGNAL(added(QList<QPieSlice*>)), d, SLOT(slicesAdded(QList<QPieSlice*>)));
60 connect(d->m_series, SIGNAL(added(QList<QPieSlice*>)), d, SLOT(slicesAdded(QList<QPieSlice*>)));
61 connect(d->m_series, SIGNAL(removed(QList<QPieSlice*>)), d, SLOT(slicesRemoved(QList<QPieSlice*>)));
61 connect(d->m_series, SIGNAL(removed(QList<QPieSlice*>)), d, SLOT(slicesRemoved(QList<QPieSlice*>)));
62 // connect(d->m_model, SIGNAL(), d, SLOT());
62 // connect(d->m_model, SIGNAL(), d, SLOT());
63 }
63 }
64
64
65 int QPieModelMapper::first() const
65 int QPieModelMapper::first() const
66 {
66 {
67 Q_D(const QPieModelMapper);
67 Q_D(const QPieModelMapper);
68 return d->m_first;
68 return d->m_first;
69 }
69 }
70
70
71 void QPieModelMapper::setFirst(int first)
71 void QPieModelMapper::setFirst(int first)
72 {
72 {
73 Q_D(QPieModelMapper);
73 Q_D(QPieModelMapper);
74 d->m_first = qMax(first, 0);
74 d->m_first = qMax(first, 0);
75 d->initializePieFromModel();
75 d->initializePieFromModel();
76 }
76 }
77
77
78 int QPieModelMapper::count() const
78 int QPieModelMapper::count() const
79 {
79 {
80 Q_D(const QPieModelMapper);
80 Q_D(const QPieModelMapper);
81 return d->m_count;
81 return d->m_count;
82 }
82 }
83
83
84 void QPieModelMapper::setCount(int count)
84 void QPieModelMapper::setCount(int count)
85 {
85 {
86 Q_D(QPieModelMapper);
86 Q_D(QPieModelMapper);
87 d->m_count = qMax(count, -1);
87 d->m_count = qMax(count, -1);
88 d->initializePieFromModel();
88 d->initializePieFromModel();
89 }
89 }
90
90
91 Qt::Orientation QPieModelMapper::orientation() const
91 Qt::Orientation QPieModelMapper::orientation() const
92 {
92 {
93 Q_D(const QPieModelMapper);
93 Q_D(const QPieModelMapper);
94 return d->m_orientation;
94 return d->m_orientation;
95 }
95 }
96
96
97 void QPieModelMapper::setOrientation(Qt::Orientation orientation)
97 void QPieModelMapper::setOrientation(Qt::Orientation orientation)
98 {
98 {
99 Q_D(QPieModelMapper);
99 Q_D(QPieModelMapper);
100 d->m_orientation = orientation;
100 d->m_orientation = orientation;
101 d->initializePieFromModel();
101 d->initializePieFromModel();
102 }
102 }
103
103
104 int QPieModelMapper::valuesIndex() const
104 int QPieModelMapper::valuesIndex() const
105 {
105 {
106 Q_D(const QPieModelMapper);
106 Q_D(const QPieModelMapper);
107 return d->m_valuesIndex;
107 return d->m_valuesIndex;
108 }
108 }
109
109
110 void QPieModelMapper::setValuesIndex(int valuesIndex)
110 void QPieModelMapper::setValuesIndex(int valuesIndex)
111 {
111 {
112 Q_D(QPieModelMapper);
112 Q_D(QPieModelMapper);
113 d->m_valuesIndex = qMax(-1, valuesIndex);
113 d->m_valuesIndex = qMax(-1, valuesIndex);
114 d->initializePieFromModel();
114 d->initializePieFromModel();
115 }
115 }
116
116
117 int QPieModelMapper::labelsIndex() const
117 int QPieModelMapper::labelsIndex() const
118 {
118 {
119 Q_D(const QPieModelMapper);
119 Q_D(const QPieModelMapper);
120 return d->m_labelsIndex;
120 return d->m_labelsIndex;
121 }
121 }
122
122
123 void QPieModelMapper::setLabelsIndex(int labelsIndex)
123 void QPieModelMapper::setLabelsIndex(int labelsIndex)
124 {
124 {
125 Q_D(QPieModelMapper);
125 Q_D(QPieModelMapper);
126 d->m_labelsIndex = qMax(-1, labelsIndex);
126 d->m_labelsIndex = qMax(-1, labelsIndex);
127 d->initializePieFromModel();
127 d->initializePieFromModel();
128 }
128 }
129
129
130 void QPieModelMapper::reset()
130 void QPieModelMapper::reset()
131 {
131 {
132 Q_D(QPieModelMapper);
132 Q_D(QPieModelMapper);
133 d->m_first = 0;
133 d->m_first = 0;
134 d->m_count = -1;
134 d->m_count = -1;
135 d->m_orientation = Qt::Vertical;
135 d->m_orientation = Qt::Vertical;
136 d->m_valuesIndex = -1;
136 d->m_valuesIndex = -1;
137 d->m_labelsIndex = -1;
137 d->m_labelsIndex = -1;
138 }
138 }
139
139
140 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
140 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
141
141
142 QPieModelMapperPrivate::QPieModelMapperPrivate(QPieModelMapper *q) :
142 QPieModelMapperPrivate::QPieModelMapperPrivate(QPieModelMapper *q) :
143 m_series(0),
144 m_model(0),
145 m_first(0),
146 m_count(-1),
147 m_orientation(Qt::Vertical),
148 m_valuesIndex(-1),
149 m_labelsIndex(-1),
150 m_seriesSignalsBlock(false),
151 m_modelSignalsBlock(false),
143 q_ptr(q)
152 q_ptr(q)
144 {
153 {
145 m_series = 0;
146 m_model = 0;
147 m_first = 0;
148 m_count = -1;
149 m_orientation = Qt::Vertical;
150 m_valuesIndex = -1;
151 m_labelsIndex = -1;
152 m_seriesSignalsBlock = false;
153 m_modelSignalsBlock = false;
154 }
154 }
155
155
156 void QPieModelMapperPrivate::blockModelSignals(bool block)
156 void QPieModelMapperPrivate::blockModelSignals(bool block)
157 {
157 {
158 m_modelSignalsBlock = block;
158 m_modelSignalsBlock = block;
159 }
159 }
160
160
161 void QPieModelMapperPrivate::blockSeriesSignals(bool block)
161 void QPieModelMapperPrivate::blockSeriesSignals(bool block)
162 {
162 {
163 m_seriesSignalsBlock = block;
163 m_seriesSignalsBlock = block;
164 }
164 }
165
165
166
166
167 QPieSlice* QPieModelMapperPrivate::pieSlice(QModelIndex index) const
167 QPieSlice* QPieModelMapperPrivate::pieSlice(QModelIndex index) const
168 {
168 {
169 if (m_orientation == Qt::Vertical && (index.column() == m_valuesIndex || index.column() == m_labelsIndex)) {
169 if (m_orientation == Qt::Vertical && (index.column() == m_valuesIndex || index.column() == m_labelsIndex)) {
170 if (index.row() >= m_first && (m_count == - 1 || index.row() < m_first + m_count))
170 if (index.row() >= m_first && (m_count == - 1 || index.row() < m_first + m_count))
171 return m_series->slices().at(index.row() - m_first);
171 return m_series->slices().at(index.row() - m_first);
172 } else if (m_orientation == Qt::Horizontal && (index.row() == m_valuesIndex || index.row() == m_labelsIndex)) {
172 } else if (m_orientation == Qt::Horizontal && (index.row() == m_valuesIndex || index.row() == m_labelsIndex)) {
173 if (index.column() >= m_first && (m_count == - 1 || index.column() < m_first + m_count))
173 if (index.column() >= m_first && (m_count == - 1 || index.column() < m_first + m_count))
174 return m_series->slices().at(index.column() - m_first);
174 return m_series->slices().at(index.column() - m_first);
175 }
175 }
176 return 0; // This part of model has not been mapped to any slice
176 return 0; // This part of model has not been mapped to any slice
177 }
177 }
178
178
179 QModelIndex QPieModelMapperPrivate::valueModelIndex(int slicePos)
179 QModelIndex QPieModelMapperPrivate::valueModelIndex(int slicePos)
180 {
180 {
181 if (m_count != -1 && slicePos >= m_count)
181 if (m_count != -1 && slicePos >= m_count)
182 return QModelIndex(); // invalid
182 return QModelIndex(); // invalid
183
183
184 if (m_orientation == Qt::Vertical)
184 if (m_orientation == Qt::Vertical)
185 return m_model->index(slicePos + m_first, m_valuesIndex);
185 return m_model->index(slicePos + m_first, m_valuesIndex);
186 else
186 else
187 return m_model->index(m_valuesIndex, slicePos + m_first);
187 return m_model->index(m_valuesIndex, slicePos + m_first);
188 }
188 }
189
189
190 QModelIndex QPieModelMapperPrivate::labelModelIndex(int slicePos)
190 QModelIndex QPieModelMapperPrivate::labelModelIndex(int slicePos)
191 {
191 {
192 if (m_count != -1 && slicePos >= m_count)
192 if (m_count != -1 && slicePos >= m_count)
193 return QModelIndex(); // invalid
193 return QModelIndex(); // invalid
194
194
195 if (m_orientation == Qt::Vertical)
195 if (m_orientation == Qt::Vertical)
196 return m_model->index(slicePos + m_first, m_labelsIndex);
196 return m_model->index(slicePos + m_first, m_labelsIndex);
197 else
197 else
198 return m_model->index(m_labelsIndex, slicePos + m_first);
198 return m_model->index(m_labelsIndex, slicePos + m_first);
199 }
199 }
200
200
201 void QPieModelMapperPrivate::slicesAdded(QList<QPieSlice*> slices)
201 void QPieModelMapperPrivate::slicesAdded(QList<QPieSlice*> slices)
202 {
202 {
203 if (m_seriesSignalsBlock)
203 if (m_seriesSignalsBlock)
204 return;
204 return;
205
205
206 if (slices.count() == 0)
206 if (slices.count() == 0)
207 return;
207 return;
208
208
209 int firstIndex = m_series->slices().indexOf(slices.at(0));
209 int firstIndex = m_series->slices().indexOf(slices.at(0));
210 if (firstIndex == -1)
210 if (firstIndex == -1)
211 return;
211 return;
212
212
213 if (m_count != -1)
213 if (m_count != -1)
214 m_count += slices.count();
214 m_count += slices.count();
215
215
216 for (int i = firstIndex; i < firstIndex + slices.count(); i++) {
216 for (int i = firstIndex; i < firstIndex + slices.count(); i++) {
217 m_slices.insert(i, slices.at(i - firstIndex));
217 m_slices.insert(i, slices.at(i - firstIndex));
218 connect(slices.at(i - firstIndex), SIGNAL(labelChanged()), this, SLOT(sliceLabelChanged()));
218 connect(slices.at(i - firstIndex), SIGNAL(labelChanged()), this, SLOT(sliceLabelChanged()));
219 connect(slices.at(i - firstIndex), SIGNAL(valueChanged()), this, SLOT(sliceValueChanged()));
219 connect(slices.at(i - firstIndex), SIGNAL(valueChanged()), this, SLOT(sliceValueChanged()));
220 }
220 }
221
221
222 blockModelSignals();
222 blockModelSignals();
223 if (m_orientation == Qt::Vertical)
223 if (m_orientation == Qt::Vertical)
224 m_model->insertRows(firstIndex + m_first, slices.count());
224 m_model->insertRows(firstIndex + m_first, slices.count());
225 else
225 else
226 m_model->insertColumns(firstIndex + m_first, slices.count());
226 m_model->insertColumns(firstIndex + m_first, slices.count());
227
227
228 for(int i = firstIndex; i < firstIndex + slices.count(); i++) {
228 for(int i = firstIndex; i < firstIndex + slices.count(); i++) {
229 m_model->setData(valueModelIndex(i), slices.at(i - firstIndex)->value());
229 m_model->setData(valueModelIndex(i), slices.at(i - firstIndex)->value());
230 m_model->setData(labelModelIndex(i), slices.at(i - firstIndex)->label());
230 m_model->setData(labelModelIndex(i), slices.at(i - firstIndex)->label());
231 }
231 }
232 blockModelSignals(false);
232 blockModelSignals(false);
233 }
233 }
234
234
235 void QPieModelMapperPrivate::slicesRemoved(QList<QPieSlice*> slices)
235 void QPieModelMapperPrivate::slicesRemoved(QList<QPieSlice*> slices)
236 {
236 {
237 if (m_seriesSignalsBlock)
237 if (m_seriesSignalsBlock)
238 return;
238 return;
239
239
240 if (slices.count() == 0)
240 if (slices.count() == 0)
241 return;
241 return;
242
242
243 int firstIndex = m_slices.indexOf(slices.at(0));
243 int firstIndex = m_slices.indexOf(slices.at(0));
244 if (firstIndex == -1)
244 if (firstIndex == -1)
245 return;
245 return;
246
246
247 if (m_count != -1)
247 if (m_count != -1)
248 m_count -= slices.count();
248 m_count -= slices.count();
249
249
250 for (int i = firstIndex + slices.count() - 1; i >= firstIndex; i--)
250 for (int i = firstIndex + slices.count() - 1; i >= firstIndex; i--)
251 m_slices.removeAt(i);
251 m_slices.removeAt(i);
252
252
253 blockModelSignals();
253 blockModelSignals();
254 if (m_orientation == Qt::Vertical)
254 if (m_orientation == Qt::Vertical)
255 m_model->removeRows(firstIndex + m_first, slices.count());
255 m_model->removeRows(firstIndex + m_first, slices.count());
256 else
256 else
257 m_model->removeColumns(firstIndex + m_first, slices.count());
257 m_model->removeColumns(firstIndex + m_first, slices.count());
258 blockModelSignals(false);
258 blockModelSignals(false);
259 }
259 }
260
260
261 void QPieModelMapperPrivate::sliceLabelChanged()
261 void QPieModelMapperPrivate::sliceLabelChanged()
262 {
262 {
263 if (m_seriesSignalsBlock)
263 if (m_seriesSignalsBlock)
264 return;
264 return;
265
265
266 blockModelSignals();
266 blockModelSignals();
267 QPieSlice *slice = qobject_cast<QPieSlice *>(QObject::sender());
267 QPieSlice *slice = qobject_cast<QPieSlice *>(QObject::sender());
268 m_model->setData(labelModelIndex(m_series->slices().indexOf(slice)), slice->label());
268 m_model->setData(labelModelIndex(m_series->slices().indexOf(slice)), slice->label());
269 blockModelSignals(false);
269 blockModelSignals(false);
270 }
270 }
271
271
272 void QPieModelMapperPrivate::sliceValueChanged()
272 void QPieModelMapperPrivate::sliceValueChanged()
273 {
273 {
274 if (m_seriesSignalsBlock)
274 if (m_seriesSignalsBlock)
275 return;
275 return;
276
276
277 blockModelSignals();
277 blockModelSignals();
278 QPieSlice *slice = qobject_cast<QPieSlice *>(QObject::sender());
278 QPieSlice *slice = qobject_cast<QPieSlice *>(QObject::sender());
279 m_model->setData(valueModelIndex(m_series->slices().indexOf(slice)), slice->value());
279 m_model->setData(valueModelIndex(m_series->slices().indexOf(slice)), slice->value());
280 blockModelSignals(false);
280 blockModelSignals(false);
281 }
281 }
282
282
283 void QPieModelMapperPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
283 void QPieModelMapperPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
284 {
284 {
285 if (m_modelSignalsBlock)
285 if (m_modelSignalsBlock)
286 return;
286 return;
287
287
288 blockSeriesSignals();
288 blockSeriesSignals();
289 QModelIndex index;
289 QModelIndex index;
290 QPieSlice *slice;
290 QPieSlice *slice;
291 for (int row = topLeft.row(); row <= bottomRight.row(); row++) {
291 for (int row = topLeft.row(); row <= bottomRight.row(); row++) {
292 for (int column = topLeft.column(); column <= bottomRight.column(); column++) {
292 for (int column = topLeft.column(); column <= bottomRight.column(); column++) {
293 index = topLeft.sibling(row, column);
293 index = topLeft.sibling(row, column);
294 slice = pieSlice(index);
294 slice = pieSlice(index);
295 if (slice) {
295 if (slice) {
296 slice->setValue(m_model->data(index, Qt::DisplayRole).toReal());
296 slice->setValue(m_model->data(index, Qt::DisplayRole).toReal());
297 slice->setLabel(m_model->data(index, Qt::DisplayRole).toString());
297 slice->setLabel(m_model->data(index, Qt::DisplayRole).toString());
298 }
298 }
299 }
299 }
300 }
300 }
301 blockSeriesSignals(false);
301 blockSeriesSignals(false);
302 }
302 }
303
303
304
304
305 void QPieModelMapperPrivate::modelRowsAdded(QModelIndex parent, int start, int end)
305 void QPieModelMapperPrivate::modelRowsAdded(QModelIndex parent, int start, int end)
306 {
306 {
307 Q_UNUSED(parent);
307 Q_UNUSED(parent);
308 if (m_modelSignalsBlock)
308 if (m_modelSignalsBlock)
309 return;
309 return;
310
310
311 blockSeriesSignals();
311 blockSeriesSignals();
312 if (m_orientation == Qt::Vertical)
312 if (m_orientation == Qt::Vertical)
313 insertData(start, end);
313 insertData(start, end);
314 else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie
314 else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie
315 initializePieFromModel();
315 initializePieFromModel();
316 blockSeriesSignals(false);
316 blockSeriesSignals(false);
317 }
317 }
318
318
319 void QPieModelMapperPrivate::modelRowsRemoved(QModelIndex parent, int start, int end)
319 void QPieModelMapperPrivate::modelRowsRemoved(QModelIndex parent, int start, int end)
320 {
320 {
321 Q_UNUSED(parent);
321 Q_UNUSED(parent);
322 if (m_modelSignalsBlock)
322 if (m_modelSignalsBlock)
323 return;
323 return;
324
324
325 blockSeriesSignals();
325 blockSeriesSignals();
326 if (m_orientation == Qt::Vertical)
326 if (m_orientation == Qt::Vertical)
327 removeData(start, end);
327 removeData(start, end);
328 else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie
328 else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie
329 initializePieFromModel();
329 initializePieFromModel();
330 blockSeriesSignals(false);
330 blockSeriesSignals(false);
331 }
331 }
332
332
333 void QPieModelMapperPrivate::modelColumnsAdded(QModelIndex parent, int start, int end)
333 void QPieModelMapperPrivate::modelColumnsAdded(QModelIndex parent, int start, int end)
334 {
334 {
335 Q_UNUSED(parent);
335 Q_UNUSED(parent);
336 if (m_modelSignalsBlock)
336 if (m_modelSignalsBlock)
337 return;
337 return;
338
338
339 blockSeriesSignals();
339 blockSeriesSignals();
340 if (m_orientation == Qt::Horizontal)
340 if (m_orientation == Qt::Horizontal)
341 insertData(start, end);
341 insertData(start, end);
342 else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie
342 else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie
343 initializePieFromModel();
343 initializePieFromModel();
344 blockSeriesSignals(false);
344 blockSeriesSignals(false);
345 }
345 }
346
346
347 void QPieModelMapperPrivate::modelColumnsRemoved(QModelIndex parent, int start, int end)
347 void QPieModelMapperPrivate::modelColumnsRemoved(QModelIndex parent, int start, int end)
348 {
348 {
349 Q_UNUSED(parent);
349 Q_UNUSED(parent);
350 if (m_modelSignalsBlock)
350 if (m_modelSignalsBlock)
351 return;
351 return;
352
352
353 blockSeriesSignals();
353 blockSeriesSignals();
354 if (m_orientation == Qt::Horizontal)
354 if (m_orientation == Qt::Horizontal)
355 removeData(start, end);
355 removeData(start, end);
356 else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie
356 else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie
357 initializePieFromModel();
357 initializePieFromModel();
358 blockSeriesSignals(false);
358 blockSeriesSignals(false);
359 }
359 }
360
360
361 void QPieModelMapperPrivate::insertData(int start, int end)
361 void QPieModelMapperPrivate::insertData(int start, int end)
362 {
362 {
363 if (m_count != -1 && start >= m_first + m_count) {
363 if (m_count != -1 && start >= m_first + m_count) {
364 return;
364 return;
365 } else {
365 } else {
366 int addedCount = end - start + 1;
366 int addedCount = end - start + 1;
367 if (m_count != -1 && addedCount > m_count)
367 if (m_count != -1 && addedCount > m_count)
368 addedCount = m_count;
368 addedCount = m_count;
369 int first = qMax(start, m_first);
369 int first = qMax(start, m_first);
370 int last = qMin(first + addedCount - 1, m_orientation == Qt::Vertical ? m_model->rowCount() - 1 : m_model->columnCount() - 1);
370 int last = qMin(first + addedCount - 1, m_orientation == Qt::Vertical ? m_model->rowCount() - 1 : m_model->columnCount() - 1);
371 for (int i = first; i <= last; i++) {
371 for (int i = first; i <= last; i++) {
372 QPieSlice *slice = new QPieSlice;
372 QPieSlice *slice = new QPieSlice;
373 slice->setValue(m_model->data(valueModelIndex(i - m_first), Qt::DisplayRole).toDouble());
373 slice->setValue(m_model->data(valueModelIndex(i - m_first), Qt::DisplayRole).toDouble());
374 slice->setLabel(m_model->data(labelModelIndex(i - m_first), Qt::DisplayRole).toString());
374 slice->setLabel(m_model->data(labelModelIndex(i - m_first), Qt::DisplayRole).toString());
375 slice->setLabelVisible();
375 slice->setLabelVisible();
376 connect(slice, SIGNAL(labelChanged()), this, SLOT(sliceLabelChanged()));
376 connect(slice, SIGNAL(labelChanged()), this, SLOT(sliceLabelChanged()));
377 connect(slice, SIGNAL(valueChanged()), this, SLOT(sliceValueChanged()));
377 connect(slice, SIGNAL(valueChanged()), this, SLOT(sliceValueChanged()));
378 m_series->insert(i - m_first, slice);
378 m_series->insert(i - m_first, slice);
379 m_slices.insert(i - m_first, slice);
379 m_slices.insert(i - m_first, slice);
380 }
380 }
381
381
382 // remove excess of slices (abouve m_count)
382 // remove excess of slices (abouve m_count)
383 if (m_count != -1 && m_series->slices().size() > m_count)
383 if (m_count != -1 && m_series->slices().size() > m_count)
384 for (int i = m_series->slices().size() - 1; i >= m_count; i--) {
384 for (int i = m_series->slices().size() - 1; i >= m_count; i--) {
385 m_series->remove(m_series->slices().at(i));
385 m_series->remove(m_series->slices().at(i));
386 m_slices.removeAt(i);
386 m_slices.removeAt(i);
387 }
387 }
388 }
388 }
389 }
389 }
390
390
391 void QPieModelMapperPrivate::removeData(int start, int end)
391 void QPieModelMapperPrivate::removeData(int start, int end)
392 {
392 {
393 int removedCount = end - start + 1;
393 int removedCount = end - start + 1;
394 if (m_count != -1 && start >= m_first + m_count) {
394 if (m_count != -1 && start >= m_first + m_count) {
395 return;
395 return;
396 } else {
396 } else {
397 int toRemove = qMin(m_series->slices().size(), removedCount); // first find how many items can actually be removed
397 int toRemove = qMin(m_series->slices().size(), removedCount); // first find how many items can actually be removed
398 int first = qMax(start, m_first); // get the index of the first item that will be removed.
398 int first = qMax(start, m_first); // get the index of the first item that will be removed.
399 int last = qMin(first + toRemove - 1, m_series->slices().size() + m_first - 1); // get the index of the last item that will be removed.
399 int last = qMin(first + toRemove - 1, m_series->slices().size() + m_first - 1); // get the index of the last item that will be removed.
400 for (int i = last; i >= first; i--) {
400 for (int i = last; i >= first; i--) {
401 m_series->remove(m_series->slices().at(i - m_first));
401 m_series->remove(m_series->slices().at(i - m_first));
402 m_slices.removeAt(i - m_first);
402 m_slices.removeAt(i - m_first);
403 }
403 }
404
404
405 if (m_count != -1) {
405 if (m_count != -1) {
406 int itemsAvailable; // check how many are available to be added
406 int itemsAvailable; // check how many are available to be added
407 if (m_orientation == Qt::Vertical)
407 if (m_orientation == Qt::Vertical)
408 itemsAvailable = m_model->rowCount() - m_first - m_series->slices().size();
408 itemsAvailable = m_model->rowCount() - m_first - m_series->slices().size();
409 else
409 else
410 itemsAvailable = m_model->columnCount() - m_first - m_series->slices().size();
410 itemsAvailable = m_model->columnCount() - m_first - m_series->slices().size();
411 int toBeAdded = qMin(itemsAvailable, m_count - m_series->slices().size()); // add not more items than there is space left to be filled.
411 int toBeAdded = qMin(itemsAvailable, m_count - m_series->slices().size()); // add not more items than there is space left to be filled.
412 int currentSize = m_series->slices().size();
412 int currentSize = m_series->slices().size();
413 if (toBeAdded > 0)
413 if (toBeAdded > 0)
414 for (int i = m_series->slices().size(); i < currentSize + toBeAdded; i++) {
414 for (int i = m_series->slices().size(); i < currentSize + toBeAdded; i++) {
415 QPieSlice *slice = new QPieSlice;
415 QPieSlice *slice = new QPieSlice;
416 if (m_orientation == Qt::Vertical) {
416 if (m_orientation == Qt::Vertical) {
417 slice->setValue(m_model->data(m_model->index(i + m_first, m_valuesIndex), Qt::DisplayRole).toDouble());
417 slice->setValue(m_model->data(m_model->index(i + m_first, m_valuesIndex), Qt::DisplayRole).toDouble());
418 slice->setLabel(m_model->data(m_model->index(i + m_first, m_labelsIndex), Qt::DisplayRole).toString());
418 slice->setLabel(m_model->data(m_model->index(i + m_first, m_labelsIndex), Qt::DisplayRole).toString());
419 } else {
419 } else {
420 slice->setValue(m_model->data(m_model->index(m_valuesIndex, i + m_first), Qt::DisplayRole).toDouble());
420 slice->setValue(m_model->data(m_model->index(m_valuesIndex, i + m_first), Qt::DisplayRole).toDouble());
421 slice->setLabel(m_model->data(m_model->index(m_labelsIndex, i + m_first), Qt::DisplayRole).toString());
421 slice->setLabel(m_model->data(m_model->index(m_labelsIndex, i + m_first), Qt::DisplayRole).toString());
422 }
422 }
423 slice->setLabelVisible();
423 slice->setLabelVisible();
424 m_series->insert(i, slice);
424 m_series->insert(i, slice);
425 m_slices.insert(i, slice);
425 m_slices.insert(i, slice);
426 }
426 }
427 }
427 }
428 }
428 }
429 }
429 }
430
430
431 void QPieModelMapperPrivate::initializePieFromModel()
431 void QPieModelMapperPrivate::initializePieFromModel()
432 {
432 {
433 if (m_model == 0 || m_series == 0)
433 if (m_model == 0 || m_series == 0)
434 return;
434 return;
435
435
436 blockSeriesSignals();
436 blockSeriesSignals();
437 // clear current content
437 // clear current content
438 m_series->clear();
438 m_series->clear();
439 m_slices.clear();
439 m_slices.clear();
440
440
441 // create the initial slices set
441 // create the initial slices set
442 int slicePos = 0;
442 int slicePos = 0;
443 QModelIndex valueIndex = valueModelIndex(slicePos);
443 QModelIndex valueIndex = valueModelIndex(slicePos);
444 QModelIndex labelIndex = labelModelIndex(slicePos);
444 QModelIndex labelIndex = labelModelIndex(slicePos);
445 while (valueIndex.isValid() && labelIndex.isValid()) {
445 while (valueIndex.isValid() && labelIndex.isValid()) {
446 QPieSlice *slice = new QPieSlice;
446 QPieSlice *slice = new QPieSlice;
447 slice->setLabel(m_model->data(labelIndex, Qt::DisplayRole).toString());
447 slice->setLabel(m_model->data(labelIndex, Qt::DisplayRole).toString());
448 slice->setValue(m_model->data(valueIndex, Qt::DisplayRole).toDouble());
448 slice->setValue(m_model->data(valueIndex, Qt::DisplayRole).toDouble());
449 connect(slice, SIGNAL(labelChanged()), this, SLOT(sliceLabelChanged()));
449 connect(slice, SIGNAL(labelChanged()), this, SLOT(sliceLabelChanged()));
450 connect(slice, SIGNAL(valueChanged()), this, SLOT(sliceValueChanged()));
450 connect(slice, SIGNAL(valueChanged()), this, SLOT(sliceValueChanged()));
451 m_series->append(slice);
451 m_series->append(slice);
452 m_slices.append(slice);
452 m_slices.append(slice);
453 // m_series->append(m_model->data(labelIndex, Qt::DisplayRole).toString(), m_model->data(valueIndex, Qt::DisplayRole).toDouble());
453 // m_series->append(m_model->data(labelIndex, Qt::DisplayRole).toString(), m_model->data(valueIndex, Qt::DisplayRole).toDouble());
454 slicePos++;
454 slicePos++;
455 valueIndex = valueModelIndex(slicePos);
455 valueIndex = valueModelIndex(slicePos);
456 labelIndex = labelModelIndex(slicePos);
456 labelIndex = labelModelIndex(slicePos);
457 }
457 }
458 m_series->setLabelsVisible(true);
458 m_series->setLabelsVisible(true);
459 blockSeriesSignals(false);
459 blockSeriesSignals(false);
460 }
460 }
461
461
462 #include "moc_qpiemodelmapper_p.cpp"
462 #include "moc_qpiemodelmapper_p.cpp"
463 #include "moc_qpiemodelmapper.cpp"
463 #include "moc_qpiemodelmapper.cpp"
464
464
465 QTCOMMERCIALCHART_END_NAMESPACE
465 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,76 +1,78
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 QPIEMODELMAPPER_H
21 #ifndef QPIEMODELMAPPER_H
22 #define QPIEMODELMAPPER_H
22 #define QPIEMODELMAPPER_H
23
23
24 #include "qchartglobal.h"
24 #include "qchartglobal.h"
25 #include <QObject>
25 #include <QObject>
26
26
27 class QAbstractItemModel;
27 class QAbstractItemModel;
28
28
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30
30
31 class QPieModelMapperPrivate;
31 class QPieModelMapperPrivate;
32 class QPieSeries;
32 class QPieSeries;
33
33
34 class QTCOMMERCIALCHART_EXPORT QPieModelMapper : public QObject
34 class QTCOMMERCIALCHART_EXPORT QPieModelMapper : public QObject
35 {
35 {
36 Q_OBJECT
36 Q_OBJECT
37 Q_PROPERTY(QPieSeries *series READ series WRITE setSeries)
38 Q_PROPERTY(QAbstractItemModel *model READ model WRITE setModel)
37 Q_PROPERTY(int first READ first WRITE setFirst)
39 Q_PROPERTY(int first READ first WRITE setFirst)
38 Q_PROPERTY(int count READ count WRITE setCount)
40 Q_PROPERTY(int count READ count WRITE setCount)
39 Q_ENUMS(Qt::Orientation)
41 Q_ENUMS(Qt::Orientation)
40
42
41 protected:
43 protected:
42 QPieModelMapper(QObject *parent = 0);
44 QPieModelMapper(QObject *parent = 0);
43
45
44 public:
46 public:
45 QAbstractItemModel* model() const;
47 QAbstractItemModel* model() const;
46 void setModel(QAbstractItemModel *model);
48 void setModel(QAbstractItemModel *model);
47
49
48 QPieSeries* series() const;
50 QPieSeries* series() const;
49 void setSeries(QPieSeries *series);
51 void setSeries(QPieSeries *series);
50
52
51 int first() const;
53 int first() const;
52 void setFirst(int first);
54 void setFirst(int first);
53
55
54 int count() const;
56 int count() const;
55 void setCount(int count);
57 void setCount(int count);
56
58
57 void reset();
59 void reset();
58
60
59 protected:
61 protected:
60 int valuesIndex() const;
62 int valuesIndex() const;
61 void setValuesIndex(int valuesIndex);
63 void setValuesIndex(int valuesIndex);
62
64
63 int labelsIndex() const;
65 int labelsIndex() const;
64 void setLabelsIndex(int labelsIndex);
66 void setLabelsIndex(int labelsIndex);
65
67
66 Qt::Orientation orientation() const;
68 Qt::Orientation orientation() const;
67 void setOrientation(Qt::Orientation orientation);
69 void setOrientation(Qt::Orientation orientation);
68
70
69 protected:
71 protected:
70 QPieModelMapperPrivate * const d_ptr;
72 QPieModelMapperPrivate * const d_ptr;
71 Q_DECLARE_PRIVATE(QPieModelMapper)
73 Q_DECLARE_PRIVATE(QPieModelMapper)
72 };
74 };
73
75
74 QTCOMMERCIALCHART_END_NAMESPACE
76 QTCOMMERCIALCHART_END_NAMESPACE
75
77
76 #endif // QPIEMODELMAPPER_H
78 #endif // QPIEMODELMAPPER_H
@@ -1,70 +1,70
1 #ifndef QPIEMODELMAPPER_P_H
1 #ifndef QPIEMODELMAPPER_P_H
2 #define QPIEMODELMAPPER_P_H
2 #define QPIEMODELMAPPER_P_H
3
3
4 #include "qpiemodelmapper.h"
4 #include "qpiemodelmapper.h"
5 #include <QObject>
5 #include <QObject>
6
6
7 class QModelIndex;
7 class QModelIndex;
8 class QAbstractItemModel;
8 class QAbstractItemModel;
9
9
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11
11
12 class QPieModelMapper;
12 class QPieModelMapper;
13 class QPieSeries;
13 class QPieSeries;
14 class QPieSlice;
14 class QPieSlice;
15
15
16 class QPieModelMapperPrivate : public QObject
16 class QPieModelMapperPrivate : public QObject
17 {
17 {
18 Q_OBJECT
18 Q_OBJECT
19
19
20 public:
20 public:
21 QPieModelMapperPrivate(QPieModelMapper *q);
21 QPieModelMapperPrivate(QPieModelMapper *q);
22
22
23 public Q_SLOTS:
23 public Q_SLOTS:
24 // for the model
24 // for the model
25 void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight);
25 void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight);
26 void modelRowsAdded(QModelIndex parent, int start, int end);
26 void modelRowsAdded(QModelIndex parent, int start, int end);
27 void modelRowsRemoved(QModelIndex parent, int start, int end);
27 void modelRowsRemoved(QModelIndex parent, int start, int end);
28 void modelColumnsAdded(QModelIndex parent, int start, int end);
28 void modelColumnsAdded(QModelIndex parent, int start, int end);
29 void modelColumnsRemoved(QModelIndex parent, int start, int end);
29 void modelColumnsRemoved(QModelIndex parent, int start, int end);
30
30
31 // for the series
31 // for the series
32 void slicesAdded(QList<QPieSlice*> slices);
32 void slicesAdded(QList<QPieSlice*> slices);
33 void slicesRemoved(QList<QPieSlice*> slices);
33 void slicesRemoved(QList<QPieSlice*> slices);
34 void sliceLabelChanged();
34 void sliceLabelChanged();
35 void sliceValueChanged();
35 void sliceValueChanged();
36
36
37 void initializePieFromModel();
37 void initializePieFromModel();
38
38
39 private:
39 private:
40 QPieSlice* pieSlice(QModelIndex index) const;
40 QPieSlice* pieSlice(QModelIndex index) const;
41 QModelIndex valueModelIndex(int slicePos);
41 QModelIndex valueModelIndex(int slicePos);
42 QModelIndex labelModelIndex(int slicePos);
42 QModelIndex labelModelIndex(int slicePos);
43 void insertData(int start, int end);
43 void insertData(int start, int end);
44 void removeData(int start, int end);
44 void removeData(int start, int end);
45
45
46 void blockModelSignals(bool block = true);
46 void blockModelSignals(bool block = true);
47 void blockSeriesSignals(bool block = true);
47 void blockSeriesSignals(bool block = true);
48
48
49 private:
49 private:
50 bool m_seriesSignalsBlock;
51 bool m_modelSignalsBlock;
52 QPieSeries *m_series;
50 QPieSeries *m_series;
53 QList<QPieSlice*> m_slices;
51 QList<QPieSlice*> m_slices;
54 QAbstractItemModel *m_model;
52 QAbstractItemModel *m_model;
55 int m_first;
53 int m_first;
56 int m_count;
54 int m_count;
57 Qt::Orientation m_orientation;
55 Qt::Orientation m_orientation;
58 int m_valuesIndex;
56 int m_valuesIndex;
59 int m_labelsIndex;
57 int m_labelsIndex;
58 bool m_seriesSignalsBlock;
59 bool m_modelSignalsBlock;
60
60
61 private:
61 private:
62
62
63 QPieModelMapper *q_ptr;
63 QPieModelMapper *q_ptr;
64 Q_DECLARE_PUBLIC(QPieModelMapper)
64 Q_DECLARE_PUBLIC(QPieModelMapper)
65 friend class QPieSeriesPrivate;
65 friend class QPieSeriesPrivate;
66 };
66 };
67
67
68 QTCOMMERCIALCHART_END_NAMESPACE
68 QTCOMMERCIALCHART_END_NAMESPACE
69
69
70 #endif // QPIEMODELMAPPER_P_H
70 #endif // QPIEMODELMAPPER_P_H
General Comments 0
You need to be logged in to leave comments. Login now