stackedbarpresenter.cpp
111 lines
| 3.2 KiB
| text/x-c
|
CppLexer
|
r216 | #include "stackedbarpresenter.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" | ||
|
r96 | #include <QDebug> | ||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
|
r338 | StackedBarPresenter::StackedBarPresenter(QBarSeries *series, QGraphicsItem *parent) : | ||
|
r288 | BarPresenterBase(series,parent) | ||
|
r96 | { | ||
} | ||||
|
r216 | void StackedBarPresenter::layoutChanged() | ||
|
r96 | { | ||
// Scale bars to new layout | ||||
// Layout for bars: | ||||
|
r288 | if (mSeries->countSets() <= 0) { | ||
|
r173 | qDebug() << "No sets in model!"; | ||
|
r96 | // Nothing to do. | ||
return; | ||||
} | ||||
|
r288 | if (mSeries->countCategories() == 0) { | ||
|
r173 | qDebug() << "No categories in model!"; | ||
|
r119 | // Nothing to do | ||
return; | ||||
} | ||||
|
r165 | if (childItems().count() == 0) { | ||
|
r256 | qDebug() << "WARNING: StackedBarPresenter::layoutChanged called before graphics items are created!"; | ||
|
r165 | 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... | ||
|
r288 | qreal maxSum = mSeries->maxCategorySum(); | ||
|
r105 | qreal h = mHeight; | ||
qreal scale = (h / maxSum); | ||||
|
r96 | int itemIndex(0); | ||
|
r256 | int labelIndex(0); | ||
|
r98 | qreal tW = mWidth; | ||
|
r288 | qreal tC = mSeries->countCategories() + 1; | ||
|
r98 | qreal xStep = (tW/tC); | ||
|
r113 | qreal xPos = ((tW/tC) - mBarDefaultWidth / 2); | ||
|
r98 | |||
|
r288 | for (int category = 0; category < mSeries->countCategories(); category++) { | ||
|
r98 | 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); | ||
|
r96 | |||
bar->resize(mBarDefaultWidth, barHeight); | ||||
|
r357 | bar->setBrush(mSeries->barsetAt(set)->brush()); | ||
|
r113 | bar->setPos(xPos, yPos-barHeight); | ||
|
r96 | itemIndex++; | ||
|
r97 | yPos -= barHeight; | ||
|
r96 | } | ||
|
r114 | |||
// TODO: Layout for labels, remove magic number | ||||
|
r256 | BarLabel* label = mLabels.at(labelIndex); | ||
|
r114 | label->setPos(xPos, mHeight + 20); | ||
labelIndex++; | ||||
|
r98 | xPos += xStep; | ||
|
r96 | } | ||
|
r114 | |||
|
r119 | // 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)); | ||||
|
r119 | 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 | 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 | ||||
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) { | ||
value->setValueString(QString::number(mSeries->valueAt(set,category))); | ||||
|
r276 | } else { | ||
value->setValueString(QString("")); | ||||
} | ||||
|
r263 | |||
itemIndex++; | ||||
yPos -= barHeight; | ||||
} | ||||
xPos += xStep; | ||||
|
r119 | } | ||
|
r161 | |||
|
r96 | mLayoutDirty = true; | ||
} | ||||
|
r288 | #include "moc_stackedbarpresenter.cpp" | ||
|
r96 | QTCOMMERCIALCHART_END_NAMESPACE | ||