barpresenter.cpp
97 lines
| 3.0 KiB
| text/x-c
|
CppLexer
|
r381 | #include "barpresenter_p.h" | ||
|
r118 | #include "bar_p.h" | ||
|
r114 | #include "barlabel_p.h" | ||
|
r263 | #include "barvalue_p.h" | ||
|
r214 | #include "qbarset.h" | ||
|
r56 | #include <QDebug> | ||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
|
r338 | BarPresenter::BarPresenter(QBarSeries *series, QGraphicsItem *parent) : | ||
|
r288 | BarPresenterBase(series, parent) | ||
|
r56 | { | ||
|
r387 | mBarDefaultWidth = 5; | ||
|
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; | ||
} | ||||
|
r101 | // TODO: better way to auto-layout? | ||
|
r99 | // Use reals 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); | ||
|
r263 | qreal tC = categoryCount+1; | ||
|
r183 | qreal xStepPerSet = (tW/tC); | ||
|
r99 | |||
|
r82 | // Scaling. | ||
|
r71 | int itemIndex(0); | ||
|
r256 | int labelIndex(0); | ||
|
r114 | |||
|
r263 | for (int category=0; category < categoryCount; category++) { | ||
qreal xPos = xStepPerSet * category + ((tW + mBarDefaultWidth*setCount)/(categoryCount*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? | ||||
bar->resize(mBarDefaultWidth, barHeight); | ||||
|
r357 | bar->setBrush(mSeries->barsetAt(set)->brush()); | ||
|
r288 | bar->setPos(xPos, yPos-barHeight); | ||
|
r99 | itemIndex++; | ||
xPos += mBarDefaultWidth; | ||||
} | ||||
|
r114 | |||
// TODO: Layout for labels, remove magic number | ||||
|
r263 | xPos = xStepPerSet * category + ((tW + mBarDefaultWidth*setCount)/(categoryCount*2)); | ||
|
r256 | BarLabel* label = mLabels.at(labelIndex); | ||
|
r114 | label->setPos(xPos, mHeight + 20); | ||
labelIndex++; | ||||
|
r99 | } | ||
|
r263 | // Position floating values | ||
itemIndex = 0; | ||||
|
r366 | for (int category=0; category < mSeries->categoryCount(); category++) { | ||
|
r263 | qreal xPos = xStepPerSet * category + ((tW + mBarDefaultWidth*setCount)/(categoryCount*2)); | ||
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); | ||
// 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++; | ||||
xPos += mBarDefaultWidth; | ||||
} | ||||
} | ||||
|
r56 | mLayoutDirty = true; | ||
} | ||||
|
r381 | #include "moc_barpresenter_p.cpp" | ||
|
r288 | |||
|
r56 | QTCOMMERCIALCHART_END_NAMESPACE | ||