bargroupbase.cpp
131 lines
| 3.4 KiB
| text/x-c
|
CppLexer
sauimone
|
r126 | #include "bargroupbase.h" | ||
#include "bar_p.h" | ||||
#include "barlabel_p.h" | ||||
#include "separator_p.h" | ||||
#include "barchartseriesbase.h" | ||||
#include <QDebug> | ||||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
BarGroupBase::BarGroupBase(BarChartSeriesBase& series, QGraphicsItem *parent) | ||||
: ChartItem(parent) | ||||
,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready | ||||
,mLayoutSet(false) | ||||
,mLayoutDirty(true) | ||||
,mSeparatorsVisible(true) | ||||
sauimone
|
r161 | ,mModel(series.model()) | ||
sauimone
|
r167 | ,mSeries(series) | ||
sauimone
|
r126 | { | ||
sauimone
|
r167 | dataChanged(); | ||
sauimone
|
r126 | } | ||
void BarGroupBase::setSeparatorsVisible(bool visible) | ||||
{ | ||||
mSeparatorsVisible = visible; | ||||
} | ||||
void BarGroupBase::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | ||||
{ | ||||
sauimone
|
r165 | // qDebug() << "BarGroupBase::paint" << childItems().count(); | ||
sauimone
|
r126 | if (!mLayoutSet) { | ||
qDebug() << "BarGroupBase::paint called without layout set. Aborting."; | ||||
return; | ||||
} | ||||
sauimone
|
r161 | // if (mLayoutDirty) { | ||
sauimone
|
r126 | // Layout or data has changed. Need to redraw. | ||
foreach(QGraphicsItem* i, childItems()) { | ||||
i->paint(painter,option,widget); | ||||
} | ||||
sauimone
|
r161 | // } | ||
sauimone
|
r126 | } | ||
QRectF BarGroupBase::boundingRect() const | ||||
{ | ||||
return QRectF(0,0,mWidth,mHeight); | ||||
} | ||||
sauimone
|
r165 | |||
sauimone
|
r126 | void BarGroupBase::setBarWidth( int w ) | ||
{ | ||||
mBarDefaultWidth = w; | ||||
} | ||||
int BarGroupBase::addColor( QColor color ) | ||||
{ | ||||
sauimone
|
r165 | // qDebug() << "BarGroupBase::addColor"; | ||
sauimone
|
r126 | int colorIndex = mColors.count(); | ||
mColors.append(color); | ||||
return colorIndex; | ||||
} | ||||
void BarGroupBase::resetColors() | ||||
{ | ||||
sauimone
|
r165 | // qDebug() << "BarGroupBase::resetColors"; | ||
sauimone
|
r126 | mColors.clear(); | ||
} | ||||
void BarGroupBase::dataChanged() | ||||
{ | ||||
sauimone
|
r165 | // qDebug() << "BarGroupBase::dataChanged"; | ||
sauimone
|
r126 | |||
// Delete old bars | ||||
foreach (QGraphicsItem* item, childItems()) { | ||||
delete item; | ||||
} | ||||
// Create new graphic items for bars | ||||
sauimone
|
r159 | int totalItems = mModel.countTotalItems(); // mSeries.countTotalItems(); | ||
sauimone
|
r126 | for (int i=0; i<totalItems; i++) { | ||
Bar *bar = new Bar(this); | ||||
childItems().append(bar); | ||||
} | ||||
// TODO: labels from series. This creates just some example labels | ||||
sauimone
|
r161 | int count = mModel.countColumns(); // mSeries.countColumns(); | ||
sauimone
|
r126 | for (int i=0; i<count; i++) { | ||
BarLabel* label = new BarLabel(this); | ||||
sauimone
|
r167 | // QString text("Label " + QString::number(i)); | ||
label->set(mSeries.label(i)); | ||||
sauimone
|
r126 | childItems().append(label); | ||
} | ||||
sauimone
|
r161 | count = mModel.countColumns() - 1; // mSeries.countColumns() - 1; // There is one less separator than columns | ||
sauimone
|
r126 | for (int i=0; i<count; i++) { | ||
Separator* sep = new Separator(this); | ||||
sep->setColor(QColor(255,0,0,255)); // TODO: color for separations from theme | ||||
childItems().append(sep); | ||||
} | ||||
// TODO: if (autolayout) { layoutChanged() } or something | ||||
mLayoutDirty = true; | ||||
} | ||||
Michal Klocek
|
r139 | //handlers | ||
void BarGroupBase::handleModelChanged(int index) | ||||
{ | ||||
sauimone
|
r167 | qDebug() << "BarGroupBase::handleModelChanged" << index; | ||
sauimone
|
r161 | dataChanged(); | ||
Michal Klocek
|
r139 | } | ||
void BarGroupBase::handleDomainChanged(const Domain& domain) | ||||
{ | ||||
sauimone
|
r165 | // qDebug() << "BarGroupBase::handleDomainChanged"; | ||
sauimone
|
r167 | |||
// TODO: Figure out the use case for this. | ||||
// Affects the size of visible item, so layout is changed. | ||||
// layoutChanged(); | ||||
Michal Klocek
|
r139 | } | ||
void BarGroupBase::handleGeometryChanged(const QRectF& rect) | ||||
{ | ||||
sauimone
|
r165 | // qDebug() << "BarGroupBase::handleGeometryChanged"; | ||
Michal Klocek
|
r139 | mWidth = rect.width(); | ||
mHeight = rect.height(); | ||||
layoutChanged(); | ||||
mLayoutSet = true; | ||||
setPos(rect.topLeft()); | ||||
} | ||||
#include "moc_bargroupbase.cpp" | ||||
sauimone
|
r126 | QTCOMMERCIALCHART_END_NAMESPACE | ||