From 2390e49cbc5174f49d3b2803bd10edfb9e92797a 2012-08-30 10:50:29 From: sauimone Date: 2012-08-30 10:50:29 Subject: [PATCH] fix: changing barset colors no more triggers layout calculations --- diff --git a/src/barchart/abstractbarchartitem.cpp b/src/barchart/abstractbarchartitem.cpp index 01a5d59..135c5f9 100644 --- a/src/barchart/abstractbarchartitem.cpp +++ b/src/barchart/abstractbarchartitem.cpp @@ -38,8 +38,10 @@ AbstractBarChartItem::AbstractBarChartItem(QAbstractBarSeries *series, ChartPres m_animation(0), m_series(series) { + setFlag(ItemClipsChildrenToShape); - connect(series->d_func(), SIGNAL(updatedBars()), this, SLOT(handleLayoutChanged())); + connect(series->d_func(), SIGNAL(updatedLayout()), this, SLOT(handleLayoutChanged())); + connect(series->d_func(), SIGNAL(updatedBars()), this, SLOT(handleUpdatedBars())); connect(series->d_func(), SIGNAL(labelsVisibleChanged(bool)), this, SLOT(handleLabelsVisibleChanged(bool))); connect(series->d_func(), SIGNAL(restructuredBars()), this, SLOT(handleDataStructureChanged())); connect(series, SIGNAL(visibleChanged()), this, SLOT(handleVisibleChanged())); @@ -173,6 +175,30 @@ void AbstractBarChartItem::handleVisibleChanged() } } +void AbstractBarChartItem::handleUpdatedBars() +{ + // Handle changes in pen, brush, labels etc. + int categoryCount = m_series->d_func()->categoryCount(); + int setCount = m_series->count(); + int itemIndex(0); + + for (int category = 0; category < categoryCount; category++) { + for (int set = 0; set < setCount; set++) { + QBarSetPrivate* barSet = m_series->d_func()->barsetAt(set)->d_ptr.data(); + Bar* bar = m_bars.at(itemIndex); + bar->setPen(barSet->m_pen); + bar->setBrush(barSet->m_brush); + bar->update(); + + QGraphicsSimpleTextItem* label = m_labels.at(itemIndex); + label->setFont(barSet->m_labelFont); + label->setBrush(barSet->m_labelBrush); + label->update(); + itemIndex++; + } + } +} + #include "moc_abstractbarchartitem_p.cpp" QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/barchart/abstractbarchartitem_p.h b/src/barchart/abstractbarchartitem_p.h index f45c7e2..ed03674 100644 --- a/src/barchart/abstractbarchartitem_p.h +++ b/src/barchart/abstractbarchartitem_p.h @@ -68,6 +68,7 @@ public Q_SLOTS: void handleLabelsVisibleChanged(bool visible); void handleDataStructureChanged(); // structure of of series has changed, recreate graphic items void handleVisibleChanged(); + void handleUpdatedBars(); protected: diff --git a/src/barchart/qabstractbarseries.cpp b/src/barchart/qabstractbarseries.cpp index 0edf2a2..a407878 100644 --- a/src/barchart/qabstractbarseries.cpp +++ b/src/barchart/qabstractbarseries.cpp @@ -405,7 +405,7 @@ void QAbstractBarSeriesPrivate::setBarWidth(qreal width) width = 0.0; } m_barWidth = width; - emit updatedBars(); + emit updatedLayout(); } qreal QAbstractBarSeriesPrivate::barWidth() const @@ -421,7 +421,7 @@ QBarSet* QAbstractBarSeriesPrivate::barsetAt(int index) void QAbstractBarSeriesPrivate::setVisible(bool visible) { m_visible = visible; - emit updatedBars(); + emit visibleChanged(); } void QAbstractBarSeriesPrivate::setLabelsVisible(bool visible) @@ -678,6 +678,7 @@ bool QAbstractBarSeriesPrivate::append(QBarSet *set) return false; } m_barSets.append(set); + QObject::connect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout())); QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars())); QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars())); emit restructuredBars(); // this notifies barchartitem @@ -695,6 +696,7 @@ bool QAbstractBarSeriesPrivate::remove(QBarSet *set) return false; } m_barSets.removeOne(set); + QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout())); QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars())); QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars())); emit restructuredBars(); // this notifies barchartitem @@ -720,6 +722,7 @@ bool QAbstractBarSeriesPrivate::append(QList sets) foreach (QBarSet* set, sets) { m_barSets.append(set); + QObject::connect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout())); QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars())); QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars())); } @@ -749,6 +752,7 @@ bool QAbstractBarSeriesPrivate::remove(QList sets) foreach (QBarSet* set, sets) { m_barSets.removeOne(set); + QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout())); QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars())); QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars())); } @@ -768,6 +772,7 @@ bool QAbstractBarSeriesPrivate::insert(int index, QBarSet *set) return false; } m_barSets.insert(index, set); + QObject::connect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout())); QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars())); QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars())); emit restructuredBars(); // this notifies barchartitem diff --git a/src/barchart/qabstractbarseries_p.h b/src/barchart/qabstractbarseries_p.h index a31d779..d8ed75b 100644 --- a/src/barchart/qabstractbarseries_p.h +++ b/src/barchart/qabstractbarseries_p.h @@ -84,8 +84,10 @@ public: Q_SIGNALS: void clicked(int index, QBarSet *barset); void updatedBars(); + void updatedLayout(); void restructuredBars(); void labelsVisibleChanged(bool visible); + void visibleChanged(); private: void populateCategories(QBarCategoryAxis* axis); diff --git a/src/barchart/qbarset.cpp b/src/barchart/qbarset.cpp index 711adc1..cc522cb 100644 --- a/src/barchart/qbarset.cpp +++ b/src/barchart/qbarset.cpp @@ -617,13 +617,13 @@ int QBarSetPrivate::remove(const int index, const int count) void QBarSetPrivate::replace(const int index, const qreal value) { m_values.replace(index,QPointF(index,value)); - emit updatedBars(); + emit updatedLayout(); } void QBarSetPrivate::replace(const int index, const QPointF value) { m_values.replace(index,value); - emit updatedBars(); + emit updatedLayout(); } qreal QBarSetPrivate::pos(const int index) diff --git a/src/barchart/qbarset_p.h b/src/barchart/qbarset_p.h index 8fd8e3d..c11dae9 100644 --- a/src/barchart/qbarset_p.h +++ b/src/barchart/qbarset_p.h @@ -63,6 +63,7 @@ public: Q_SIGNALS: void restructuredBars(); void updatedBars(); + void updatedLayout(); public: QBarSet * const q_ptr;