bargroup.cpp
70 lines
| 2.1 KiB
| text/x-c
|
CppLexer
sauimone
|
r56 | #include "bargroup.h" | ||
sauimone
|
r118 | #include "bar_p.h" | ||
sauimone
|
r114 | #include "barlabel_p.h" | ||
sauimone
|
r56 | #include <QDebug> | ||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
sauimone
|
r78 | BarGroup::BarGroup(BarChartSeries& series, QGraphicsItem *parent) : | ||
sauimone
|
r126 | BarGroupBase(series,parent) | ||
sauimone
|
r56 | { | ||
sauimone
|
r126 | mBarDefaultWidth = 10; | ||
sauimone
|
r56 | } | ||
void BarGroup::layoutChanged() | ||||
{ | ||||
sauimone
|
r161 | qDebug() << "BarGroup::layoutChanged"; | ||
sauimone
|
r56 | // Scale bars to new layout | ||
// Layout for bars: | ||||
sauimone
|
r161 | if (mModel.countRows() <= 0) { | ||
sauimone
|
r159 | return; | ||
} | ||||
sauimone
|
r56 | |||
sauimone
|
r165 | if (childItems().count() == 0) { | ||
qDebug() << "WARNING: BarGroup::layoutChanged called before graphics items are created!"; | ||||
return; | ||||
} | ||||
sauimone
|
r101 | // TODO: better way to auto-layout? | ||
sauimone
|
r99 | // Use reals for accurancy (we might get some compiler warnings... :) | ||
sauimone
|
r161 | int itemCount = mModel.countColumns(); | ||
int seriesCount = mModel.countRows(); | ||||
sauimone
|
r56 | |||
sauimone
|
r99 | qreal tW = mWidth; | ||
qreal tH = mHeight; | ||||
qreal tM = mMax; | ||||
qreal scale = (tH/tM); | ||||
sauimone
|
r159 | qreal tC = itemCount+1; | ||
sauimone
|
r99 | qreal xStepPerSeries = (tW/tC); | ||
sauimone
|
r82 | // Scaling. | ||
sauimone
|
r71 | int itemIndex(0); | ||
sauimone
|
r159 | int labelIndex = itemCount * seriesCount; | ||
sauimone
|
r114 | |||
sauimone
|
r159 | for (int item=0; item < itemCount; item++) { | ||
qreal xPos = xStepPerSeries * item + ((tW + mBarDefaultWidth*seriesCount)/(itemCount*2)); | ||||
sauimone
|
r113 | qreal yPos = mHeight; | ||
sauimone
|
r159 | for (int series = 0; series < seriesCount; series++) { | ||
qreal barHeight = mModel.valueAt(series, item) * scale; | ||||
sauimone
|
r99 | Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex)); | ||
// TODO: width settable per bar? | ||||
bar->resize(mBarDefaultWidth, barHeight); | ||||
sauimone
|
r159 | bar->setColor(mColors.at(series)); | ||
sauimone
|
r113 | bar->setPos(xPos, yPos-barHeight); // item*posStep+startPos + series * mBarDefaultWidth, mHeight); | ||
sauimone
|
r99 | itemIndex++; | ||
xPos += mBarDefaultWidth; | ||||
} | ||||
sauimone
|
r114 | |||
// TODO: Layout for labels, remove magic number | ||||
sauimone
|
r159 | xPos = xStepPerSeries * item + ((tW + mBarDefaultWidth*seriesCount)/(itemCount*2)); | ||
sauimone
|
r114 | BarLabel* label = reinterpret_cast<BarLabel*> (childItems().at(labelIndex)); | ||
label->setPos(xPos, mHeight + 20); | ||||
labelIndex++; | ||||
sauimone
|
r99 | } | ||
sauimone
|
r56 | mLayoutDirty = true; | ||
} | ||||
QTCOMMERCIALCHART_END_NAMESPACE | ||||