barpresenterbase.cpp
115 lines
| 3.1 KiB
| text/x-c
|
CppLexer
|
r216 | #include "barpresenterbase.h" | ||
|
r126 | #include "bar_p.h" | ||
#include "barlabel_p.h" | ||||
#include "separator_p.h" | ||||
|
r240 | #include "qbarset.h" | ||
|
r126 | #include <QDebug> | ||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
|
r216 | BarPresenterBase::BarPresenterBase(BarChartModel& model, QGraphicsItem *parent) | ||
|
r126 | : ChartItem(parent) | ||
,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready | ||||
,mLayoutSet(false) | ||||
,mLayoutDirty(true) | ||||
,mSeparatorsVisible(true) | ||||
|
r172 | ,mModel(model) | ||
|
r126 | { | ||
|
r167 | dataChanged(); | ||
|
r126 | } | ||
|
r216 | void BarPresenterBase::setSeparatorsVisible(bool visible) | ||
|
r126 | { | ||
mSeparatorsVisible = visible; | ||||
} | ||||
|
r216 | void BarPresenterBase::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | ||
|
r126 | { | ||
|
r165 | // qDebug() << "BarGroupBase::paint" << childItems().count(); | ||
|
r126 | if (!mLayoutSet) { | ||
qDebug() << "BarGroupBase::paint called without layout set. Aborting."; | ||||
return; | ||||
} | ||||
|
r161 | // if (mLayoutDirty) { | ||
|
r126 | // Layout or data has changed. Need to redraw. | ||
foreach(QGraphicsItem* i, childItems()) { | ||||
i->paint(painter,option,widget); | ||||
} | ||||
|
r161 | // } | ||
|
r126 | } | ||
|
r216 | QRectF BarPresenterBase::boundingRect() const | ||
|
r126 | { | ||
return QRectF(0,0,mWidth,mHeight); | ||||
} | ||||
|
r165 | |||
|
r216 | void BarPresenterBase::setBarWidth( int w ) | ||
|
r126 | { | ||
mBarDefaultWidth = w; | ||||
} | ||||
|
r183 | |||
|
r216 | void BarPresenterBase::dataChanged() | ||
|
r126 | { | ||
|
r172 | // TODO: performance optimizations. Do we really need to delete and create items every time data is changed or can we reuse them? | ||
|
r126 | |||
// Delete old bars | ||||
foreach (QGraphicsItem* item, childItems()) { | ||||
delete item; | ||||
} | ||||
// Create new graphic items for bars | ||||
|
r239 | for (int s=0; s<mModel.countSets(); s++) { | ||
QBarSet *set = mModel.nextSet(0==s); | ||||
for (int c=0; c<mModel.countCategories(); c++) { | ||||
Bar *bar = new Bar(this); | ||||
childItems().append(bar); | ||||
|
r247 | //connect(bar,SIGNAL(clicked()),set,SLOT(barClicked())); | ||
|
r239 | } | ||
|
r126 | } | ||
|
r173 | int count = mModel.countCategories(); | ||
|
r126 | for (int i=0; i<count; i++) { | ||
BarLabel* label = new BarLabel(this); | ||||
|
r172 | label->set(mModel.label(i)); | ||
|
r126 | childItems().append(label); | ||
} | ||||
|
r173 | count = mModel.countCategories() - 1; // There is one less separator than columns | ||
|
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; | ||||
} | ||||
|
r139 | //handlers | ||
|
r216 | void BarPresenterBase::handleModelChanged(int index) | ||
|
r139 | { | ||
|
r173 | // qDebug() << "BarGroupBase::handleModelChanged" << index; | ||
|
r161 | dataChanged(); | ||
|
r139 | } | ||
|
r216 | void BarPresenterBase::handleDomainChanged(const Domain& domain) | ||
|
r139 | { | ||
|
r165 | // qDebug() << "BarGroupBase::handleDomainChanged"; | ||
|
r167 | // TODO: Figure out the use case for this. | ||
// Affects the size of visible item, so layout is changed. | ||||
// layoutChanged(); | ||||
|
r139 | } | ||
|
r216 | void BarPresenterBase::handleGeometryChanged(const QRectF& rect) | ||
|
r139 | { | ||
mWidth = rect.width(); | ||||
mHeight = rect.height(); | ||||
layoutChanged(); | ||||
mLayoutSet = true; | ||||
setPos(rect.topLeft()); | ||||
} | ||||
|
r216 | #include "moc_barpresenterbase.cpp" | ||
|
r139 | |||
|
r126 | QTCOMMERCIALCHART_END_NAMESPACE | ||