barpresenter.cpp
95 lines
| 3.0 KiB
| text/x-c
|
CppLexer
sauimone
|
r216 | #include "barpresenter.h" | ||
sauimone
|
r118 | #include "bar_p.h" | ||
sauimone
|
r114 | #include "barlabel_p.h" | ||
sauimone
|
r263 | #include "barvalue_p.h" | ||
sauimone
|
r214 | #include "qbarset.h" | ||
sauimone
|
r56 | #include <QDebug> | ||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
sauimone
|
r216 | BarPresenter::BarPresenter(BarChartModel& model, QGraphicsItem *parent) : | ||
BarPresenterBase(model,parent) | ||||
sauimone
|
r56 | { | ||
sauimone
|
r263 | mBarDefaultWidth = 15; | ||
sauimone
|
r56 | } | ||
sauimone
|
r216 | void BarPresenter::layoutChanged() | ||
sauimone
|
r56 | { | ||
// Scale bars to new layout | ||||
// Layout for bars: | ||||
sauimone
|
r171 | if (mModel.countSets() <= 0) { | ||
sauimone
|
r173 | qDebug() << "No sets in model!"; | ||
sauimone
|
r159 | return; | ||
} | ||||
sauimone
|
r56 | |||
sauimone
|
r165 | if (childItems().count() == 0) { | ||
sauimone
|
r256 | qDebug() << "WARNING: BarPresenter::layoutChanged called before graphics items are created!"; | ||
sauimone
|
r165 | return; | ||
} | ||||
sauimone
|
r101 | // TODO: better way to auto-layout? | ||
sauimone
|
r99 | // Use reals for accurancy (we might get some compiler warnings... :) | ||
sauimone
|
r263 | int categoryCount = mModel.countCategories(); | ||
sauimone
|
r183 | int setCount = mModel.countSets(); | ||
sauimone
|
r56 | |||
sauimone
|
r99 | qreal tW = mWidth; | ||
qreal tH = mHeight; | ||||
sauimone
|
r167 | qreal tM = mModel.max(); | ||
sauimone
|
r99 | qreal scale = (tH/tM); | ||
sauimone
|
r263 | qreal tC = categoryCount+1; | ||
sauimone
|
r183 | qreal xStepPerSet = (tW/tC); | ||
sauimone
|
r99 | |||
sauimone
|
r82 | // Scaling. | ||
sauimone
|
r71 | int itemIndex(0); | ||
sauimone
|
r256 | int labelIndex(0); | ||
sauimone
|
r114 | |||
sauimone
|
r263 | for (int category=0; category < categoryCount; category++) { | ||
qreal xPos = xStepPerSet * category + ((tW + mBarDefaultWidth*setCount)/(categoryCount*2)); | ||||
sauimone
|
r113 | qreal yPos = mHeight; | ||
sauimone
|
r183 | for (int set = 0; set < setCount; set++) { | ||
sauimone
|
r263 | qreal barHeight = mModel.valueAt(set, category) * scale; | ||
sauimone
|
r256 | Bar* bar = mBars.at(itemIndex); | ||
sauimone
|
r99 | |||
// TODO: width settable per bar? | ||||
bar->resize(mBarDefaultWidth, barHeight); | ||||
sauimone
|
r273 | bar->setBrush(mModel.setAt(set)->brush()); | ||
sauimone
|
r183 | bar->setPos(xPos, yPos-barHeight); // item*posStep+startPos + set * mBarDefaultWidth, mHeight); | ||
sauimone
|
r99 | itemIndex++; | ||
xPos += mBarDefaultWidth; | ||||
} | ||||
sauimone
|
r114 | |||
// TODO: Layout for labels, remove magic number | ||||
sauimone
|
r263 | xPos = xStepPerSet * category + ((tW + mBarDefaultWidth*setCount)/(categoryCount*2)); | ||
sauimone
|
r256 | BarLabel* label = mLabels.at(labelIndex); | ||
sauimone
|
r114 | label->setPos(xPos, mHeight + 20); | ||
labelIndex++; | ||||
sauimone
|
r99 | } | ||
sauimone
|
r263 | // Position floating values | ||
itemIndex = 0; | ||||
for (int category=0; category < mModel.countCategories(); category++) { | ||||
qreal xPos = xStepPerSet * category + ((tW + mBarDefaultWidth*setCount)/(categoryCount*2)); | ||||
qreal yPos = mHeight; | ||||
for (int set=0; set < mModel.countSets(); set++) { | ||||
qreal barHeight = mModel.valueAt(set,category) * scale; | ||||
BarValue* value = mFloatingValues.at(itemIndex); | ||||
// TODO: remove hard coding, apply layout | ||||
value->resize(100,50); | ||||
sauimone
|
r276 | value->setPos(xPos, yPos-barHeight/2); | ||
sauimone
|
r263 | value->setPen(QPen(QColor(255,255,255,255))); | ||
sauimone
|
r276 | if (mModel.valueAt(set,category) != 0) { | ||
value->setValueString(QString::number(mModel.valueAt(set,category))); | ||||
} else { | ||||
value->setValueString(QString("")); | ||||
} | ||||
sauimone
|
r263 | |||
itemIndex++; | ||||
xPos += mBarDefaultWidth; | ||||
} | ||||
} | ||||
sauimone
|
r56 | mLayoutDirty = true; | ||
} | ||||
QTCOMMERCIALCHART_END_NAMESPACE | ||||