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