#include "stackedbargroup.h" #include "bar.h" #include QTCOMMERCIALCHART_BEGIN_NAMESPACE StackedBarGroup::StackedBarGroup(StackedBarChartSeries& series, QGraphicsItem *parent) : ChartItem(parent) ,mSeries(series) ,mLayoutSet(false) ,mLayoutDirty(true) ,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready { dataChanged(); } void StackedBarGroup::setSize(const QSize& size) { // qDebug() << "StackedBarGroup::setSize"; mWidth = size.width(); mHeight = size.height(); layoutChanged(); mLayoutSet = true; } void StackedBarGroup::setPlotDomain(const PlotDomain& data) { qDebug() << "StackedBarGroup::setPlotDomain"; // TODO: } void StackedBarGroup::setTheme(ChartTheme *theme) { qDebug() << "StackedBarGroup::setTheme"; // TODO: } void StackedBarGroup::setBarWidth( int w ) { mBarDefaultWidth = w; } int StackedBarGroup::addColor( QColor color ) { int colorIndex = mColors.count(); mColors.append(color); return colorIndex; } void StackedBarGroup::resetColors() { mColors.clear(); } void StackedBarGroup::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { if (!mLayoutSet) { qDebug() << "QBarChart::paint called without layout set. Aborting."; return; } if (mLayoutDirty) { // Layout or data has changed. Need to redraw. foreach(QGraphicsItem* i, childItems()) { i->paint(painter,option,widget); } } } QRectF StackedBarGroup::boundingRect() const { return QRectF(0,0,mWidth,mHeight); } void StackedBarGroup::dataChanged() { qDebug() << "QBarChart::dataChanged mSeries"; // Find out maximum and minimum of all series mMax = mSeries.max(); mMin = mSeries.min(); // Delete old bars // Is this correct way to delete childItems? foreach (QGraphicsItem* item, childItems()) { delete item; } // Create new graphic items for bars int totalItems = mSeries.countTotalItems(); for (int i=0; i (childItems().at(itemIndex)); // TODO: width settable per bar? bar->resize(mBarDefaultWidth, barHeight); bar->setColor(mColors.at(row)); bar->setPos(xPos, yPos); itemIndex++; yPos -= barHeight; } xPos += xStep; } mLayoutDirty = true; } QTCOMMERCIALCHART_END_NAMESPACE