percentbargroup.cpp
133 lines
| 3.2 KiB
| text/x-c
|
CppLexer
|
r101 | #include "percentbargroup.h" | ||
#include "bar.h" | ||||
#include <QDebug> | ||||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
|
r105 | PercentBarGroup::PercentBarGroup(PercentBarChartSeries& series, QGraphicsItem *parent) : | ||
ChartItem(parent) | ||||
,mSeries(series) | ||||
,mLayoutSet(false) | ||||
,mLayoutDirty(true) | ||||
,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready | ||||
|
r101 | { | ||
dataChanged(); | ||||
} | ||||
|
r105 | |||
void PercentBarGroup::setSize(const QSize& size) | ||||
|
r103 | { | ||
|
r105 | // qDebug() << "PercentBarGroup::setSize"; | ||
|
r101 | mWidth = size.width(); | ||
mHeight = size.height(); | ||||
layoutChanged(); | ||||
mLayoutSet = true; | ||||
} | ||||
|
r105 | void PercentBarGroup::setPlotDomain(const PlotDomain& data) | ||
|
r101 | { | ||
|
r105 | qDebug() << "PercentBarGroup::setPlotDomain"; | ||
|
r101 | // TODO: | ||
} | ||||
|
r105 | void PercentBarGroup::setBarWidth( int w ) | ||
|
r101 | { | ||
mBarDefaultWidth = w; | ||||
} | ||||
|
r105 | int PercentBarGroup::addColor( QColor color ) | ||
|
r101 | { | ||
int colorIndex = mColors.count(); | ||||
mColors.append(color); | ||||
return colorIndex; | ||||
} | ||||
|
r105 | void PercentBarGroup::resetColors() | ||
|
r101 | { | ||
mColors.clear(); | ||||
} | ||||
|
r105 | void PercentBarGroup::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | ||
|
r101 | { | ||
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); | ||||
} | ||||
} | ||||
} | ||||
|
r105 | QRectF PercentBarGroup::boundingRect() const | ||
|
r101 | { | ||
return QRectF(0,0,mWidth,mHeight); | ||||
} | ||||
|
r105 | void PercentBarGroup::dataChanged() | ||
|
r101 | { | ||
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<totalItems; i++) { | ||||
Bar *bar = new Bar(this); | ||||
childItems().append(bar); | ||||
} | ||||
mLayoutDirty = true; | ||||
} | ||||
|
r105 | void PercentBarGroup::layoutChanged() | ||
|
r101 | { | ||
// Scale bars to new layout | ||||
// Layout for bars: | ||||
if (mSeries.countRows() <= 0) { | ||||
// Nothing to do. | ||||
return; | ||||
} | ||||
// TODO: better way to auto-layout | ||||
// Use reals for accurancy (we might get some compiler warnings... :) | ||||
int count = mSeries.countColumns(); | ||||
int itemIndex(0); | ||||
qreal tW = mWidth; | ||||
qreal tC = count+1; | ||||
qreal xStep = (tW/tC); | ||||
qreal xPos = ((tW/tC) + mBarDefaultWidth / 2); | ||||
for (int column = 0; column < mSeries.countColumns(); column++) { | ||||
|
r105 | qreal colSum = mSeries.columnSum(column); | ||
qreal h = mHeight; | ||||
qreal scale = (h / colSum); | ||||
|
r101 | qreal yPos = h; | ||
for (int row=0; row < mSeries.countRows(); row++) { | ||||
qreal barHeight = mSeries.valueAt(row, column) * scale; | ||||
Bar* bar = reinterpret_cast<Bar*> (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 | ||||