stackedbargroup.cpp
81 lines
| 2.4 KiB
| text/x-c
|
CppLexer
|
r105 | #include "stackedbargroup.h" | ||
|
r118 | #include "bar_p.h" | ||
|
r114 | #include "barlabel_p.h" | ||
|
r126 | #include "separator_p.h" | ||
|
r96 | #include <QDebug> | ||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
|
r105 | StackedBarGroup::StackedBarGroup(StackedBarChartSeries& series, QGraphicsItem *parent) : | ||
|
r126 | BarGroupBase(series,parent) | ||
|
r96 | { | ||
} | ||||
|
r105 | void StackedBarGroup::layoutChanged() | ||
|
r96 | { | ||
|
r161 | |||
|
r96 | // Scale bars to new layout | ||
// Layout for bars: | ||||
|
r161 | if (mModel.countRows() <= 0) { | ||
|
r96 | // Nothing to do. | ||
return; | ||||
} | ||||
|
r161 | if (mModel.countColumns() == 0) { | ||
|
r119 | // Nothing to do | ||
return; | ||||
} | ||||
|
r96 | // TODO: better way to auto-layout | ||
|
r99 | // Use reals for accurancy (we might get some compiler warnings... :) | ||
|
r119 | // TODO: use temp variable for column count... | ||
|
r161 | qreal maxSum = mModel.maxColumnSum(); | ||
|
r105 | qreal h = mHeight; | ||
qreal scale = (h / maxSum); | ||||
|
r96 | int itemIndex(0); | ||
|
r98 | qreal tW = mWidth; | ||
|
r161 | qreal tC = mModel.countColumns() + 1; | ||
|
r98 | qreal xStep = (tW/tC); | ||
|
r113 | qreal xPos = ((tW/tC) - mBarDefaultWidth / 2); | ||
|
r161 | int labelIndex = mModel.countRows() * mModel.countColumns(); | ||
|
r98 | |||
|
r161 | for (int column = 0; column < mModel.countColumns(); column++) { | ||
|
r98 | qreal yPos = h; | ||
|
r161 | for (int row=0; row < mModel.countRows(); row++) { | ||
qreal barHeight = mModel.valueAt(row, column) * scale; | ||||
|
r96 | Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex)); | ||
// TODO: width settable per bar? | ||||
|
r162 | // TODO: theme stuff | ||
|
r113 | // mTheme->themeForSeries(); | ||
|
r96 | bar->resize(mBarDefaultWidth, barHeight); | ||
bar->setColor(mColors.at(row)); | ||||
|
r113 | bar->setPos(xPos, yPos-barHeight); | ||
|
r96 | itemIndex++; | ||
|
r97 | yPos -= barHeight; | ||
|
r96 | } | ||
|
r114 | |||
// TODO: Layout for labels, remove magic number | ||||
BarLabel* label = reinterpret_cast<BarLabel*> (childItems().at(labelIndex)); | ||||
label->setPos(xPos, mHeight + 20); | ||||
labelIndex++; | ||||
|
r98 | xPos += xStep; | ||
|
r96 | } | ||
|
r114 | |||
|
r126 | |||
|
r119 | // Position separators | ||
|
r126 | int separatorIndex = labelIndex; // Separators are after labels in childItems(). TODO: better way to store these? | ||
xPos = xStep + xStep/2; // Initial position is between first and second group. ie one and half steps from left. | ||||
|
r161 | for (int s=0; s < mModel.countColumns() - 1; s++) { | ||
|
r126 | Separator* sep = reinterpret_cast<Separator*> (childItems().at(separatorIndex)); | ||
sep->setPos(xPos,0); | ||||
sep->setSize(QSizeF(1,mHeight)); | ||||
|
r119 | xPos += xStep; | ||
|
r126 | separatorIndex++; | ||
|
r119 | } | ||
|
r161 | |||
|
r96 | mLayoutDirty = true; | ||
} | ||||
QTCOMMERCIALCHART_END_NAMESPACE | ||||