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