bargroup.cpp
73 lines
| 2.2 KiB
| text/x-c
|
CppLexer
sauimone
|
r56 | #include "bargroup.h" | ||
sauimone
|
r118 | #include "bar_p.h" | ||
sauimone
|
r114 | #include "barlabel_p.h" | ||
sauimone
|
r56 | #include <QDebug> | ||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
sauimone
|
r172 | BarGroup::BarGroup(BarChartModel& model, QGraphicsItem *parent) : | ||
BarGroupBase(model,parent) | ||||
sauimone
|
r56 | { | ||
sauimone
|
r173 | mBarDefaultWidth = 5; | ||
sauimone
|
r56 | } | ||
void BarGroup::layoutChanged() | ||||
{ | ||||
sauimone
|
r169 | // qDebug() << "BarGroup::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) { | ||
qDebug() << "WARNING: BarGroup::layoutChanged called before graphics items are created!"; | ||||
return; | ||||
} | ||||
sauimone
|
r101 | // TODO: better way to auto-layout? | ||
sauimone
|
r99 | // Use reals for accurancy (we might get some compiler warnings... :) | ||
sauimone
|
r171 | int itemCount = 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
|
r159 | qreal tC = itemCount+1; | ||
sauimone
|
r183 | qreal xStepPerSet = (tW/tC); | ||
sauimone
|
r99 | |||
sauimone
|
r82 | // Scaling. | ||
sauimone
|
r71 | int itemIndex(0); | ||
sauimone
|
r183 | int labelIndex = itemCount * setCount; | ||
sauimone
|
r114 | |||
sauimone
|
r159 | for (int item=0; item < itemCount; item++) { | ||
sauimone
|
r183 | qreal xPos = xStepPerSet * item + ((tW + mBarDefaultWidth*setCount)/(itemCount*2)); | ||
sauimone
|
r113 | qreal yPos = mHeight; | ||
sauimone
|
r183 | for (int set = 0; set < setCount; set++) { | ||
qreal barHeight = mModel.valueAt(set, item) * scale; | ||||
sauimone
|
r99 | Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex)); | ||
// TODO: width settable per bar? | ||||
bar->resize(mBarDefaultWidth, barHeight); | ||||
sauimone
|
r183 | bar->setBrush(mBrushes.at(set)); | ||
// bar->setPen(mModel.barSet(set).pen()); | ||||
// bar->setColor(mColors.at(set)); | ||||
// bar->setPen(); | ||||
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
|
r183 | xPos = xStepPerSet * item + ((tW + mBarDefaultWidth*setCount)/(itemCount*2)); | ||
sauimone
|
r114 | BarLabel* label = reinterpret_cast<BarLabel*> (childItems().at(labelIndex)); | ||
label->setPos(xPos, mHeight + 20); | ||||
labelIndex++; | ||||
sauimone
|
r99 | } | ||
sauimone
|
r56 | mLayoutDirty = true; | ||
} | ||||
QTCOMMERCIALCHART_END_NAMESPACE | ||||