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