@@ -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 | 13 | $$PWD/qgroupedbarseries.cpp \ |
|
14 | 14 | $$PWD/stackedbarchartitem.cpp \ |
|
15 | 15 | $$PWD/barlabel.cpp \ |
|
16 | $$PWD/qbarmodelmapper.cpp | |
|
16 | 17 | |
|
17 | 18 | PRIVATE_HEADERS += \ |
|
18 | 19 | $$PWD/bar_p.h \ |
@@ -32,5 +33,5 PUBLIC_HEADERS += \ | |||
|
32 | 33 | $$PWD/qbarset.h \ |
|
33 | 34 | $$PWD/qpercentbarseries.h \ |
|
34 | 35 | $$PWD/qstackedbarseries.h \ |
|
35 | $$PWD/qgroupedbarseries.h | |
|
36 | ||
|
36 | $$PWD/qgroupedbarseries.h \ | |
|
37 | $$PWD/qbarmodelmapper.h |
@@ -30,6 +30,7 | |||
|
30 | 30 | |
|
31 | 31 | #include <QAbstractItemModel> |
|
32 | 32 | #include <QModelIndex> |
|
33 | #include <QBarModelMapper> | |
|
33 | 34 | |
|
34 | 35 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
35 | 36 | |
@@ -215,30 +216,59 QList<QBarSet*> QBarSeries::barSets() const | |||
|
215 | 216 | */ |
|
216 | 217 | void QBarSeries::setModel(QAbstractItemModel *model) |
|
217 | 218 | { |
|
218 | Q_UNUSED(model); | |
|
219 | // Q_D(QBarSeries); | |
|
220 |
|
|
|
219 | Q_D(QBarSeries); | |
|
220 | // disconnect signals from old 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 | /*! | |
|
224 | \fn bool QBarSeries::setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation) | |
|
225 | Sets column/row specified by \a categories to be used as a list of bar series categories. | |
|
226 | Parameter \a bottomBoundry indicates the column/row where the first bar set is located in the model. | |
|
227 | Parameter \a topBoundry indicates the column/row where the last bar set is located in the model. | |
|
228 | All the columns/rows inbetween those two values are also used as data for bar sets. | |
|
229 | The \a orientation parameter specifies whether the data is in columns or in rows. | |
|
230 | */ | |
|
231 | //void QBarSeries::setModelMapping(int categories, int bottomBoundary, int topBoundary, Qt::Orientation orientation) | |
|
232 | //{ | |
|
233 | // Q_D(QBarSeries); | |
|
234 | // d->setModelMapping(categories,bottomBoundary,topBoundary,orientation); | |
|
235 | //} | |
|
236 | ||
|
237 | //void QBarSeries::setModelMappingRange(int first, int count) | |
|
238 | //{ | |
|
239 | // Q_D(QBarSeries); | |
|
240 | // d->setModelMappingRange(first, count); | |
|
241 | //} | |
|
247 | void QBarSeries::setModelMapper(QBarModelMapper *mapper) | |
|
248 | { | |
|
249 | Q_D(QBarSeries); | |
|
250 | // disconnect signals from old mapper | |
|
251 | if (d->m_mapper) { | |
|
252 | QObject::disconnect(d->m_mapper, 0, this, 0); | |
|
253 | } | |
|
254 | ||
|
255 | if (mapper) { | |
|
256 | d->m_mapper = mapper; | |
|
257 | // connect the signal from the mapper | |
|
258 | connect(d->m_mapper, SIGNAL(updated()), d, SLOT(initializePieFromModel())); | |
|
259 | ||
|
260 | if (d->m_model) | |
|
261 | d->initializeDataFromModel(); | |
|
262 | } else { | |
|
263 | d->m_mapper = 0; | |
|
264 | } | |
|
265 | } | |
|
266 | ||
|
267 | QBarModelMapper* QBarSeries::modelMapper() const | |
|
268 | { | |
|
269 | Q_D(const QBarSeries); | |
|
270 | return d->m_mapper; | |
|
271 | } | |
|
242 | 272 | |
|
243 | 273 | /*! |
|
244 | 274 | Returns the bar categories of the series. |
@@ -264,9 +294,8 void QBarSeries::setLabelsVisible(bool visible) | |||
|
264 | 294 | QBarSeriesPrivate::QBarSeriesPrivate(QBarSeries *q) : |
|
265 | 295 | QAbstractSeriesPrivate(q), |
|
266 | 296 | m_barMargin(0.05), // Default value is 5% of category width |
|
267 |
m_map |
|
|
268 | m_mapBarBottom(-1), | |
|
269 | m_mapBarTop(-1) | |
|
297 | m_mapper(0) | |
|
298 | // m_categories(categories), | |
|
270 | 299 | { |
|
271 | 300 | } |
|
272 | 301 | |
@@ -405,184 +434,38 qreal QBarSeriesPrivate::maxCategorySum() | |||
|
405 | 434 | return max; |
|
406 | 435 | } |
|
407 | 436 | |
|
408 | //void QBarSeriesPrivate::setModel(QAbstractItemModel *model) | |
|
409 | //{ | |
|
410 | // // disconnect signals from old model | |
|
411 | // if(m_model) | |
|
412 | // { | |
|
413 | // disconnect(m_model, 0, this, 0); | |
|
414 | // m_mapCategories = -1; | |
|
415 | // m_mapBarBottom = -1; | |
|
416 | // m_mapBarTop = -1; | |
|
417 | // m_mapOrientation = Qt::Vertical; | |
|
418 | // } | |
|
419 | ||
|
420 | // // set new model | |
|
421 | // if(model) | |
|
422 | // { | |
|
423 | // m_model = model; | |
|
424 | // } | |
|
425 | // else | |
|
426 | // { | |
|
427 | // m_model = 0; | |
|
428 | // } | |
|
429 | //} | |
|
430 | ||
|
431 | //void QBarSeriesPrivate::setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation) | |
|
432 | //{ | |
|
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 | //} | |
|
437 | void QBarSeriesPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight) | |
|
438 | { | |
|
439 | for (int row = topLeft.row(); row <= bottomRight.row(); row++) { | |
|
440 | for (int column = topLeft.column(); column <= bottomRight.column(); column++) { | |
|
441 | if (m_mapper->orientation() == Qt::Vertical) | |
|
442 | { | |
|
443 | // model update is relevant to BarSeries if the change was made to the part of the model that was mapped to BarSeries | |
|
444 | if ( row >= m_mapper->first() && (m_mapper->count() == - 1 || row < m_mapper->first() + m_mapper->count())) { | |
|
445 | if (column >= m_mapper->mapBarBottom() && column <= m_mapper->mapBarTop()) | |
|
446 | barsetAt(column - m_mapper->mapBarBottom())->replace(row - m_mapper->first(), m_model->data(topLeft, Qt::DisplayRole).toDouble()); | |
|
447 | // if (column == m_mapper->mapCategories());// TODO: | |
|
448 | } | |
|
449 | } | |
|
450 | else | |
|
451 | { | |
|
452 | // model update is relevant to BarSeries if the change was made to the part of the model that was mapped to BarSeries | |
|
453 | if (column >= m_mapper->first() && (m_mapper->count() == - 1 || column < m_mapper->first() + m_mapper->count())) { | |
|
454 | if (row >= m_mapper->mapBarBottom() && row <= m_mapper->mapBarTop()) | |
|
455 | barsetAt(row - m_mapper->mapBarBottom())->replace(column - m_mapper->first(), m_model->data(topLeft, Qt::DisplayRole).toDouble()); | |
|
456 | // if (row == m_mapper->mapCategories());// TODO: | |
|
457 | } | |
|
458 | } | |
|
459 | } | |
|
460 | } | |
|
461 | } | |
|
521 | 462 | |
|
522 | 463 | void QBarSeriesPrivate::modelDataAdded(QModelIndex parent, int start, int end) |
|
523 | 464 | { |
|
524 | 465 | Q_UNUSED(parent); |
|
525 | 466 | Q_UNUSED(start); |
|
526 | 467 | Q_UNUSED(end); |
|
527 |
|
|
|
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 | // } | |
|
468 | initializeDataFromModel(); | |
|
586 | 469 | } |
|
587 | 470 | |
|
588 | 471 | void QBarSeriesPrivate::modelDataRemoved(QModelIndex parent, int start, int end) |
@@ -590,66 +473,56 void QBarSeriesPrivate::modelDataRemoved(QModelIndex parent, int start, int end) | |||
|
590 | 473 | Q_UNUSED(parent); |
|
591 | 474 | Q_UNUSED(start); |
|
592 | 475 | Q_UNUSED(end); |
|
593 |
|
|
|
594 | } | |
|
595 | ||
|
596 |
|
|
|
597 | //{ | |
|
598 |
|
|
|
599 | ||
|
600 |
|
|
|
601 |
|
|
|
602 | ||
|
603 |
|
|
|
604 | //// connect(m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex,QModelIndex))); | |
|
605 | //// if (m_mapOrientation == Qt::Vertical) { | |
|
606 | //// connect(m_model,SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(modelDataAdded(QModelIndex,int,int))); | |
|
607 | //// connect(m_model,SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SLOT(modelDataRemoved(QModelIndex,int,int))); | |
|
608 | //// } else { | |
|
609 | //// connect(m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), this, SLOT(modelDataAdded(QModelIndex,int,int))); | |
|
610 | //// connect(m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), this, SLOT(modelDataRemoved(QModelIndex,int,int))); | |
|
611 | //// } | |
|
612 | ||
|
613 | // // create the initial bars | |
|
614 | // m_categories.clear(); | |
|
615 | // m_barSets.clear(); | |
|
616 | //// emit restructuredBars(); | |
|
617 | // if (m_mapOrientation == Qt::Vertical) { | |
|
618 | // int rowCount = 0; | |
|
619 | // if(m_mapCount == -1) | |
|
620 | // rowCount = m_model->rowCount() - m_mapFirst; | |
|
621 | // else | |
|
622 | // rowCount = qMin(m_mapCount, m_model->rowCount() - m_mapFirst); | |
|
623 | // for (int k = m_mapFirst; k < m_mapFirst + rowCount; k++) { | |
|
624 | // m_categories << m_model->data(m_model->index(k, m_mapCategories), Qt::DisplayRole).toString(); | |
|
625 | // } | |
|
626 | ||
|
627 | // for (int i = m_mapBarBottom; i <= m_mapBarTop; i++) { | |
|
628 | // QBarSet* barSet = new QBarSet(m_model->headerData(i, Qt::Horizontal, Qt::DisplayRole).toString()); | |
|
629 |
|
|
|
630 |
|
|
|
631 | // q->appendBarSet(barSet); | |
|
632 | // } | |
|
633 | // } else { | |
|
634 | // int columnCount = 0; | |
|
635 | // if(m_mapCount == -1) | |
|
636 | // columnCount = m_model->columnCount() - m_mapFirst; | |
|
637 | // else | |
|
638 | // columnCount = qMin(m_mapCount, m_model->columnCount() - m_mapFirst); | |
|
639 | // for (int k = m_mapFirst; k < m_mapFirst + columnCount; k++) { | |
|
640 | // m_categories << m_model->data(m_model->index(m_mapCategories, k), Qt::DisplayRole).toString(); | |
|
641 | // } | |
|
642 | ||
|
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 | //} | |
|
476 | initializeDataFromModel(); | |
|
477 | } | |
|
478 | ||
|
479 | void QBarSeriesPrivate::initializeDataFromModel() | |
|
480 | { | |
|
481 | Q_Q(QBarSeries); | |
|
482 | ||
|
483 | if (m_model == 0 || m_mapper == 0) | |
|
484 | return; | |
|
485 | ||
|
486 | // create the initial bars | |
|
487 | m_categories.clear(); | |
|
488 | m_barSets.clear(); | |
|
489 | // emit restructuredBars(); | |
|
490 | if (m_mapper->orientation() == Qt::Vertical) { | |
|
491 | int rowCount = 0; | |
|
492 | if(m_mapper->count() == -1) | |
|
493 | rowCount = m_model->rowCount() - m_mapper->first(); | |
|
494 | else | |
|
495 | rowCount = qMin(m_mapper->count(), m_model->rowCount() - m_mapper->first()); | |
|
496 | for (int k = m_mapper->first(); k < m_mapper->first() + rowCount; k++) { | |
|
497 | m_categories << m_model->data(m_model->index(k, m_mapper->mapCategories()), Qt::DisplayRole).toString(); | |
|
498 | } | |
|
499 | ||
|
500 | for (int i = m_mapper->mapBarBottom(); i <= m_mapper->mapBarTop(); i++) { | |
|
501 | QBarSet* barSet = new QBarSet(m_model->headerData(i, Qt::Horizontal, Qt::DisplayRole).toString()); | |
|
502 | for(int m = m_mapper->first(); m < m_mapper->first() + rowCount; m++) | |
|
503 | *barSet << m_model->data(m_model->index(m, i), Qt::DisplayRole).toDouble(); | |
|
504 | q->appendBarSet(barSet); | |
|
505 | } | |
|
506 | } else { | |
|
507 | int columnCount = 0; | |
|
508 | if(m_mapper->count() == -1) | |
|
509 | columnCount = m_model->columnCount() - m_mapper->first(); | |
|
510 | else | |
|
511 | columnCount = qMin(m_mapper->count(), m_model->columnCount() - m_mapper->first()); | |
|
512 | for (int k = m_mapper->first(); k < m_mapper->first() + columnCount; k++) { | |
|
513 | m_categories << m_model->data(m_model->index(m_mapper->mapCategories(), k), Qt::DisplayRole).toString(); | |
|
514 | } | |
|
515 | ||
|
516 | for (int i = m_mapper->mapBarBottom(); i <= m_mapper->mapBarTop(); i++) { | |
|
517 | QBarSet* barSet = new QBarSet(m_model->headerData(i, Qt::Vertical, Qt::DisplayRole).toString()); | |
|
518 | for(int m = m_mapper->first(); m < m_mapper->first() + columnCount; m++) | |
|
519 | *barSet << m_model->data(m_model->index(i, m), Qt::DisplayRole).toDouble(); | |
|
520 | q->appendBarSet(barSet); | |
|
521 | } | |
|
522 | } | |
|
523 | emit restructuredBars(); | |
|
524 | // emit updatedBars(); | |
|
525 | } | |
|
653 | 526 | |
|
654 | 527 | void QBarSeriesPrivate::insertCategory(int index, const QString category) |
|
655 | 528 | { |
@@ -33,6 +33,7 typedef QStringList QBarCategories; | |||
|
33 | 33 | class QBarSet; |
|
34 | 34 | class BarCategory; |
|
35 | 35 | class QBarSeriesPrivate; |
|
36 | class QBarModelMapper; | |
|
36 | 37 | |
|
37 | 38 | // Container for series |
|
38 | 39 | class QTCOMMERCIALCHART_EXPORT QBarSeries : public QAbstractSeries |
@@ -57,8 +58,8 public: | |||
|
57 | 58 | void setLabelsVisible(bool visible = true); |
|
58 | 59 | |
|
59 | 60 | void setModel(QAbstractItemModel *model); |
|
60 | // void setModelMapping(int categories, int bottomBoundary, int topBoundary, Qt::Orientation orientation = Qt::Vertical); | |
|
61 | // void setModelMappingRange(int first, int count = -1); | |
|
61 | void setModelMapper(QBarModelMapper *mapper); | |
|
62 | QBarModelMapper* modelMapper() const; | |
|
62 | 63 | |
|
63 | 64 | protected: |
|
64 | 65 | explicit QBarSeries(QBarSeriesPrivate &d,QObject *parent = 0); |
@@ -10,6 +10,8 class QModelIndex; | |||
|
10 | 10 | |
|
11 | 11 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
12 | 12 | |
|
13 | class QBarModelMapper; | |
|
14 | ||
|
13 | 15 | // Container for series |
|
14 | 16 | class QBarSeriesPrivate : public QAbstractSeriesPrivate |
|
15 | 17 | { |
@@ -24,10 +26,6 public: | |||
|
24 | 26 | Chart* createGraphics(ChartPresenter* presenter); |
|
25 | 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 | 29 | void insertCategory(int index, const QString category); |
|
32 | 30 | void removeCategory(int index); |
|
33 | 31 | |
@@ -49,20 +47,16 Q_SIGNALS: | |||
|
49 | 47 | |
|
50 | 48 | private Q_SLOTS: |
|
51 | 49 | // slots for updating bars when data in model changes |
|
52 |
|
|
|
50 | void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight); | |
|
53 | 51 | void modelDataAdded(QModelIndex parent, int start, int end); |
|
54 | 52 | void modelDataRemoved(QModelIndex parent, int start, int end); |
|
55 |
|
|
|
53 | void initializeDataFromModel(); | |
|
56 | 54 | void barsetChanged(); |
|
57 | 55 | |
|
58 | 56 | protected: |
|
59 | 57 | QList<QBarSet *> m_barSets; |
|
60 | 58 | QBarCategories m_categories; |
|
61 | ||
|
62 | qreal m_barMargin; | |
|
63 | int m_mapCategories; | |
|
64 | int m_mapBarBottom; | |
|
65 | int m_mapBarTop; | |
|
59 | QBarModelMapper *m_mapper; | |
|
66 | 60 | private: |
|
67 | 61 | Q_DECLARE_PUBLIC(QBarSeries) |
|
68 | 62 | }; |
@@ -34,6 +34,7 | |||
|
34 | 34 | #include <QAreaSeries> |
|
35 | 35 | #include <QBarSeries> |
|
36 | 36 | #include <QBarSet> |
|
37 | #include <QBarModelMapper> | |
|
37 | 38 | #include <QPushButton> |
|
38 | 39 | #include <QRadioButton> |
|
39 | 40 | #include <QLabel> |
@@ -413,28 +414,34 void TableWidget::updateChartType(bool toggle) | |||
|
413 | 414 | // m_model->addMapping(seriesColorHex, QRect(0, 1, 2, 5)); |
|
414 | 415 | // m_model->addMapping(seriesColorHex, QRect(2, 0, 2, 1000)); |
|
415 | 416 | // } |
|
416 |
|
|
|
417 |
|
|
|
418 |
|
|
|
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 | // } | |
|
417 | else if (m_barRadioButton->isChecked()) | |
|
418 | { | |
|
419 | m_chart->setAnimationOptions(QChart::SeriesAnimations); | |
|
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()) { | |
|
435 | // m_chart->axisX()->setRange(0, 500); | |
|
436 |
|
|
|
437 | // } | |
|
440 | ||
|
441 | if (!m_barRadioButton->isChecked()) { | |
|
442 | m_chart->axisX()->setRange(0, 500); | |
|
443 | m_chart->axisY()->setRange(0, 220); | |
|
444 | } | |
|
438 | 445 | m_chart->legend()->setVisible(true); |
|
439 | 446 | |
|
440 | 447 | // repaint table view colors |
General Comments 0
You need to be logged in to leave comments.
Login now