##// END OF EJS Templates
Added docs for NOTIFY signals of the PieModelMapper
Marek Rosa -
r1476:ef9c1b689dd4
parent child
Show More
@@ -1,101 +1,113
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 "qhpiemodelmapper.h"
21 #include "qhpiemodelmapper.h"
22
22
23 QTCOMMERCIALCHART_BEGIN_NAMESPACE
23 QTCOMMERCIALCHART_BEGIN_NAMESPACE
24
24
25 /*!
25 /*!
26 \class QHPieModelMapper
26 \class QHPieModelMapper
27 \brief part of QtCommercial chart API.
27 \brief part of QtCommercial chart API.
28 \mainclass
28 \mainclass
29
29
30 Model mappers allow you to use QAbstractItemModel derived models as a data source for a chart series.
30 Model mappers allow you to use QAbstractItemModel derived models as a data source for a chart series.
31 Horizontal model mapper is used to create a connection between QPieSeries and QAbstractItemModel derived model object that keeps the consecutive pie slices data in rows.
31 Horizontal model mapper is used to create a connection between QPieSeries and QAbstractItemModel derived model object that keeps the consecutive pie slices data in rows.
32 It is possible to use both QAbstractItemModel and QPieSeries model API. QHPieModelMapper makes sure that Pie and the model are kept in sync.
32 It is possible to use both QAbstractItemModel and QPieSeries model API. QHPieModelMapper makes sure that Pie and the model are kept in sync.
33 NOTE: used model has to support adding/removing rows/columns and modifying the data of the cells.
33 NOTE: used model has to support adding/removing rows/columns and modifying the data of the cells.
34 */
34 */
35
35
36 /*!
36 /*!
37 \property QHPieModelMapper::valuesRow
37 \property QHPieModelMapper::valuesRow
38 \brief Defines which row of the model is kept in sync with the values of the pie's slices
38 \brief Defines which row of the model is kept in sync with the values of the pie's slices
39
39
40 Default value is: -1 (invalid mapping)
40 Default value is: -1 (invalid mapping)
41 */
41 */
42
42
43 /*!
43 /*!
44 \property QHPieModelMapper::labelsRow
44 \property QHPieModelMapper::labelsRow
45 \brief Defines which row of the model is kept in sync with the labels of the pie's slices
45 \brief Defines which row of the model is kept in sync with the labels of the pie's slices
46
46
47 Default value is: -1 (invalid mapping)
47 Default value is: -1 (invalid mapping)
48 */
48 */
49
49
50 /*!
50 /*!
51 \fn void QHPieModelMapper::valuesRowChanged()
52
53 Emitted when the valuesRow has changed.
54 */
55
56 /*!
57 \fn void QHPieModelMapper::labelsRowChanged()
58
59 Emitted when the labelsRow has changed.
60 */
61
62 /*!
51 Constructs a mapper object which is a child of \a parent.
63 Constructs a mapper object which is a child of \a parent.
52 */
64 */
53 QHPieModelMapper::QHPieModelMapper(QObject *parent) :
65 QHPieModelMapper::QHPieModelMapper(QObject *parent) :
54 QPieModelMapper(parent)
66 QPieModelMapper(parent)
55 {
67 {
56 QPieModelMapper::setOrientation(Qt::Horizontal);
68 QPieModelMapper::setOrientation(Qt::Horizontal);
57 }
69 }
58
70
59 /*!
71 /*!
60 Returns which row of the model is kept in sync with the values of the pie's slices
72 Returns which row of the model is kept in sync with the values of the pie's slices
61 */
73 */
62 int QHPieModelMapper::valuesRow() const
74 int QHPieModelMapper::valuesRow() const
63 {
75 {
64 return QPieModelMapper::valuesSection();
76 return QPieModelMapper::valuesSection();
65 }
77 }
66
78
67 /*!
79 /*!
68 Sets the model row that is kept in sync with the pie slices values.
80 Sets the model row that is kept in sync with the pie slices values.
69 Parameter \a valuesRow specifies the row of the model.
81 Parameter \a valuesRow specifies the row of the model.
70 */
82 */
71 void QHPieModelMapper::setValuesRow(int valuesRow)
83 void QHPieModelMapper::setValuesRow(int valuesRow)
72 {
84 {
73 if (valuesRow != valuesSection()) {
85 if (valuesRow != valuesSection()) {
74 QPieModelMapper::setValuesSection(valuesRow);
86 QPieModelMapper::setValuesSection(valuesRow);
75 emit valuesRowChanged();
87 emit valuesRowChanged();
76 }
88 }
77 }
89 }
78
90
79 /*!
91 /*!
80 Returns which row of the model is kept in sync with the labels of the pie's slices
92 Returns which row of the model is kept in sync with the labels of the pie's slices
81 */
93 */
82 int QHPieModelMapper::labelsRow() const
94 int QHPieModelMapper::labelsRow() const
83 {
95 {
84 return QPieModelMapper::labelsSection();
96 return QPieModelMapper::labelsSection();
85 }
97 }
86
98
87 /*!
99 /*!
88 Sets the model row that is kept in sync with the pie's slices labels.
100 Sets the model row that is kept in sync with the pie's slices labels.
89 Parameter \a labelsRow specifies the row of the model.
101 Parameter \a labelsRow specifies the row of the model.
90 */
102 */
91 void QHPieModelMapper::setLabelsRow(int labelsRow)
103 void QHPieModelMapper::setLabelsRow(int labelsRow)
92 {
104 {
93 if (labelsRow != labelsSection()) {
105 if (labelsRow != labelsSection()) {
94 QPieModelMapper::setLabelsSection(labelsRow);
106 QPieModelMapper::setLabelsSection(labelsRow);
95 emit labelsRowChanged();
107 emit labelsRowChanged();
96 }
108 }
97 }
109 }
98
110
99 #include "moc_qhpiemodelmapper.cpp"
111 #include "moc_qhpiemodelmapper.cpp"
100
112
101 QTCOMMERCIALCHART_END_NAMESPACE
113 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,605 +1,629
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 "qpiemodelmapper_p.h"
21 #include "qpiemodelmapper_p.h"
22 #include "qpiemodelmapper.h"
22 #include "qpiemodelmapper.h"
23 #include "qpieseries.h"
23 #include "qpieseries.h"
24 #include "qpieslice.h"
24 #include "qpieslice.h"
25 #include <QAbstractItemModel>
25 #include <QAbstractItemModel>
26
26
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28
28
29 /*!
29 /*!
30 \class QPieModelMapper
30 \class QPieModelMapper
31 \brief part of QtCommercial chart API.
31 \brief part of QtCommercial chart API.
32 \mainclass
32 \mainclass
33
33
34 Model mappers allow you to use QAbstractItemModel derived models as a data source for a chart series.
34 Model mappers allow you to use QAbstractItemModel derived models as a data source for a chart series.
35 The instance of this class cannot be created directly. QHPieModelMapper of QVPieModelMapper should be used instead. This class is used to create a connection between QPieSeries and QAbstractItemModel derived model object.
35 The instance of this class cannot be created directly. QHPieModelMapper of QVPieModelMapper should be used instead. This class is used to create a connection between QPieSeries and QAbstractItemModel derived model object.
36 It is possible to use both QAbstractItemModel and QPieSeries model API. QPieModelMapper makes sure that Pie and the model are kept in sync.
36 It is possible to use both QAbstractItemModel and QPieSeries model API. QPieModelMapper makes sure that Pie and the model are kept in sync.
37 NOTE: used model has to support adding/removing rows/columns and modifying the data of the cells.
37 NOTE: used model has to support adding/removing rows/columns and modifying the data of the cells.
38 */
38 */
39
39
40 /*!
40 /*!
41 \property QPieModelMapper::series
41 \property QPieModelMapper::series
42 \brief Defines the QPieSeries object that is used by the mapper.
42 \brief Defines the QPieSeries object that is used by the mapper.
43
43
44 All the data in the series is discarded when it is set to the mapper.
44 All the data in the series is discarded when it is set to the mapper.
45 When new series is specified the old series is disconnected (it preserves its data)
45 When new series is specified the old series is disconnected (it preserves its data)
46 */
46 */
47
47
48 /*!
48 /*!
49 \property QPieModelMapper::model
49 \property QPieModelMapper::model
50 \brief Defines the model that is used by the mapper.
50 \brief Defines the model that is used by the mapper.
51 */
51 */
52
52
53 /*!
53 /*!
54 \property QPieModelMapper::first
54 \property QPieModelMapper::first
55 \brief Defines which item of the model's row/column should be mapped as the value/label of the first slice of the pie
55 \brief Defines which item of the model's row/column should be mapped as the value/label of the first slice of the pie
56
56
57 Minimal and default value is: 0
57 Minimal and default value is: 0
58 */
58 */
59
59
60 /*!
60 /*!
61 \property QPieModelMapper::count
61 \property QPieModelMapper::count
62 \brief Defines the number of rows/columns of the model that are mapped as the data for the pie.
62 \brief Defines the number of rows/columns of the model that are mapped as the data for the pie.
63
63
64 Minimal and default value is: -1 (count limited by the number of rows/columns in the model)
64 Minimal and default value is: -1 (count limited by the number of rows/columns in the model)
65 */
65 */
66
66
67 /*!
67 /*!
68 \fn void QPieModelMapper::seriesReplaced()
69
70 Emitted when the series to which mapper is connected to has changed.
71 */
72
73 /*!
74 \fn void QPieModelMapper::modelReplaced()
75
76 Emitted when the model to which mapper is connected to has changed.
77 */
78
79 /*!
80 \fn void QPieModelMapper::firstChanged()
81
82 Emitted when the value for the first has changed.
83 */
84
85 /*!
86 \fn void QPieModelMapper::countChanged()
87
88 Emitted when the value for the count has changed.
89 */
90
91 /*!
68 Constructs a mapper object which is a child of \a parent.
92 Constructs a mapper object which is a child of \a parent.
69 */
93 */
70 QPieModelMapper::QPieModelMapper(QObject *parent) :
94 QPieModelMapper::QPieModelMapper(QObject *parent) :
71 QObject(parent),
95 QObject(parent),
72 d_ptr(new QPieModelMapperPrivate(this))
96 d_ptr(new QPieModelMapperPrivate(this))
73 {
97 {
74 }
98 }
75
99
76 QAbstractItemModel* QPieModelMapper::model() const
100 QAbstractItemModel* QPieModelMapper::model() const
77 {
101 {
78 Q_D(const QPieModelMapper);
102 Q_D(const QPieModelMapper);
79 return d->m_model;
103 return d->m_model;
80 }
104 }
81
105
82 void QPieModelMapper::setModel(QAbstractItemModel *model)
106 void QPieModelMapper::setModel(QAbstractItemModel *model)
83 {
107 {
84 if (model == 0)
108 if (model == 0)
85 return;
109 return;
86
110
87 Q_D(QPieModelMapper);
111 Q_D(QPieModelMapper);
88 if (d->m_model) {
112 if (d->m_model) {
89 disconnect(d->m_model, 0, d, 0);
113 disconnect(d->m_model, 0, d, 0);
90 }
114 }
91
115
92 d->m_model = model;
116 d->m_model = model;
93 d->initializePieFromModel();
117 d->initializePieFromModel();
94 // connect signals from the model
118 // connect signals from the model
95 connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex)));
119 connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex)));
96 connect(d->m_model, SIGNAL(rowsInserted(QModelIndex,int,int)), d, SLOT(modelRowsAdded(QModelIndex,int,int)));
120 connect(d->m_model, SIGNAL(rowsInserted(QModelIndex,int,int)), d, SLOT(modelRowsAdded(QModelIndex,int,int)));
97 connect(d->m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)), d, SLOT(modelRowsRemoved(QModelIndex,int,int)));
121 connect(d->m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)), d, SLOT(modelRowsRemoved(QModelIndex,int,int)));
98 connect(d->m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), d, SLOT(modelColumnsAdded(QModelIndex,int,int)));
122 connect(d->m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), d, SLOT(modelColumnsAdded(QModelIndex,int,int)));
99 connect(d->m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), d, SLOT(modelColumnsRemoved(QModelIndex,int,int)));
123 connect(d->m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), d, SLOT(modelColumnsRemoved(QModelIndex,int,int)));
100
124
101 emit modelReplaced();
125 emit modelReplaced();
102 }
126 }
103
127
104 QPieSeries* QPieModelMapper::series() const
128 QPieSeries* QPieModelMapper::series() const
105 {
129 {
106 Q_D(const QPieModelMapper);
130 Q_D(const QPieModelMapper);
107 return d->m_series;
131 return d->m_series;
108 }
132 }
109
133
110 void QPieModelMapper::setSeries(QPieSeries *series)
134 void QPieModelMapper::setSeries(QPieSeries *series)
111 {
135 {
112 Q_D(QPieModelMapper);
136 Q_D(QPieModelMapper);
113 if (d->m_series) {
137 if (d->m_series) {
114 disconnect(d->m_series, 0, d, 0);
138 disconnect(d->m_series, 0, d, 0);
115 }
139 }
116
140
117 if (series == 0)
141 if (series == 0)
118 return;
142 return;
119
143
120 d->m_series = series;
144 d->m_series = series;
121 d->initializePieFromModel();
145 d->initializePieFromModel();
122 // connect the signals from the series
146 // connect the signals from the series
123 connect(d->m_series, SIGNAL(added(QList<QPieSlice*>)), d, SLOT(slicesAdded(QList<QPieSlice*>)));
147 connect(d->m_series, SIGNAL(added(QList<QPieSlice*>)), d, SLOT(slicesAdded(QList<QPieSlice*>)));
124 connect(d->m_series, SIGNAL(removed(QList<QPieSlice*>)), d, SLOT(slicesRemoved(QList<QPieSlice*>)));
148 connect(d->m_series, SIGNAL(removed(QList<QPieSlice*>)), d, SLOT(slicesRemoved(QList<QPieSlice*>)));
125
149
126 emit seriesReplaced();
150 emit seriesReplaced();
127 }
151 }
128
152
129 int QPieModelMapper::first() const
153 int QPieModelMapper::first() const
130 {
154 {
131 Q_D(const QPieModelMapper);
155 Q_D(const QPieModelMapper);
132 return d->m_first;
156 return d->m_first;
133 }
157 }
134
158
135 void QPieModelMapper::setFirst(int first)
159 void QPieModelMapper::setFirst(int first)
136 {
160 {
137 Q_D(QPieModelMapper);
161 Q_D(QPieModelMapper);
138 if (first != d->m_first) {
162 if (first != d->m_first) {
139 d->m_first = qMax(first, 0);
163 d->m_first = qMax(first, 0);
140 d->initializePieFromModel();
164 d->initializePieFromModel();
141
165
142 emit firstChanged();
166 emit firstChanged();
143 }
167 }
144 }
168 }
145
169
146 int QPieModelMapper::count() const
170 int QPieModelMapper::count() const
147 {
171 {
148 Q_D(const QPieModelMapper);
172 Q_D(const QPieModelMapper);
149 return d->m_count;
173 return d->m_count;
150 }
174 }
151
175
152 void QPieModelMapper::setCount(int count)
176 void QPieModelMapper::setCount(int count)
153 {
177 {
154 Q_D(QPieModelMapper);
178 Q_D(QPieModelMapper);
155 if (count != d->m_count) {
179 if (count != d->m_count) {
156 d->m_count = qMax(count, -1);
180 d->m_count = qMax(count, -1);
157 d->initializePieFromModel();
181 d->initializePieFromModel();
158
182
159 emit countChanged();
183 emit countChanged();
160 }
184 }
161 }
185 }
162
186
163 /*!
187 /*!
164 Returns the orientation that is used when QPieModelMapper accesses the model.
188 Returns the orientation that is used when QPieModelMapper accesses the model.
165 This mean whether the consecutive values/labels of the pie are read from row (Qt::Horizontal)
189 This mean whether the consecutive values/labels of the pie are read from row (Qt::Horizontal)
166 or from columns (Qt::Vertical)
190 or from columns (Qt::Vertical)
167 */
191 */
168 Qt::Orientation QPieModelMapper::orientation() const
192 Qt::Orientation QPieModelMapper::orientation() const
169 {
193 {
170 Q_D(const QPieModelMapper);
194 Q_D(const QPieModelMapper);
171 return d->m_orientation;
195 return d->m_orientation;
172 }
196 }
173
197
174 /*!
198 /*!
175 Returns the \a orientation that is used when QPieModelMapper accesses the model.
199 Returns the \a orientation that is used when QPieModelMapper accesses the model.
176 This mean whether the consecutive values/labels of the pie are read from row (Qt::Horizontal)
200 This mean whether the consecutive values/labels of the pie are read from row (Qt::Horizontal)
177 or from columns (Qt::Vertical)
201 or from columns (Qt::Vertical)
178 */
202 */
179 void QPieModelMapper::setOrientation(Qt::Orientation orientation)
203 void QPieModelMapper::setOrientation(Qt::Orientation orientation)
180 {
204 {
181 Q_D(QPieModelMapper);
205 Q_D(QPieModelMapper);
182 d->m_orientation = orientation;
206 d->m_orientation = orientation;
183 d->initializePieFromModel();
207 d->initializePieFromModel();
184 }
208 }
185
209
186 /*!
210 /*!
187 Returns which section of the model is kept in sync with the values of the pie's slices
211 Returns which section of the model is kept in sync with the values of the pie's slices
188 */
212 */
189 int QPieModelMapper::valuesSection() const
213 int QPieModelMapper::valuesSection() const
190 {
214 {
191 Q_D(const QPieModelMapper);
215 Q_D(const QPieModelMapper);
192 return d->m_valuesSection;
216 return d->m_valuesSection;
193 }
217 }
194
218
195 /*!
219 /*!
196 Sets the model section that is kept in sync with the pie slices values.
220 Sets the model section that is kept in sync with the pie slices values.
197 Parameter \a valuesSection specifies the section of the model.
221 Parameter \a valuesSection specifies the section of the model.
198 */
222 */
199 void QPieModelMapper::setValuesSection(int valuesSection)
223 void QPieModelMapper::setValuesSection(int valuesSection)
200 {
224 {
201 Q_D(QPieModelMapper);
225 Q_D(QPieModelMapper);
202 d->m_valuesSection = qMax(-1, valuesSection);
226 d->m_valuesSection = qMax(-1, valuesSection);
203 d->initializePieFromModel();
227 d->initializePieFromModel();
204 }
228 }
205
229
206 /*!
230 /*!
207 Returns which section of the model is kept in sync with the labels of the pie's slices
231 Returns which section of the model is kept in sync with the labels of the pie's slices
208 */
232 */
209 int QPieModelMapper::labelsSection() const
233 int QPieModelMapper::labelsSection() const
210 {
234 {
211 Q_D(const QPieModelMapper);
235 Q_D(const QPieModelMapper);
212 return d->m_labelsSection;
236 return d->m_labelsSection;
213 }
237 }
214
238
215 /*!
239 /*!
216 Sets the model section that is kept in sync with the pie slices labels.
240 Sets the model section that is kept in sync with the pie slices labels.
217 Parameter \a labelsSection specifies the section of the model.
241 Parameter \a labelsSection specifies the section of the model.
218 */
242 */
219 void QPieModelMapper::setLabelsSection(int labelsSection)
243 void QPieModelMapper::setLabelsSection(int labelsSection)
220 {
244 {
221 Q_D(QPieModelMapper);
245 Q_D(QPieModelMapper);
222 d->m_labelsSection = qMax(-1, labelsSection);
246 d->m_labelsSection = qMax(-1, labelsSection);
223 d->initializePieFromModel();
247 d->initializePieFromModel();
224 }
248 }
225
249
226 /*!
250 /*!
227 Resets the QPieModelMapper to the default state.
251 Resets the QPieModelMapper to the default state.
228 first: 0; count: -1; valuesSection: -1; labelsSection: -1;
252 first: 0; count: -1; valuesSection: -1; labelsSection: -1;
229 */
253 */
230 void QPieModelMapper::reset()
254 void QPieModelMapper::reset()
231 {
255 {
232 Q_D(QPieModelMapper);
256 Q_D(QPieModelMapper);
233 d->m_first = 0;
257 d->m_first = 0;
234 d->m_count = -1;
258 d->m_count = -1;
235 d->m_valuesSection = -1;
259 d->m_valuesSection = -1;
236 d->m_labelsSection = -1;
260 d->m_labelsSection = -1;
237 }
261 }
238
262
239 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
263 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
240
264
241 QPieModelMapperPrivate::QPieModelMapperPrivate(QPieModelMapper *q) :
265 QPieModelMapperPrivate::QPieModelMapperPrivate(QPieModelMapper *q) :
242 m_series(0),
266 m_series(0),
243 m_model(0),
267 m_model(0),
244 m_first(0),
268 m_first(0),
245 m_count(-1),
269 m_count(-1),
246 m_orientation(Qt::Vertical),
270 m_orientation(Qt::Vertical),
247 m_valuesSection(-1),
271 m_valuesSection(-1),
248 m_labelsSection(-1),
272 m_labelsSection(-1),
249 m_seriesSignalsBlock(false),
273 m_seriesSignalsBlock(false),
250 m_modelSignalsBlock(false),
274 m_modelSignalsBlock(false),
251 q_ptr(q)
275 q_ptr(q)
252 {
276 {
253 }
277 }
254
278
255 void QPieModelMapperPrivate::blockModelSignals(bool block)
279 void QPieModelMapperPrivate::blockModelSignals(bool block)
256 {
280 {
257 m_modelSignalsBlock = block;
281 m_modelSignalsBlock = block;
258 }
282 }
259
283
260 void QPieModelMapperPrivate::blockSeriesSignals(bool block)
284 void QPieModelMapperPrivate::blockSeriesSignals(bool block)
261 {
285 {
262 m_seriesSignalsBlock = block;
286 m_seriesSignalsBlock = block;
263 }
287 }
264
288
265
289
266 QPieSlice* QPieModelMapperPrivate::pieSlice(QModelIndex index) const
290 QPieSlice* QPieModelMapperPrivate::pieSlice(QModelIndex index) const
267 {
291 {
268 if (!index.isValid())
292 if (!index.isValid())
269 return 0; // index is invalid
293 return 0; // index is invalid
270
294
271 if (m_orientation == Qt::Vertical && (index.column() == m_valuesSection || index.column() == m_labelsSection)) {
295 if (m_orientation == Qt::Vertical && (index.column() == m_valuesSection || index.column() == m_labelsSection)) {
272 if (index.row() >= m_first && (m_count == - 1 || index.row() < m_first + m_count)) {
296 if (index.row() >= m_first && (m_count == - 1 || index.row() < m_first + m_count)) {
273 if (m_model->index(index.row(), m_valuesSection).isValid() && m_model->index(index.row(), m_labelsSection).isValid())
297 if (m_model->index(index.row(), m_valuesSection).isValid() && m_model->index(index.row(), m_labelsSection).isValid())
274 return m_series->slices().at(index.row() - m_first);
298 return m_series->slices().at(index.row() - m_first);
275 else
299 else
276 return 0;
300 return 0;
277 }
301 }
278 } else if (m_orientation == Qt::Horizontal && (index.row() == m_valuesSection || index.row() == m_labelsSection)) {
302 } else if (m_orientation == Qt::Horizontal && (index.row() == m_valuesSection || index.row() == m_labelsSection)) {
279 if (index.column() >= m_first && (m_count == - 1 || index.column() < m_first + m_count)) {
303 if (index.column() >= m_first && (m_count == - 1 || index.column() < m_first + m_count)) {
280 if (m_model->index(m_valuesSection, index.column()).isValid() && m_model->index(m_labelsSection, index.column()).isValid())
304 if (m_model->index(m_valuesSection, index.column()).isValid() && m_model->index(m_labelsSection, index.column()).isValid())
281 return m_series->slices().at(index.column() - m_first);
305 return m_series->slices().at(index.column() - m_first);
282 else
306 else
283 return 0;
307 return 0;
284 }
308 }
285 }
309 }
286 return 0; // This part of model has not been mapped to any slice
310 return 0; // This part of model has not been mapped to any slice
287 }
311 }
288
312
289 QModelIndex QPieModelMapperPrivate::valueModelIndex(int slicePos)
313 QModelIndex QPieModelMapperPrivate::valueModelIndex(int slicePos)
290 {
314 {
291 if (m_count != -1 && slicePos >= m_count)
315 if (m_count != -1 && slicePos >= m_count)
292 return QModelIndex(); // invalid
316 return QModelIndex(); // invalid
293
317
294 if (m_orientation == Qt::Vertical)
318 if (m_orientation == Qt::Vertical)
295 return m_model->index(slicePos + m_first, m_valuesSection);
319 return m_model->index(slicePos + m_first, m_valuesSection);
296 else
320 else
297 return m_model->index(m_valuesSection, slicePos + m_first);
321 return m_model->index(m_valuesSection, slicePos + m_first);
298 }
322 }
299
323
300 QModelIndex QPieModelMapperPrivate::labelModelIndex(int slicePos)
324 QModelIndex QPieModelMapperPrivate::labelModelIndex(int slicePos)
301 {
325 {
302 if (m_count != -1 && slicePos >= m_count)
326 if (m_count != -1 && slicePos >= m_count)
303 return QModelIndex(); // invalid
327 return QModelIndex(); // invalid
304
328
305 if (m_orientation == Qt::Vertical)
329 if (m_orientation == Qt::Vertical)
306 return m_model->index(slicePos + m_first, m_labelsSection);
330 return m_model->index(slicePos + m_first, m_labelsSection);
307 else
331 else
308 return m_model->index(m_labelsSection, slicePos + m_first);
332 return m_model->index(m_labelsSection, slicePos + m_first);
309 }
333 }
310
334
311 bool QPieModelMapperPrivate::isLabelIndex(QModelIndex index) const
335 bool QPieModelMapperPrivate::isLabelIndex(QModelIndex index) const
312 {
336 {
313 if (m_orientation == Qt::Vertical && index.column() == m_labelsSection)
337 if (m_orientation == Qt::Vertical && index.column() == m_labelsSection)
314 return true;
338 return true;
315 else if (m_orientation == Qt::Horizontal && index.row() == m_labelsSection)
339 else if (m_orientation == Qt::Horizontal && index.row() == m_labelsSection)
316 return true;
340 return true;
317
341
318 return false;
342 return false;
319 }
343 }
320
344
321 bool QPieModelMapperPrivate::isValueIndex(QModelIndex index) const
345 bool QPieModelMapperPrivate::isValueIndex(QModelIndex index) const
322 {
346 {
323 if (m_orientation == Qt::Vertical && index.column() == m_valuesSection)
347 if (m_orientation == Qt::Vertical && index.column() == m_valuesSection)
324 return true;
348 return true;
325 else if (m_orientation == Qt::Horizontal && index.row() == m_valuesSection)
349 else if (m_orientation == Qt::Horizontal && index.row() == m_valuesSection)
326 return true;
350 return true;
327
351
328 return false;
352 return false;
329 }
353 }
330
354
331 void QPieModelMapperPrivate::slicesAdded(QList<QPieSlice*> slices)
355 void QPieModelMapperPrivate::slicesAdded(QList<QPieSlice*> slices)
332 {
356 {
333 if (m_seriesSignalsBlock)
357 if (m_seriesSignalsBlock)
334 return;
358 return;
335
359
336 if (slices.count() == 0)
360 if (slices.count() == 0)
337 return;
361 return;
338
362
339 int firstIndex = m_series->slices().indexOf(slices.at(0));
363 int firstIndex = m_series->slices().indexOf(slices.at(0));
340 if (firstIndex == -1)
364 if (firstIndex == -1)
341 return;
365 return;
342
366
343 if (m_count != -1)
367 if (m_count != -1)
344 m_count += slices.count();
368 m_count += slices.count();
345
369
346 for (int i = firstIndex; i < firstIndex + slices.count(); i++) {
370 for (int i = firstIndex; i < firstIndex + slices.count(); i++) {
347 m_slices.insert(i, slices.at(i - firstIndex));
371 m_slices.insert(i, slices.at(i - firstIndex));
348 connect(slices.at(i - firstIndex), SIGNAL(labelChanged()), this, SLOT(sliceLabelChanged()));
372 connect(slices.at(i - firstIndex), SIGNAL(labelChanged()), this, SLOT(sliceLabelChanged()));
349 connect(slices.at(i - firstIndex), SIGNAL(valueChanged()), this, SLOT(sliceValueChanged()));
373 connect(slices.at(i - firstIndex), SIGNAL(valueChanged()), this, SLOT(sliceValueChanged()));
350 }
374 }
351
375
352 blockModelSignals();
376 blockModelSignals();
353 if (m_orientation == Qt::Vertical)
377 if (m_orientation == Qt::Vertical)
354 m_model->insertRows(firstIndex + m_first, slices.count());
378 m_model->insertRows(firstIndex + m_first, slices.count());
355 else
379 else
356 m_model->insertColumns(firstIndex + m_first, slices.count());
380 m_model->insertColumns(firstIndex + m_first, slices.count());
357
381
358 for(int i = firstIndex; i < firstIndex + slices.count(); i++) {
382 for(int i = firstIndex; i < firstIndex + slices.count(); i++) {
359 m_model->setData(valueModelIndex(i), slices.at(i - firstIndex)->value());
383 m_model->setData(valueModelIndex(i), slices.at(i - firstIndex)->value());
360 m_model->setData(labelModelIndex(i), slices.at(i - firstIndex)->label());
384 m_model->setData(labelModelIndex(i), slices.at(i - firstIndex)->label());
361 }
385 }
362 blockModelSignals(false);
386 blockModelSignals(false);
363 }
387 }
364
388
365 void QPieModelMapperPrivate::slicesRemoved(QList<QPieSlice*> slices)
389 void QPieModelMapperPrivate::slicesRemoved(QList<QPieSlice*> slices)
366 {
390 {
367 if (m_seriesSignalsBlock)
391 if (m_seriesSignalsBlock)
368 return;
392 return;
369
393
370 if (slices.count() == 0)
394 if (slices.count() == 0)
371 return;
395 return;
372
396
373 int firstIndex = m_slices.indexOf(slices.at(0));
397 int firstIndex = m_slices.indexOf(slices.at(0));
374 if (firstIndex == -1)
398 if (firstIndex == -1)
375 return;
399 return;
376
400
377 if (m_count != -1)
401 if (m_count != -1)
378 m_count -= slices.count();
402 m_count -= slices.count();
379
403
380 for (int i = firstIndex + slices.count() - 1; i >= firstIndex; i--)
404 for (int i = firstIndex + slices.count() - 1; i >= firstIndex; i--)
381 m_slices.removeAt(i);
405 m_slices.removeAt(i);
382
406
383 blockModelSignals();
407 blockModelSignals();
384 if (m_orientation == Qt::Vertical)
408 if (m_orientation == Qt::Vertical)
385 m_model->removeRows(firstIndex + m_first, slices.count());
409 m_model->removeRows(firstIndex + m_first, slices.count());
386 else
410 else
387 m_model->removeColumns(firstIndex + m_first, slices.count());
411 m_model->removeColumns(firstIndex + m_first, slices.count());
388 blockModelSignals(false);
412 blockModelSignals(false);
389 }
413 }
390
414
391 void QPieModelMapperPrivate::sliceLabelChanged()
415 void QPieModelMapperPrivate::sliceLabelChanged()
392 {
416 {
393 if (m_seriesSignalsBlock)
417 if (m_seriesSignalsBlock)
394 return;
418 return;
395
419
396 blockModelSignals();
420 blockModelSignals();
397 QPieSlice *slice = qobject_cast<QPieSlice *>(QObject::sender());
421 QPieSlice *slice = qobject_cast<QPieSlice *>(QObject::sender());
398 m_model->setData(labelModelIndex(m_series->slices().indexOf(slice)), slice->label());
422 m_model->setData(labelModelIndex(m_series->slices().indexOf(slice)), slice->label());
399 blockModelSignals(false);
423 blockModelSignals(false);
400 }
424 }
401
425
402 void QPieModelMapperPrivate::sliceValueChanged()
426 void QPieModelMapperPrivate::sliceValueChanged()
403 {
427 {
404 if (m_seriesSignalsBlock)
428 if (m_seriesSignalsBlock)
405 return;
429 return;
406
430
407 blockModelSignals();
431 blockModelSignals();
408 QPieSlice *slice = qobject_cast<QPieSlice *>(QObject::sender());
432 QPieSlice *slice = qobject_cast<QPieSlice *>(QObject::sender());
409 m_model->setData(valueModelIndex(m_series->slices().indexOf(slice)), slice->value());
433 m_model->setData(valueModelIndex(m_series->slices().indexOf(slice)), slice->value());
410 blockModelSignals(false);
434 blockModelSignals(false);
411 }
435 }
412
436
413 void QPieModelMapperPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
437 void QPieModelMapperPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
414 {
438 {
415 if (m_model == 0 || m_series == 0)
439 if (m_model == 0 || m_series == 0)
416 return;
440 return;
417
441
418 if (m_modelSignalsBlock)
442 if (m_modelSignalsBlock)
419 return;
443 return;
420
444
421 blockSeriesSignals();
445 blockSeriesSignals();
422 QModelIndex index;
446 QModelIndex index;
423 QPieSlice *slice;
447 QPieSlice *slice;
424 for (int row = topLeft.row(); row <= bottomRight.row(); row++) {
448 for (int row = topLeft.row(); row <= bottomRight.row(); row++) {
425 for (int column = topLeft.column(); column <= bottomRight.column(); column++) {
449 for (int column = topLeft.column(); column <= bottomRight.column(); column++) {
426 index = topLeft.sibling(row, column);
450 index = topLeft.sibling(row, column);
427 slice = pieSlice(index);
451 slice = pieSlice(index);
428 if (slice) {
452 if (slice) {
429 if (isValueIndex(index))
453 if (isValueIndex(index))
430 slice->setValue(m_model->data(index, Qt::DisplayRole).toReal());
454 slice->setValue(m_model->data(index, Qt::DisplayRole).toReal());
431 if (isLabelIndex(index))
455 if (isLabelIndex(index))
432 slice->setLabel(m_model->data(index, Qt::DisplayRole).toString());
456 slice->setLabel(m_model->data(index, Qt::DisplayRole).toString());
433 }
457 }
434 }
458 }
435 }
459 }
436 blockSeriesSignals(false);
460 blockSeriesSignals(false);
437 }
461 }
438
462
439
463
440 void QPieModelMapperPrivate::modelRowsAdded(QModelIndex parent, int start, int end)
464 void QPieModelMapperPrivate::modelRowsAdded(QModelIndex parent, int start, int end)
441 {
465 {
442 Q_UNUSED(parent);
466 Q_UNUSED(parent);
443 if (m_modelSignalsBlock)
467 if (m_modelSignalsBlock)
444 return;
468 return;
445
469
446 blockSeriesSignals();
470 blockSeriesSignals();
447 if (m_orientation == Qt::Vertical)
471 if (m_orientation == Qt::Vertical)
448 insertData(start, end);
472 insertData(start, end);
449 else if (start <= m_valuesSection || start <= m_labelsSection) // if the changes affect the map - reinitialize the pie
473 else if (start <= m_valuesSection || start <= m_labelsSection) // if the changes affect the map - reinitialize the pie
450 initializePieFromModel();
474 initializePieFromModel();
451 blockSeriesSignals(false);
475 blockSeriesSignals(false);
452 }
476 }
453
477
454 void QPieModelMapperPrivate::modelRowsRemoved(QModelIndex parent, int start, int end)
478 void QPieModelMapperPrivate::modelRowsRemoved(QModelIndex parent, int start, int end)
455 {
479 {
456 Q_UNUSED(parent);
480 Q_UNUSED(parent);
457 if (m_modelSignalsBlock)
481 if (m_modelSignalsBlock)
458 return;
482 return;
459
483
460 blockSeriesSignals();
484 blockSeriesSignals();
461 if (m_orientation == Qt::Vertical)
485 if (m_orientation == Qt::Vertical)
462 removeData(start, end);
486 removeData(start, end);
463 else if (start <= m_valuesSection || start <= m_labelsSection) // if the changes affect the map - reinitialize the pie
487 else if (start <= m_valuesSection || start <= m_labelsSection) // if the changes affect the map - reinitialize the pie
464 initializePieFromModel();
488 initializePieFromModel();
465 blockSeriesSignals(false);
489 blockSeriesSignals(false);
466 }
490 }
467
491
468 void QPieModelMapperPrivate::modelColumnsAdded(QModelIndex parent, int start, int end)
492 void QPieModelMapperPrivate::modelColumnsAdded(QModelIndex parent, int start, int end)
469 {
493 {
470 Q_UNUSED(parent);
494 Q_UNUSED(parent);
471 if (m_modelSignalsBlock)
495 if (m_modelSignalsBlock)
472 return;
496 return;
473
497
474 blockSeriesSignals();
498 blockSeriesSignals();
475 if (m_orientation == Qt::Horizontal)
499 if (m_orientation == Qt::Horizontal)
476 insertData(start, end);
500 insertData(start, end);
477 else if (start <= m_valuesSection || start <= m_labelsSection) // if the changes affect the map - reinitialize the pie
501 else if (start <= m_valuesSection || start <= m_labelsSection) // if the changes affect the map - reinitialize the pie
478 initializePieFromModel();
502 initializePieFromModel();
479 blockSeriesSignals(false);
503 blockSeriesSignals(false);
480 }
504 }
481
505
482 void QPieModelMapperPrivate::modelColumnsRemoved(QModelIndex parent, int start, int end)
506 void QPieModelMapperPrivate::modelColumnsRemoved(QModelIndex parent, int start, int end)
483 {
507 {
484 Q_UNUSED(parent);
508 Q_UNUSED(parent);
485 if (m_modelSignalsBlock)
509 if (m_modelSignalsBlock)
486 return;
510 return;
487
511
488 blockSeriesSignals();
512 blockSeriesSignals();
489 if (m_orientation == Qt::Horizontal)
513 if (m_orientation == Qt::Horizontal)
490 removeData(start, end);
514 removeData(start, end);
491 else if (start <= m_valuesSection || start <= m_labelsSection) // if the changes affect the map - reinitialize the pie
515 else if (start <= m_valuesSection || start <= m_labelsSection) // if the changes affect the map - reinitialize the pie
492 initializePieFromModel();
516 initializePieFromModel();
493 blockSeriesSignals(false);
517 blockSeriesSignals(false);
494 }
518 }
495
519
496 void QPieModelMapperPrivate::insertData(int start, int end)
520 void QPieModelMapperPrivate::insertData(int start, int end)
497 {
521 {
498 if (m_model == 0 || m_series == 0)
522 if (m_model == 0 || m_series == 0)
499 return;
523 return;
500
524
501 if (m_count != -1 && start >= m_first + m_count) {
525 if (m_count != -1 && start >= m_first + m_count) {
502 return;
526 return;
503 } else {
527 } else {
504 int addedCount = end - start + 1;
528 int addedCount = end - start + 1;
505 if (m_count != -1 && addedCount > m_count)
529 if (m_count != -1 && addedCount > m_count)
506 addedCount = m_count;
530 addedCount = m_count;
507 int first = qMax(start, m_first);
531 int first = qMax(start, m_first);
508 int last = qMin(first + addedCount - 1, m_orientation == Qt::Vertical ? m_model->rowCount() - 1 : m_model->columnCount() - 1);
532 int last = qMin(first + addedCount - 1, m_orientation == Qt::Vertical ? m_model->rowCount() - 1 : m_model->columnCount() - 1);
509 for (int i = first; i <= last; i++) {
533 for (int i = first; i <= last; i++) {
510 QModelIndex valueIndex = valueModelIndex(i - m_first);
534 QModelIndex valueIndex = valueModelIndex(i - m_first);
511 QModelIndex labelIndex = labelModelIndex(i - m_first);
535 QModelIndex labelIndex = labelModelIndex(i - m_first);
512 if (valueIndex.isValid() && labelIndex.isValid()) {
536 if (valueIndex.isValid() && labelIndex.isValid()) {
513 QPieSlice *slice = new QPieSlice;
537 QPieSlice *slice = new QPieSlice;
514 slice->setValue(m_model->data(valueIndex, Qt::DisplayRole).toDouble());
538 slice->setValue(m_model->data(valueIndex, Qt::DisplayRole).toDouble());
515 slice->setLabel(m_model->data(labelIndex, Qt::DisplayRole).toString());
539 slice->setLabel(m_model->data(labelIndex, Qt::DisplayRole).toString());
516 connect(slice, SIGNAL(labelChanged()), this, SLOT(sliceLabelChanged()));
540 connect(slice, SIGNAL(labelChanged()), this, SLOT(sliceLabelChanged()));
517 connect(slice, SIGNAL(valueChanged()), this, SLOT(sliceValueChanged()));
541 connect(slice, SIGNAL(valueChanged()), this, SLOT(sliceValueChanged()));
518 m_series->insert(i - m_first, slice);
542 m_series->insert(i - m_first, slice);
519 m_slices.insert(i - m_first, slice);
543 m_slices.insert(i - m_first, slice);
520 }
544 }
521 }
545 }
522
546
523 // remove excess of slices (abouve m_count)
547 // remove excess of slices (abouve m_count)
524 if (m_count != -1 && m_series->slices().size() > m_count)
548 if (m_count != -1 && m_series->slices().size() > m_count)
525 for (int i = m_series->slices().size() - 1; i >= m_count; i--) {
549 for (int i = m_series->slices().size() - 1; i >= m_count; i--) {
526 m_series->remove(m_series->slices().at(i));
550 m_series->remove(m_series->slices().at(i));
527 m_slices.removeAt(i);
551 m_slices.removeAt(i);
528 }
552 }
529 }
553 }
530 }
554 }
531
555
532 void QPieModelMapperPrivate::removeData(int start, int end)
556 void QPieModelMapperPrivate::removeData(int start, int end)
533 {
557 {
534 if (m_model == 0 || m_series == 0)
558 if (m_model == 0 || m_series == 0)
535 return;
559 return;
536
560
537 int removedCount = end - start + 1;
561 int removedCount = end - start + 1;
538 if (m_count != -1 && start >= m_first + m_count) {
562 if (m_count != -1 && start >= m_first + m_count) {
539 return;
563 return;
540 } else {
564 } else {
541 int toRemove = qMin(m_series->slices().size(), removedCount); // first find how many items can actually be removed
565 int toRemove = qMin(m_series->slices().size(), removedCount); // first find how many items can actually be removed
542 int first = qMax(start, m_first); // get the index of the first item that will be removed.
566 int first = qMax(start, m_first); // get the index of the first item that will be removed.
543 int last = qMin(first + toRemove - 1, m_series->slices().size() + m_first - 1); // get the index of the last item that will be removed.
567 int last = qMin(first + toRemove - 1, m_series->slices().size() + m_first - 1); // get the index of the last item that will be removed.
544 for (int i = last; i >= first; i--) {
568 for (int i = last; i >= first; i--) {
545 m_series->remove(m_series->slices().at(i - m_first));
569 m_series->remove(m_series->slices().at(i - m_first));
546 m_slices.removeAt(i - m_first);
570 m_slices.removeAt(i - m_first);
547 }
571 }
548
572
549 if (m_count != -1) {
573 if (m_count != -1) {
550 int itemsAvailable; // check how many are available to be added
574 int itemsAvailable; // check how many are available to be added
551 if (m_orientation == Qt::Vertical)
575 if (m_orientation == Qt::Vertical)
552 itemsAvailable = m_model->rowCount() - m_first - m_series->slices().size();
576 itemsAvailable = m_model->rowCount() - m_first - m_series->slices().size();
553 else
577 else
554 itemsAvailable = m_model->columnCount() - m_first - m_series->slices().size();
578 itemsAvailable = m_model->columnCount() - m_first - m_series->slices().size();
555 int toBeAdded = qMin(itemsAvailable, m_count - m_series->slices().size()); // add not more items than there is space left to be filled.
579 int toBeAdded = qMin(itemsAvailable, m_count - m_series->slices().size()); // add not more items than there is space left to be filled.
556 int currentSize = m_series->slices().size();
580 int currentSize = m_series->slices().size();
557 if (toBeAdded > 0)
581 if (toBeAdded > 0)
558 for (int i = m_series->slices().size(); i < currentSize + toBeAdded; i++) {
582 for (int i = m_series->slices().size(); i < currentSize + toBeAdded; i++) {
559 QModelIndex valueIndex = valueModelIndex(i - m_first);
583 QModelIndex valueIndex = valueModelIndex(i - m_first);
560 QModelIndex labelIndex = labelModelIndex(i - m_first);
584 QModelIndex labelIndex = labelModelIndex(i - m_first);
561 if (valueIndex.isValid() && labelIndex.isValid()) {
585 if (valueIndex.isValid() && labelIndex.isValid()) {
562 QPieSlice *slice = new QPieSlice;
586 QPieSlice *slice = new QPieSlice;
563 slice->setValue(m_model->data(valueIndex, Qt::DisplayRole).toDouble());
587 slice->setValue(m_model->data(valueIndex, Qt::DisplayRole).toDouble());
564 slice->setLabel(m_model->data(labelIndex, Qt::DisplayRole).toString());
588 slice->setLabel(m_model->data(labelIndex, Qt::DisplayRole).toString());
565 m_series->insert(i, slice);
589 m_series->insert(i, slice);
566 m_slices.insert(i, slice);
590 m_slices.insert(i, slice);
567 }
591 }
568 }
592 }
569 }
593 }
570 }
594 }
571 }
595 }
572
596
573 void QPieModelMapperPrivate::initializePieFromModel()
597 void QPieModelMapperPrivate::initializePieFromModel()
574 {
598 {
575 if (m_model == 0 || m_series == 0)
599 if (m_model == 0 || m_series == 0)
576 return;
600 return;
577
601
578 blockSeriesSignals();
602 blockSeriesSignals();
579 // clear current content
603 // clear current content
580 m_series->clear();
604 m_series->clear();
581 m_slices.clear();
605 m_slices.clear();
582
606
583 // create the initial slices set
607 // create the initial slices set
584 int slicePos = 0;
608 int slicePos = 0;
585 QModelIndex valueIndex = valueModelIndex(slicePos);
609 QModelIndex valueIndex = valueModelIndex(slicePos);
586 QModelIndex labelIndex = labelModelIndex(slicePos);
610 QModelIndex labelIndex = labelModelIndex(slicePos);
587 while (valueIndex.isValid() && labelIndex.isValid()) {
611 while (valueIndex.isValid() && labelIndex.isValid()) {
588 QPieSlice *slice = new QPieSlice;
612 QPieSlice *slice = new QPieSlice;
589 slice->setLabel(m_model->data(labelIndex, Qt::DisplayRole).toString());
613 slice->setLabel(m_model->data(labelIndex, Qt::DisplayRole).toString());
590 slice->setValue(m_model->data(valueIndex, Qt::DisplayRole).toDouble());
614 slice->setValue(m_model->data(valueIndex, Qt::DisplayRole).toDouble());
591 connect(slice, SIGNAL(labelChanged()), this, SLOT(sliceLabelChanged()));
615 connect(slice, SIGNAL(labelChanged()), this, SLOT(sliceLabelChanged()));
592 connect(slice, SIGNAL(valueChanged()), this, SLOT(sliceValueChanged()));
616 connect(slice, SIGNAL(valueChanged()), this, SLOT(sliceValueChanged()));
593 m_series->append(slice);
617 m_series->append(slice);
594 m_slices.append(slice);
618 m_slices.append(slice);
595 slicePos++;
619 slicePos++;
596 valueIndex = valueModelIndex(slicePos);
620 valueIndex = valueModelIndex(slicePos);
597 labelIndex = labelModelIndex(slicePos);
621 labelIndex = labelModelIndex(slicePos);
598 }
622 }
599 blockSeriesSignals(false);
623 blockSeriesSignals(false);
600 }
624 }
601
625
602 #include "moc_qpiemodelmapper_p.cpp"
626 #include "moc_qpiemodelmapper_p.cpp"
603 #include "moc_qpiemodelmapper.cpp"
627 #include "moc_qpiemodelmapper.cpp"
604
628
605 QTCOMMERCIALCHART_END_NAMESPACE
629 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,101 +1,113
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 "qvpiemodelmapper.h"
21 #include "qvpiemodelmapper.h"
22
22
23 QTCOMMERCIALCHART_BEGIN_NAMESPACE
23 QTCOMMERCIALCHART_BEGIN_NAMESPACE
24
24
25 /*!
25 /*!
26 \class QVPieModelMapper
26 \class QVPieModelMapper
27 \brief part of QtCommercial chart API.
27 \brief part of QtCommercial chart API.
28 \mainclass
28 \mainclass
29
29
30 Model mappers allow you to use QAbstractItemModel derived models as a data source for a chart series.
30 Model mappers allow you to use QAbstractItemModel derived models as a data source for a chart series.
31 Vertical model mapper is used to create a connection between QPieSeries and QAbstractItemModel derived model object that keeps the consecutive pie slices data in columns.
31 Vertical model mapper is used to create a connection between QPieSeries and QAbstractItemModel derived model object that keeps the consecutive pie slices data in columns.
32 It is possible to use both QAbstractItemModel and QPieSeries model API. QVPieModelMapper makes sure that Pie and the model are kept in sync.
32 It is possible to use both QAbstractItemModel and QPieSeries model API. QVPieModelMapper makes sure that Pie and the model are kept in sync.
33 NOTE: used model has to support adding/removing rows/columns and modifying the data of the cells.
33 NOTE: used model has to support adding/removing rows/columns and modifying the data of the cells.
34 */
34 */
35
35
36 /*!
36 /*!
37 \property QVPieModelMapper::valuesColumn
37 \property QVPieModelMapper::valuesColumn
38 \brief Defines which column of the model is kept in sync with the values of the pie's slices
38 \brief Defines which column of the model is kept in sync with the values of the pie's slices
39
39
40 Default value is: -1 (invalid mapping)
40 Default value is: -1 (invalid mapping)
41 */
41 */
42
42
43 /*!
43 /*!
44 \property QVPieModelMapper::labelsColumn
44 \property QVPieModelMapper::labelsColumn
45 \brief Defines which column of the model is kept in sync with the labels of the pie's slices
45 \brief Defines which column of the model is kept in sync with the labels of the pie's slices
46
46
47 Default value is: -1 (invalid mapping)
47 Default value is: -1 (invalid mapping)
48 */
48 */
49
49
50 /*!
50 /*!
51 \fn void QVPieModelMapper::valuesColumnChanged()
52
53 Emitted when the valuesColumn has changed.
54 */
55
56 /*!
57 \fn void QVPieModelMapper::labelsColumnChanged()
58
59 Emitted when the labelsColumn has changed.
60 */
61
62 /*!
51 Constructs a mapper object which is a child of \a parent.
63 Constructs a mapper object which is a child of \a parent.
52 */
64 */
53 QVPieModelMapper::QVPieModelMapper(QObject *parent) :
65 QVPieModelMapper::QVPieModelMapper(QObject *parent) :
54 QPieModelMapper(parent)
66 QPieModelMapper(parent)
55 {
67 {
56 QPieModelMapper::setOrientation(Qt::Vertical);
68 QPieModelMapper::setOrientation(Qt::Vertical);
57 }
69 }
58
70
59 /*!
71 /*!
60 Returns which column of the model is kept in sync with the values of the pie's slices
72 Returns which column of the model is kept in sync with the values of the pie's slices
61 */
73 */
62 int QVPieModelMapper::valuesColumn() const
74 int QVPieModelMapper::valuesColumn() const
63 {
75 {
64 return QPieModelMapper::valuesSection();
76 return QPieModelMapper::valuesSection();
65 }
77 }
66
78
67 /*!
79 /*!
68 Sets the model column that is kept in sync with the pie slices values.
80 Sets the model column that is kept in sync with the pie slices values.
69 Parameter \a valuesColumn specifies the row of the model.
81 Parameter \a valuesColumn specifies the row of the model.
70 */
82 */
71 void QVPieModelMapper::setValuesColumn(int valuesColumn)
83 void QVPieModelMapper::setValuesColumn(int valuesColumn)
72 {
84 {
73 if (valuesColumn != valuesSection()) {
85 if (valuesColumn != valuesSection()) {
74 QPieModelMapper::setValuesSection(valuesColumn);
86 QPieModelMapper::setValuesSection(valuesColumn);
75 emit valuesColumnChanged();
87 emit valuesColumnChanged();
76 }
88 }
77 }
89 }
78
90
79 /*!
91 /*!
80 Returns which column of the model is kept in sync with the labels of the pie's slices
92 Returns which column of the model is kept in sync with the labels of the pie's slices
81 */
93 */
82 int QVPieModelMapper::labelsColumn() const
94 int QVPieModelMapper::labelsColumn() const
83 {
95 {
84 return QPieModelMapper::labelsSection();
96 return QPieModelMapper::labelsSection();
85 }
97 }
86
98
87 /*!
99 /*!
88 Sets the model column that is kept in sync with the pie's slices labels.
100 Sets the model column that is kept in sync with the pie's slices labels.
89 Parameter \a labelsColumn specifies the row of the model.
101 Parameter \a labelsColumn specifies the row of the model.
90 */
102 */
91 void QVPieModelMapper::setLabelsColumn(int labelsColumn)
103 void QVPieModelMapper::setLabelsColumn(int labelsColumn)
92 {
104 {
93 if (labelsColumn != labelsSection()) {
105 if (labelsColumn != labelsSection()) {
94 QPieModelMapper::setLabelsSection(labelsColumn);
106 QPieModelMapper::setLabelsSection(labelsColumn);
95 emit labelsColumnChanged();
107 emit labelsColumnChanged();
96 }
108 }
97 }
109 }
98
110
99 #include "moc_qvpiemodelmapper.cpp"
111 #include "moc_qvpiemodelmapper.cpp"
100
112
101 QTCOMMERCIALCHART_END_NAMESPACE
113 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now