stackedbarpresenter.cpp
106 lines
| 3.0 KiB
| text/x-c
|
CppLexer
|
r381 | #include "stackedbarpresenter_p.h" | ||
|
r118 | #include "bar_p.h" | ||
|
r263 | #include "barvalue_p.h" | ||
|
r126 | #include "separator_p.h" | ||
|
r214 | #include "qbarset.h" | ||
|
r96 | #include <QDebug> | ||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
|
r487 | StackedBarPresenter::StackedBarPresenter(QBarSeries *series, QChart *parent) : | ||
|
r288 | BarPresenterBase(series,parent) | ||
|
r96 | { | ||
} | ||||
|
r438 | StackedBarPresenter::~StackedBarPresenter() | ||
{ | ||||
} | ||||
|
r216 | void StackedBarPresenter::layoutChanged() | ||
|
r96 | { | ||
// Scale bars to new layout | ||||
// Layout for bars: | ||||
|
r366 | if (mSeries->barsetCount() <= 0) { | ||
|
r173 | qDebug() << "No sets in model!"; | ||
|
r96 | // Nothing to do. | ||
return; | ||||
} | ||||
|
r366 | if (mSeries->categoryCount() == 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; | ||
} | ||||
|
r473 | // Use temporary qreals for accurancy (we might get some compiler warnings... :) | ||
|
r288 | qreal maxSum = mSeries->maxCategorySum(); | ||
|
r105 | qreal h = mHeight; | ||
qreal scale = (h / maxSum); | ||||
|
r98 | qreal tW = mWidth; | ||
|
r366 | qreal tC = mSeries->categoryCount() + 1; | ||
|
r473 | qreal cC = mSeries->categoryCount() * 2 + 1; | ||
mBarWidth = tW / cC; | ||||
|
r98 | qreal xStep = (tW/tC); | ||
|
r473 | qreal xPos = ((tW/tC) - mBarWidth / 2); | ||
|
r98 | |||
|
r473 | int itemIndex(0); | ||
|
r366 | for (int category = 0; category < mSeries->categoryCount(); category++) { | ||
|
r98 | qreal yPos = h; | ||
|
r366 | for (int set=0; set < mSeries->barsetCount(); set++) { | ||
|
r288 | qreal barHeight = mSeries->valueAt(set, category) * scale; | ||
|
r256 | Bar* bar = mBars.at(itemIndex); | ||
|
r96 | |||
|
r473 | bar->resize(mBarWidth, barHeight); | ||
|
r357 | bar->setBrush(mSeries->barsetAt(set)->brush()); | ||
|
r113 | bar->setPos(xPos, yPos-barHeight); | ||
|
r96 | itemIndex++; | ||
|
r97 | yPos -= barHeight; | ||
|
r96 | } | ||
|
r98 | xPos += xStep; | ||
|
r96 | } | ||
|
r592 | /* | ||
|
r119 | // Position separators | ||
|
r256 | xPos = xStep + xStep/2; | ||
|
r366 | for (int s=0; s < mSeries->categoryCount() - 1; s++) { | ||
|
r263 | Separator* sep = mSeparators.at(s); | ||
|
r126 | sep->setPos(xPos,0); | ||
sep->setSize(QSizeF(1,mHeight)); | ||||
|
r119 | xPos += xStep; | ||
|
r263 | } | ||
|
r592 | */ | ||
|
r263 | // Position floating values | ||
itemIndex = 0; | ||||
|
r474 | xPos = (tW/tC); | ||
|
r366 | for (int category=0; category < mSeries->categoryCount(); category++) { | ||
|
r263 | qreal yPos = h; | ||
|
r366 | for (int set=0; set < mSeries->barsetCount(); set++) { | ||
|
r288 | qreal barHeight = mSeries->valueAt(set,category) * scale; | ||
|
r263 | BarValue* value = mFloatingValues.at(itemIndex); | ||
|
r512 | QBarSet* barSet = mSeries->barsetAt(set); | ||
value->resize(100,50); // TODO: proper layout for this. | ||||
|
r276 | value->setPos(xPos, yPos-barHeight/2); | ||
|
r512 | value->setPen(barSet->floatingValuePen()); | ||
|
r263 | |||
|
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 | } | ||
|
r96 | } | ||
|
r381 | #include "moc_stackedbarpresenter_p.cpp" | ||
|
r288 | |||
|
r96 | QTCOMMERCIALCHART_END_NAMESPACE | ||