percentbarpresenter.cpp
80 lines
| 2.6 KiB
| text/x-c
|
CppLexer
|
r216 | #include "percentbarpresenter.h" | ||
|
r118 | #include "bar_p.h" | ||
|
r114 | #include "barlabel_p.h" | ||
|
r126 | #include "separator_p.h" | ||
|
r214 | #include "qbarset.h" | ||
|
r101 | #include <QDebug> | ||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
|
r216 | PercentBarPresenter::PercentBarPresenter(BarChartModel& model, QGraphicsItem *parent) : | ||
BarPresenterBase(model, parent) | ||||
|
r101 | { | ||
} | ||||
|
r216 | void PercentBarPresenter::layoutChanged() | ||
|
r101 | { | ||
|
r173 | // qDebug() << "PercentBarGroup::layoutChanged"; | ||
|
r101 | // Scale bars to new layout | ||
// Layout for bars: | ||||
|
r171 | if (mModel.countSets() <= 0) { | ||
|
r173 | qDebug() << "No sets in model!"; | ||
|
r101 | // Nothing to do. | ||
return; | ||||
} | ||||
|
r165 | if (childItems().count() == 0) { | ||
qDebug() << "WARNING: PercentBarGroup::layoutChanged called before graphics items are created!"; | ||||
return; | ||||
} | ||||
|
r101 | // TODO: better way to auto-layout | ||
// Use reals for accurancy (we might get some compiler warnings... :) | ||||
|
r171 | int count = mModel.countCategories(); | ||
|
r101 | int itemIndex(0); | ||
qreal tW = mWidth; | ||||
qreal tC = count+1; | ||||
qreal xStep = (tW/tC); | ||||
|
r114 | qreal xPos = ((tW/tC) - mBarDefaultWidth / 2); | ||
|
r171 | int labelIndex = mModel.countCategories() * mModel.countSets(); | ||
|
r101 | |||
|
r183 | for (int category = 0; category < mModel.countCategories(); category++) { | ||
qreal colSum = mModel.categorySum(category); | ||||
|
r105 | qreal h = mHeight; | ||
qreal scale = (h / colSum); | ||||
|
r101 | qreal yPos = h; | ||
|
r183 | for (int set=0; set < mModel.countSets(); set++) { | ||
qreal barHeight = mModel.valueAt(set, category) * scale; | ||||
|
r101 | Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex)); | ||
// TODO: width settable per bar? | ||||
bar->resize(mBarDefaultWidth, barHeight); | ||||
|
r214 | bar->setBrush(mModel.setAt(set).brush()); | ||
|
r113 | bar->setPos(xPos, yPos-barHeight); | ||
|
r101 | itemIndex++; | ||
yPos -= barHeight; | ||||
} | ||||
|
r114 | |||
// TODO: Layout for labels, remove magic number | ||||
BarLabel* label = reinterpret_cast<BarLabel*> (childItems().at(labelIndex)); | ||||
label->setPos(xPos, mHeight + 20); | ||||
labelIndex++; | ||||
|
r101 | xPos += xStep; | ||
} | ||||
|
r126 | |||
// Position separators | ||||
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)); | ||||
xPos += xStep; | ||||
separatorIndex++; | ||||
} | ||||
|
r161 | |||
|
r101 | mLayoutDirty = true; | ||
} | ||||
QTCOMMERCIALCHART_END_NAMESPACE | ||||