stackedbarpresenter.cpp
85 lines
| 2.7 KiB
| text/x-c
|
CppLexer
|
r216 | #include "stackedbarpresenter.h" | ||
|
r118 | #include "bar_p.h" | ||
|
r114 | #include "barlabel_p.h" | ||
|
r126 | #include "separator_p.h" | ||
|
r214 | #include "qbarset.h" | ||
|
r96 | #include <QDebug> | ||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
|
r216 | StackedBarPresenter::StackedBarPresenter(BarChartModel& model, QGraphicsItem *parent) : | ||
BarPresenterBase(model,parent) | ||||
|
r96 | { | ||
} | ||||
|
r216 | void StackedBarPresenter::layoutChanged() | ||
|
r96 | { | ||
|
r169 | // qDebug() << "StackedBarGroup::layoutChanged"; | ||
|
r96 | // Scale bars to new layout | ||
// Layout for bars: | ||||
|
r171 | if (mModel.countSets() <= 0) { | ||
|
r173 | qDebug() << "No sets in model!"; | ||
|
r96 | // Nothing to do. | ||
return; | ||||
} | ||||
|
r171 | if (mModel.countCategories() == 0) { | ||
|
r173 | qDebug() << "No categories in model!"; | ||
|
r119 | // Nothing to do | ||
return; | ||||
} | ||||
|
r165 | if (childItems().count() == 0) { | ||
qDebug() << "WARNING: StackedBarGroup::layoutChanged called before graphics items are created!"; | ||||
return; | ||||
} | ||||
|
r96 | // TODO: better way to auto-layout | ||
|
r99 | // Use reals for accurancy (we might get some compiler warnings... :) | ||
|
r183 | // TODO: use temp variable for category count... | ||
|
r171 | qreal maxSum = mModel.maxCategorySum(); | ||
|
r105 | qreal h = mHeight; | ||
qreal scale = (h / maxSum); | ||||
|
r96 | int itemIndex(0); | ||
|
r98 | qreal tW = mWidth; | ||
|
r171 | qreal tC = mModel.countCategories() + 1; | ||
|
r98 | qreal xStep = (tW/tC); | ||
|
r113 | qreal xPos = ((tW/tC) - mBarDefaultWidth / 2); | ||
|
r171 | int labelIndex = mModel.countSets() * mModel.countCategories(); | ||
|
r98 | |||
|
r183 | for (int category = 0; category < mModel.countCategories(); category++) { | ||
|
r98 | qreal yPos = h; | ||
|
r183 | for (int set=0; set < mModel.countSets(); set++) { | ||
qreal barHeight = mModel.valueAt(set, category) * scale; | ||||
|
r96 | Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex)); | ||
bar->resize(mBarDefaultWidth, barHeight); | ||||
|
r214 | bar->setBrush(mModel.setAt(set).brush()); | ||
|
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 | |||
|
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. | ||||
|
r171 | for (int s=0; s < mModel.countCategories() - 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 | ||||