bargroup.cpp
138 lines
| 3.4 KiB
| text/x-c
|
CppLexer
|
r56 | #include "bargroup.h" | ||
#include "bar.h" | ||||
#include <QDebug> | ||||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
|
r71 | // TODO: singleton? | ||
//BarGroup* BarGroup::mBarGroupInstance = NULL; | ||||
//BarGroup::BarGroup(QGraphicsItem *parent) : | ||||
// QGraphicsItem(parent) | ||||
// ,mSeries(series) | ||||
|
r78 | BarGroup::BarGroup(BarChartSeries& series, QGraphicsItem *parent) : | ||
|
r74 | ChartItem(parent) | ||
|
r71 | ,mSeries(series) | ||
,mLayoutSet(false) | ||||
,mLayoutDirty(true) | ||||
|
r78 | ,mBarDefaultWidth(10) | ||
|
r56 | { | ||
dataChanged(); | ||||
} | ||||
|
r78 | |||
void BarGroup::setSize(const QSize& size) | ||||
{ | ||||
// TODO: refactor this | ||||
qDebug() << "BarGroup::setSize"; | ||||
resize(size.width(),size.height()); | ||||
} | ||||
void BarGroup::setPlotDomain(const PlotDomain& data) | ||||
{ | ||||
qDebug() << "BarGroup::setPlotDomain"; | ||||
|
r82 | // TODO: | ||
|
r78 | } | ||
|
r56 | void BarGroup::resize( int w, int h ) | ||
{ | ||||
qDebug() << "QBarChart::resize"; | ||||
mWidth = w; | ||||
mHeight = h; | ||||
layoutChanged(); | ||||
mLayoutSet = true; | ||||
} | ||||
void BarGroup::setBarWidth( int w ) | ||||
{ | ||||
mBarDefaultWidth = w; | ||||
} | ||||
|
r82 | int BarGroup::addColor( QColor color ) | ||
|
r56 | { | ||
|
r82 | int colorIndex = mColors.count(); | ||
mColors.append(color); | ||||
return colorIndex; | ||||
|
r56 | } | ||
void BarGroup::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 BarGroup::boundingRect() const | ||||
{ | ||||
return QRectF(0,0,mWidth,mHeight); | ||||
} | ||||
void BarGroup::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 | ||||
|
r71 | int totalItems = mSeries.countTotalItems(); | ||
for (int i=0; i<totalItems; i++) { | ||||
|
r56 | Bar *bar = new Bar(this); | ||
childItems().append(bar); | ||||
} | ||||
mLayoutDirty = true; | ||||
} | ||||
void BarGroup::layoutChanged() | ||||
{ | ||||
// Scale bars to new layout | ||||
// Layout for bars: | ||||
|
r78 | if (mSeries.countSeries() <= 0) { | ||
|
r56 | // Nothing to do. | ||
return; | ||||
} | ||||
|
r82 | // TODO: better way to auto-layout | ||
|
r78 | int count = mSeries.countItemsInSeries(); | ||
|
r82 | int posStep = (mWidth / (count+1)); | ||
int startPos = (mWidth / (count+1)) - mSeries.countSeries() * mBarDefaultWidth /2; | ||||
|
r56 | qDebug() << "startpos" << startPos; | ||
|
r82 | // Scaling. | ||
|
r71 | int itemIndex(0); | ||
|
r78 | for (int series = 0; series < mSeries.countSeries(); series++) { | ||
for (int item=0; item < mSeries.countItemsInSeries(); item++) { | ||||
qDebug() << itemIndex; | ||||
|
r71 | int barHeight = mSeries.valueAt(series, item) * mHeight / mMax; | ||
Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex)); | ||||
|
r82 | // TODO: width settable per bar? | ||
bar->resize(mBarDefaultWidth, barHeight); | ||||
bar->setColor(mColors.at(series)); | ||||
// TODO: bar width shouldn't affect height. Currently it does because pen width also affects height. (QPainter thingy...) | ||||
bar->setPos(item*posStep+startPos + series * mBarDefaultWidth, mHeight - barHeight + mBarDefaultWidth); | ||||
|
r71 | itemIndex++; | ||
} | ||||
|
r56 | } | ||
mLayoutDirty = true; | ||||
} | ||||
QTCOMMERCIALCHART_END_NAMESPACE | ||||