bargroup.cpp
157 lines
| 4.0 KiB
| text/x-c
|
CppLexer
sauimone
|
r56 | #include "bargroup.h" | ||
#include "bar.h" | ||||
sauimone
|
r114 | #include "barlabel_p.h" | ||
sauimone
|
r56 | #include <QDebug> | ||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
sauimone
|
r78 | BarGroup::BarGroup(BarChartSeries& series, QGraphicsItem *parent) : | ||
Tero Ahola
|
r104 | ChartItem(parent) | ||
,mSeries(series) | ||||
,mLayoutSet(false) | ||||
,mLayoutDirty(true) | ||||
,mBarDefaultWidth(10) | ||||
sauimone
|
r56 | { | ||
dataChanged(); | ||||
} | ||||
Tero Ahola
|
r104 | void BarGroup::setSize(const QSize& size) | ||
sauimone
|
r78 | { | ||
qDebug() << "BarGroup::setSize"; | ||||
sauimone
|
r90 | mWidth = size.width(); | ||
mHeight = size.height(); | ||||
layoutChanged(); | ||||
mLayoutSet = true; | ||||
sauimone
|
r78 | } | ||
void BarGroup::setPlotDomain(const PlotDomain& data) | ||||
{ | ||||
qDebug() << "BarGroup::setPlotDomain"; | ||||
sauimone
|
r82 | // TODO: | ||
sauimone
|
r101 | mPlotDomain = data; | ||
sauimone
|
r78 | } | ||
sauimone
|
r56 | void BarGroup::setBarWidth( int w ) | ||
{ | ||||
mBarDefaultWidth = w; | ||||
} | ||||
sauimone
|
r82 | int BarGroup::addColor( QColor color ) | ||
sauimone
|
r56 | { | ||
sauimone
|
r82 | int colorIndex = mColors.count(); | ||
mColors.append(color); | ||||
return colorIndex; | ||||
sauimone
|
r56 | } | ||
sauimone
|
r92 | void BarGroup::resetColors() | ||
{ | ||||
mColors.clear(); | ||||
} | ||||
sauimone
|
r56 | void BarGroup::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | ||
{ | ||||
if (!mLayoutSet) { | ||||
qDebug() << "QBarChart::paint called without layout set. Aborting."; | ||||
return; | ||||
} | ||||
if (mLayoutDirty) { | ||||
// Layout or data has changed. Need to redraw. | ||||
foreach(QGraphicsItem* i, childItems()) { | ||||
i->paint(painter,option,widget); | ||||
sauimone
|
r114 | mLayoutDirty = false; | ||
sauimone
|
r56 | } | ||
} | ||||
} | ||||
QRectF BarGroup::boundingRect() const | ||||
{ | ||||
return QRectF(0,0,mWidth,mHeight); | ||||
} | ||||
void BarGroup::dataChanged() | ||||
{ | ||||
qDebug() << "QBarChart::dataChanged mSeries"; | ||||
// Find out maximum and minimum of all series | ||||
mMax = mSeries.max(); | ||||
mMin = mSeries.min(); | ||||
// Delete old bars | ||||
// Is this correct way to delete childItems? | ||||
foreach (QGraphicsItem* item, childItems()) { | ||||
delete item; | ||||
} | ||||
// Create new graphic items for bars | ||||
sauimone
|
r71 | int totalItems = mSeries.countTotalItems(); | ||
for (int i=0; i<totalItems; i++) { | ||||
sauimone
|
r56 | Bar *bar = new Bar(this); | ||
childItems().append(bar); | ||||
} | ||||
sauimone
|
r114 | // TODO: labels from series. This creates just some example labels | ||
int count = mSeries.countColumns(); | ||||
for (int i=0; i<count; i++) { | ||||
BarLabel* label = new BarLabel(this); | ||||
QString text("Label " + QString::number(i)); | ||||
label->set(text); | ||||
childItems().append(label); | ||||
} | ||||
// TODO: if (autolayout) { layoutChanged() } or something | ||||
sauimone
|
r56 | mLayoutDirty = true; | ||
} | ||||
void BarGroup::layoutChanged() | ||||
{ | ||||
// Scale bars to new layout | ||||
// Layout for bars: | ||||
sauimone
|
r99 | if (mSeries.countRows() <= 0) { | ||
sauimone
|
r56 | // Nothing to do. | ||
return; | ||||
} | ||||
sauimone
|
r101 | // TODO: better way to auto-layout? | ||
sauimone
|
r99 | // Use reals for accurancy (we might get some compiler warnings... :) | ||
int columnCount = mSeries.countColumns(); | ||||
int rowCount = mSeries.countRows(); | ||||
sauimone
|
r56 | |||
sauimone
|
r99 | qreal tW = mWidth; | ||
qreal tH = mHeight; | ||||
qreal tM = mMax; | ||||
qreal scale = (tH/tM); | ||||
qreal tC = columnCount+1; | ||||
qreal xStepPerSeries = (tW/tC); | ||||
sauimone
|
r82 | // Scaling. | ||
sauimone
|
r71 | int itemIndex(0); | ||
sauimone
|
r114 | int labelIndex = mSeries.countColumns() * mSeries.countRows(); | ||
sauimone
|
r99 | for (int column=0; column < columnCount; column++) { | ||
qreal xPos = xStepPerSeries * column + ((tW + mBarDefaultWidth*rowCount)/(columnCount*2)); | ||||
sauimone
|
r113 | qreal yPos = mHeight; | ||
sauimone
|
r99 | for (int row = 0; row < rowCount; row++) { | ||
qreal barHeight = mSeries.valueAt(row, column) * scale; | ||||
Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex)); | ||||
// TODO: width settable per bar? | ||||
bar->resize(mBarDefaultWidth, barHeight); | ||||
bar->setColor(mColors.at(row)); | ||||
sauimone
|
r113 | bar->setPos(xPos, yPos-barHeight); // item*posStep+startPos + series * mBarDefaultWidth, mHeight); | ||
sauimone
|
r99 | itemIndex++; | ||
xPos += mBarDefaultWidth; | ||||
} | ||||
sauimone
|
r114 | |||
// TODO: Layout for labels, remove magic number | ||||
xPos = xStepPerSeries * column + ((tW + mBarDefaultWidth*rowCount)/(columnCount*2)); | ||||
BarLabel* label = reinterpret_cast<BarLabel*> (childItems().at(labelIndex)); | ||||
label->setPos(xPos, mHeight + 20); | ||||
labelIndex++; | ||||
sauimone
|
r99 | } | ||
sauimone
|
r56 | mLayoutDirty = true; | ||
} | ||||
QTCOMMERCIALCHART_END_NAMESPACE | ||||