##// END OF EJS Templates
QPieModelMapper: added support for labelChanged and valueChanged signals from the slice
Marek Rosa -
r1236:a37d91e87542
parent child
Show More
@@ -216,6 +216,15 void QPieModelMapperPrivate::slicesAdded(QList<QPieSlice*> slices)
216 216 if (firstIndex == -1)
217 217 return;
218 218
219 if (m_count != -1)
220 m_count += slices.count();
221
222 for (int i = firstIndex; i < firstIndex + slices.count(); i++) {
223 m_slices.insert(i, slices.at(i - firstIndex));
224 connect(slices.at(i - firstIndex), SIGNAL(labelChanged()), this, SLOT(sliceLabelChanged()));
225 connect(slices.at(i - firstIndex), SIGNAL(valueChanged()), this, SLOT(sliceValueChanged()));
226 }
227
219 228 blockModelSignals();
220 229 if (m_orientation == Qt::Vertical)
221 230 m_model->insertRows(firstIndex + m_first, slices.count());
@@ -237,10 +246,16 void QPieModelMapperPrivate::slicesRemoved(QList<QPieSlice*> slices)
237 246 if (slices.count() == 0)
238 247 return;
239 248
240 int firstIndex = m_series->slices().indexOf(slices.at(0));
249 int firstIndex = m_slices.indexOf(slices.at(0));
241 250 if (firstIndex == -1)
242 251 return;
243 252
253 if (m_count != -1)
254 m_count -= slices.count();
255
256 for (int i = firstIndex + slices.count() - 1; i >= firstIndex; i--)
257 m_slices.removeAt(i);
258
244 259 blockModelSignals();
245 260 if (m_orientation == Qt::Vertical)
246 261 m_model->removeRows(firstIndex + m_first, slices.count());
@@ -249,9 +264,25 void QPieModelMapperPrivate::slicesRemoved(QList<QPieSlice*> slices)
249 264 blockModelSignals(false);
250 265 }
251 266
252 void QPieModelMapperPrivate::sliceChanged()
267 void QPieModelMapperPrivate::sliceLabelChanged()
253 268 {
269 if (m_seriesSignalsBlock)
270 return;
271
254 272 blockModelSignals();
273 QPieSlice *slice = qobject_cast<QPieSlice *>(QObject::sender());
274 m_model->setData(labelModelIndex(m_series->slices().indexOf(slice)), slice->label());
275 blockModelSignals(false);
276 }
277
278 void QPieModelMapperPrivate::sliceValueChanged()
279 {
280 if (m_seriesSignalsBlock)
281 return;
282
283 blockModelSignals();
284 QPieSlice *slice = qobject_cast<QPieSlice *>(QObject::sender());
285 m_model->setData(valueModelIndex(m_series->slices().indexOf(slice)), slice->value());
255 286 blockModelSignals(false);
256 287 }
257 288
@@ -348,13 +379,18 void QPieModelMapperPrivate::insertData(int start, int end)
348 379 slice->setValue(m_model->data(valueModelIndex(i - m_first), Qt::DisplayRole).toDouble());
349 380 slice->setLabel(m_model->data(labelModelIndex(i - m_first), Qt::DisplayRole).toString());
350 381 slice->setLabelVisible();
382 connect(slice, SIGNAL(labelChanged()), this, SLOT(sliceLabelChanged()));
383 connect(slice, SIGNAL(valueChanged()), this, SLOT(sliceValueChanged()));
351 384 m_series->insert(i - m_first, slice);
385 m_slices.insert(i - m_first, slice);
352 386 }
353 387
354 388 // remove excess of slices (abouve m_count)
355 389 if (m_count != -1 && m_series->slices().size() > m_count)
356 for (int i = m_series->slices().size() - 1; i >= m_count; i--)
390 for (int i = m_series->slices().size() - 1; i >= m_count; i--) {
357 391 m_series->remove(m_series->slices().at(i));
392 m_slices.removeAt(i);
393 }
358 394 }
359 395 }
360 396
@@ -367,8 +403,10 void QPieModelMapperPrivate::removeData(int start, int end)
367 403 int toRemove = qMin(m_series->slices().size(), removedCount); // first find how many items can actually be removed
368 404 int first = qMax(start, m_first); // get the index of the first item that will be removed.
369 405 int last = qMin(first + toRemove - 1, m_series->slices().size() + m_first - 1); // get the index of the last item that will be removed.
370 for (int i = last; i >= first; i--)
406 for (int i = last; i >= first; i--) {
371 407 m_series->remove(m_series->slices().at(i - m_first));
408 m_slices.removeAt(i - m_first);
409 }
372 410
373 411 if (m_count != -1) {
374 412 int itemsAvailable; // check how many are available to be added
@@ -390,6 +428,7 void QPieModelMapperPrivate::removeData(int start, int end)
390 428 }
391 429 slice->setLabelVisible();
392 430 m_series->insert(i, slice);
431 m_slices.insert(i, slice);
393 432 }
394 433 }
395 434 }
@@ -400,20 +439,28 void QPieModelMapperPrivate::initializePieFromModel()
400 439 if (m_model == 0 || m_series == 0)
401 440 return;
402 441
403 // clear current content
404 m_series->clear();
405
406 442 // check if mappings are set
407 443 if (m_valuesIndex == -1 || m_labelsIndex == -1)
408 444 return;
409 445
410 446 blockSeriesSignals();
447 // clear current content
448 m_series->clear();
449 m_slices.clear();
450
411 451 // create the initial slices set
412 452 int slicePos = 0;
413 453 QModelIndex valueIndex = valueModelIndex(slicePos);
414 454 QModelIndex labelIndex = labelModelIndex(slicePos);
415 455 while (valueIndex.isValid() && labelIndex.isValid()) {
416 m_series->append(m_model->data(labelIndex, Qt::DisplayRole).toString(), m_model->data(valueIndex, Qt::DisplayRole).toDouble());
456 QPieSlice *slice = new QPieSlice;
457 slice->setLabel(m_model->data(labelIndex, Qt::DisplayRole).toString());
458 slice->setValue(m_model->data(valueIndex, Qt::DisplayRole).toDouble());
459 connect(slice, SIGNAL(labelChanged()), this, SLOT(sliceLabelChanged()));
460 connect(slice, SIGNAL(valueChanged()), this, SLOT(sliceValueChanged()));
461 m_series->append(slice);
462 m_slices.append(slice);
463 // m_series->append(m_model->data(labelIndex, Qt::DisplayRole).toString(), m_model->data(valueIndex, Qt::DisplayRole).toDouble());
417 464 slicePos++;
418 465 valueIndex = valueModelIndex(slicePos);
419 466 labelIndex = labelModelIndex(slicePos);
@@ -47,7 +47,7 public:
47 47
48 48 void reset();
49 49
50 private:
50 protected:
51 51 QPieModelMapperPrivate * const d_ptr;
52 52 Q_DECLARE_PRIVATE(QPieModelMapper)
53 53 };
@@ -31,7 +31,8 public Q_SLOTS:
31 31 // for the series
32 32 void slicesAdded(QList<QPieSlice*> slices);
33 33 void slicesRemoved(QList<QPieSlice*> slices);
34 void sliceChanged();
34 void sliceLabelChanged();
35 void sliceValueChanged();
35 36
36 37 void initializePieFromModel();
37 38
@@ -49,6 +50,7 private:
49 50 bool m_seriesSignalsBlock;
50 51 bool m_modelSignalsBlock;
51 52 QPieSeries *m_series;
53 QList<QPieSlice*> m_slices;
52 54 QAbstractItemModel *m_model;
53 55 int m_first;
54 56 int m_count;
@@ -87,6 +87,9 TableWidget::TableWidget(QWidget *parent)
87 87 QPushButton* specialPieButton2 = new QPushButton("Remove slices from series");
88 88 connect(specialPieButton2, SIGNAL(clicked()), this, SLOT(testPie2()));
89 89
90 QPushButton* specialPieButton3 = new QPushButton("Remove slices from series");
91 connect(specialPieButton3, SIGNAL(clicked()), this, SLOT(testPie3()));
92
90 93
91 94 // QLabel *spinBoxLabel = new QLabel("Rows affected:");
92 95
@@ -106,6 +109,7 TableWidget::TableWidget(QWidget *parent)
106 109 // buttonsLayout->addWidget(removeColumnButton);
107 110 buttonsLayout->addWidget(specialPieButton);
108 111 buttonsLayout->addWidget(specialPieButton2);
112 buttonsLayout->addWidget(specialPieButton3);
109 113 buttonsLayout->addStretch();
110 114
111 115 // chart type radio buttons
@@ -347,11 +351,11 void TableWidget::updateChartType(bool toggle)
347 351
348 352 m_pieMapper = new QPieModelMapper;
349 353 m_pieMapper->setValuesIndex(1);
350 m_pieMapper->setLabelsIndex(1);
354 m_pieMapper->setLabelsIndex(7);
351 355 m_pieMapper->setSeries(m_pieSeries);
352 356 m_pieMapper->setModel(m_model);
353 357 m_pieMapper->setFirst(2);
354 // m_pieMapper->setCount(5);
358 m_pieMapper->setCount(5);
355 359 // pieSeries->setModelMapper(mapper);
356 360
357 361 m_pieSeries->setLabelsVisible(true);
@@ -361,7 +365,7 void TableWidget::updateChartType(bool toggle)
361 365
362 366 m_chart->addSeries(m_pieSeries);
363 367 seriesColorHex = "#" + QString::number(m_pieSeries->slices().at(m_pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
364 m_model->addMapping(seriesColorHex, QRect(0, 2, 2, 50));
368 m_model->addMapping(seriesColorHex, QRect(0, 2, 2, 5));
365 369
366 370
367 371 // pieSeries->slices().at(0)->setValue(400);
@@ -493,6 +497,22 void TableWidget::testPie2()
493 497 }
494 498 }
495 499
500 void TableWidget::testPie3()
501 {
502 QPieSlice *slice;
503 if (m_pieSeries->count() > 0) {
504 slice = m_pieSeries->slices().last();
505 slice->setLabel("Dalej");
506 slice->setValue(222);
507 }
508
509 if (m_pieSeries->count() > 0) {
510 slice = m_pieSeries->slices().first();
511 slice->setLabel("Prawie");
512 slice->setValue(111);
513 }
514 }
515
496 516 TableWidget::~TableWidget()
497 517 {
498 518
@@ -53,6 +53,7 public:
53 53 void updateChartType(bool toggle);
54 54 void testPie();
55 55 void testPie2();
56 void testPie3();
56 57
57 58 private:
58 59 QChartView* m_chartView;
General Comments 0
You need to be logged in to leave comments. Login now