##// END OF EJS Templates
Refactored PieSeriesModelMapper
Marek Rosa -
r1231:c31e07d12ed2
parent child
Show More
@@ -57,8 +57,8 void QPieModelMapper::setSeries(QPieSeries *series)
57 d->m_series = series;
57 d->m_series = series;
58 d->initializePieFromModel();
58 d->initializePieFromModel();
59 // connect the signals from the series
59 // connect the signals from the series
60 connect(d->m_series, SIGNAL(added(QList<QPieSlice*>)), d, SLOT(slicesAdded()));
60 connect(d->m_series, SIGNAL(added(QList<QPieSlice*>)), d, SLOT(slicesAdded(QList<QPieSlice*>)));
61 connect(d->m_series, SIGNAL(removed(QList<QPieSlice*>)), d, SLOT(slicesRemoved()));
61 connect(d->m_series, SIGNAL(removed(QList<QPieSlice*>)), d, SLOT(slicesRemoved(QList<QPieSlice*>)));
62 // connect(d->m_model, SIGNAL(), d, SLOT());
62 // connect(d->m_model, SIGNAL(), d, SLOT());
63 }
63 }
64
64
@@ -155,6 +155,18 QPieModelMapperPrivate::QPieModelMapperPrivate(QPieModelMapper *q) :
155 m_orientation = Qt::Vertical;
155 m_orientation = Qt::Vertical;
156 m_valuesIndex = -1;
156 m_valuesIndex = -1;
157 m_labelsIndex = -1;
157 m_labelsIndex = -1;
158 m_seriesSignalsBlock = false;
159 m_modelSignalsBlock = false;
160 }
161
162 void QPieModelMapperPrivate::blockModelSignals(bool block)
163 {
164 m_modelSignalsBlock = block;
165 }
166
167 void QPieModelMapperPrivate::blockSeriesSignals(bool block)
168 {
169 m_seriesSignalsBlock = block;
158 }
170 }
159
171
160
172
@@ -192,23 +204,63 QModelIndex QPieModelMapperPrivate::labelModelIndex(int slicePos)
192 return m_model->index(m_labelsIndex, slicePos + m_first);
204 return m_model->index(m_labelsIndex, slicePos + m_first);
193 }
205 }
194
206
195 void QPieModelMapperPrivate::slicesAdded()
207 void QPieModelMapperPrivate::slicesAdded(QList<QPieSlice*> slices)
196 {
208 {
197 //
209 if (m_seriesSignalsBlock)
210 return;
211
212 if (slices.count() == 0)
213 return;
214
215 int firstIndex = m_series->slices().indexOf(slices.at(0));
216 if (firstIndex == -1)
217 return;
218
219 blockModelSignals();
220 if (m_orientation == Qt::Vertical)
221 m_model->insertRows(firstIndex + m_first, slices.count());
222 else
223 m_model->insertColumns(firstIndex + m_first, slices.count());
224
225 for(int i = firstIndex; i < firstIndex + slices.count(); i++) {
226 m_model->setData(valueModelIndex(i), slices.at(i - firstIndex)->value());
227 m_model->setData(labelModelIndex(i), slices.at(i - firstIndex)->label());
228 }
229 blockModelSignals(false);
198 }
230 }
199
231
200 void QPieModelMapperPrivate::slicesRemoved()
232 void QPieModelMapperPrivate::slicesRemoved(QList<QPieSlice*> slices)
201 {
233 {
202 //
234 if (m_seriesSignalsBlock)
235 return;
236
237 if (slices.count() == 0)
238 return;
239
240 int firstIndex = m_series->slices().indexOf(slices.at(0));
241 if (firstIndex == -1)
242 return;
243
244 blockModelSignals();
245 if (m_orientation == Qt::Vertical)
246 m_model->removeRows(firstIndex + m_first, slices.count());
247 else
248 m_model->removeColumns(firstIndex + m_first, slices.count());
249 blockModelSignals(false);
203 }
250 }
204
251
205 void QPieModelMapperPrivate::sliceChanged()
252 void QPieModelMapperPrivate::sliceChanged()
206 {
253 {
207 //
254 blockModelSignals();
255 blockModelSignals(false);
208 }
256 }
209
257
210 void QPieModelMapperPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
258 void QPieModelMapperPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
211 {
259 {
260 if (m_modelSignalsBlock)
261 return;
262
263 blockSeriesSignals();
212 QModelIndex index;
264 QModelIndex index;
213 QPieSlice *slice;
265 QPieSlice *slice;
214 for (int row = topLeft.row(); row <= bottomRight.row(); row++) {
266 for (int row = topLeft.row(); row <= bottomRight.row(); row++) {
@@ -219,64 +271,66 void QPieModelMapperPrivate::modelUpdated(QModelIndex topLeft, QModelIndex botto
219 slice->setValue(m_model->data(index, Qt::DisplayRole).toReal());
271 slice->setValue(m_model->data(index, Qt::DisplayRole).toReal());
220 slice->setLabel(m_model->data(index, Qt::DisplayRole).toString());
272 slice->setLabel(m_model->data(index, Qt::DisplayRole).toString());
221 }
273 }
222
223 // if (m_orientation == Qt::Vertical)
224 // {
225 // if ( topLeft.row() >= m_first && (m_count == - 1 || topLeft.row() < m_first + m_count)) {
226 // if (topLeft.column() == m_valuesIndex)
227 // m_series->slices().at(topLeft.row() - m_first)->setValue(m_model->data(topLeft, Qt::DisplayRole).toDouble());
228 // if (topLeft.column() == m_labelsIndex)
229 // m_series->slices().at(topLeft.row() - m_first)->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString());
230 // }
231 // }
232 // else
233 // {
234 // if (topLeft.column() >= m_first && (m_count == - 1 || topLeft.column() < m_first + m_count)) {
235 // if (topLeft.row() == m_valuesIndex)
236 // m_series->slices().at(topLeft.column() - m_first)->setValue(m_model->data(topLeft, Qt::DisplayRole).toDouble());
237 // if (topLeft.row() == m_labelsIndex)
238 // m_series->slices().at(topLeft.column() - m_first)->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString());
239 // }
240 // }
241 }
274 }
242 }
275 }
276 blockSeriesSignals(false);
243 }
277 }
244
278
245
279
246 void QPieModelMapperPrivate::modelRowsAdded(QModelIndex parent, int start, int end)
280 void QPieModelMapperPrivate::modelRowsAdded(QModelIndex parent, int start, int end)
247 {
281 {
248 Q_UNUSED(parent);
282 Q_UNUSED(parent);
283 if (m_modelSignalsBlock)
284 return;
285
286 blockSeriesSignals();
249 if (m_orientation == Qt::Vertical)
287 if (m_orientation == Qt::Vertical)
250 insertData(start, end);
288 insertData(start, end);
251 else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie
289 else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie
252 initializePieFromModel();
290 initializePieFromModel();
291 blockSeriesSignals(false);
253 }
292 }
254
293
255 void QPieModelMapperPrivate::modelRowsRemoved(QModelIndex parent, int start, int end)
294 void QPieModelMapperPrivate::modelRowsRemoved(QModelIndex parent, int start, int end)
256 {
295 {
257 Q_UNUSED(parent);
296 Q_UNUSED(parent);
297 if (m_modelSignalsBlock)
298 return;
299
300 blockSeriesSignals();
258 if (m_orientation == Qt::Vertical)
301 if (m_orientation == Qt::Vertical)
259 removeData(start, end);
302 removeData(start, end);
260 else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie
303 else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie
261 initializePieFromModel();
304 initializePieFromModel();
305 blockSeriesSignals(false);
262 }
306 }
263
307
264 void QPieModelMapperPrivate::modelColumnsAdded(QModelIndex parent, int start, int end)
308 void QPieModelMapperPrivate::modelColumnsAdded(QModelIndex parent, int start, int end)
265 {
309 {
266 Q_UNUSED(parent);
310 Q_UNUSED(parent);
311 if (m_modelSignalsBlock)
312 return;
313
314 blockSeriesSignals();
267 if (m_orientation == Qt::Horizontal)
315 if (m_orientation == Qt::Horizontal)
268 insertData(start, end);
316 insertData(start, end);
269 else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie
317 else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie
270 initializePieFromModel();
318 initializePieFromModel();
319 blockSeriesSignals(false);
271 }
320 }
272
321
273 void QPieModelMapperPrivate::modelColumnsRemoved(QModelIndex parent, int start, int end)
322 void QPieModelMapperPrivate::modelColumnsRemoved(QModelIndex parent, int start, int end)
274 {
323 {
275 Q_UNUSED(parent);
324 Q_UNUSED(parent);
325 if (m_modelSignalsBlock)
326 return;
327
328 blockSeriesSignals();
276 if (m_orientation == Qt::Horizontal)
329 if (m_orientation == Qt::Horizontal)
277 removeData(start, end);
330 removeData(start, end);
278 else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie
331 else if (start <= m_valuesIndex || start <= m_labelsIndex) // if the changes affect the map - reinitialize the pie
279 initializePieFromModel();
332 initializePieFromModel();
333 blockSeriesSignals(false);
280 }
334 }
281
335
282 void QPieModelMapperPrivate::insertData(int start, int end)
336 void QPieModelMapperPrivate::insertData(int start, int end)
@@ -293,16 +347,11 void QPieModelMapperPrivate::insertData(int start, int end)
293 QPieSlice *slice = new QPieSlice;
347 QPieSlice *slice = new QPieSlice;
294 slice->setValue(m_model->data(valueModelIndex(i - m_first), Qt::DisplayRole).toDouble());
348 slice->setValue(m_model->data(valueModelIndex(i - m_first), Qt::DisplayRole).toDouble());
295 slice->setLabel(m_model->data(labelModelIndex(i - m_first), Qt::DisplayRole).toString());
349 slice->setLabel(m_model->data(labelModelIndex(i - m_first), Qt::DisplayRole).toString());
296 // if (m_orientation == Qt::Vertical) {
297 // slice->setValue(m_model->data(m_model->index(i, m_valuesIndex), Qt::DisplayRole).toDouble());
298 // slice->setLabel(m_model->data(m_model->index(i, m_labelsIndex), Qt::DisplayRole).toString());
299 // } else {
300 // slice->setValue(m_model->data(m_model->index(m_valuesIndex, i), Qt::DisplayRole).toDouble());
301 // slice->setLabel(m_model->data(m_model->index(m_labelsIndex, i), Qt::DisplayRole).toString());
302 // }
303 slice->setLabelVisible();
350 slice->setLabelVisible();
304 m_series->insert(i - m_first, slice);
351 m_series->insert(i - m_first, slice);
305 }
352 }
353
354 // remove excess of slices (abouve m_count)
306 if (m_count != -1 && m_series->slices().size() > m_count)
355 if (m_count != -1 && m_series->slices().size() > m_count)
307 for (int i = m_series->slices().size() - 1; i >= m_count; i--)
356 for (int i = m_series->slices().size() - 1; i >= m_count; i--)
308 m_series->remove(m_series->slices().at(i));
357 m_series->remove(m_series->slices().at(i));
@@ -358,6 +407,7 void QPieModelMapperPrivate::initializePieFromModel()
358 if (m_valuesIndex == -1 || m_labelsIndex == -1)
407 if (m_valuesIndex == -1 || m_labelsIndex == -1)
359 return;
408 return;
360
409
410 blockSeriesSignals();
361 // create the initial slices set
411 // create the initial slices set
362 int slicePos = 0;
412 int slicePos = 0;
363 QModelIndex valueIndex = valueModelIndex(slicePos);
413 QModelIndex valueIndex = valueModelIndex(slicePos);
@@ -368,53 +418,8 void QPieModelMapperPrivate::initializePieFromModel()
368 valueIndex = valueModelIndex(slicePos);
418 valueIndex = valueModelIndex(slicePos);
369 labelIndex = labelModelIndex(slicePos);
419 labelIndex = labelModelIndex(slicePos);
370 }
420 }
371
372 // if (m_orientation == Qt::Vertical) {
373 // if (m_valuesIndex >= m_model->columnCount() || m_labelsIndex >= m_model->columnCount())
374 // return; // mapped columns are not existing
375
376 // int sliceCount = 0;
377 // if(m_count == -1)
378 // sliceCount = m_model->rowCount() - m_first;
379 // else
380 // sliceCount = qMin(m_count, m_model->rowCount() - m_first);
381 // } else {
382 // if (m_valuesIndex >= m_model->rowCount() || m_labelsIndex >= m_model->rowCount())
383 // return; // mapped columns are not existing
384
385 // int sliceCount = 0;
386 // if(m_count == -1)
387 // sliceCount = m_model->columnCount() - m_first;
388 // else
389 // sliceCount = qMin(m_count, m_model->columnCount() - m_first);
390 // }
391 // for (int i = m_first; i < m_first + sliceCount; i++)
392 // m_series->append(m_model->data(labelModelIndex(i), Qt::DisplayRole).toString(), m_model->data(valueModelIndex(i), Qt::DisplayRole).toDouble());
393 m_series->setLabelsVisible(true);
421 m_series->setLabelsVisible(true);
394 // // create the initial slices set
422 blockSeriesSignals(false);
395 // if (m_orientation == Qt::Vertical) {
396 // if (m_valuesIndex >= m_model->columnCount() || m_labelsIndex >= m_model->columnCount())
397 // return; // mapped columns are not existing
398
399 // int sliceCount = 0;
400 // if(m_count == -1)
401 // sliceCount = m_model->rowCount() - m_first;
402 // else
403 // sliceCount = qMin(m_count, m_model->rowCount() - m_first);
404 // for (int i = m_first; i < m_first + sliceCount; i++)
405 // m_series->append(m_model->data(m_model->index(i, m_labelsIndex), Qt::DisplayRole).toString(), m_model->data(m_model->index(i, m_valuesIndex), Qt::DisplayRole).toDouble());
406 // } else {
407 // if (m_valuesIndex >= m_model->rowCount() || m_labelsIndex >= m_model->rowCount())
408 // return; // mapped columns are not existing
409
410 // int sliceCount = 0;
411 // if(m_count == -1)
412 // sliceCount = m_model->columnCount() - m_first;
413 // else
414 // sliceCount = qMin(m_count, m_model->columnCount() - m_first);
415 // for (int i = m_first; i < m_first + sliceCount; i++)
416 // m_series->append(m_model->data(m_model->index(m_labelsIndex, i), Qt::DisplayRole).toString(), m_model->data(m_model->index(m_valuesIndex, i), Qt::DisplayRole).toDouble());
417 // m_series->setLabelsVisible(true);
418 }
423 }
419
424
420 #include "moc_qpiemodelmapper_p.cpp"
425 #include "moc_qpiemodelmapper_p.cpp"
@@ -29,8 +29,8 public Q_SLOTS:
29 void modelColumnsRemoved(QModelIndex parent, int start, int end);
29 void modelColumnsRemoved(QModelIndex parent, int start, int end);
30
30
31 // for the series
31 // for the series
32 void slicesAdded();
32 void slicesAdded(QList<QPieSlice*> slices);
33 void slicesRemoved();
33 void slicesRemoved(QList<QPieSlice*> slices);
34 void sliceChanged();
34 void sliceChanged();
35
35
36 void initializePieFromModel();
36 void initializePieFromModel();
@@ -42,7 +42,12 private:
42 void insertData(int start, int end);
42 void insertData(int start, int end);
43 void removeData(int start, int end);
43 void removeData(int start, int end);
44
44
45 void blockModelSignals(bool block = true);
46 void blockSeriesSignals(bool block = true);
47
45 private:
48 private:
49 bool m_seriesSignalsBlock;
50 bool m_modelSignalsBlock;
46 QPieSeries *m_series;
51 QPieSeries *m_series;
47 QAbstractItemModel *m_model;
52 QAbstractItemModel *m_model;
48 int m_first;
53 int m_first;
@@ -81,9 +81,12 TableWidget::TableWidget(QWidget *parent)
81 QPushButton* removeColumnButton = new QPushButton("Remove column");
81 QPushButton* removeColumnButton = new QPushButton("Remove column");
82 connect(removeColumnButton, SIGNAL(clicked()), this, SLOT(removeColumn()));
82 connect(removeColumnButton, SIGNAL(clicked()), this, SLOT(removeColumn()));
83
83
84 QPushButton* specialPieButton = new QPushButton("Test pie");
84 QPushButton* specialPieButton = new QPushButton("Add slices from series");
85 connect(specialPieButton, SIGNAL(clicked()), this, SLOT(testPie()));
85 connect(specialPieButton, SIGNAL(clicked()), this, SLOT(testPie()));
86
86
87 QPushButton* specialPieButton2 = new QPushButton("Remove slices from series");
88 connect(specialPieButton2, SIGNAL(clicked()), this, SLOT(testPie2()));
89
87
90
88 // QLabel *spinBoxLabel = new QLabel("Rows affected:");
91 // QLabel *spinBoxLabel = new QLabel("Rows affected:");
89
92
@@ -102,6 +105,7 TableWidget::TableWidget(QWidget *parent)
102 // buttonsLayout->addWidget(addColumnRightButton);
105 // buttonsLayout->addWidget(addColumnRightButton);
103 // buttonsLayout->addWidget(removeColumnButton);
106 // buttonsLayout->addWidget(removeColumnButton);
104 buttonsLayout->addWidget(specialPieButton);
107 buttonsLayout->addWidget(specialPieButton);
108 buttonsLayout->addWidget(specialPieButton2);
105 buttonsLayout->addStretch();
109 buttonsLayout->addStretch();
106
110
107 // chart type radio buttons
111 // chart type radio buttons
@@ -339,25 +343,25 void TableWidget::updateChartType(bool toggle)
339 m_chart->setAnimationOptions(QChart::SeriesAnimations);
343 m_chart->setAnimationOptions(QChart::SeriesAnimations);
340
344
341 // pie 1
345 // pie 1
342 QPieSeries* pieSeries = new QPieSeries;
346 m_pieSeries = new QPieSeries;
343
347
344 m_pieMapper = new QPieModelMapper;
348 m_pieMapper = new QPieModelMapper;
345 m_pieMapper->setValuesIndex(1);
349 m_pieMapper->setValuesIndex(1);
346 m_pieMapper->setLabelsIndex(1);
350 m_pieMapper->setLabelsIndex(1);
347 m_pieMapper->setSeries(pieSeries);
351 m_pieMapper->setSeries(m_pieSeries);
348 m_pieMapper->setModel(m_model);
352 m_pieMapper->setModel(m_model);
349 m_pieMapper->setFirst(2);
353 m_pieMapper->setFirst(2);
350 m_pieMapper->setCount(5);
354 // m_pieMapper->setCount(5);
351 // pieSeries->setModelMapper(mapper);
355 // pieSeries->setModelMapper(mapper);
352
356
353 pieSeries->setLabelsVisible(true);
357 m_pieSeries->setLabelsVisible(true);
354 pieSeries->setPieSize(0.75);
358 m_pieSeries->setPieSize(0.75);
355 // pieSeries->setHorizontalPosition(0.2);
359 // pieSeries->setHorizontalPosition(0.2);
356 // pieSeries->setVerticalPosition(0.3);
360 // pieSeries->setVerticalPosition(0.3);
357
361
358 m_chart->addSeries(pieSeries);
362 m_chart->addSeries(m_pieSeries);
359 seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
363 seriesColorHex = "#" + QString::number(m_pieSeries->slices().at(m_pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
360 m_model->addMapping(seriesColorHex, QRect(0, 2, 2, 5));
364 m_model->addMapping(seriesColorHex, QRect(0, 2, 2, 50));
361
365
362
366
363 // pieSeries->slices().at(0)->setValue(400);
367 // pieSeries->slices().at(0)->setValue(400);
@@ -458,7 +462,14 void TableWidget::updateChartType(bool toggle)
458
462
459 void TableWidget::testPie()
463 void TableWidget::testPie()
460 {
464 {
461 m_pieMapper->setCount(-1);
465 // m_pieMapper->setCount(-1);
466 QPieSlice *slice = new QPieSlice("Hehe", 145);
467 slice->setLabelVisible();
468 m_pieSeries->append(slice);
469
470 slice = new QPieSlice("Hoho", 34);
471 slice->setLabelVisible();
472 m_pieSeries->append(slice);
462 // m_series->modelMapper()->setMapX(4);
473 // m_series->modelMapper()->setMapX(4);
463 // m_tableView->setColumnWidth(10, 250);
474 // m_tableView->setColumnWidth(10, 250);
464 // if (specialPie) {
475 // if (specialPie) {
@@ -468,6 +479,20 void TableWidget::testPie()
468 // }
479 // }
469 }
480 }
470
481
482 void TableWidget::testPie2()
483 {
484 QPieSlice *slice;
485 if (m_pieSeries->count() > 0) {
486 slice = m_pieSeries->slices().last();
487 m_pieSeries->remove(slice);
488 }
489
490 if (m_pieSeries->count() > 0) {
491 slice = m_pieSeries->slices().first();
492 m_pieSeries->remove(slice);
493 }
494 }
495
471 TableWidget::~TableWidget()
496 TableWidget::~TableWidget()
472 {
497 {
473
498
@@ -25,7 +25,7
25 //#include <QChartGlobal>
25 //#include <QChartGlobal>
26 #include "qchartview.h"
26 #include "qchartview.h"
27 //#include "qxyseries.h"
27 //#include "qxyseries.h"
28 //#include <QPieSeries>
28 #include <QPieSeries>
29 #include <QPieModelMapper>
29 #include <QPieModelMapper>
30
30
31 class CustomTableModel;
31 class CustomTableModel;
@@ -52,6 +52,7 public:
52 void removeColumn();
52 void removeColumn();
53 void updateChartType(bool toggle);
53 void updateChartType(bool toggle);
54 void testPie();
54 void testPie();
55 void testPie2();
55
56
56 private:
57 private:
57 QChartView* m_chartView;
58 QChartView* m_chartView;
@@ -67,6 +68,7 public:
67 QRadioButton* m_barRadioButton;
68 QRadioButton* m_barRadioButton;
68 QSpinBox* m_linesCountSpinBox;
69 QSpinBox* m_linesCountSpinBox;
69 QPieModelMapper *m_pieMapper;
70 QPieModelMapper *m_pieMapper;
71 QPieSeries* m_pieSeries;
70 // QPieSeries* specialPie;
72 // QPieSeries* specialPie;
71 };
73 };
72
74
General Comments 0
You need to be logged in to leave comments. Login now