From 27902acc0ab130e056da4a44f76a9dc75dce22e9 2012-04-03 09:42:59 From: sauimone Date: 2012-04-03 09:42:59 Subject: [PATCH] fixed clipping in barcharts --- diff --git a/src/barchart/barchartitem.cpp b/src/barchart/barchartitem.cpp index 088581f..919171e 100644 --- a/src/barchart/barchartitem.cpp +++ b/src/barchart/barchartitem.cpp @@ -29,8 +29,8 @@ #include "chartpresenter_p.h" #include "chartanimator_p.h" #include "chartdataset_p.h" -#include #include +#include QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -49,6 +49,7 @@ BarChartItem::BarChartItem(QBarSeries *series, ChartPresenter *presenter) : BarChartItem::~BarChartItem() { disconnect(this,SLOT(showToolTip(QPoint,QString))); + deleteItems(); } void BarChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) @@ -58,21 +59,25 @@ void BarChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *opti return; } - foreach(QGraphicsItem* i, childItems()) - i->paint(painter,option,widget); + painter->save(); + painter->setClipRect(m_clipRect); + foreach (Bar *bar, m_bars) + bar->paint(painter,option,widget); + foreach (BarLabel* label, m_labels) + label->paint(painter,option,widget); + painter->restore(); } QRectF BarChartItem::boundingRect() const { - return m_rect; + return m_rect.translated(-m_rect.topLeft()); +// return m_rect; } void BarChartItem::dataChanged() { // TODO: performance optimizations. Do we really need to delete and create items every time data is changed or can we reuse them? - // Delete old bars - foreach (QGraphicsItem *item, childItems()) - delete item; + deleteItems(); m_bars.clear(); m_labels.clear(); @@ -83,8 +88,7 @@ void BarChartItem::dataChanged() QString category = m_series->categoryName(c); for (int s = 0; s < m_series->barsetCount(); s++) { QBarSet *set = m_series->barsetAt(s); - Bar *bar = new Bar(category,this); - childItems().append(bar); + Bar *bar = new Bar(category,0); // Null parent is best :) m_bars.append(bar); connect(bar, SIGNAL(clicked(QString,Qt::MouseButtons)), set, SIGNAL(clicked(QString,Qt::MouseButtons))); connect(bar, SIGNAL(hoverEntered(QPoint)), set, SLOT(barHoverEnterEvent(QPoint))); @@ -97,8 +101,7 @@ void BarChartItem::dataChanged() for (int category = 0; category < m_series->categoryCount(); category++) { for (int s = 0; s < m_series->barsetCount(); s++) { QBarSet *set = m_series->barsetAt(s); - BarLabel *value = new BarLabel(*set, this); - childItems().append(value); + BarLabel *value = new BarLabel(*set, 0); m_labels.append(value); connect(set,SIGNAL(labelsVisibleChanged(bool)),value,SLOT(labelsVisibleChanged(bool))); } @@ -152,7 +155,7 @@ QVector BarChartItem::calculateLayout() label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2) ,yPos - barHeight/2 - label->boundingRect().height()/2); -// value->setFont(barSet->valueFont()); + label->setFont(barSet->labelFont()); itemIndex++; xPos += barWidth; @@ -179,6 +182,14 @@ void BarChartItem::setLayout(const QVector &layout) update(); } +void BarChartItem::deleteItems() +{ + foreach (Bar *bar, m_bars) + delete bar; + foreach (BarLabel* label, m_labels) + delete label; +} + //handlers void BarChartItem::handleModelChanged(int index) @@ -198,6 +209,7 @@ void BarChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal void BarChartItem::handleGeometryChanged(const QRectF &rect) { + m_clipRect = rect.translated(-rect.topLeft()); m_rect = rect; handleLayoutChanged(); m_layoutSet = true; diff --git a/src/barchart/barchartitem_p.h b/src/barchart/barchartitem_p.h index c2dc3d4..a510f3d 100644 --- a/src/barchart/barchartitem_p.h +++ b/src/barchart/barchartitem_p.h @@ -57,6 +57,9 @@ public: QRectF geometry() const { return m_rect;} +private: + void deleteItems(); + public Q_SLOTS: void handleModelChanged(int index); void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY); @@ -75,6 +78,7 @@ protected: qreal m_domainMaxY; QRectF m_rect; + QRectF m_clipRect; bool m_layoutSet; // True, if component has been laid out. QVector m_layout; diff --git a/src/barchart/percentbarchartitem.cpp b/src/barchart/percentbarchartitem.cpp index 66bbbe3..fce1356 100644 --- a/src/barchart/percentbarchartitem.cpp +++ b/src/barchart/percentbarchartitem.cpp @@ -22,7 +22,6 @@ #include "bar_p.h" #include "barlabel_p.h" #include "qbarset.h" -#include QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -35,7 +34,7 @@ QVector PercentBarChartItem::calculateLayout() { QVector layout; - // Use temporary qreals for accurancy (we might get some compiler warnings... :) + // Use temporary qreals for accurancy qreal width = geometry().width(); qreal height = geometry().height(); @@ -72,7 +71,7 @@ QVector PercentBarChartItem::calculateLayout() label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2) ,yPos - barHeight/2 - label->boundingRect().height()/2); -// value->setFont(barSet->valueFont()); + label->setFont(barSet->labelFont()); itemIndex++; yPos -= barHeight; } diff --git a/src/barchart/qbarseries.cpp b/src/barchart/qbarseries.cpp index c5f55ac..8ff4614 100644 --- a/src/barchart/qbarseries.cpp +++ b/src/barchart/qbarseries.cpp @@ -391,6 +391,9 @@ QBarCategories QBarSeries::categories() const return categories; } +/*! + Sets the visibility of labels in series to \a visible +*/ void QBarSeries::setLabelsVisible(bool visible) { foreach (QBarSet* s, barSets()) { diff --git a/src/barchart/qbarset.cpp b/src/barchart/qbarset.cpp index c224805..0beb7ba 100644 --- a/src/barchart/qbarset.cpp +++ b/src/barchart/qbarset.cpp @@ -184,7 +184,7 @@ QBrush QBarSet::brush() const } /*! - Sets pen of the values that are drawn on top of this barset + Sets \a pen of the values that are drawn on top of this barset */ void QBarSet::setLabelPen(const QPen &pen) { @@ -201,7 +201,7 @@ QPen QBarSet::labelPen() const } /*! - Sets brush of the values that are drawn on top of this barset + Sets \a brush of the values that are drawn on top of this barset */ void QBarSet::setLabelBrush(const QBrush &brush) { @@ -218,7 +218,7 @@ QBrush QBarSet::labelBrush() const } /*! - Sets the pen for values that are drawn on top of this barset + Sets the \a font for values that are drawn on top of this barset */ void QBarSet::setLabelFont(const QFont &font) { diff --git a/src/barchart/stackedbarchartitem.cpp b/src/barchart/stackedbarchartitem.cpp index ef49195..2a11cde 100644 --- a/src/barchart/stackedbarchartitem.cpp +++ b/src/barchart/stackedbarchartitem.cpp @@ -22,7 +22,6 @@ #include "bar_p.h" #include "barlabel_p.h" #include "qbarset.h" -#include QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -31,24 +30,16 @@ StackedBarChartItem::StackedBarChartItem(QBarSeries *series, ChartPresenter *pre { } -StackedBarChartItem::~StackedBarChartItem() -{ -} - QVector StackedBarChartItem::calculateLayout() { QVector layout; - // Use temporary qreals for accurancy (we might get some compiler warnings... :) + // Use temporary qreals for accurancy - qreal maxSum = m_series->maxCategorySum(); // Domain: - if (m_domainMaxY > maxSum) { - maxSum = m_domainMaxY; - } - + qreal range = m_domainMaxY - m_domainMinY; qreal height = geometry().height(); qreal width = geometry().width(); - qreal scale = (height / m_series->maxCategorySum()); + qreal scale = (height / range); qreal categotyCount = m_series->categoryCount(); qreal barWidth = width / (categotyCount * 2); qreal xStep = width / categotyCount; @@ -56,7 +47,7 @@ QVector StackedBarChartItem::calculateLayout() int itemIndex(0); for (int category = 0; category < categotyCount; category++) { - qreal yPos = height; + qreal yPos = height + scale * m_domainMinY; for (int set=0; set < m_series->barsetCount(); set++) { QBarSet* barSet = m_series->barsetAt(set); @@ -77,7 +68,7 @@ QVector StackedBarChartItem::calculateLayout() label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2) ,yPos - barHeight/2 - label->boundingRect().height()/2); -// value->setFont(barSet->valueFont()); + label->setFont(barSet->labelFont()); itemIndex++; yPos -= barHeight; } diff --git a/src/barchart/stackedbarchartitem_p.h b/src/barchart/stackedbarchartitem_p.h index 599d129..462b866 100644 --- a/src/barchart/stackedbarchartitem_p.h +++ b/src/barchart/stackedbarchartitem_p.h @@ -32,7 +32,6 @@ class StackedBarChartItem : public BarChartItem Q_OBJECT public: StackedBarChartItem(QBarSeries *series, ChartPresenter *presenter); - ~StackedBarChartItem(); private: // From BarChartItem