barpresenter.cpp
96 lines
| 3.0 KiB
| text/x-c
|
CppLexer
|
r381 | #include "barpresenter_p.h" | ||
|
r118 | #include "bar_p.h" | ||
|
r263 | #include "barvalue_p.h" | ||
|
r505 | #include "separator_p.h" | ||
|
r214 | #include "qbarset.h" | ||
|
r56 | #include <QDebug> | ||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
|
r487 | BarPresenter::BarPresenter(QBarSeries *series, QChart *parent) : | ||
|
r288 | BarPresenterBase(series, parent) | ||
|
r56 | { | ||
} | ||||
|
r216 | void BarPresenter::layoutChanged() | ||
|
r56 | { | ||
// Scale bars to new layout | ||||
// Layout for bars: | ||||
|
r366 | if (mSeries->barsetCount() <= 0) { | ||
|
r173 | qDebug() << "No sets in model!"; | ||
|
r159 | return; | ||
} | ||||
|
r56 | |||
|
r165 | if (childItems().count() == 0) { | ||
|
r256 | qDebug() << "WARNING: BarPresenter::layoutChanged called before graphics items are created!"; | ||
|
r165 | return; | ||
} | ||||
|
r473 | // Use temporary qreals for accurancy (we might get some compiler warnings... :) | ||
|
r366 | int categoryCount = mSeries->categoryCount(); | ||
int setCount = mSeries->barsetCount(); | ||||
|
r56 | |||
|
r99 | qreal tW = mWidth; | ||
qreal tH = mHeight; | ||||
|
r288 | qreal tM = mSeries->max(); | ||
|
r99 | qreal scale = (tH/tM); | ||
|
r473 | qreal tC = categoryCount + 1; | ||
|
r505 | qreal categoryWidth = tW/tC; | ||
mBarWidth = categoryWidth / (setCount+1); | ||||
|
r99 | |||
|
r71 | int itemIndex(0); | ||
|
r263 | for (int category=0; category < categoryCount; category++) { | ||
|
r505 | qreal xPos = categoryWidth * category + categoryWidth /2; | ||
|
r113 | qreal yPos = mHeight; | ||
|
r183 | for (int set = 0; set < setCount; set++) { | ||
|
r288 | qreal barHeight = mSeries->valueAt(set,category) * scale; | ||
|
r256 | Bar* bar = mBars.at(itemIndex); | ||
|
r99 | |||
// TODO: width settable per bar? | ||||
|
r473 | bar->resize(mBarWidth, barHeight); | ||
|
r357 | bar->setBrush(mSeries->barsetAt(set)->brush()); | ||
|
r288 | bar->setPos(xPos, yPos-barHeight); | ||
|
r99 | itemIndex++; | ||
|
r473 | xPos += mBarWidth; | ||
|
r99 | } | ||
} | ||||
|
r505 | // Position separators | ||
qreal xPos = categoryWidth + categoryWidth/2 - mBarWidth /2; | ||||
for (int s=0; s < mSeparators.count(); s++) { | ||||
Separator* sep = mSeparators.at(s); | ||||
sep->setPos(xPos,0); | ||||
sep->setSize(QSizeF(1,mHeight)); | ||||
|
r512 | sep->setColor(QColor(255,0,0,255)); // TODO: color for separations from theme | ||
|
r505 | xPos += categoryWidth; | ||
} | ||||
|
r263 | // Position floating values | ||
itemIndex = 0; | ||||
|
r366 | for (int category=0; category < mSeries->categoryCount(); category++) { | ||
|
r505 | qreal xPos = categoryWidth * category + categoryWidth/2 + mBarWidth/2; | ||
|
r263 | qreal yPos = mHeight; | ||
|
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++; | ||||
|
r473 | xPos += mBarWidth; | ||
|
r263 | } | ||
} | ||||
|
r56 | } | ||
|
r381 | #include "moc_barpresenter_p.cpp" | ||
|
r288 | |||
|
r56 | QTCOMMERCIALCHART_END_NAMESPACE | ||