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