##// END OF EJS Templates
Fix to pie model mapper
Marek Rosa -
r1258:9ffa0de920ec
parent child
Show More
@@ -1,492 +1,495
1 1 #include "qpiemodelmapper_p.h"
2 2 #include "qpiemodelmapper.h"
3 3 #include "qpieseries.h"
4 4 #include "qpieslice.h"
5 5 #include <QAbstractItemModel>
6 6
7 7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 8
9 9 QPieModelMapper::QPieModelMapper(QObject *parent) :
10 10 QObject(parent),
11 11 d_ptr(new QPieModelMapperPrivate(this))
12 12 {
13 13 }
14 14
15 15 QAbstractItemModel* QPieModelMapper::model() const
16 16 {
17 17 Q_D(const QPieModelMapper);
18 18 return d->m_model;
19 19 }
20 20
21 21 void QPieModelMapper::setModel(QAbstractItemModel *model)
22 22 {
23 23 if (model == 0)
24 24 return;
25 25
26 26 Q_D(QPieModelMapper);
27 27 if (d->m_model) {
28 28 disconnect(d->m_model, 0, d, 0);
29 29 }
30 30
31 31 d->m_model = model;
32 32 d->initializePieFromModel();
33 33 // connect signals from the model
34 34 connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex)));
35 35 connect(d->m_model, SIGNAL(rowsInserted(QModelIndex,int,int)), d, SLOT(modelRowsAdded(QModelIndex,int,int)));
36 36 connect(d->m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)), d, SLOT(modelRowsRemoved(QModelIndex,int,int)));
37 37 connect(d->m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), d, SLOT(modelColumnsAdded(QModelIndex,int,int)));
38 38 connect(d->m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), d, SLOT(modelColumnsRemoved(QModelIndex,int,int)));
39 39 }
40 40
41 41 QPieSeries* QPieModelMapper::series() const
42 42 {
43 43 Q_D(const QPieModelMapper);
44 44 return d->m_series;
45 45 }
46 46
47 47 void QPieModelMapper::setSeries(QPieSeries *series)
48 48 {
49 49 Q_D(QPieModelMapper);
50 50 if (d->m_series) {
51 51 disconnect(d->m_series, 0, d, 0);
52 52 }
53 53
54 54 if (series == 0)
55 55 return;
56 56
57 57 d->m_series = series;
58 58 d->initializePieFromModel();
59 59 // connect the signals from the series
60 60 connect(d->m_series, SIGNAL(added(QList<QPieSlice*>)), d, SLOT(slicesAdded(QList<QPieSlice*>)));
61 61 connect(d->m_series, SIGNAL(removed(QList<QPieSlice*>)), d, SLOT(slicesRemoved(QList<QPieSlice*>)));
62 62 }
63 63
64 64 int QPieModelMapper::first() const
65 65 {
66 66 Q_D(const QPieModelMapper);
67 67 return d->m_first;
68 68 }
69 69
70 70 void QPieModelMapper::setFirst(int first)
71 71 {
72 72 Q_D(QPieModelMapper);
73 73 d->m_first = qMax(first, 0);
74 74 d->initializePieFromModel();
75 75 }
76 76
77 77 int QPieModelMapper::count() const
78 78 {
79 79 Q_D(const QPieModelMapper);
80 80 return d->m_count;
81 81 }
82 82
83 83 void QPieModelMapper::setCount(int count)
84 84 {
85 85 Q_D(QPieModelMapper);
86 86 d->m_count = qMax(count, -1);
87 87 d->initializePieFromModel();
88 88 }
89 89
90 90 Qt::Orientation QPieModelMapper::orientation() const
91 91 {
92 92 Q_D(const QPieModelMapper);
93 93 return d->m_orientation;
94 94 }
95 95
96 96 void QPieModelMapper::setOrientation(Qt::Orientation orientation)
97 97 {
98 98 Q_D(QPieModelMapper);
99 99 d->m_orientation = orientation;
100 100 d->initializePieFromModel();
101 101 }
102 102
103 103 int QPieModelMapper::valuesIndex() const
104 104 {
105 105 Q_D(const QPieModelMapper);
106 106 return d->m_valuesIndex;
107 107 }
108 108
109 109 void QPieModelMapper::setValuesIndex(int valuesIndex)
110 110 {
111 111 Q_D(QPieModelMapper);
112 112 d->m_valuesIndex = qMax(-1, valuesIndex);
113 113 d->initializePieFromModel();
114 114 }
115 115
116 116 int QPieModelMapper::labelsIndex() const
117 117 {
118 118 Q_D(const QPieModelMapper);
119 119 return d->m_labelsIndex;
120 120 }
121 121
122 122 void QPieModelMapper::setLabelsIndex(int labelsIndex)
123 123 {
124 124 Q_D(QPieModelMapper);
125 125 d->m_labelsIndex = qMax(-1, labelsIndex);
126 126 d->initializePieFromModel();
127 127 }
128 128
129 129 void QPieModelMapper::reset()
130 130 {
131 131 Q_D(QPieModelMapper);
132 132 d->m_first = 0;
133 133 d->m_count = -1;
134 134 d->m_orientation = Qt::Vertical;
135 135 d->m_valuesIndex = -1;
136 136 d->m_labelsIndex = -1;
137 137 }
138 138
139 139 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
140 140
141 141 QPieModelMapperPrivate::QPieModelMapperPrivate(QPieModelMapper *q) :
142 142 m_series(0),
143 143 m_model(0),
144 144 m_first(0),
145 145 m_count(-1),
146 146 m_orientation(Qt::Vertical),
147 147 m_valuesIndex(-1),
148 148 m_labelsIndex(-1),
149 149 m_seriesSignalsBlock(false),
150 150 m_modelSignalsBlock(false),
151 151 q_ptr(q)
152 152 {
153 153 }
154 154
155 155 void QPieModelMapperPrivate::blockModelSignals(bool block)
156 156 {
157 157 m_modelSignalsBlock = block;
158 158 }
159 159
160 160 void QPieModelMapperPrivate::blockSeriesSignals(bool block)
161 161 {
162 162 m_seriesSignalsBlock = block;
163 163 }
164 164
165 165
166 166 QPieSlice* QPieModelMapperPrivate::pieSlice(QModelIndex index) const
167 167 {
168 168 if (m_orientation == Qt::Vertical && (index.column() == m_valuesIndex || index.column() == m_labelsIndex)) {
169 169 if (index.row() >= m_first && (m_count == - 1 || index.row() < m_first + m_count))
170 170 return m_series->slices().at(index.row() - m_first);
171 171 } else if (m_orientation == Qt::Horizontal && (index.row() == m_valuesIndex || index.row() == m_labelsIndex)) {
172 172 if (index.column() >= m_first && (m_count == - 1 || index.column() < m_first + m_count))
173 173 return m_series->slices().at(index.column() - m_first);
174 174 }
175 175 return 0; // This part of model has not been mapped to any slice
176 176 }
177 177
178 178 QModelIndex QPieModelMapperPrivate::valueModelIndex(int slicePos)
179 179 {
180 180 if (m_count != -1 && slicePos >= m_count)
181 181 return QModelIndex(); // invalid
182 182
183 183 if (m_orientation == Qt::Vertical)
184 184 return m_model->index(slicePos + m_first, m_valuesIndex);
185 185 else
186 186 return m_model->index(m_valuesIndex, slicePos + m_first);
187 187 }
188 188
189 189 QModelIndex QPieModelMapperPrivate::labelModelIndex(int slicePos)
190 190 {
191 191 if (m_count != -1 && slicePos >= m_count)
192 192 return QModelIndex(); // invalid
193 193
194 194 if (m_orientation == Qt::Vertical)
195 195 return m_model->index(slicePos + m_first, m_labelsIndex);
196 196 else
197 197 return m_model->index(m_labelsIndex, slicePos + m_first);
198 198 }
199 199
200 200 bool QPieModelMapperPrivate::isLabelIndex(QModelIndex index) const
201 201 {
202 202 if (m_orientation == Qt::Vertical && index.column() == m_labelsIndex)
203 203 return true;
204 204 else if (m_orientation == Qt::Horizontal && index.row() == m_labelsIndex)
205 205 return true;
206 206
207 207 return false;
208 208 }
209 209
210 210 bool QPieModelMapperPrivate::isValueIndex(QModelIndex index) const
211 211 {
212 212 if (m_orientation == Qt::Vertical && index.column() == m_valuesIndex)
213 213 return true;
214 214 else if (m_orientation == Qt::Horizontal && index.row() == m_valuesIndex)
215 215 return true;
216 216
217 217 return false;
218 218 }
219 219
220 220 void QPieModelMapperPrivate::slicesAdded(QList<QPieSlice*> slices)
221 221 {
222 222 if (m_seriesSignalsBlock)
223 223 return;
224 224
225 225 if (slices.count() == 0)
226 226 return;
227 227
228 228 int firstIndex = m_series->slices().indexOf(slices.at(0));
229 229 if (firstIndex == -1)
230 230 return;
231 231
232 232 if (m_count != -1)
233 233 m_count += slices.count();
234 234
235 235 for (int i = firstIndex; i < firstIndex + slices.count(); i++) {
236 236 m_slices.insert(i, slices.at(i - firstIndex));
237 237 connect(slices.at(i - firstIndex), SIGNAL(labelChanged()), this, SLOT(sliceLabelChanged()));
238 238 connect(slices.at(i - firstIndex), SIGNAL(valueChanged()), this, SLOT(sliceValueChanged()));
239 239 }
240 240
241 241 blockModelSignals();
242 242 if (m_orientation == Qt::Vertical)
243 243 m_model->insertRows(firstIndex + m_first, slices.count());
244 244 else
245 245 m_model->insertColumns(firstIndex + m_first, slices.count());
246 246
247 247 for(int i = firstIndex; i < firstIndex + slices.count(); i++) {
248 248 m_model->setData(valueModelIndex(i), slices.at(i - firstIndex)->value());
249 249 m_model->setData(labelModelIndex(i), slices.at(i - firstIndex)->label());
250 250 }
251 251 blockModelSignals(false);
252 252 }
253 253
254 254 void QPieModelMapperPrivate::slicesRemoved(QList<QPieSlice*> slices)
255 255 {
256 256 if (m_seriesSignalsBlock)
257 257 return;
258 258
259 259 if (slices.count() == 0)
260 260 return;
261 261
262 262 int firstIndex = m_slices.indexOf(slices.at(0));
263 263 if (firstIndex == -1)
264 264 return;
265 265
266 266 if (m_count != -1)
267 267 m_count -= slices.count();
268 268
269 269 for (int i = firstIndex + slices.count() - 1; i >= firstIndex; i--)
270 270 m_slices.removeAt(i);
271 271
272 272 blockModelSignals();
273 273 if (m_orientation == Qt::Vertical)
274 274 m_model->removeRows(firstIndex + m_first, slices.count());
275 275 else
276 276 m_model->removeColumns(firstIndex + m_first, slices.count());
277 277 blockModelSignals(false);
278 278 }
279 279
280 280 void QPieModelMapperPrivate::sliceLabelChanged()
281 281 {
282 282 if (m_seriesSignalsBlock)
283 283 return;
284 284
285 285 blockModelSignals();
286 286 QPieSlice *slice = qobject_cast<QPieSlice *>(QObject::sender());
287 287 m_model->setData(labelModelIndex(m_series->slices().indexOf(slice)), slice->label());
288 288 blockModelSignals(false);
289 289 }
290 290
291 291 void QPieModelMapperPrivate::sliceValueChanged()
292 292 {
293 293 if (m_seriesSignalsBlock)
294 294 return;
295 295
296 296 blockModelSignals();
297 297 QPieSlice *slice = qobject_cast<QPieSlice *>(QObject::sender());
298 298 m_model->setData(valueModelIndex(m_series->slices().indexOf(slice)), slice->value());
299 299 blockModelSignals(false);
300 300 }
301 301
302 302 void QPieModelMapperPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
303 303 {
304 if (m_model == 0 || m_series == 0)
305 return;
306
304 307 if (m_modelSignalsBlock)
305 308 return;
306 309
307 310 blockSeriesSignals();
308 311 QModelIndex index;
309 312 QPieSlice *slice;
310 313 for (int row = topLeft.row(); row <= bottomRight.row(); row++) {
311 314 for (int column = topLeft.column(); column <= bottomRight.column(); column++) {
312 315 index = topLeft.sibling(row, column);
313 316 slice = pieSlice(index);
314 317 if (slice) {
315 318 if (isValueIndex(index))
316 319 slice->setValue(m_model->data(index, Qt::DisplayRole).toReal());
317 320 if (isLabelIndex(index))
318 321 slice->setLabel(m_model->data(index, Qt::DisplayRole).toString());
319 322 }
320 323 }
321 324 }
322 325 blockSeriesSignals(false);
323 326 }
324 327
325 328
326 329 void QPieModelMapperPrivate::modelRowsAdded(QModelIndex parent, int start, int end)
327 330 {
328 331 Q_UNUSED(parent);
329 332 if (m_modelSignalsBlock)
330 333 return;
331 334
332 335 blockSeriesSignals();
333 336 if (m_orientation == Qt::Vertical)
334 337 insertData(start, end);
335 338 else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie
336 339 initializePieFromModel();
337 340 blockSeriesSignals(false);
338 341 }
339 342
340 343 void QPieModelMapperPrivate::modelRowsRemoved(QModelIndex parent, int start, int end)
341 344 {
342 345 Q_UNUSED(parent);
343 346 if (m_modelSignalsBlock)
344 347 return;
345 348
346 349 blockSeriesSignals();
347 350 if (m_orientation == Qt::Vertical)
348 351 removeData(start, end);
349 352 else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie
350 353 initializePieFromModel();
351 354 blockSeriesSignals(false);
352 355 }
353 356
354 357 void QPieModelMapperPrivate::modelColumnsAdded(QModelIndex parent, int start, int end)
355 358 {
356 359 Q_UNUSED(parent);
357 360 if (m_modelSignalsBlock)
358 361 return;
359 362
360 363 blockSeriesSignals();
361 364 if (m_orientation == Qt::Horizontal)
362 365 insertData(start, end);
363 366 else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie
364 367 initializePieFromModel();
365 368 blockSeriesSignals(false);
366 369 }
367 370
368 371 void QPieModelMapperPrivate::modelColumnsRemoved(QModelIndex parent, int start, int end)
369 372 {
370 373 Q_UNUSED(parent);
371 374 if (m_modelSignalsBlock)
372 375 return;
373 376
374 377 blockSeriesSignals();
375 378 if (m_orientation == Qt::Horizontal)
376 379 removeData(start, end);
377 380 else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie
378 381 initializePieFromModel();
379 382 blockSeriesSignals(false);
380 383 }
381 384
382 385 void QPieModelMapperPrivate::insertData(int start, int end)
383 386 {
384 387 if (m_model == 0 || m_series == 0)
385 388 return;
386 389
387 390 if (m_count != -1 && start >= m_first + m_count) {
388 391 return;
389 392 } else {
390 393 int addedCount = end - start + 1;
391 394 if (m_count != -1 && addedCount > m_count)
392 395 addedCount = m_count;
393 396 int first = qMax(start, m_first);
394 397 int last = qMin(first + addedCount - 1, m_orientation == Qt::Vertical ? m_model->rowCount() - 1 : m_model->columnCount() - 1);
395 398 for (int i = first; i <= last; i++) {
396 399 QPieSlice *slice = new QPieSlice;
397 400 slice->setValue(m_model->data(valueModelIndex(i - m_first), Qt::DisplayRole).toDouble());
398 401 slice->setLabel(m_model->data(labelModelIndex(i - m_first), Qt::DisplayRole).toString());
399 402 slice->setLabelVisible();
400 403 connect(slice, SIGNAL(labelChanged()), this, SLOT(sliceLabelChanged()));
401 404 connect(slice, SIGNAL(valueChanged()), this, SLOT(sliceValueChanged()));
402 405 m_series->insert(i - m_first, slice);
403 406 m_slices.insert(i - m_first, slice);
404 407 }
405 408
406 409 // remove excess of slices (abouve m_count)
407 410 if (m_count != -1 && m_series->slices().size() > m_count)
408 411 for (int i = m_series->slices().size() - 1; i >= m_count; i--) {
409 412 m_series->remove(m_series->slices().at(i));
410 413 m_slices.removeAt(i);
411 414 }
412 415 }
413 416 }
414 417
415 418 void QPieModelMapperPrivate::removeData(int start, int end)
416 419 {
417 420 if (m_model == 0 || m_series == 0)
418 421 return;
419 422
420 423 int removedCount = end - start + 1;
421 424 if (m_count != -1 && start >= m_first + m_count) {
422 425 return;
423 426 } else {
424 427 int toRemove = qMin(m_series->slices().size(), removedCount); // first find how many items can actually be removed
425 428 int first = qMax(start, m_first); // get the index of the first item that will be removed.
426 429 int last = qMin(first + toRemove - 1, m_series->slices().size() + m_first - 1); // get the index of the last item that will be removed.
427 430 for (int i = last; i >= first; i--) {
428 431 m_series->remove(m_series->slices().at(i - m_first));
429 432 m_slices.removeAt(i - m_first);
430 433 }
431 434
432 435 if (m_count != -1) {
433 436 int itemsAvailable; // check how many are available to be added
434 437 if (m_orientation == Qt::Vertical)
435 438 itemsAvailable = m_model->rowCount() - m_first - m_series->slices().size();
436 439 else
437 440 itemsAvailable = m_model->columnCount() - m_first - m_series->slices().size();
438 441 int toBeAdded = qMin(itemsAvailable, m_count - m_series->slices().size()); // add not more items than there is space left to be filled.
439 442 int currentSize = m_series->slices().size();
440 443 if (toBeAdded > 0)
441 444 for (int i = m_series->slices().size(); i < currentSize + toBeAdded; i++) {
442 445 QPieSlice *slice = new QPieSlice;
443 446 if (m_orientation == Qt::Vertical) {
444 447 slice->setValue(m_model->data(m_model->index(i + m_first, m_valuesIndex), Qt::DisplayRole).toDouble());
445 448 slice->setLabel(m_model->data(m_model->index(i + m_first, m_labelsIndex), Qt::DisplayRole).toString());
446 449 } else {
447 450 slice->setValue(m_model->data(m_model->index(m_valuesIndex, i + m_first), Qt::DisplayRole).toDouble());
448 451 slice->setLabel(m_model->data(m_model->index(m_labelsIndex, i + m_first), Qt::DisplayRole).toString());
449 452 }
450 453 slice->setLabelVisible();
451 454 m_series->insert(i, slice);
452 455 m_slices.insert(i, slice);
453 456 }
454 457 }
455 458 }
456 459 }
457 460
458 461 void QPieModelMapperPrivate::initializePieFromModel()
459 462 {
460 463 if (m_model == 0 || m_series == 0)
461 464 return;
462 465
463 466 blockSeriesSignals();
464 467 // clear current content
465 468 m_series->clear();
466 469 m_slices.clear();
467 470
468 471 // create the initial slices set
469 472 int slicePos = 0;
470 473 QModelIndex valueIndex = valueModelIndex(slicePos);
471 474 QModelIndex labelIndex = labelModelIndex(slicePos);
472 475 while (valueIndex.isValid() && labelIndex.isValid()) {
473 476 QPieSlice *slice = new QPieSlice;
474 477 slice->setLabel(m_model->data(labelIndex, Qt::DisplayRole).toString());
475 478 slice->setValue(m_model->data(valueIndex, Qt::DisplayRole).toDouble());
476 479 connect(slice, SIGNAL(labelChanged()), this, SLOT(sliceLabelChanged()));
477 480 connect(slice, SIGNAL(valueChanged()), this, SLOT(sliceValueChanged()));
478 481 m_series->append(slice);
479 482 m_slices.append(slice);
480 483 // m_series->append(m_model->data(labelIndex, Qt::DisplayRole).toString(), m_model->data(valueIndex, Qt::DisplayRole).toDouble());
481 484 slicePos++;
482 485 valueIndex = valueModelIndex(slicePos);
483 486 labelIndex = labelModelIndex(slicePos);
484 487 }
485 488 m_series->setLabelsVisible(true);
486 489 blockSeriesSignals(false);
487 490 }
488 491
489 492 #include "moc_qpiemodelmapper_p.cpp"
490 493 #include "moc_qpiemodelmapper.cpp"
491 494
492 495 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now