barpresenterbase.cpp
150 lines
| 4.3 KiB
| text/x-c
|
CppLexer
|
r216 | #include "barpresenterbase.h" | ||
|
r126 | #include "bar_p.h" | ||
|
r263 | #include "barvalue_p.h" | ||
|
r126 | #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) | ||||
|
r263 | ,mSeparatorsVisible(false) | ||
|
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 | { | ||
if (!mLayoutSet) { | ||||
|
r256 | qDebug() << "BarPresenterBase::paint called without layout set. Aborting."; | ||
|
r126 | 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? | ||
|
r256 | qDebug() << "datachanged"; | ||
|
r126 | // Delete old bars | ||
foreach (QGraphicsItem* item, childItems()) { | ||||
delete item; | ||||
} | ||||
|
r256 | mBars.clear(); | ||
mLabels.clear(); | ||||
mSeparators.clear(); | ||||
|
r263 | mFloatingValues.clear(); | ||
|
r256 | |||
|
r126 | // Create new graphic items for bars | ||
|
r273 | for (int c=0; c<mModel.countCategories(); c++) { | ||
for (int s=0; s<mModel.countSets(); s++) { | ||||
QBarSet *set = mModel.setAt(s); | ||||
|
r239 | Bar *bar = new Bar(this); | ||
childItems().append(bar); | ||||
|
r256 | mBars.append(bar); | ||
|
r273 | connect(bar,SIGNAL(clicked()),set,SLOT(barClicked())); | ||
|
r280 | // TODO: should the event be passed to set or not? | ||
//connect(bar,SIGNAL(hoverEntered(QGraphicsSceneHoverEvent* event)),set,SLOT(barHoverEntered(QGraphicsSceneHoverEvent* event))); | ||||
//connect(bar,SIGNAL(hoverLeaved(QGraphicsSceneHoverEvent* event)),set,SLOT(barHoverLeaved(QGraphicsSceneHoverEvent *event))); | ||||
|
r239 | } | ||
|
r126 | } | ||
|
r263 | // Create labels | ||
|
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); | ||
|
r256 | mLabels.append(label); | ||
|
r126 | } | ||
|
r263 | // Create separators | ||
|
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); | ||||
|
r256 | mSeparators.append(sep); | ||
|
r126 | } | ||
|
r263 | // Create floating values | ||
|
r273 | for (int category=0; category<mModel.countCategories(); category++) { | ||
for (int s=0; s<mModel.countSets(); s++) { | ||||
QBarSet *set = mModel.setAt(s); | ||||
|
r267 | BarValue *value = new BarValue(*set, this); | ||
|
r263 | childItems().append(value); | ||
mFloatingValues.append(value); | ||||
|
r276 | connect(set,SIGNAL(toggleFloatingValues()),value,SLOT(toggleVisible())); | ||
|
r263 | } | ||
} | ||||
|
r126 | // TODO: if (autolayout) { layoutChanged() } or something | ||
mLayoutDirty = true; | ||||
} | ||||
|
r139 | //handlers | ||
|
r216 | void BarPresenterBase::handleModelChanged(int index) | ||
|
r139 | { | ||
|
r256 | // qDebug() << "BarPresenterBase::handleModelChanged" << index; | ||
|
r161 | dataChanged(); | ||
|
r139 | } | ||
|
r216 | void BarPresenterBase::handleDomainChanged(const Domain& domain) | ||
|
r139 | { | ||
|
r256 | // qDebug() << "BarPresenterBase::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()); | ||||
} | ||||
|
r280 | |||
void BarPresenterBase::barHoverEntered(QGraphicsSceneHoverEvent *event) | ||||
{ | ||||
//TODO: show tooltip (name of series, where bar belongs...) | ||||
} | ||||
void BarPresenterBase::barHoverLeaved(QGraphicsSceneHoverEvent *event) | ||||
{ | ||||
//TODO: hide tooltip (name of series, where bar belongs...) | ||||
} | ||||
|
r216 | #include "moc_barpresenterbase.cpp" | ||
|
r139 | |||
|
r126 | QTCOMMERCIALCHART_END_NAMESPACE | ||