percentbarpresenter.cpp
107 lines
| 3.2 KiB
| text/x-c
|
CppLexer
sauimone
|
r216 | #include "percentbarpresenter.h" | ||
sauimone
|
r118 | #include "bar_p.h" | ||
sauimone
|
r114 | #include "barlabel_p.h" | ||
sauimone
|
r263 | #include "barvalue_p.h" | ||
sauimone
|
r126 | #include "separator_p.h" | ||
sauimone
|
r214 | #include "qbarset.h" | ||
sauimone
|
r101 | #include <QDebug> | ||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
sauimone
|
r216 | PercentBarPresenter::PercentBarPresenter(BarChartModel& model, QGraphicsItem *parent) : | ||
BarPresenterBase(model, parent) | ||||
sauimone
|
r101 | { | ||
} | ||||
sauimone
|
r216 | void PercentBarPresenter::layoutChanged() | ||
sauimone
|
r101 | { | ||
// Scale bars to new layout | ||||
// Layout for bars: | ||||
sauimone
|
r171 | if (mModel.countSets() <= 0) { | ||
sauimone
|
r173 | qDebug() << "No sets in model!"; | ||
sauimone
|
r101 | // Nothing to do. | ||
return; | ||||
} | ||||
sauimone
|
r165 | if (childItems().count() == 0) { | ||
sauimone
|
r256 | qDebug() << "WARNING: PercentBarPresenter::layoutChanged called before graphics items are created!"; | ||
sauimone
|
r165 | return; | ||
} | ||||
sauimone
|
r101 | // TODO: better way to auto-layout | ||
// Use reals for accurancy (we might get some compiler warnings... :) | ||||
sauimone
|
r171 | int count = mModel.countCategories(); | ||
sauimone
|
r101 | int itemIndex(0); | ||
sauimone
|
r256 | int labelIndex(0); | ||
sauimone
|
r101 | qreal tW = mWidth; | ||
qreal tC = count+1; | ||||
qreal xStep = (tW/tC); | ||||
sauimone
|
r114 | qreal xPos = ((tW/tC) - mBarDefaultWidth / 2); | ||
sauimone
|
r263 | qreal h = mHeight; | ||
sauimone
|
r101 | |||
sauimone
|
r183 | for (int category = 0; category < mModel.countCategories(); category++) { | ||
qreal colSum = mModel.categorySum(category); | ||||
sauimone
|
r105 | qreal scale = (h / colSum); | ||
sauimone
|
r101 | qreal yPos = h; | ||
sauimone
|
r183 | for (int set=0; set < mModel.countSets(); set++) { | ||
qreal barHeight = mModel.valueAt(set, category) * scale; | ||||
sauimone
|
r256 | Bar* bar = mBars.at(itemIndex); | ||
sauimone
|
r101 | |||
// TODO: width settable per bar? | ||||
bar->resize(mBarDefaultWidth, barHeight); | ||||
sauimone
|
r273 | bar->setBrush(mModel.setAt(set)->brush()); | ||
sauimone
|
r113 | bar->setPos(xPos, yPos-barHeight); | ||
sauimone
|
r101 | itemIndex++; | ||
yPos -= barHeight; | ||||
} | ||||
sauimone
|
r114 | |||
// TODO: Layout for labels, remove magic number | ||||
sauimone
|
r256 | BarLabel* label = mLabels.at(labelIndex); | ||
sauimone
|
r114 | label->setPos(xPos, mHeight + 20); | ||
labelIndex++; | ||||
sauimone
|
r101 | xPos += xStep; | ||
} | ||||
sauimone
|
r126 | |||
// Position separators | ||||
sauimone
|
r256 | xPos = xStep + xStep/2; | ||
sauimone
|
r171 | for (int s=0; s < mModel.countCategories() - 1; s++) { | ||
sauimone
|
r263 | Separator* sep = mSeparators.at(s); | ||
sauimone
|
r126 | sep->setPos(xPos,0); | ||
sep->setSize(QSizeF(1,mHeight)); | ||||
xPos += xStep; | ||||
sauimone
|
r263 | } | ||
// Position floating values | ||||
itemIndex = 0; | ||||
xPos = ((tW/tC) - mBarDefaultWidth / 2); | ||||
for (int category=0; category < mModel.countCategories(); category++) { | ||||
qreal yPos = h; | ||||
qreal colSum = mModel.categorySum(category); | ||||
qreal scale = (h / colSum); | ||||
for (int set=0; set < mModel.countSets(); set++) { | ||||
qreal barHeight = mModel.valueAt(set,category) * scale; | ||||
BarValue* value = mFloatingValues.at(itemIndex); | ||||
// TODO: remove hard coding, apply layout | ||||
sauimone
|
r276 | value->setPos(xPos, yPos-barHeight/2); | ||
sauimone
|
r263 | value->setPen(QPen(QColor(255,255,255,255))); | ||
sauimone
|
r276 | if (mModel.valueAt(set,category) != 0) { | ||
QString vString(QString::number(mModel.percentageAt(set,category) * 100)); | ||||
vString.append("%"); | ||||
value->setValueString(vString); | ||||
} else { | ||||
value->setValueString(QString("")); | ||||
} | ||||
sauimone
|
r263 | |||
itemIndex++; | ||||
yPos -= barHeight; | ||||
} | ||||
xPos += xStep; | ||||
sauimone
|
r126 | } | ||
sauimone
|
r161 | |||
sauimone
|
r101 | mLayoutDirty = true; | ||
} | ||||
QTCOMMERCIALCHART_END_NAMESPACE | ||||