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