From 0ff3e515e1f068e5360ada358f8cec400314d109 2012-07-03 15:52:25 From: sauimone Date: 2012-07-03 15:52:25 Subject: [PATCH] fixed crash in barchartitem after refactoring --- diff --git a/src/barchart/barchartitem.cpp b/src/barchart/barchartitem.cpp index 4d44323..bd33810 100644 --- a/src/barchart/barchartitem.cpp +++ b/src/barchart/barchartitem.cpp @@ -84,8 +84,8 @@ QVector BarChartItem::calculateLayout() qreal yPos = height + scaleY * m_domainMinY + geometry().topLeft().y(); for (int set = 0; set < setCount; set++) { QBarSetPrivate* barSet = m_series->d_func()->barsetAt(set)->d_ptr.data(); - qreal xPos = (barSet->m_values.at(category).x() - m_domainMinX) * scaleX + m_rect.left() - barWidth/2; - qreal barHeight = barSet->m_values.at(category).y() * scaleY; + qreal xPos = (barSet->at(category).x() - m_domainMinX) * scaleX + m_rect.left() - barWidth/2; + qreal barHeight = barSet->at(category).y() * scaleY; Bar* bar = m_bars.at(itemIndex); QRectF rect(xPos, yPos - barHeight, barWidth, barHeight); @@ -93,12 +93,16 @@ QVector BarChartItem::calculateLayout() layout.append(rect); bar->setPen(barSet->m_pen); bar->setBrush(barSet->m_brush); - bar->setVisible(barsVisible); + if (qFuzzyIsNull(barHeight)) { + bar->setVisible(false); + } else { + bar->setVisible(barsVisible); + } QGraphicsSimpleTextItem* label = m_labels.at(itemIndex); - if (!qFuzzyIsNull(barSet->m_values.at(category).y())) { - label->setText(QString::number(barSet->m_values.at(category).y())); + if (!qFuzzyIsNull(barSet->at(category).y())) { + label->setText(QString::number(barSet->at(category).y())); } else { label->setText(QString("")); } diff --git a/src/barchart/groupedbarchartitem.cpp b/src/barchart/groupedbarchartitem.cpp index 5a25e6a..2a290b6 100644 --- a/src/barchart/groupedbarchartitem.cpp +++ b/src/barchart/groupedbarchartitem.cpp @@ -56,22 +56,26 @@ QVector GroupedBarChartItem::calculateLayout() for (int set = 0; set < setCount; set++) { QBarSetPrivate* barSet = m_series->d_func()->barsetAt(set)->d_ptr.data(); - qreal xPos = (barSet->m_values.at(category).x() - m_domainMinX) * scaleX + m_rect.left(); + qreal xPos = (barSet->at(category).x() - m_domainMinX) * scaleX + m_rect.left(); xPos -= setCount*barWidth/2; xPos += set*barWidth; - qreal barHeight = barSet->m_values.at(category).y() * scaleY; + qreal barHeight = barSet->at(category).y() * scaleY; Bar* bar = m_bars.at(itemIndex); QRectF rect(xPos, yPos - barHeight, barWidth, barHeight); layout.append(rect); bar->setPen(barSet->m_pen); bar->setBrush(barSet->m_brush); - bar->setVisible(barsVisible); + if (qFuzzyIsNull(barHeight)) { + bar->setVisible(false); + } else { + bar->setVisible(barsVisible); + } QGraphicsSimpleTextItem* label = m_labels.at(itemIndex); - if (!qFuzzyIsNull(barSet->m_values.at(category).y())) { - label->setText(QString::number(barSet->m_values.at(category).y())); + if (!qFuzzyIsNull(barSet->at(category).y())) { + label->setText(QString::number(barSet->at(category).y())); } else { label->setText(QString("")); } diff --git a/src/barchart/percentbarchartitem.cpp b/src/barchart/percentbarchartitem.cpp index 70f7e28..e4960e7 100644 --- a/src/barchart/percentbarchartitem.cpp +++ b/src/barchart/percentbarchartitem.cpp @@ -58,13 +58,17 @@ QVector PercentBarChartItem::calculateLayout() for (int set=0; set < setCount; set++) { QBarSetPrivate* barSet = m_series->d_func()->barsetAt(set)->d_ptr.data(); - qreal xPos = (barSet->m_values.at(category).x() - m_domainMinX) * scaleX + m_rect.left() - barWidth/2; + qreal xPos = (barSet->at(category).x() - m_domainMinX) * scaleX + m_rect.left() - barWidth/2; - qreal barHeight = barSet->m_values.at(category).y() * percentage * scaleY; + qreal barHeight = barSet->at(category).y() * percentage * scaleY; Bar* bar = m_bars.at(itemIndex); bar->setPen(barSet->m_pen); bar->setBrush(barSet->m_brush); - bar->setVisible(barsVisible); + if (qFuzzyIsNull(barHeight)) { + bar->setVisible(false); + } else { + bar->setVisible(barsVisible); + } QRectF rect(xPos, yPos-barHeight, barWidth, barHeight); layout.append(rect); diff --git a/src/barchart/qbarset.cpp b/src/barchart/qbarset.cpp index dd0f32a..a69ec96 100644 --- a/src/barchart/qbarset.cpp +++ b/src/barchart/qbarset.cpp @@ -611,6 +611,16 @@ void QBarSetPrivate::replace(const int index, const QPointF value) emit updatedBars(); } +QPointF QBarSetPrivate::at(const int index) +{ + if (index < 0 || index >= m_values.count()) { + return QPointF(0,0); + } + + return m_values.at(index); +} + + #include "moc_qbarset.cpp" #include "moc_qbarset_p.cpp" diff --git a/src/barchart/qbarset_p.h b/src/barchart/qbarset_p.h index 1888dba..6ff61b0 100644 --- a/src/barchart/qbarset_p.h +++ b/src/barchart/qbarset_p.h @@ -57,6 +57,8 @@ public: void replace(const int index, const qreal value); void replace(const int index, const QPointF value); + QPointF at(const int index); + Q_SIGNALS: void restructuredBars(); void updatedBars(); diff --git a/src/barchart/stackedbarchartitem.cpp b/src/barchart/stackedbarchartitem.cpp index c437c54..b119017 100644 --- a/src/barchart/stackedbarchartitem.cpp +++ b/src/barchart/stackedbarchartitem.cpp @@ -55,21 +55,25 @@ QVector StackedBarChartItem::calculateLayout() for (int set=0; set < setCount; set++) { QBarSetPrivate* barSet = m_series->d_func()->barsetAt(set)->d_ptr.data(); - qreal xPos = (barSet->m_values.at(category).x() - m_domainMinX) * scaleX + m_rect.left() - barWidth/2; + qreal xPos = (barSet->at(category).x() - m_domainMinX) * scaleX + m_rect.left() - barWidth/2; - qreal barHeight = barSet->m_values.at(category).y() * scaleY; + qreal barHeight = barSet->at(category).y() * scaleY; Bar* bar = m_bars.at(itemIndex); bar->setPen(barSet->m_pen); bar->setBrush(barSet->m_brush); - bar->setVisible(barsVisible); + if (qFuzzyIsNull(barHeight)) { + bar->setVisible(false); + } else { + bar->setVisible(barsVisible); + } QRectF rect(xPos, yPos-barHeight, barWidth, barHeight); layout.append(rect); QGraphicsSimpleTextItem* label = m_labels.at(itemIndex); - if (!qFuzzyIsNull(barSet->m_values.at(category).y())) { - label->setText(QString::number(barSet->m_values.at(category).y())); + if (!qFuzzyIsNull(barSet->at(category).y())) { + label->setText(QString::number(barSet->at(category).y())); } else { label->setText(QString("")); }