From 495016ed179417ae2a74e95584bb3c4483e62a68 2012-06-01 12:06:20 From: sauimone Date: 2012-06-01 12:06:20 Subject: [PATCH] fixed crash in barseries with empty sets --- diff --git a/src/barchart/barchartitem.cpp b/src/barchart/barchartitem.cpp index 27b51a7..0ca718a 100644 --- a/src/barchart/barchartitem.cpp +++ b/src/barchart/barchartitem.cpp @@ -38,9 +38,10 @@ BarChartItem::BarChartItem(QBarSeries *series, ChartPresenter *presenter) : { setFlag(ItemClipsChildrenToShape); connect(series->d_func(), SIGNAL(updatedBars()), this, SLOT(handleLayoutChanged())); - connect(series->d_func(), SIGNAL(labelsVisibleChanged(bool)), this, SLOT(labelsVisibleChanged(bool))); + connect(series->d_func(), SIGNAL(labelsVisibleChanged(bool)), this, SLOT(handleLabelsVisibleChanged(bool))); + connect(series->d_func(), SIGNAL(restructuredBars()), this, SLOT(handleDataStructureChanged())); setZValue(ChartPresenter::BarSeriesZValue); - dataChanged(); + handleDataStructureChanged(); } BarChartItem::~BarChartItem() @@ -59,7 +60,7 @@ QRectF BarChartItem::boundingRect() const return m_rect; } -void BarChartItem::dataChanged() +void BarChartItem::handleDataStructureChanged() { foreach(QGraphicsItem *item, childItems()) { delete item; @@ -92,6 +93,7 @@ void BarChartItem::dataChanged() // TODO: Is this the right place to call it? // presenter()->chartTheme()->decorate(m_series, presenter()->dataSet()->seriesIndex(m_series)); + handleLayoutChanged(); } QVector BarChartItem::calculateLayout() @@ -150,18 +152,20 @@ QVector BarChartItem::calculateLayout() void BarChartItem::applyLayout(const QVector &layout) { - if (animator()) + if (animator()) { animator()->updateLayout(this, m_layout, layout); - else + } else { setLayout(layout); + } } void BarChartItem::setLayout(const QVector &layout) { m_layout = layout; - for (int i=0; i < m_bars.count(); i++) + for (int i=0; i < m_bars.count(); i++) { m_bars.at(i)->setRect(layout.at(i)); + } update(); } @@ -194,7 +198,7 @@ void BarChartItem::handleLayoutChanged() update(); } -void BarChartItem::labelsVisibleChanged(bool visible) +void BarChartItem::handleLabelsVisibleChanged(bool visible) { foreach (QGraphicsSimpleTextItem* label, m_labels) { label->setVisible(visible); diff --git a/src/barchart/barchartitem_p.h b/src/barchart/barchartitem_p.h index 3fe469b..de25a00 100644 --- a/src/barchart/barchartitem_p.h +++ b/src/barchart/barchartitem_p.h @@ -53,8 +53,6 @@ public: void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); QRectF boundingRect() const; - virtual void dataChanged(); // data of series has changed -> need to recalculate bar sizes - virtual QVector calculateLayout(); void applyLayout(const QVector &layout); void setLayout(const QVector &layout); @@ -66,7 +64,8 @@ public Q_SLOTS: void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY); void handleGeometryChanged(const QRectF &size); void handleLayoutChanged(); - void labelsVisibleChanged(bool visible); + void handleLabelsVisibleChanged(bool visible); + void handleDataStructureChanged(); // structure of of series has changed, recreate graphic items protected: diff --git a/src/barchart/qbarseries.cpp b/src/barchart/qbarseries.cpp index f2ec941..9633235 100644 --- a/src/barchart/qbarseries.cpp +++ b/src/barchart/qbarseries.cpp @@ -428,11 +428,6 @@ qreal QBarSeriesPrivate::maxCategorySum() return max; } -void QBarSeriesPrivate::barsetChanged() -{ - emit updatedBars(); -} - void QBarSeriesPrivate::scaleDomain(Domain& domain) { qreal minX(domain.minX()); @@ -486,7 +481,8 @@ bool QBarSeriesPrivate::append(QBarSet *set) return false; } m_barSets.append(set); - QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SLOT(barsetChanged())); + QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars())); + QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars())); if (m_dataset) { m_dataset->updateSeries(q); // this notifies legend } @@ -502,7 +498,8 @@ bool QBarSeriesPrivate::remove(QBarSet *set) return false; } m_barSets.removeOne(set); - QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SLOT(barsetChanged())); + QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars())); + QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars())); if (m_dataset) { m_dataset->updateSeries(q); // this notifies legend } @@ -526,7 +523,8 @@ bool QBarSeriesPrivate::append(QList sets) foreach (QBarSet* set, sets) { m_barSets.append(set); - QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SLOT(barsetChanged())); + QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars())); + QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars())); } if (m_dataset) { m_dataset->updateSeries(q); // this notifies legend @@ -542,7 +540,8 @@ bool QBarSeriesPrivate::remove(QList sets) foreach (QBarSet* set, sets) { if (m_barSets.contains(set)) { m_barSets.removeOne(set); - QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SLOT(barsetChanged())); + QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars())); + QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars())); setsRemoved = true; } } diff --git a/src/barchart/qbarseries_p.h b/src/barchart/qbarseries_p.h index b21d661..d562018 100644 --- a/src/barchart/qbarseries_p.h +++ b/src/barchart/qbarseries_p.h @@ -83,9 +83,6 @@ Q_SIGNALS: void categoriesUpdated(); void labelsVisibleChanged(bool visible); -private Q_SLOTS: - void barsetChanged(); - protected: QList m_barSets; QStringList m_categories; diff --git a/tests/auto/qbarset/tst_qbarset.cpp b/tests/auto/qbarset/tst_qbarset.cpp index 4e59a6d..fdb980f 100644 --- a/tests/auto/qbarset/tst_qbarset.cpp +++ b/tests/auto/qbarset/tst_qbarset.cpp @@ -349,7 +349,8 @@ void tst_QBarSet::customize() // Append set1 to series QGroupedBarSeries *series = new QGroupedBarSeries(); - series->append(set1); + bool success = series->append(set1); + QVERIFY(success); // Add series to the chart QChartView view(new QChart());