##// END OF EJS Templates
Added license text to piemodelmapper classes
Marek Rosa -
r1309:62cab2834ea4
parent child
Show More
@@ -1,33 +1,53
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
1 #include "qhpiemodelmapper.h"
21 #include "qhpiemodelmapper.h"
2
22
3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
23 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4
24
5 QHPieModelMapper::QHPieModelMapper(QObject *parent) :
25 QHPieModelMapper::QHPieModelMapper(QObject *parent) :
6 QPieModelMapper(parent)
26 QPieModelMapper(parent)
7 {
27 {
8 QPieModelMapper::setOrientation(Qt::Horizontal);
28 QPieModelMapper::setOrientation(Qt::Horizontal);
9 }
29 }
10
30
11 int QHPieModelMapper::valuesRow() const
31 int QHPieModelMapper::valuesRow() const
12 {
32 {
13 return QPieModelMapper::valuesIndex();
33 return QPieModelMapper::valuesIndex();
14 }
34 }
15
35
16 void QHPieModelMapper::setValuesRow(int valuesRow)
36 void QHPieModelMapper::setValuesRow(int valuesRow)
17 {
37 {
18 QPieModelMapper::setValuesIndex(valuesRow);
38 QPieModelMapper::setValuesIndex(valuesRow);
19 }
39 }
20
40
21 int QHPieModelMapper::labelsRow() const
41 int QHPieModelMapper::labelsRow() const
22 {
42 {
23 return QPieModelMapper::labelsIndex();
43 return QPieModelMapper::labelsIndex();
24 }
44 }
25
45
26 void QHPieModelMapper::setLabelsRow(int labelsRow)
46 void QHPieModelMapper::setLabelsRow(int labelsRow)
27 {
47 {
28 QPieModelMapper::setLabelsIndex(labelsRow);
48 QPieModelMapper::setLabelsIndex(labelsRow);
29 }
49 }
30
50
31 #include "moc_qhpiemodelmapper.cpp"
51 #include "moc_qhpiemodelmapper.cpp"
32
52
33 QTCOMMERCIALCHART_END_NAMESPACE
53 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,27 +1,47
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
1 #ifndef QHPIEMODELMAPPER_H
21 #ifndef QHPIEMODELMAPPER_H
2 #define QHPIEMODELMAPPER_H
22 #define QHPIEMODELMAPPER_H
3
23
4 #include "qpiemodelmapper.h"
24 #include "qpiemodelmapper.h"
5
25
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7
27
8 class QTCOMMERCIALCHART_EXPORT QHPieModelMapper : public QPieModelMapper
28 class QTCOMMERCIALCHART_EXPORT QHPieModelMapper : public QPieModelMapper
9 {
29 {
10 Q_OBJECT
30 Q_OBJECT
11 Q_PROPERTY(int valuesRow READ valuesRow WRITE setValuesRow)
31 Q_PROPERTY(int valuesRow READ valuesRow WRITE setValuesRow)
12 Q_PROPERTY(int labelsRow READ labelsRow WRITE setLabelsRow)
32 Q_PROPERTY(int labelsRow READ labelsRow WRITE setLabelsRow)
13
33
14 public:
34 public:
15 QHPieModelMapper(QObject *parent = 0);
35 QHPieModelMapper(QObject *parent = 0);
16
36
17 int valuesRow() const;
37 int valuesRow() const;
18 void setValuesRow(int valuesRow);
38 void setValuesRow(int valuesRow);
19
39
20 int labelsRow() const;
40 int labelsRow() const;
21 void setLabelsRow(int labelsRow);
41 void setLabelsRow(int labelsRow);
22
42
23 };
43 };
24
44
25 QTCOMMERCIALCHART_END_NAMESPACE
45 QTCOMMERCIALCHART_END_NAMESPACE
26
46
27 #endif // QHPIEMODELMAPPER_H
47 #endif // QHPIEMODELMAPPER_H
@@ -1,501 +1,521
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
1 #include "qpiemodelmapper_p.h"
21 #include "qpiemodelmapper_p.h"
2 #include "qpiemodelmapper.h"
22 #include "qpiemodelmapper.h"
3 #include "qpieseries.h"
23 #include "qpieseries.h"
4 #include "qpieslice.h"
24 #include "qpieslice.h"
5 #include <QAbstractItemModel>
25 #include <QAbstractItemModel>
6
26
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8
28
9 QPieModelMapper::QPieModelMapper(QObject *parent) :
29 QPieModelMapper::QPieModelMapper(QObject *parent) :
10 QObject(parent),
30 QObject(parent),
11 d_ptr(new QPieModelMapperPrivate(this))
31 d_ptr(new QPieModelMapperPrivate(this))
12 {
32 {
13 }
33 }
14
34
15 QPieModelMapper::~QPieModelMapper()
35 QPieModelMapper::~QPieModelMapper()
16 {
36 {
17 // Q_D(QPieModelMapper);
37 // Q_D(QPieModelMapper);
18 // disconnect(d->m_model, 0, d, 0);
38 // disconnect(d->m_model, 0, d, 0);
19 }
39 }
20
40
21 QAbstractItemModel* QPieModelMapper::model() const
41 QAbstractItemModel* QPieModelMapper::model() const
22 {
42 {
23 Q_D(const QPieModelMapper);
43 Q_D(const QPieModelMapper);
24 return d->m_model;
44 return d->m_model;
25 }
45 }
26
46
27 void QPieModelMapper::setModel(QAbstractItemModel *model)
47 void QPieModelMapper::setModel(QAbstractItemModel *model)
28 {
48 {
29 if (model == 0)
49 if (model == 0)
30 return;
50 return;
31
51
32 Q_D(QPieModelMapper);
52 Q_D(QPieModelMapper);
33 if (d->m_model) {
53 if (d->m_model) {
34 disconnect(d->m_model, 0, d, 0);
54 disconnect(d->m_model, 0, d, 0);
35 }
55 }
36
56
37 d->m_model = model;
57 d->m_model = model;
38 d->initializePieFromModel();
58 d->initializePieFromModel();
39 // connect signals from the model
59 // connect signals from the model
40 connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex)));
60 connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex)));
41 connect(d->m_model, SIGNAL(rowsInserted(QModelIndex,int,int)), d, SLOT(modelRowsAdded(QModelIndex,int,int)));
61 connect(d->m_model, SIGNAL(rowsInserted(QModelIndex,int,int)), d, SLOT(modelRowsAdded(QModelIndex,int,int)));
42 connect(d->m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)), d, SLOT(modelRowsRemoved(QModelIndex,int,int)));
62 connect(d->m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)), d, SLOT(modelRowsRemoved(QModelIndex,int,int)));
43 connect(d->m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), d, SLOT(modelColumnsAdded(QModelIndex,int,int)));
63 connect(d->m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), d, SLOT(modelColumnsAdded(QModelIndex,int,int)));
44 connect(d->m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), d, SLOT(modelColumnsRemoved(QModelIndex,int,int)));
64 connect(d->m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), d, SLOT(modelColumnsRemoved(QModelIndex,int,int)));
45 }
65 }
46
66
47 QPieSeries* QPieModelMapper::series() const
67 QPieSeries* QPieModelMapper::series() const
48 {
68 {
49 Q_D(const QPieModelMapper);
69 Q_D(const QPieModelMapper);
50 return d->m_series;
70 return d->m_series;
51 }
71 }
52
72
53 void QPieModelMapper::setSeries(QPieSeries *series)
73 void QPieModelMapper::setSeries(QPieSeries *series)
54 {
74 {
55 Q_D(QPieModelMapper);
75 Q_D(QPieModelMapper);
56 if (d->m_series) {
76 if (d->m_series) {
57 disconnect(d->m_series, 0, d, 0);
77 disconnect(d->m_series, 0, d, 0);
58 }
78 }
59
79
60 if (series == 0)
80 if (series == 0)
61 return;
81 return;
62
82
63 d->m_series = series;
83 d->m_series = series;
64 d->initializePieFromModel();
84 d->initializePieFromModel();
65 // connect the signals from the series
85 // connect the signals from the series
66 connect(d->m_series, SIGNAL(added(QList<QPieSlice*>)), d, SLOT(slicesAdded(QList<QPieSlice*>)));
86 connect(d->m_series, SIGNAL(added(QList<QPieSlice*>)), d, SLOT(slicesAdded(QList<QPieSlice*>)));
67 connect(d->m_series, SIGNAL(removed(QList<QPieSlice*>)), d, SLOT(slicesRemoved(QList<QPieSlice*>)));
87 connect(d->m_series, SIGNAL(removed(QList<QPieSlice*>)), d, SLOT(slicesRemoved(QList<QPieSlice*>)));
68 }
88 }
69
89
70 int QPieModelMapper::first() const
90 int QPieModelMapper::first() const
71 {
91 {
72 Q_D(const QPieModelMapper);
92 Q_D(const QPieModelMapper);
73 return d->m_first;
93 return d->m_first;
74 }
94 }
75
95
76 void QPieModelMapper::setFirst(int first)
96 void QPieModelMapper::setFirst(int first)
77 {
97 {
78 Q_D(QPieModelMapper);
98 Q_D(QPieModelMapper);
79 d->m_first = qMax(first, 0);
99 d->m_first = qMax(first, 0);
80 d->initializePieFromModel();
100 d->initializePieFromModel();
81 }
101 }
82
102
83 int QPieModelMapper::count() const
103 int QPieModelMapper::count() const
84 {
104 {
85 Q_D(const QPieModelMapper);
105 Q_D(const QPieModelMapper);
86 return d->m_count;
106 return d->m_count;
87 }
107 }
88
108
89 void QPieModelMapper::setCount(int count)
109 void QPieModelMapper::setCount(int count)
90 {
110 {
91 Q_D(QPieModelMapper);
111 Q_D(QPieModelMapper);
92 d->m_count = qMax(count, -1);
112 d->m_count = qMax(count, -1);
93 d->initializePieFromModel();
113 d->initializePieFromModel();
94 }
114 }
95
115
96 Qt::Orientation QPieModelMapper::orientation() const
116 Qt::Orientation QPieModelMapper::orientation() const
97 {
117 {
98 Q_D(const QPieModelMapper);
118 Q_D(const QPieModelMapper);
99 return d->m_orientation;
119 return d->m_orientation;
100 }
120 }
101
121
102 void QPieModelMapper::setOrientation(Qt::Orientation orientation)
122 void QPieModelMapper::setOrientation(Qt::Orientation orientation)
103 {
123 {
104 Q_D(QPieModelMapper);
124 Q_D(QPieModelMapper);
105 d->m_orientation = orientation;
125 d->m_orientation = orientation;
106 d->initializePieFromModel();
126 d->initializePieFromModel();
107 }
127 }
108
128
109 int QPieModelMapper::valuesIndex() const
129 int QPieModelMapper::valuesIndex() const
110 {
130 {
111 Q_D(const QPieModelMapper);
131 Q_D(const QPieModelMapper);
112 return d->m_valuesIndex;
132 return d->m_valuesIndex;
113 }
133 }
114
134
115 void QPieModelMapper::setValuesIndex(int valuesIndex)
135 void QPieModelMapper::setValuesIndex(int valuesIndex)
116 {
136 {
117 Q_D(QPieModelMapper);
137 Q_D(QPieModelMapper);
118 d->m_valuesIndex = qMax(-1, valuesIndex);
138 d->m_valuesIndex = qMax(-1, valuesIndex);
119 d->initializePieFromModel();
139 d->initializePieFromModel();
120 }
140 }
121
141
122 int QPieModelMapper::labelsIndex() const
142 int QPieModelMapper::labelsIndex() const
123 {
143 {
124 Q_D(const QPieModelMapper);
144 Q_D(const QPieModelMapper);
125 return d->m_labelsIndex;
145 return d->m_labelsIndex;
126 }
146 }
127
147
128 void QPieModelMapper::setLabelsIndex(int labelsIndex)
148 void QPieModelMapper::setLabelsIndex(int labelsIndex)
129 {
149 {
130 Q_D(QPieModelMapper);
150 Q_D(QPieModelMapper);
131 d->m_labelsIndex = qMax(-1, labelsIndex);
151 d->m_labelsIndex = qMax(-1, labelsIndex);
132 d->initializePieFromModel();
152 d->initializePieFromModel();
133 }
153 }
134
154
135 void QPieModelMapper::reset()
155 void QPieModelMapper::reset()
136 {
156 {
137 Q_D(QPieModelMapper);
157 Q_D(QPieModelMapper);
138 d->m_first = 0;
158 d->m_first = 0;
139 d->m_count = -1;
159 d->m_count = -1;
140 d->m_orientation = Qt::Vertical;
160 d->m_orientation = Qt::Vertical;
141 d->m_valuesIndex = -1;
161 d->m_valuesIndex = -1;
142 d->m_labelsIndex = -1;
162 d->m_labelsIndex = -1;
143 }
163 }
144
164
145 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
165 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
146
166
147 QPieModelMapperPrivate::QPieModelMapperPrivate(QPieModelMapper *q) :
167 QPieModelMapperPrivate::QPieModelMapperPrivate(QPieModelMapper *q) :
148 m_series(0),
168 m_series(0),
149 m_model(0),
169 m_model(0),
150 m_first(0),
170 m_first(0),
151 m_count(-1),
171 m_count(-1),
152 m_orientation(Qt::Vertical),
172 m_orientation(Qt::Vertical),
153 m_valuesIndex(-1),
173 m_valuesIndex(-1),
154 m_labelsIndex(-1),
174 m_labelsIndex(-1),
155 m_seriesSignalsBlock(false),
175 m_seriesSignalsBlock(false),
156 m_modelSignalsBlock(false),
176 m_modelSignalsBlock(false),
157 q_ptr(q)
177 q_ptr(q)
158 {
178 {
159 }
179 }
160
180
161 void QPieModelMapperPrivate::blockModelSignals(bool block)
181 void QPieModelMapperPrivate::blockModelSignals(bool block)
162 {
182 {
163 m_modelSignalsBlock = block;
183 m_modelSignalsBlock = block;
164 }
184 }
165
185
166 void QPieModelMapperPrivate::blockSeriesSignals(bool block)
186 void QPieModelMapperPrivate::blockSeriesSignals(bool block)
167 {
187 {
168 m_seriesSignalsBlock = block;
188 m_seriesSignalsBlock = block;
169 }
189 }
170
190
171
191
172 QPieSlice* QPieModelMapperPrivate::pieSlice(QModelIndex index) const
192 QPieSlice* QPieModelMapperPrivate::pieSlice(QModelIndex index) const
173 {
193 {
174 if (m_orientation == Qt::Vertical && (index.column() == m_valuesIndex || index.column() == m_labelsIndex)) {
194 if (m_orientation == Qt::Vertical && (index.column() == m_valuesIndex || index.column() == m_labelsIndex)) {
175 if (index.row() >= m_first && (m_count == - 1 || index.row() < m_first + m_count))
195 if (index.row() >= m_first && (m_count == - 1 || index.row() < m_first + m_count))
176 return m_series->slices().at(index.row() - m_first);
196 return m_series->slices().at(index.row() - m_first);
177 } else if (m_orientation == Qt::Horizontal && (index.row() == m_valuesIndex || index.row() == m_labelsIndex)) {
197 } else if (m_orientation == Qt::Horizontal && (index.row() == m_valuesIndex || index.row() == m_labelsIndex)) {
178 if (index.column() >= m_first && (m_count == - 1 || index.column() < m_first + m_count))
198 if (index.column() >= m_first && (m_count == - 1 || index.column() < m_first + m_count))
179 return m_series->slices().at(index.column() - m_first);
199 return m_series->slices().at(index.column() - m_first);
180 }
200 }
181 return 0; // This part of model has not been mapped to any slice
201 return 0; // This part of model has not been mapped to any slice
182 }
202 }
183
203
184 QModelIndex QPieModelMapperPrivate::valueModelIndex(int slicePos)
204 QModelIndex QPieModelMapperPrivate::valueModelIndex(int slicePos)
185 {
205 {
186 if (m_count != -1 && slicePos >= m_count)
206 if (m_count != -1 && slicePos >= m_count)
187 return QModelIndex(); // invalid
207 return QModelIndex(); // invalid
188
208
189 if (m_orientation == Qt::Vertical)
209 if (m_orientation == Qt::Vertical)
190 return m_model->index(slicePos + m_first, m_valuesIndex);
210 return m_model->index(slicePos + m_first, m_valuesIndex);
191 else
211 else
192 return m_model->index(m_valuesIndex, slicePos + m_first);
212 return m_model->index(m_valuesIndex, slicePos + m_first);
193 }
213 }
194
214
195 QModelIndex QPieModelMapperPrivate::labelModelIndex(int slicePos)
215 QModelIndex QPieModelMapperPrivate::labelModelIndex(int slicePos)
196 {
216 {
197 if (m_count != -1 && slicePos >= m_count)
217 if (m_count != -1 && slicePos >= m_count)
198 return QModelIndex(); // invalid
218 return QModelIndex(); // invalid
199
219
200 if (m_orientation == Qt::Vertical)
220 if (m_orientation == Qt::Vertical)
201 return m_model->index(slicePos + m_first, m_labelsIndex);
221 return m_model->index(slicePos + m_first, m_labelsIndex);
202 else
222 else
203 return m_model->index(m_labelsIndex, slicePos + m_first);
223 return m_model->index(m_labelsIndex, slicePos + m_first);
204 }
224 }
205
225
206 bool QPieModelMapperPrivate::isLabelIndex(QModelIndex index) const
226 bool QPieModelMapperPrivate::isLabelIndex(QModelIndex index) const
207 {
227 {
208 if (m_orientation == Qt::Vertical && index.column() == m_labelsIndex)
228 if (m_orientation == Qt::Vertical && index.column() == m_labelsIndex)
209 return true;
229 return true;
210 else if (m_orientation == Qt::Horizontal && index.row() == m_labelsIndex)
230 else if (m_orientation == Qt::Horizontal && index.row() == m_labelsIndex)
211 return true;
231 return true;
212
232
213 return false;
233 return false;
214 }
234 }
215
235
216 bool QPieModelMapperPrivate::isValueIndex(QModelIndex index) const
236 bool QPieModelMapperPrivate::isValueIndex(QModelIndex index) const
217 {
237 {
218 if (m_orientation == Qt::Vertical && index.column() == m_valuesIndex)
238 if (m_orientation == Qt::Vertical && index.column() == m_valuesIndex)
219 return true;
239 return true;
220 else if (m_orientation == Qt::Horizontal && index.row() == m_valuesIndex)
240 else if (m_orientation == Qt::Horizontal && index.row() == m_valuesIndex)
221 return true;
241 return true;
222
242
223 return false;
243 return false;
224 }
244 }
225
245
226 void QPieModelMapperPrivate::slicesAdded(QList<QPieSlice*> slices)
246 void QPieModelMapperPrivate::slicesAdded(QList<QPieSlice*> slices)
227 {
247 {
228 if (m_seriesSignalsBlock)
248 if (m_seriesSignalsBlock)
229 return;
249 return;
230
250
231 if (slices.count() == 0)
251 if (slices.count() == 0)
232 return;
252 return;
233
253
234 int firstIndex = m_series->slices().indexOf(slices.at(0));
254 int firstIndex = m_series->slices().indexOf(slices.at(0));
235 if (firstIndex == -1)
255 if (firstIndex == -1)
236 return;
256 return;
237
257
238 if (m_count != -1)
258 if (m_count != -1)
239 m_count += slices.count();
259 m_count += slices.count();
240
260
241 for (int i = firstIndex; i < firstIndex + slices.count(); i++) {
261 for (int i = firstIndex; i < firstIndex + slices.count(); i++) {
242 m_slices.insert(i, slices.at(i - firstIndex));
262 m_slices.insert(i, slices.at(i - firstIndex));
243 connect(slices.at(i - firstIndex), SIGNAL(labelChanged()), this, SLOT(sliceLabelChanged()));
263 connect(slices.at(i - firstIndex), SIGNAL(labelChanged()), this, SLOT(sliceLabelChanged()));
244 connect(slices.at(i - firstIndex), SIGNAL(valueChanged()), this, SLOT(sliceValueChanged()));
264 connect(slices.at(i - firstIndex), SIGNAL(valueChanged()), this, SLOT(sliceValueChanged()));
245 }
265 }
246
266
247 blockModelSignals();
267 blockModelSignals();
248 if (m_orientation == Qt::Vertical)
268 if (m_orientation == Qt::Vertical)
249 m_model->insertRows(firstIndex + m_first, slices.count());
269 m_model->insertRows(firstIndex + m_first, slices.count());
250 else
270 else
251 m_model->insertColumns(firstIndex + m_first, slices.count());
271 m_model->insertColumns(firstIndex + m_first, slices.count());
252
272
253 for(int i = firstIndex; i < firstIndex + slices.count(); i++) {
273 for(int i = firstIndex; i < firstIndex + slices.count(); i++) {
254 m_model->setData(valueModelIndex(i), slices.at(i - firstIndex)->value());
274 m_model->setData(valueModelIndex(i), slices.at(i - firstIndex)->value());
255 m_model->setData(labelModelIndex(i), slices.at(i - firstIndex)->label());
275 m_model->setData(labelModelIndex(i), slices.at(i - firstIndex)->label());
256 }
276 }
257 blockModelSignals(false);
277 blockModelSignals(false);
258 }
278 }
259
279
260 void QPieModelMapperPrivate::slicesRemoved(QList<QPieSlice*> slices)
280 void QPieModelMapperPrivate::slicesRemoved(QList<QPieSlice*> slices)
261 {
281 {
262 if (m_seriesSignalsBlock)
282 if (m_seriesSignalsBlock)
263 return;
283 return;
264
284
265 if (slices.count() == 0)
285 if (slices.count() == 0)
266 return;
286 return;
267
287
268 int firstIndex = m_slices.indexOf(slices.at(0));
288 int firstIndex = m_slices.indexOf(slices.at(0));
269 if (firstIndex == -1)
289 if (firstIndex == -1)
270 return;
290 return;
271
291
272 if (m_count != -1)
292 if (m_count != -1)
273 m_count -= slices.count();
293 m_count -= slices.count();
274
294
275 for (int i = firstIndex + slices.count() - 1; i >= firstIndex; i--)
295 for (int i = firstIndex + slices.count() - 1; i >= firstIndex; i--)
276 m_slices.removeAt(i);
296 m_slices.removeAt(i);
277
297
278 blockModelSignals();
298 blockModelSignals();
279 if (m_orientation == Qt::Vertical)
299 if (m_orientation == Qt::Vertical)
280 m_model->removeRows(firstIndex + m_first, slices.count());
300 m_model->removeRows(firstIndex + m_first, slices.count());
281 else
301 else
282 m_model->removeColumns(firstIndex + m_first, slices.count());
302 m_model->removeColumns(firstIndex + m_first, slices.count());
283 blockModelSignals(false);
303 blockModelSignals(false);
284 }
304 }
285
305
286 void QPieModelMapperPrivate::sliceLabelChanged()
306 void QPieModelMapperPrivate::sliceLabelChanged()
287 {
307 {
288 if (m_seriesSignalsBlock)
308 if (m_seriesSignalsBlock)
289 return;
309 return;
290
310
291 blockModelSignals();
311 blockModelSignals();
292 QPieSlice *slice = qobject_cast<QPieSlice *>(QObject::sender());
312 QPieSlice *slice = qobject_cast<QPieSlice *>(QObject::sender());
293 m_model->setData(labelModelIndex(m_series->slices().indexOf(slice)), slice->label());
313 m_model->setData(labelModelIndex(m_series->slices().indexOf(slice)), slice->label());
294 blockModelSignals(false);
314 blockModelSignals(false);
295 }
315 }
296
316
297 void QPieModelMapperPrivate::sliceValueChanged()
317 void QPieModelMapperPrivate::sliceValueChanged()
298 {
318 {
299 if (m_seriesSignalsBlock)
319 if (m_seriesSignalsBlock)
300 return;
320 return;
301
321
302 blockModelSignals();
322 blockModelSignals();
303 QPieSlice *slice = qobject_cast<QPieSlice *>(QObject::sender());
323 QPieSlice *slice = qobject_cast<QPieSlice *>(QObject::sender());
304 m_model->setData(valueModelIndex(m_series->slices().indexOf(slice)), slice->value());
324 m_model->setData(valueModelIndex(m_series->slices().indexOf(slice)), slice->value());
305 blockModelSignals(false);
325 blockModelSignals(false);
306 }
326 }
307
327
308 void QPieModelMapperPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
328 void QPieModelMapperPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
309 {
329 {
310 if (m_model == 0 || m_series == 0)
330 if (m_model == 0 || m_series == 0)
311 return;
331 return;
312
332
313 if (m_modelSignalsBlock)
333 if (m_modelSignalsBlock)
314 return;
334 return;
315
335
316 blockSeriesSignals();
336 blockSeriesSignals();
317 QModelIndex index;
337 QModelIndex index;
318 QPieSlice *slice;
338 QPieSlice *slice;
319 for (int row = topLeft.row(); row <= bottomRight.row(); row++) {
339 for (int row = topLeft.row(); row <= bottomRight.row(); row++) {
320 for (int column = topLeft.column(); column <= bottomRight.column(); column++) {
340 for (int column = topLeft.column(); column <= bottomRight.column(); column++) {
321 index = topLeft.sibling(row, column);
341 index = topLeft.sibling(row, column);
322 slice = pieSlice(index);
342 slice = pieSlice(index);
323 if (slice) {
343 if (slice) {
324 if (isValueIndex(index))
344 if (isValueIndex(index))
325 slice->setValue(m_model->data(index, Qt::DisplayRole).toReal());
345 slice->setValue(m_model->data(index, Qt::DisplayRole).toReal());
326 if (isLabelIndex(index))
346 if (isLabelIndex(index))
327 slice->setLabel(m_model->data(index, Qt::DisplayRole).toString());
347 slice->setLabel(m_model->data(index, Qt::DisplayRole).toString());
328 }
348 }
329 }
349 }
330 }
350 }
331 blockSeriesSignals(false);
351 blockSeriesSignals(false);
332 }
352 }
333
353
334
354
335 void QPieModelMapperPrivate::modelRowsAdded(QModelIndex parent, int start, int end)
355 void QPieModelMapperPrivate::modelRowsAdded(QModelIndex parent, int start, int end)
336 {
356 {
337 Q_UNUSED(parent);
357 Q_UNUSED(parent);
338 if (m_modelSignalsBlock)
358 if (m_modelSignalsBlock)
339 return;
359 return;
340
360
341 blockSeriesSignals();
361 blockSeriesSignals();
342 if (m_orientation == Qt::Vertical)
362 if (m_orientation == Qt::Vertical)
343 insertData(start, end);
363 insertData(start, end);
344 else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie
364 else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie
345 initializePieFromModel();
365 initializePieFromModel();
346 blockSeriesSignals(false);
366 blockSeriesSignals(false);
347 }
367 }
348
368
349 void QPieModelMapperPrivate::modelRowsRemoved(QModelIndex parent, int start, int end)
369 void QPieModelMapperPrivate::modelRowsRemoved(QModelIndex parent, int start, int end)
350 {
370 {
351 Q_UNUSED(parent);
371 Q_UNUSED(parent);
352 if (m_modelSignalsBlock)
372 if (m_modelSignalsBlock)
353 return;
373 return;
354
374
355 blockSeriesSignals();
375 blockSeriesSignals();
356 if (m_orientation == Qt::Vertical)
376 if (m_orientation == Qt::Vertical)
357 removeData(start, end);
377 removeData(start, end);
358 else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie
378 else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie
359 initializePieFromModel();
379 initializePieFromModel();
360 blockSeriesSignals(false);
380 blockSeriesSignals(false);
361 }
381 }
362
382
363 void QPieModelMapperPrivate::modelColumnsAdded(QModelIndex parent, int start, int end)
383 void QPieModelMapperPrivate::modelColumnsAdded(QModelIndex parent, int start, int end)
364 {
384 {
365 Q_UNUSED(parent);
385 Q_UNUSED(parent);
366 if (m_modelSignalsBlock)
386 if (m_modelSignalsBlock)
367 return;
387 return;
368
388
369 blockSeriesSignals();
389 blockSeriesSignals();
370 if (m_orientation == Qt::Horizontal)
390 if (m_orientation == Qt::Horizontal)
371 insertData(start, end);
391 insertData(start, end);
372 else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie
392 else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie
373 initializePieFromModel();
393 initializePieFromModel();
374 blockSeriesSignals(false);
394 blockSeriesSignals(false);
375 }
395 }
376
396
377 void QPieModelMapperPrivate::modelColumnsRemoved(QModelIndex parent, int start, int end)
397 void QPieModelMapperPrivate::modelColumnsRemoved(QModelIndex parent, int start, int end)
378 {
398 {
379 Q_UNUSED(parent);
399 Q_UNUSED(parent);
380 if (m_modelSignalsBlock)
400 if (m_modelSignalsBlock)
381 return;
401 return;
382
402
383 blockSeriesSignals();
403 blockSeriesSignals();
384 if (m_orientation == Qt::Horizontal)
404 if (m_orientation == Qt::Horizontal)
385 removeData(start, end);
405 removeData(start, end);
386 else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie
406 else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie
387 initializePieFromModel();
407 initializePieFromModel();
388 blockSeriesSignals(false);
408 blockSeriesSignals(false);
389 }
409 }
390
410
391 void QPieModelMapperPrivate::insertData(int start, int end)
411 void QPieModelMapperPrivate::insertData(int start, int end)
392 {
412 {
393 if (m_model == 0 || m_series == 0)
413 if (m_model == 0 || m_series == 0)
394 return;
414 return;
395
415
396 if (m_count != -1 && start >= m_first + m_count) {
416 if (m_count != -1 && start >= m_first + m_count) {
397 return;
417 return;
398 } else {
418 } else {
399 int addedCount = end - start + 1;
419 int addedCount = end - start + 1;
400 if (m_count != -1 && addedCount > m_count)
420 if (m_count != -1 && addedCount > m_count)
401 addedCount = m_count;
421 addedCount = m_count;
402 int first = qMax(start, m_first);
422 int first = qMax(start, m_first);
403 int last = qMin(first + addedCount - 1, m_orientation == Qt::Vertical ? m_model->rowCount() - 1 : m_model->columnCount() - 1);
423 int last = qMin(first + addedCount - 1, m_orientation == Qt::Vertical ? m_model->rowCount() - 1 : m_model->columnCount() - 1);
404 for (int i = first; i <= last; i++) {
424 for (int i = first; i <= last; i++) {
405 QPieSlice *slice = new QPieSlice;
425 QPieSlice *slice = new QPieSlice;
406 slice->setValue(m_model->data(valueModelIndex(i - m_first), Qt::DisplayRole).toDouble());
426 slice->setValue(m_model->data(valueModelIndex(i - m_first), Qt::DisplayRole).toDouble());
407 slice->setLabel(m_model->data(labelModelIndex(i - m_first), Qt::DisplayRole).toString());
427 slice->setLabel(m_model->data(labelModelIndex(i - m_first), Qt::DisplayRole).toString());
408 slice->setLabelVisible();
428 slice->setLabelVisible();
409 connect(slice, SIGNAL(labelChanged()), this, SLOT(sliceLabelChanged()));
429 connect(slice, SIGNAL(labelChanged()), this, SLOT(sliceLabelChanged()));
410 connect(slice, SIGNAL(valueChanged()), this, SLOT(sliceValueChanged()));
430 connect(slice, SIGNAL(valueChanged()), this, SLOT(sliceValueChanged()));
411 m_series->insert(i - m_first, slice);
431 m_series->insert(i - m_first, slice);
412 m_slices.insert(i - m_first, slice);
432 m_slices.insert(i - m_first, slice);
413 }
433 }
414
434
415 // remove excess of slices (abouve m_count)
435 // remove excess of slices (abouve m_count)
416 if (m_count != -1 && m_series->slices().size() > m_count)
436 if (m_count != -1 && m_series->slices().size() > m_count)
417 for (int i = m_series->slices().size() - 1; i >= m_count; i--) {
437 for (int i = m_series->slices().size() - 1; i >= m_count; i--) {
418 m_series->remove(m_series->slices().at(i));
438 m_series->remove(m_series->slices().at(i));
419 m_slices.removeAt(i);
439 m_slices.removeAt(i);
420 }
440 }
421 }
441 }
422 }
442 }
423
443
424 void QPieModelMapperPrivate::removeData(int start, int end)
444 void QPieModelMapperPrivate::removeData(int start, int end)
425 {
445 {
426 if (m_model == 0 || m_series == 0)
446 if (m_model == 0 || m_series == 0)
427 return;
447 return;
428
448
429 int removedCount = end - start + 1;
449 int removedCount = end - start + 1;
430 if (m_count != -1 && start >= m_first + m_count) {
450 if (m_count != -1 && start >= m_first + m_count) {
431 return;
451 return;
432 } else {
452 } else {
433 int toRemove = qMin(m_series->slices().size(), removedCount); // first find how many items can actually be removed
453 int toRemove = qMin(m_series->slices().size(), removedCount); // first find how many items can actually be removed
434 int first = qMax(start, m_first); // get the index of the first item that will be removed.
454 int first = qMax(start, m_first); // get the index of the first item that will be removed.
435 int last = qMin(first + toRemove - 1, m_series->slices().size() + m_first - 1); // get the index of the last item that will be removed.
455 int last = qMin(first + toRemove - 1, m_series->slices().size() + m_first - 1); // get the index of the last item that will be removed.
436 for (int i = last; i >= first; i--) {
456 for (int i = last; i >= first; i--) {
437 m_series->remove(m_series->slices().at(i - m_first));
457 m_series->remove(m_series->slices().at(i - m_first));
438 m_slices.removeAt(i - m_first);
458 m_slices.removeAt(i - m_first);
439 }
459 }
440
460
441 if (m_count != -1) {
461 if (m_count != -1) {
442 int itemsAvailable; // check how many are available to be added
462 int itemsAvailable; // check how many are available to be added
443 if (m_orientation == Qt::Vertical)
463 if (m_orientation == Qt::Vertical)
444 itemsAvailable = m_model->rowCount() - m_first - m_series->slices().size();
464 itemsAvailable = m_model->rowCount() - m_first - m_series->slices().size();
445 else
465 else
446 itemsAvailable = m_model->columnCount() - m_first - m_series->slices().size();
466 itemsAvailable = m_model->columnCount() - m_first - m_series->slices().size();
447 int toBeAdded = qMin(itemsAvailable, m_count - m_series->slices().size()); // add not more items than there is space left to be filled.
467 int toBeAdded = qMin(itemsAvailable, m_count - m_series->slices().size()); // add not more items than there is space left to be filled.
448 int currentSize = m_series->slices().size();
468 int currentSize = m_series->slices().size();
449 if (toBeAdded > 0)
469 if (toBeAdded > 0)
450 for (int i = m_series->slices().size(); i < currentSize + toBeAdded; i++) {
470 for (int i = m_series->slices().size(); i < currentSize + toBeAdded; i++) {
451 QPieSlice *slice = new QPieSlice;
471 QPieSlice *slice = new QPieSlice;
452 if (m_orientation == Qt::Vertical) {
472 if (m_orientation == Qt::Vertical) {
453 slice->setValue(m_model->data(m_model->index(i + m_first, m_valuesIndex), Qt::DisplayRole).toDouble());
473 slice->setValue(m_model->data(m_model->index(i + m_first, m_valuesIndex), Qt::DisplayRole).toDouble());
454 slice->setLabel(m_model->data(m_model->index(i + m_first, m_labelsIndex), Qt::DisplayRole).toString());
474 slice->setLabel(m_model->data(m_model->index(i + m_first, m_labelsIndex), Qt::DisplayRole).toString());
455 } else {
475 } else {
456 slice->setValue(m_model->data(m_model->index(m_valuesIndex, i + m_first), Qt::DisplayRole).toDouble());
476 slice->setValue(m_model->data(m_model->index(m_valuesIndex, i + m_first), Qt::DisplayRole).toDouble());
457 slice->setLabel(m_model->data(m_model->index(m_labelsIndex, i + m_first), Qt::DisplayRole).toString());
477 slice->setLabel(m_model->data(m_model->index(m_labelsIndex, i + m_first), Qt::DisplayRole).toString());
458 }
478 }
459 slice->setLabelVisible();
479 slice->setLabelVisible();
460 m_series->insert(i, slice);
480 m_series->insert(i, slice);
461 m_slices.insert(i, slice);
481 m_slices.insert(i, slice);
462 }
482 }
463 }
483 }
464 }
484 }
465 }
485 }
466
486
467 void QPieModelMapperPrivate::initializePieFromModel()
487 void QPieModelMapperPrivate::initializePieFromModel()
468 {
488 {
469 if (m_model == 0 || m_series == 0)
489 if (m_model == 0 || m_series == 0)
470 return;
490 return;
471
491
472 blockSeriesSignals();
492 blockSeriesSignals();
473 // clear current content
493 // clear current content
474 m_series->clear();
494 m_series->clear();
475 m_slices.clear();
495 m_slices.clear();
476
496
477 // create the initial slices set
497 // create the initial slices set
478 int slicePos = 0;
498 int slicePos = 0;
479 QModelIndex valueIndex = valueModelIndex(slicePos);
499 QModelIndex valueIndex = valueModelIndex(slicePos);
480 QModelIndex labelIndex = labelModelIndex(slicePos);
500 QModelIndex labelIndex = labelModelIndex(slicePos);
481 while (valueIndex.isValid() && labelIndex.isValid()) {
501 while (valueIndex.isValid() && labelIndex.isValid()) {
482 QPieSlice *slice = new QPieSlice;
502 QPieSlice *slice = new QPieSlice;
483 slice->setLabel(m_model->data(labelIndex, Qt::DisplayRole).toString());
503 slice->setLabel(m_model->data(labelIndex, Qt::DisplayRole).toString());
484 slice->setValue(m_model->data(valueIndex, Qt::DisplayRole).toDouble());
504 slice->setValue(m_model->data(valueIndex, Qt::DisplayRole).toDouble());
485 connect(slice, SIGNAL(labelChanged()), this, SLOT(sliceLabelChanged()));
505 connect(slice, SIGNAL(labelChanged()), this, SLOT(sliceLabelChanged()));
486 connect(slice, SIGNAL(valueChanged()), this, SLOT(sliceValueChanged()));
506 connect(slice, SIGNAL(valueChanged()), this, SLOT(sliceValueChanged()));
487 m_series->append(slice);
507 m_series->append(slice);
488 m_slices.append(slice);
508 m_slices.append(slice);
489 // m_series->append(m_model->data(labelIndex, Qt::DisplayRole).toString(), m_model->data(valueIndex, Qt::DisplayRole).toDouble());
509 // m_series->append(m_model->data(labelIndex, Qt::DisplayRole).toString(), m_model->data(valueIndex, Qt::DisplayRole).toDouble());
490 slicePos++;
510 slicePos++;
491 valueIndex = valueModelIndex(slicePos);
511 valueIndex = valueModelIndex(slicePos);
492 labelIndex = labelModelIndex(slicePos);
512 labelIndex = labelModelIndex(slicePos);
493 }
513 }
494 m_series->setLabelsVisible(true);
514 m_series->setLabelsVisible(true);
495 blockSeriesSignals(false);
515 blockSeriesSignals(false);
496 }
516 }
497
517
498 #include "moc_qpiemodelmapper_p.cpp"
518 #include "moc_qpiemodelmapper_p.cpp"
499 #include "moc_qpiemodelmapper.cpp"
519 #include "moc_qpiemodelmapper.cpp"
500
520
501 QTCOMMERCIALCHART_END_NAMESPACE
521 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,72 +1,101
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 // W A R N I N G
22 // -------------
23 //
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 // implementation detail. This header file may change from version to
26 // version without notice, or even be removed.
27 //
28 // We mean it.
29
1 #ifndef QPIEMODELMAPPER_P_H
30 #ifndef QPIEMODELMAPPER_P_H
2 #define QPIEMODELMAPPER_P_H
31 #define QPIEMODELMAPPER_P_H
3
32
4 #include "qpiemodelmapper.h"
33 #include "qpiemodelmapper.h"
5 #include <QObject>
34 #include <QObject>
6
35
7 class QModelIndex;
36 class QModelIndex;
8 class QAbstractItemModel;
37 class QAbstractItemModel;
9
38
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
39 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11
40
12 class QPieModelMapper;
41 class QPieModelMapper;
13 class QPieSeries;
42 class QPieSeries;
14 class QPieSlice;
43 class QPieSlice;
15
44
16 class QPieModelMapperPrivate : public QObject
45 class QPieModelMapperPrivate : public QObject
17 {
46 {
18 Q_OBJECT
47 Q_OBJECT
19
48
20 public:
49 public:
21 QPieModelMapperPrivate(QPieModelMapper *q);
50 QPieModelMapperPrivate(QPieModelMapper *q);
22
51
23 public Q_SLOTS:
52 public Q_SLOTS:
24 // for the model
53 // for the model
25 void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight);
54 void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight);
26 void modelRowsAdded(QModelIndex parent, int start, int end);
55 void modelRowsAdded(QModelIndex parent, int start, int end);
27 void modelRowsRemoved(QModelIndex parent, int start, int end);
56 void modelRowsRemoved(QModelIndex parent, int start, int end);
28 void modelColumnsAdded(QModelIndex parent, int start, int end);
57 void modelColumnsAdded(QModelIndex parent, int start, int end);
29 void modelColumnsRemoved(QModelIndex parent, int start, int end);
58 void modelColumnsRemoved(QModelIndex parent, int start, int end);
30
59
31 // for the series
60 // for the series
32 void slicesAdded(QList<QPieSlice*> slices);
61 void slicesAdded(QList<QPieSlice*> slices);
33 void slicesRemoved(QList<QPieSlice*> slices);
62 void slicesRemoved(QList<QPieSlice*> slices);
34 void sliceLabelChanged();
63 void sliceLabelChanged();
35 void sliceValueChanged();
64 void sliceValueChanged();
36
65
37 void initializePieFromModel();
66 void initializePieFromModel();
38
67
39 private:
68 private:
40 QPieSlice* pieSlice(QModelIndex index) const;
69 QPieSlice* pieSlice(QModelIndex index) const;
41 bool isLabelIndex(QModelIndex index) const;
70 bool isLabelIndex(QModelIndex index) const;
42 bool isValueIndex(QModelIndex index) const;
71 bool isValueIndex(QModelIndex index) const;
43 QModelIndex valueModelIndex(int slicePos);
72 QModelIndex valueModelIndex(int slicePos);
44 QModelIndex labelModelIndex(int slicePos);
73 QModelIndex labelModelIndex(int slicePos);
45 void insertData(int start, int end);
74 void insertData(int start, int end);
46 void removeData(int start, int end);
75 void removeData(int start, int end);
47
76
48 void blockModelSignals(bool block = true);
77 void blockModelSignals(bool block = true);
49 void blockSeriesSignals(bool block = true);
78 void blockSeriesSignals(bool block = true);
50
79
51 private:
80 private:
52 QPieSeries *m_series;
81 QPieSeries *m_series;
53 QList<QPieSlice*> m_slices;
82 QList<QPieSlice*> m_slices;
54 QAbstractItemModel *m_model;
83 QAbstractItemModel *m_model;
55 int m_first;
84 int m_first;
56 int m_count;
85 int m_count;
57 Qt::Orientation m_orientation;
86 Qt::Orientation m_orientation;
58 int m_valuesIndex;
87 int m_valuesIndex;
59 int m_labelsIndex;
88 int m_labelsIndex;
60 bool m_seriesSignalsBlock;
89 bool m_seriesSignalsBlock;
61 bool m_modelSignalsBlock;
90 bool m_modelSignalsBlock;
62
91
63 private:
92 private:
64
93
65 QPieModelMapper *q_ptr;
94 QPieModelMapper *q_ptr;
66 Q_DECLARE_PUBLIC(QPieModelMapper)
95 Q_DECLARE_PUBLIC(QPieModelMapper)
67 friend class QPieSeriesPrivate;
96 friend class QPieSeriesPrivate;
68 };
97 };
69
98
70 QTCOMMERCIALCHART_END_NAMESPACE
99 QTCOMMERCIALCHART_END_NAMESPACE
71
100
72 #endif // QPIEMODELMAPPER_P_H
101 #endif // QPIEMODELMAPPER_P_H
@@ -1,34 +1,54
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
1 #include "qpiemodelmapper_p.h"
21 #include "qpiemodelmapper_p.h"
2 #include "qvpiemodelmapper.h"
22 #include "qvpiemodelmapper.h"
3
23
4 QTCOMMERCIALCHART_BEGIN_NAMESPACE
24 QTCOMMERCIALCHART_BEGIN_NAMESPACE
5
25
6 QVPieModelMapper::QVPieModelMapper(QObject *parent) :
26 QVPieModelMapper::QVPieModelMapper(QObject *parent) :
7 QPieModelMapper(parent)
27 QPieModelMapper(parent)
8 {
28 {
9 QPieModelMapper::setOrientation(Qt::Vertical);
29 QPieModelMapper::setOrientation(Qt::Vertical);
10 }
30 }
11
31
12 int QVPieModelMapper::valuesColumn() const
32 int QVPieModelMapper::valuesColumn() const
13 {
33 {
14 return QPieModelMapper::valuesIndex();
34 return QPieModelMapper::valuesIndex();
15 }
35 }
16
36
17 void QVPieModelMapper::setValuesColumn(int valuesColumn)
37 void QVPieModelMapper::setValuesColumn(int valuesColumn)
18 {
38 {
19 QPieModelMapper::setValuesIndex(valuesColumn);
39 QPieModelMapper::setValuesIndex(valuesColumn);
20 }
40 }
21
41
22 int QVPieModelMapper::labelsColumn() const
42 int QVPieModelMapper::labelsColumn() const
23 {
43 {
24 return QPieModelMapper::labelsIndex();
44 return QPieModelMapper::labelsIndex();
25 }
45 }
26
46
27 void QVPieModelMapper::setLabelsColumn(int labelsColumn)
47 void QVPieModelMapper::setLabelsColumn(int labelsColumn)
28 {
48 {
29 QPieModelMapper::setLabelsIndex(labelsColumn);
49 QPieModelMapper::setLabelsIndex(labelsColumn);
30 }
50 }
31
51
32 #include "moc_qvpiemodelmapper.cpp"
52 #include "moc_qvpiemodelmapper.cpp"
33
53
34 QTCOMMERCIALCHART_END_NAMESPACE
54 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,26 +1,46
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
1 #ifndef QVPIEMODELMAPPER_H
21 #ifndef QVPIEMODELMAPPER_H
2 #define QVPIEMODELMAPPER_H
22 #define QVPIEMODELMAPPER_H
3
23
4 #include "qpiemodelmapper.h"
24 #include "qpiemodelmapper.h"
5
25
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7
27
8 class QTCOMMERCIALCHART_EXPORT QVPieModelMapper : public QPieModelMapper
28 class QTCOMMERCIALCHART_EXPORT QVPieModelMapper : public QPieModelMapper
9 {
29 {
10 Q_OBJECT
30 Q_OBJECT
11 Q_PROPERTY(int valuesColumn READ valuesColumn WRITE setValuesColumn)
31 Q_PROPERTY(int valuesColumn READ valuesColumn WRITE setValuesColumn)
12 Q_PROPERTY(int labelsColumn READ labelsColumn WRITE setLabelsColumn)
32 Q_PROPERTY(int labelsColumn READ labelsColumn WRITE setLabelsColumn)
13
33
14 public:
34 public:
15 explicit QVPieModelMapper(QObject *parent = 0);
35 explicit QVPieModelMapper(QObject *parent = 0);
16
36
17 int valuesColumn() const;
37 int valuesColumn() const;
18 void setValuesColumn(int valuesColumn);
38 void setValuesColumn(int valuesColumn);
19
39
20 int labelsColumn() const;
40 int labelsColumn() const;
21 void setLabelsColumn(int labelsColumn);
41 void setLabelsColumn(int labelsColumn);
22 };
42 };
23
43
24 QTCOMMERCIALCHART_END_NAMESPACE
44 QTCOMMERCIALCHART_END_NAMESPACE
25
45
26 #endif // QVPIEMODELMAPPER_H
46 #endif // QVPIEMODELMAPPER_H
@@ -1,225 +1,222
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 "qsplineseries.h"
21 #include "qsplineseries.h"
22 #include "qsplineseries_p.h"
22 #include "qsplineseries_p.h"
23 #include "splinechartitem_p.h"
23 #include "splinechartitem_p.h"
24 #include "chartdataset_p.h"
24 #include "chartdataset_p.h"
25 #include "charttheme_p.h"
25 #include "charttheme_p.h"
26 #include "chartanimator_p.h"
26 #include "chartanimator_p.h"
27
27
28 /*!
28 /*!
29 \class QSplineSeries
29 \class QSplineSeries
30 \brief Series type used to store data needed to draw a spline.
30 \brief Series type used to store data needed to draw a spline.
31
31
32 QSplineSeries stores the data points along with the segment control points needed by QPainterPath to draw spline
32 QSplineSeries stores the data points along with the segment control points needed by QPainterPath to draw spline
33 Control points are automatically calculated when data changes. The algorithm computes the points so that the normal spline can be drawn.
33 Control points are automatically calculated when data changes. The algorithm computes the points so that the normal spline can be drawn.
34
34
35 \image examples_splinechart.png
35 \image examples_splinechart.png
36
36
37 Creating basic spline chart is simple:
37 Creating basic spline chart is simple:
38 \code
38 \code
39 QSplineSeries* series = new QSplineSeries();
39 QSplineSeries* series = new QSplineSeries();
40 series->append(0, 6);
40 series->append(0, 6);
41 series->append(2, 4);
41 series->append(2, 4);
42 ...
42 ...
43 chart->addSeries(series);
43 chart->addSeries(series);
44 \endcode
44 \endcode
45 */
45 */
46
46
47 /*!
47 /*!
48 \fn QSeriesType QSplineSeries::type() const
48 \fn QSeriesType QSplineSeries::type() const
49 Returns the type of the series
49 Returns the type of the series
50 */
50 */
51
51
52 QTCOMMERCIALCHART_BEGIN_NAMESPACE
52 QTCOMMERCIALCHART_BEGIN_NAMESPACE
53
53
54 /*!
54 /*!
55 Constructs empty series object which is a child of \a parent.
55 Constructs empty series object which is a child of \a parent.
56 When series object is added to QChartView or QChart instance then the ownerships is transferred.
56 When series object is added to QChartView or QChart instance then the ownerships is transferred.
57 */
57 */
58
58
59 QSplineSeries::QSplineSeries(QObject *parent) :
59 QSplineSeries::QSplineSeries(QObject *parent) :
60 QLineSeries(*new QSplineSeriesPrivate(this),parent)
60 QLineSeries(*new QSplineSeriesPrivate(this),parent)
61 {
61 {
62 Q_D(QSplineSeries);
62 Q_D(QSplineSeries);
63 QObject::connect(this,SIGNAL(pointAdded(int)), d, SLOT(updateControlPoints()));
63 QObject::connect(this,SIGNAL(pointAdded(int)), d, SLOT(updateControlPoints()));
64 QObject::connect(this,SIGNAL(pointRemoved(int)), d, SLOT(updateControlPoints()));
64 QObject::connect(this,SIGNAL(pointRemoved(int)), d, SLOT(updateControlPoints()));
65 QObject::connect(this,SIGNAL(pointReplaced(int)), d, SLOT(updateControlPoints()));
65 QObject::connect(this,SIGNAL(pointReplaced(int)), d, SLOT(updateControlPoints()));
66 }
66 }
67
67
68 QSplineSeries::~QSplineSeries()
68 QSplineSeries::~QSplineSeries()
69 {
69 {
70 Q_D(QSplineSeries);
70 Q_D(QSplineSeries);
71 if(d->m_dataset){
71 if(d->m_dataset){
72 d->m_dataset->removeSeries(this);
72 d->m_dataset->removeSeries(this);
73 }
73 }
74 }
74 }
75
75
76 QAbstractSeries::SeriesType QSplineSeries::type() const
76 QAbstractSeries::SeriesType QSplineSeries::type() const
77 {
77 {
78 return QAbstractSeries::SeriesTypeSpline;
78 return QAbstractSeries::SeriesTypeSpline;
79 }
79 }
80
80
81 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
81 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
82
82
83 QSplineSeriesPrivate::QSplineSeriesPrivate(QSplineSeries* q):QLineSeriesPrivate(q)
83 QSplineSeriesPrivate::QSplineSeriesPrivate(QSplineSeries* q):QLineSeriesPrivate(q)
84 {
84 {
85 // QObject::connect(this,SIGNAL(pointAdded(int)), this, SLOT(updateControlPoints()));
85 }
86 // QObject::connect(this,SIGNAL(pointRemoved(int)), this, SLOT(updateControlPoints()));
87 // QObject::connect(this,SIGNAL(pointReplaced(int)), this, SLOT(updateControlPoints()));
88 };
89
86
90 /*!
87 /*!
91 Calculates control points which are needed by QPainterPath.cubicTo function to draw the cubic Bezier cureve between two points.
88 Calculates control points which are needed by QPainterPath.cubicTo function to draw the cubic Bezier cureve between two points.
92 */
89 */
93 void QSplineSeriesPrivate::calculateControlPoints()
90 void QSplineSeriesPrivate::calculateControlPoints()
94 {
91 {
95 Q_Q(QSplineSeries);
92 Q_Q(QSplineSeries);
96
93
97 const QList<QPointF>& points = q->points();
94 const QList<QPointF>& points = q->points();
98
95
99 int n = points.count() - 1;
96 int n = points.count() - 1;
100
97
101 if (n == 1)
98 if (n == 1)
102 {
99 {
103 //for n==1
100 //for n==1
104 m_controlPoints[0].setX((2 * points[0].x() + points[1].x()) / 3);
101 m_controlPoints[0].setX((2 * points[0].x() + points[1].x()) / 3);
105 m_controlPoints[0].setY((2 * points[0].y() + points[1].y()) / 3);
102 m_controlPoints[0].setY((2 * points[0].y() + points[1].y()) / 3);
106 m_controlPoints[1].setX(2 * m_controlPoints[0].x() - points[0].x());
103 m_controlPoints[1].setX(2 * m_controlPoints[0].x() - points[0].x());
107 m_controlPoints[1].setY(2 * m_controlPoints[0].y() - points[0].y());
104 m_controlPoints[1].setY(2 * m_controlPoints[0].y() - points[0].y());
108 return;
105 return;
109 }
106 }
110
107
111 // Calculate first Bezier control points
108 // Calculate first Bezier control points
112 // Right hand side vector
109 // Right hand side vector
113 // Set of equations for P0 to Pn points.
110 // Set of equations for P0 to Pn points.
114 //
111 //
115 // | 2 1 0 0 ... 0 0 0 ... 0 0 0 | | P1_1 | | P0 + 2 * P1 |
112 // | 2 1 0 0 ... 0 0 0 ... 0 0 0 | | P1_1 | | P0 + 2 * P1 |
116 // | 1 4 1 0 ... 0 0 0 ... 0 0 0 | | P1_2 | | 4 * P1 + 2 * P2 |
113 // | 1 4 1 0 ... 0 0 0 ... 0 0 0 | | P1_2 | | 4 * P1 + 2 * P2 |
117 // | 0 1 4 1 ... 0 0 0 ... 0 0 0 | | P1_3 | | 4 * P2 + 2 * P3 |
114 // | 0 1 4 1 ... 0 0 0 ... 0 0 0 | | P1_3 | | 4 * P2 + 2 * P3 |
118 // | . . . . . . . . . . . . | | ... | | ... |
115 // | . . . . . . . . . . . . | | ... | | ... |
119 // | 0 0 0 0 ... 1 4 1 ... 0 0 0 | * | P1_i | = | 4 * P(i-1) + 2 * Pi |
116 // | 0 0 0 0 ... 1 4 1 ... 0 0 0 | * | P1_i | = | 4 * P(i-1) + 2 * Pi |
120 // | . . . . . . . . . . . . | | ... | | ... |
117 // | . . . . . . . . . . . . | | ... | | ... |
121 // | 0 0 0 0 0 0 0 0 ... 1 4 1 | | P1_(n-1)| | 4 * P(n-2) + 2 * P(n-1) |
118 // | 0 0 0 0 0 0 0 0 ... 1 4 1 | | P1_(n-1)| | 4 * P(n-2) + 2 * P(n-1) |
122 // | 0 0 0 0 0 0 0 0 ... 0 2 7 | | P1_n | | 8 * P(n-1) + Pn |
119 // | 0 0 0 0 0 0 0 0 ... 0 2 7 | | P1_n | | 8 * P(n-1) + Pn |
123 //
120 //
124 QVector<qreal> vector;
121 QVector<qreal> vector;
125 vector.resize(n);
122 vector.resize(n);
126
123
127 vector[0] = points[0].x() + 2 * points[1].x();
124 vector[0] = points[0].x() + 2 * points[1].x();
128
125
129
126
130 for (int i = 1; i < n - 1; ++i){
127 for (int i = 1; i < n - 1; ++i){
131 vector[i] = 4 * points[i].x() + 2 * points[i + 1].x();
128 vector[i] = 4 * points[i].x() + 2 * points[i + 1].x();
132 }
129 }
133
130
134 vector[n - 1] = (8 * points[n-1].x() + points[n].x()) / 2.0;
131 vector[n - 1] = (8 * points[n-1].x() + points[n].x()) / 2.0;
135
132
136 QVector<qreal> xControl = firstControlPoints(vector);
133 QVector<qreal> xControl = firstControlPoints(vector);
137
134
138 vector[0] = points[0].y() + 2 * points[1].y();
135 vector[0] = points[0].y() + 2 * points[1].y();
139
136
140 for (int i = 1; i < n - 1; ++i) {
137 for (int i = 1; i < n - 1; ++i) {
141 vector[i] = 4 * points[i].y() + 2 * points[i + 1].y();
138 vector[i] = 4 * points[i].y() + 2 * points[i + 1].y();
142 }
139 }
143
140
144 vector[n - 1] = (8 * points[n-1].y() + points[n].y()) / 2.0;
141 vector[n - 1] = (8 * points[n-1].y() + points[n].y()) / 2.0;
145
142
146 QVector<qreal> yControl = firstControlPoints(vector);
143 QVector<qreal> yControl = firstControlPoints(vector);
147
144
148 for (int i = 0,j =0; i < n; ++i, ++j) {
145 for (int i = 0,j =0; i < n; ++i, ++j) {
149
146
150 m_controlPoints[j].setX(xControl[i]);
147 m_controlPoints[j].setX(xControl[i]);
151 m_controlPoints[j].setY(yControl[i]);
148 m_controlPoints[j].setY(yControl[i]);
152
149
153 j++;
150 j++;
154
151
155 if (i < n - 1){
152 if (i < n - 1){
156 m_controlPoints[j].setX(2 * points[i+1].x() - xControl[i + 1]);
153 m_controlPoints[j].setX(2 * points[i+1].x() - xControl[i + 1]);
157 m_controlPoints[j].setY(2 * points[i+1].y() - yControl[i + 1]);
154 m_controlPoints[j].setY(2 * points[i+1].y() - yControl[i + 1]);
158 }else{
155 }else{
159 m_controlPoints[j].setX((points[n].x() + xControl[n - 1]) / 2);
156 m_controlPoints[j].setX((points[n].x() + xControl[n - 1]) / 2);
160 m_controlPoints[j].setY((points[n].y() + yControl[n - 1]) / 2);
157 m_controlPoints[j].setY((points[n].y() + yControl[n - 1]) / 2);
161 }
158 }
162 }
159 }
163 }
160 }
164
161
165 QVector<qreal> QSplineSeriesPrivate::firstControlPoints(const QVector<qreal>& vector)
162 QVector<qreal> QSplineSeriesPrivate::firstControlPoints(const QVector<qreal>& vector)
166 {
163 {
167 QVector<qreal> result;
164 QVector<qreal> result;
168
165
169 int count = vector.count();
166 int count = vector.count();
170 result.resize(count);
167 result.resize(count);
171 result[0] = vector[0] / 2.0;
168 result[0] = vector[0] / 2.0;
172
169
173 QVector<qreal> temp;
170 QVector<qreal> temp;
174 temp.resize(count);
171 temp.resize(count);
175 temp[0] = 0;
172 temp[0] = 0;
176
173
177 qreal b = 2.0;
174 qreal b = 2.0;
178
175
179 for (int i = 1; i < count; i++) {
176 for (int i = 1; i < count; i++) {
180 temp[i] = 1 / b;
177 temp[i] = 1 / b;
181 b = (i < count - 1 ? 4.0 : 3.5) - temp[i];
178 b = (i < count - 1 ? 4.0 : 3.5) - temp[i];
182 result[i]=(vector[i] - result[i - 1]) / b;
179 result[i]=(vector[i] - result[i - 1]) / b;
183 }
180 }
184 for (int i = 1; i < count; i++)
181 for (int i = 1; i < count; i++)
185 result[count - i - 1] -= temp[count - i] * result[count - i];
182 result[count - i - 1] -= temp[count - i] * result[count - i];
186
183
187 return result;
184 return result;
188 }
185 }
189
186
190 QPointF QSplineSeriesPrivate::controlPoint(int index) const
187 QPointF QSplineSeriesPrivate::controlPoint(int index) const
191 {
188 {
192 // Q_D(const QSplineSeries);
189 // Q_D(const QSplineSeries);
193 // return d->m_controlPoints[index];
190 // return d->m_controlPoints[index];
194 return m_controlPoints[index];
191 return m_controlPoints[index];
195 }
192 }
196
193
197 /*!
194 /*!
198 Updates the control points, besed on currently avaiable knots.
195 Updates the control points, besed on currently avaiable knots.
199 */
196 */
200 void QSplineSeriesPrivate::updateControlPoints()
197 void QSplineSeriesPrivate::updateControlPoints()
201 {
198 {
202 Q_Q(QSplineSeries);
199 Q_Q(QSplineSeries);
203 if (q->count() > 1) {
200 if (q->count() > 1) {
204 m_controlPoints.resize(2*q->count()-2);
201 m_controlPoints.resize(2*q->count()-2);
205 calculateControlPoints();
202 calculateControlPoints();
206 }
203 }
207 }
204 }
208
205
209 Chart* QSplineSeriesPrivate::createGraphics(ChartPresenter* presenter)
206 Chart* QSplineSeriesPrivate::createGraphics(ChartPresenter* presenter)
210 {
207 {
211 Q_Q(QSplineSeries);
208 Q_Q(QSplineSeries);
212 SplineChartItem* spline = new SplineChartItem(q,presenter);
209 SplineChartItem* spline = new SplineChartItem(q,presenter);
213 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
210 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
214 spline->setAnimator(presenter->animator());
211 spline->setAnimator(presenter->animator());
215 spline->setAnimation(new SplineAnimation(spline));
212 spline->setAnimation(new SplineAnimation(spline));
216 }
213 }
217
214
218 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
215 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
219 return spline;
216 return spline;
220 }
217 }
221
218
222 #include "moc_qsplineseries.cpp"
219 #include "moc_qsplineseries.cpp"
223 #include "moc_qsplineseries_p.cpp"
220 #include "moc_qsplineseries_p.cpp"
224
221
225 QTCOMMERCIALCHART_END_NAMESPACE
222 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now