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