##// END OF EJS Templates
BarSeries Model mapper added
Marek Rosa -
r1170:0973e1eca7c6
parent child
Show More
@@ -0,0 +1,94
1 #include "qbarmodelmapper.h"
2
3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4
5 QBarModelMapper::QBarModelMapper(QObject *parent) :
6 QObject(parent),
7 m_first(0),
8 m_count(-1),
9 m_orientation(Qt::Vertical),
10 m_mapBarBottom(-1),
11 m_mapBarTop(-1),
12 m_mapCategories(-1)
13 {
14 }
15
16 int QBarModelMapper::first() const
17 {
18 return m_first;
19 }
20
21 void QBarModelMapper::setFirst(int first)
22 {
23 m_first = qMax(first, 0);
24 emit updated();
25 }
26
27 int QBarModelMapper::count() const
28 {
29 return m_count;
30 }
31
32 void QBarModelMapper::setCount(int count)
33 {
34 m_count = qMax(count, -1);
35 emit updated();
36 }
37
38 Qt::Orientation QBarModelMapper::orientation() const
39 {
40 return m_orientation;
41 }
42
43 void QBarModelMapper::setOrientation(Qt::Orientation orientation)
44 {
45 m_orientation = orientation;
46 emit updated();
47 }
48
49 int QBarModelMapper::mapBarBottom() const
50 {
51 return m_mapBarBottom;
52 }
53
54 void QBarModelMapper::setMapBarBottom(int mapValues)
55 {
56 m_mapBarBottom = mapValues;
57 emit updated();
58 }
59
60 int QBarModelMapper::mapBarTop() const
61 {
62 return m_mapBarTop;
63 }
64
65 void QBarModelMapper::setMapBarTop(int mapLabels)
66 {
67 m_mapBarTop = mapLabels;
68 emit updated();
69 }
70
71 int QBarModelMapper::mapCategories() const
72 {
73 return m_mapCategories;
74 }
75
76 void QBarModelMapper::setMapCategories(int mapCategories)
77 {
78 m_mapCategories = mapCategories;
79 emit updated();
80 }
81
82 void QBarModelMapper::reset()
83 {
84 m_first = 0;
85 m_count = -1;
86 m_orientation = Qt::Vertical;
87 m_mapBarBottom = -1;
88 m_mapBarTop = -1;
89 m_mapCategories = -1;
90 }
91
92 #include "moc_qbarmodelmapper.cpp"
93
94 QTCOMMERCIALCHART_END_NAMESPACE
@@ -0,0 +1,50
1 #ifndef QBARMODELMAPPER_H
2 #define QBARMODELMAPPER_H
3
4 #include "qchartglobal.h"
5 #include <QObject>
6
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8
9 class QTCOMMERCIALCHART_EXPORT QBarModelMapper : public QObject
10 {
11 Q_OBJECT
12 public:
13 explicit QBarModelMapper(QObject *parent = 0);
14
15 int first() const;
16 void setFirst(int first);
17
18 int count() const;
19 void setCount(int count);
20
21 Qt::Orientation orientation() const;
22 void setOrientation(Qt::Orientation orientation);
23
24 int mapBarBottom() const;
25 void setMapBarBottom(int mapBarBottom);
26
27 int mapBarTop() const;
28 void setMapBarTop(int mapBarTop);
29
30 int mapCategories() const;
31 void setMapCategories(int mapCategories);
32
33 void reset();
34
35 Q_SIGNALS:
36 void updated();
37
38 private:
39 int m_first;
40 int m_count;
41 Qt::Orientation m_orientation;
42 int m_mapBarBottom;
43 int m_mapBarTop;
44 int m_mapCategories;
45
46 };
47
48 QTCOMMERCIALCHART_END_NAMESPACE
49
50 #endif // QBARMODELMAPPER_H
@@ -13,6 +13,7 SOURCES += \
13 $$PWD/qgroupedbarseries.cpp \
13 $$PWD/qgroupedbarseries.cpp \
14 $$PWD/stackedbarchartitem.cpp \
14 $$PWD/stackedbarchartitem.cpp \
15 $$PWD/barlabel.cpp \
15 $$PWD/barlabel.cpp \
16 $$PWD/qbarmodelmapper.cpp
16
17
17 PRIVATE_HEADERS += \
18 PRIVATE_HEADERS += \
18 $$PWD/bar_p.h \
19 $$PWD/bar_p.h \
@@ -32,5 +33,5 PUBLIC_HEADERS += \
32 $$PWD/qbarset.h \
33 $$PWD/qbarset.h \
33 $$PWD/qpercentbarseries.h \
34 $$PWD/qpercentbarseries.h \
34 $$PWD/qstackedbarseries.h \
35 $$PWD/qstackedbarseries.h \
35 $$PWD/qgroupedbarseries.h
36 $$PWD/qgroupedbarseries.h \
36
37 $$PWD/qbarmodelmapper.h
@@ -30,6 +30,7
30
30
31 #include <QAbstractItemModel>
31 #include <QAbstractItemModel>
32 #include <QModelIndex>
32 #include <QModelIndex>
33 #include <QBarModelMapper>
33
34
34 QTCOMMERCIALCHART_BEGIN_NAMESPACE
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
35
36
@@ -215,30 +216,59 QList<QBarSet*> QBarSeries::barSets() const
215 */
216 */
216 void QBarSeries::setModel(QAbstractItemModel *model)
217 void QBarSeries::setModel(QAbstractItemModel *model)
217 {
218 {
218 Q_UNUSED(model);
219 Q_D(QBarSeries);
219 // Q_D(QBarSeries);
220 // disconnect signals from old model
220 // d->setModel(model);
221 if(d->m_model)
222 {
223 disconnect(d->m_model, 0, this, 0);
224 }
225
226 // set new model
227 if(model)
228 {
229 d->m_model = model;
230
231 // connect the signals
232 connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex)));
233 connect(d->m_model, SIGNAL(rowsInserted(QModelIndex,int,int)), d, SLOT(modelDataAdded(QModelIndex,int,int)));
234 connect(d->m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)), d, SLOT(modelDataRemoved(QModelIndex,int,int)));
235 connect(d->m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), d, SLOT(modelDataAdded(QModelIndex,int,int)));
236 connect(d->m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), d, SLOT(modelDataRemoved(QModelIndex,int,int)));
237
238 if (d->m_mapper)
239 d->initializeDataFromModel();
240 }
241 else
242 {
243 d->m_model = 0;
244 }
221 }
245 }
222
246
223 /*!
247 void QBarSeries::setModelMapper(QBarModelMapper *mapper)
224 \fn bool QBarSeries::setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation)
248 {
225 Sets column/row specified by \a categories to be used as a list of bar series categories.
249 Q_D(QBarSeries);
226 Parameter \a bottomBoundry indicates the column/row where the first bar set is located in the model.
250 // disconnect signals from old mapper
227 Parameter \a topBoundry indicates the column/row where the last bar set is located in the model.
251 if (d->m_mapper) {
228 All the columns/rows inbetween those two values are also used as data for bar sets.
252 QObject::disconnect(d->m_mapper, 0, this, 0);
229 The \a orientation parameter specifies whether the data is in columns or in rows.
253 }
230 */
254
231 //void QBarSeries::setModelMapping(int categories, int bottomBoundary, int topBoundary, Qt::Orientation orientation)
255 if (mapper) {
232 //{
256 d->m_mapper = mapper;
233 // Q_D(QBarSeries);
257 // connect the signal from the mapper
234 // d->setModelMapping(categories,bottomBoundary,topBoundary,orientation);
258 connect(d->m_mapper, SIGNAL(updated()), d, SLOT(initializePieFromModel()));
235 //}
259
236
260 if (d->m_model)
237 //void QBarSeries::setModelMappingRange(int first, int count)
261 d->initializeDataFromModel();
238 //{
262 } else {
239 // Q_D(QBarSeries);
263 d->m_mapper = 0;
240 // d->setModelMappingRange(first, count);
264 }
241 //}
265 }
266
267 QBarModelMapper* QBarSeries::modelMapper() const
268 {
269 Q_D(const QBarSeries);
270 return d->m_mapper;
271 }
242
272
243 /*!
273 /*!
244 Returns the bar categories of the series.
274 Returns the bar categories of the series.
@@ -264,9 +294,8 void QBarSeries::setLabelsVisible(bool visible)
264 QBarSeriesPrivate::QBarSeriesPrivate(QBarSeries *q) :
294 QBarSeriesPrivate::QBarSeriesPrivate(QBarSeries *q) :
265 QAbstractSeriesPrivate(q),
295 QAbstractSeriesPrivate(q),
266 m_barMargin(0.05), // Default value is 5% of category width
296 m_barMargin(0.05), // Default value is 5% of category width
267 m_mapCategories(-1),
297 m_mapper(0)
268 m_mapBarBottom(-1),
298 // m_categories(categories),
269 m_mapBarTop(-1)
270 {
299 {
271 }
300 }
272
301
@@ -405,184 +434,38 qreal QBarSeriesPrivate::maxCategorySum()
405 return max;
434 return max;
406 }
435 }
407
436
408 //void QBarSeriesPrivate::setModel(QAbstractItemModel *model)
437 void QBarSeriesPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
409 //{
438 {
410 // // disconnect signals from old model
439 for (int row = topLeft.row(); row <= bottomRight.row(); row++) {
411 // if(m_model)
440 for (int column = topLeft.column(); column <= bottomRight.column(); column++) {
412 // {
441 if (m_mapper->orientation() == Qt::Vertical)
413 // disconnect(m_model, 0, this, 0);
442 {
414 // m_mapCategories = -1;
443 // model update is relevant to BarSeries if the change was made to the part of the model that was mapped to BarSeries
415 // m_mapBarBottom = -1;
444 if ( row >= m_mapper->first() && (m_mapper->count() == - 1 || row < m_mapper->first() + m_mapper->count())) {
416 // m_mapBarTop = -1;
445 if (column >= m_mapper->mapBarBottom() && column <= m_mapper->mapBarTop())
417 // m_mapOrientation = Qt::Vertical;
446 barsetAt(column - m_mapper->mapBarBottom())->replace(row - m_mapper->first(), m_model->data(topLeft, Qt::DisplayRole).toDouble());
418 // }
447 // if (column == m_mapper->mapCategories());// TODO:
419
448 }
420 // // set new model
449 }
421 // if(model)
450 else
422 // {
451 {
423 // m_model = model;
452 // model update is relevant to BarSeries if the change was made to the part of the model that was mapped to BarSeries
424 // }
453 if (column >= m_mapper->first() && (m_mapper->count() == - 1 || column < m_mapper->first() + m_mapper->count())) {
425 // else
454 if (row >= m_mapper->mapBarBottom() && row <= m_mapper->mapBarTop())
426 // {
455 barsetAt(row - m_mapper->mapBarBottom())->replace(column - m_mapper->first(), m_model->data(topLeft, Qt::DisplayRole).toDouble());
427 // m_model = 0;
456 // if (row == m_mapper->mapCategories());// TODO:
428 // }
457 }
429 //}
458 }
430
459 }
431 //void QBarSeriesPrivate::setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation)
460 }
432 //{
461 }
433 // Q_Q(QBarSeries);
434
435 // if (m_model == 0)
436 // return;
437
438 // m_mapCategories = categories;
439 // m_mapBarBottom = bottomBoundry;
440 // m_mapBarTop = topBoundry;
441 // m_mapOrientation = orientation;
442
443 // // connect the signals
444 // connect(m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex,QModelIndex)));
445 // if (m_mapOrientation == Qt::Vertical) {
446 // connect(m_model,SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(modelDataAdded(QModelIndex,int,int)));
447 // connect(m_model,SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SLOT(modelDataRemoved(QModelIndex,int,int)));
448 // } else {
449 // connect(m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), this, SLOT(modelDataAdded(QModelIndex,int,int)));
450 // connect(m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), this, SLOT(modelDataRemoved(QModelIndex,int,int)));
451 // }
452
453 // // create the initial bars
454 // m_categories.clear();
455 // if (m_mapOrientation == Qt::Vertical) {
456 // int rowCount = 0;
457 // if(m_mapCount == -1)
458 // rowCount = m_model->rowCount() - m_mapFirst;
459 // else
460 // rowCount = qMin(m_mapCount, m_model->rowCount() - m_mapFirst);
461 // for (int k = m_mapFirst; k < m_mapFirst + rowCount; k++) {
462 // m_categories << m_model->data(m_model->index(k, m_mapCategories), Qt::DisplayRole).toString();
463 // }
464
465 // for (int i = m_mapBarBottom; i <= m_mapBarTop; i++) {
466 // QBarSet* barSet = new QBarSet(m_model->headerData(i, Qt::Horizontal, Qt::DisplayRole).toString());
467 // for(int m = m_mapFirst; m < m_mapFirst + rowCount; m++)
468 // *barSet << m_model->data(m_model->index(m, i), Qt::DisplayRole).toDouble();
469 // q->appendBarSet(barSet);
470 // }
471 // } else {
472 // int columnCount = 0;
473 // if(m_mapCount == -1)
474 // columnCount = m_model->columnCount() - m_mapFirst;
475 // else
476 // columnCount = qMin(m_mapCount, m_model->columnCount() - m_mapFirst);
477 // for (int k = m_mapFirst; k < m_mapFirst + columnCount; k++) {
478 // m_categories << m_model->data(m_model->index(m_mapCategories, k), Qt::DisplayRole).toString();
479 // }
480
481 // for (int i = m_mapBarBottom; i <= m_mapBarTop; i++) {
482 // QBarSet* barSet = new QBarSet(m_model->headerData(i, Qt::Vertical, Qt::DisplayRole).toString());
483 // for(int m = m_mapFirst; m < m_mapFirst + columnCount; m++)
484 // *barSet << m_model->data(m_model->index(i, m), Qt::DisplayRole).toDouble();
485 // q->appendBarSet(barSet);
486 // }
487 // }
488 //}
489
490 //void QBarSeriesPrivate::setModelMappingRange(int first, int count)
491 //{
492 // m_mapFirst = first;
493 // m_mapCount = count;
494 //}
495
496 //void QBarSeriesPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
497 //{
498 // for (int row = topLeft.row(); row <= bottomRight.row(); row++) {
499 // for (int column = topLeft.column(); column <= bottomRight.column(); column++) {
500 // if (m_mapOrientation == Qt::Vertical)
501 // {
502 // // model update is relevant to BarSeries if the change was made to the part of the model that was mapped to BarSeries
503 // if ( row >= m_mapFirst && (m_mapCount == - 1 || row < m_mapFirst + m_mapCount)) {
504 // if (column >= m_mapBarBottom && column <= m_mapBarTop)
505 // barsetAt(column - m_mapBarBottom)->replace(row - m_mapFirst, m_model->data(topLeft, Qt::DisplayRole).toDouble());
506 // // if (column == m_mapCategories);// TODO:
507 // }
508 // }
509 // else
510 // {
511 // // model update is relevant to BarSeries if the change was made to the part of the model that was mapped to BarSeries
512 // if (column >= m_mapFirst && (m_mapCount == - 1 || column < m_mapFirst + m_mapCount)) {
513 // if (row >= m_mapBarBottom && row <= m_mapBarTop)
514 // barsetAt(row - m_mapBarBottom)->replace(column - m_mapFirst, m_model->data(topLeft, Qt::DisplayRole).toDouble());
515 // // if (row == m_mapCategories);// TODO:
516 // }
517 // }
518 // }
519 // }
520 //}
521
462
522 void QBarSeriesPrivate::modelDataAdded(QModelIndex parent, int start, int end)
463 void QBarSeriesPrivate::modelDataAdded(QModelIndex parent, int start, int end)
523 {
464 {
524 Q_UNUSED(parent);
465 Q_UNUSED(parent);
525 Q_UNUSED(start);
466 Q_UNUSED(start);
526 Q_UNUSED(end);
467 Q_UNUSED(end);
527 // initializeDataFromModel();
468 initializeDataFromModel();
528 // // series uses model as a data sourceupda
529 // int addedCount = end - start + 1;
530 // if (m_mapCount != -1 && start >= m_mapFirst + m_mapCount) {
531 // return;
532 // } else {
533
534 // for (int bar = m_mapBarBottom; bar <= m_mapBarTop; bar++) {
535 // QBarSet *barSet = barsetAt(bar - m_mapBarBottom);
536 // // adding items to unlimited map
537 // if (m_mapCount == -1 && start >= m_mapFirst) {
538 // for (int i = start; i <= end; i++) {
539 // if (bar == m_mapBarBottom)
540 // insertCategory(i - m_mapFirst, m_model->data(m_model->index(i, m_mapCategories), Qt::DisplayRole).toString());
541 // barSet->insert(i - m_mapFirst, m_model->data(m_model->index(i, bar), Qt::DisplayRole).toDouble());
542 // }
543 // } else if (m_mapCount == - 1 && start < m_mapFirst) {
544 // // not all newly added items
545 // for (int i = m_mapFirst; i < m_mapFirst + addedCount; i++) {
546 // if (bar == m_mapBarBottom)
547 // insertCategory(i - m_mapFirst, m_model->data(m_model->index(i, m_mapCategories), Qt::DisplayRole).toString());
548 // barSet->insert(i - m_mapFirst, m_model->data(m_model->index(i, bar), Qt::DisplayRole).toDouble());
549 // }
550 // }
551
552 // // adding items to limited map
553 // else if (start >= m_mapFirst) {
554 // // remove the items that will no longer fit into the map
555 // // int toRemove = addedCount - (count - points().size());
556 // for (int i = start; i <= end; i++) {
557 // if (bar == m_mapBarBottom)
558 // insertCategory(i - m_mapFirst, m_model->data(m_model->index(i, m_mapCategories), Qt::DisplayRole).toString());
559 // barSet->insert(i - m_mapFirst, m_model->data(m_model->index(i, bar), Qt::DisplayRole).toDouble());
560 // }
561 // if (m_barSets.size() > m_mapCount)
562 // for (int i = m_barSets.size() - 1; i >= m_mapCount; i--) {
563 // if (bar == m_mapBarBottom)
564 // removeCategory(i);
565 // barSet->remove(i);
566 // }
567 // } else {
568 // //
569 // for (int i = m_mapFirst; i < m_mapFirst + addedCount; i++) {
570 // if (bar == m_mapBarBottom)
571 // insertCategory(i - m_mapFirst, m_model->data(m_model->index(i, m_mapCategories), Qt::DisplayRole).toString());
572 // barSet->insert(i - m_mapFirst, m_model->data(m_model->index(i, bar), Qt::DisplayRole).toDouble());
573 // }
574 // if (m_barSets.size() > m_mapCount)
575 // for (int i = m_barSets.size() - 1; i >= m_mapCount; i--) {
576 // if (bar == m_mapBarBottom)
577 // removeCategory(i);
578 // barSet->remove(i);
579 // }
580 // }
581 // }
582 // emit restructuredBars();
583 // emit barsetChanged();
584 // emit categoriesUpdated();
585 // }
586 }
469 }
587
470
588 void QBarSeriesPrivate::modelDataRemoved(QModelIndex parent, int start, int end)
471 void QBarSeriesPrivate::modelDataRemoved(QModelIndex parent, int start, int end)
@@ -590,66 +473,56 void QBarSeriesPrivate::modelDataRemoved(QModelIndex parent, int start, int end)
590 Q_UNUSED(parent);
473 Q_UNUSED(parent);
591 Q_UNUSED(start);
474 Q_UNUSED(start);
592 Q_UNUSED(end);
475 Q_UNUSED(end);
593 // initializeDataFromModel();
476 initializeDataFromModel();
594 }
477 }
595
478
596 //void QBarSeriesPrivate::initializeDataFromModel()
479 void QBarSeriesPrivate::initializeDataFromModel()
597 //{
480 {
598 // Q_Q(QBarSeries);
481 Q_Q(QBarSeries);
599
482
600 // if (m_model == 0)
483 if (m_model == 0 || m_mapper == 0)
601 // return;
484 return;
602
485
603 // // connect the signals
486 // create the initial bars
604 //// connect(m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex,QModelIndex)));
487 m_categories.clear();
605 //// if (m_mapOrientation == Qt::Vertical) {
488 m_barSets.clear();
606 //// connect(m_model,SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(modelDataAdded(QModelIndex,int,int)));
489 // emit restructuredBars();
607 //// connect(m_model,SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SLOT(modelDataRemoved(QModelIndex,int,int)));
490 if (m_mapper->orientation() == Qt::Vertical) {
608 //// } else {
491 int rowCount = 0;
609 //// connect(m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), this, SLOT(modelDataAdded(QModelIndex,int,int)));
492 if(m_mapper->count() == -1)
610 //// connect(m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), this, SLOT(modelDataRemoved(QModelIndex,int,int)));
493 rowCount = m_model->rowCount() - m_mapper->first();
611 //// }
494 else
612
495 rowCount = qMin(m_mapper->count(), m_model->rowCount() - m_mapper->first());
613 // // create the initial bars
496 for (int k = m_mapper->first(); k < m_mapper->first() + rowCount; k++) {
614 // m_categories.clear();
497 m_categories << m_model->data(m_model->index(k, m_mapper->mapCategories()), Qt::DisplayRole).toString();
615 // m_barSets.clear();
498 }
616 //// emit restructuredBars();
499
617 // if (m_mapOrientation == Qt::Vertical) {
500 for (int i = m_mapper->mapBarBottom(); i <= m_mapper->mapBarTop(); i++) {
618 // int rowCount = 0;
501 QBarSet* barSet = new QBarSet(m_model->headerData(i, Qt::Horizontal, Qt::DisplayRole).toString());
619 // if(m_mapCount == -1)
502 for(int m = m_mapper->first(); m < m_mapper->first() + rowCount; m++)
620 // rowCount = m_model->rowCount() - m_mapFirst;
503 *barSet << m_model->data(m_model->index(m, i), Qt::DisplayRole).toDouble();
621 // else
504 q->appendBarSet(barSet);
622 // rowCount = qMin(m_mapCount, m_model->rowCount() - m_mapFirst);
505 }
623 // for (int k = m_mapFirst; k < m_mapFirst + rowCount; k++) {
506 } else {
624 // m_categories << m_model->data(m_model->index(k, m_mapCategories), Qt::DisplayRole).toString();
507 int columnCount = 0;
625 // }
508 if(m_mapper->count() == -1)
626
509 columnCount = m_model->columnCount() - m_mapper->first();
627 // for (int i = m_mapBarBottom; i <= m_mapBarTop; i++) {
510 else
628 // QBarSet* barSet = new QBarSet(m_model->headerData(i, Qt::Horizontal, Qt::DisplayRole).toString());
511 columnCount = qMin(m_mapper->count(), m_model->columnCount() - m_mapper->first());
629 // for(int m = m_mapFirst; m < m_mapFirst + rowCount; m++)
512 for (int k = m_mapper->first(); k < m_mapper->first() + columnCount; k++) {
630 // *barSet << m_model->data(m_model->index(m, i), Qt::DisplayRole).toDouble();
513 m_categories << m_model->data(m_model->index(m_mapper->mapCategories(), k), Qt::DisplayRole).toString();
631 // q->appendBarSet(barSet);
514 }
632 // }
515
633 // } else {
516 for (int i = m_mapper->mapBarBottom(); i <= m_mapper->mapBarTop(); i++) {
634 // int columnCount = 0;
517 QBarSet* barSet = new QBarSet(m_model->headerData(i, Qt::Vertical, Qt::DisplayRole).toString());
635 // if(m_mapCount == -1)
518 for(int m = m_mapper->first(); m < m_mapper->first() + columnCount; m++)
636 // columnCount = m_model->columnCount() - m_mapFirst;
519 *barSet << m_model->data(m_model->index(i, m), Qt::DisplayRole).toDouble();
637 // else
520 q->appendBarSet(barSet);
638 // columnCount = qMin(m_mapCount, m_model->columnCount() - m_mapFirst);
521 }
639 // for (int k = m_mapFirst; k < m_mapFirst + columnCount; k++) {
522 }
640 // m_categories << m_model->data(m_model->index(m_mapCategories, k), Qt::DisplayRole).toString();
523 emit restructuredBars();
641 // }
524 // emit updatedBars();
642
525 }
643 // for (int i = m_mapBarBottom; i <= m_mapBarTop; i++) {
644 // QBarSet* barSet = new QBarSet(m_model->headerData(i, Qt::Vertical, Qt::DisplayRole).toString());
645 // for(int m = m_mapFirst; m < m_mapFirst + columnCount; m++)
646 // *barSet << m_model->data(m_model->index(i, m), Qt::DisplayRole).toDouble();
647 // q->appendBarSet(barSet);
648 // }
649 // }
650 // emit restructuredBars();
651 //// emit updatedBars();
652 //}
653
526
654 void QBarSeriesPrivate::insertCategory(int index, const QString category)
527 void QBarSeriesPrivate::insertCategory(int index, const QString category)
655 {
528 {
@@ -33,6 +33,7 typedef QStringList QBarCategories;
33 class QBarSet;
33 class QBarSet;
34 class BarCategory;
34 class BarCategory;
35 class QBarSeriesPrivate;
35 class QBarSeriesPrivate;
36 class QBarModelMapper;
36
37
37 // Container for series
38 // Container for series
38 class QTCOMMERCIALCHART_EXPORT QBarSeries : public QAbstractSeries
39 class QTCOMMERCIALCHART_EXPORT QBarSeries : public QAbstractSeries
@@ -57,8 +58,8 public:
57 void setLabelsVisible(bool visible = true);
58 void setLabelsVisible(bool visible = true);
58
59
59 void setModel(QAbstractItemModel *model);
60 void setModel(QAbstractItemModel *model);
60 // void setModelMapping(int categories, int bottomBoundary, int topBoundary, Qt::Orientation orientation = Qt::Vertical);
61 void setModelMapper(QBarModelMapper *mapper);
61 // void setModelMappingRange(int first, int count = -1);
62 QBarModelMapper* modelMapper() const;
62
63
63 protected:
64 protected:
64 explicit QBarSeries(QBarSeriesPrivate &d,QObject *parent = 0);
65 explicit QBarSeries(QBarSeriesPrivate &d,QObject *parent = 0);
@@ -10,6 +10,8 class QModelIndex;
10
10
11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12
12
13 class QBarModelMapper;
14
13 // Container for series
15 // Container for series
14 class QBarSeriesPrivate : public QAbstractSeriesPrivate
16 class QBarSeriesPrivate : public QAbstractSeriesPrivate
15 {
17 {
@@ -24,10 +26,6 public:
24 Chart* createGraphics(ChartPresenter* presenter);
26 Chart* createGraphics(ChartPresenter* presenter);
25 QList<LegendMarker*> createLegendMarker(QLegend* legend);
27 QList<LegendMarker*> createLegendMarker(QLegend* legend);
26
28
27 // void setModel(QAbstractItemModel *model);
28 // void setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation = Qt::Vertical);
29 // void setModelMappingRange(int first, int count = -1);
30
31 void insertCategory(int index, const QString category);
29 void insertCategory(int index, const QString category);
32 void removeCategory(int index);
30 void removeCategory(int index);
33
31
@@ -49,20 +47,16 Q_SIGNALS:
49
47
50 private Q_SLOTS:
48 private Q_SLOTS:
51 // slots for updating bars when data in model changes
49 // slots for updating bars when data in model changes
52 // void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight);
50 void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight);
53 void modelDataAdded(QModelIndex parent, int start, int end);
51 void modelDataAdded(QModelIndex parent, int start, int end);
54 void modelDataRemoved(QModelIndex parent, int start, int end);
52 void modelDataRemoved(QModelIndex parent, int start, int end);
55 // void initializeDataFromModel();
53 void initializeDataFromModel();
56 void barsetChanged();
54 void barsetChanged();
57
55
58 protected:
56 protected:
59 QList<QBarSet *> m_barSets;
57 QList<QBarSet *> m_barSets;
60 QBarCategories m_categories;
58 QBarCategories m_categories;
61
59 QBarModelMapper *m_mapper;
62 qreal m_barMargin;
63 int m_mapCategories;
64 int m_mapBarBottom;
65 int m_mapBarTop;
66 private:
60 private:
67 Q_DECLARE_PUBLIC(QBarSeries)
61 Q_DECLARE_PUBLIC(QBarSeries)
68 };
62 };
@@ -34,6 +34,7
34 #include <QAreaSeries>
34 #include <QAreaSeries>
35 #include <QBarSeries>
35 #include <QBarSeries>
36 #include <QBarSet>
36 #include <QBarSet>
37 #include <QBarModelMapper>
37 #include <QPushButton>
38 #include <QPushButton>
38 #include <QRadioButton>
39 #include <QRadioButton>
39 #include <QLabel>
40 #include <QLabel>
@@ -413,28 +414,34 void TableWidget::updateChartType(bool toggle)
413 // m_model->addMapping(seriesColorHex, QRect(0, 1, 2, 5));
414 // m_model->addMapping(seriesColorHex, QRect(0, 1, 2, 5));
414 // m_model->addMapping(seriesColorHex, QRect(2, 0, 2, 1000));
415 // m_model->addMapping(seriesColorHex, QRect(2, 0, 2, 1000));
415 // }
416 // }
416 // else if (m_barRadioButton->isChecked())
417 else if (m_barRadioButton->isChecked())
417 // {
418 {
418 // m_chart->setAnimationOptions(QChart::SeriesAnimations);
419 m_chart->setAnimationOptions(QChart::SeriesAnimations);
419
420 // QBarSeries* barSeries = new QBarSeries();
421 // barSeries->setCategories(QStringList());
422 // barSeries->setModel(m_model);
423 // // barSeries->setModelMappingRange(2, 5);
424 // barSeries->setModelMapping(5, 2, 4, Qt::Vertical);
425 // m_chart->addSeries(barSeries);
426 // QList<QBarSet*> barsets = barSeries->barSets();
427 // for (int i = 0; i < barsets.count(); i++) {
428 // seriesColorHex = "#" + QString::number(barsets.at(i)->brush().color().rgb(), 16).right(6).toUpper();
429 // m_model->addMapping(seriesColorHex, QRect(2 + i, 0, 1, 1000));
430 // }
431 // }
432
420
421 QBarSeries* barSeries = new QBarSeries();
422 barSeries->setCategories(QStringList());
423 barSeries->setModel(m_model);
424 // barSeries->setModelMappingRange(2, 5);
425 // barSeries->setModelMapping(5, 2, 4, Qt::Vertical);
426
427 QBarModelMapper *mapper = new QBarModelMapper;
428 mapper->setMapCategories(5);
429 mapper->setMapBarBottom(2);
430 mapper->setMapBarTop(4);
431 barSeries->setModelMapper(mapper);
432 m_chart->addSeries(barSeries);
433 QList<QBarSet*> barsets = barSeries->barSets();
434 for (int i = 0; i < barsets.count(); i++) {
435 seriesColorHex = "#" + QString::number(barsets.at(i)->brush().color().rgb(), 16).right(6).toUpper();
436 m_model->addMapping(seriesColorHex, QRect(2 + i, 0, 1, 1000));
437 }
438 }
433
439
434 // if (!m_barRadioButton->isChecked()) {
440
435 // m_chart->axisX()->setRange(0, 500);
441 if (!m_barRadioButton->isChecked()) {
436 // m_chart->axisY()->setRange(0, 220);
442 m_chart->axisX()->setRange(0, 500);
437 // }
443 m_chart->axisY()->setRange(0, 220);
444 }
438 m_chart->legend()->setVisible(true);
445 m_chart->legend()->setVisible(true);
439
446
440 // repaint table view colors
447 // repaint table view colors
General Comments 0
You need to be logged in to leave comments. Login now