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