barpresenterbase.cpp
138 lines
| 3.9 KiB
| text/x-c
|
CppLexer
sauimone
|
r216 | #include "barpresenterbase.h" | ||
sauimone
|
r126 | #include "bar_p.h" | ||
sauimone
|
r263 | #include "barvalue_p.h" | ||
sauimone
|
r126 | #include "barlabel_p.h" | ||
#include "separator_p.h" | ||||
sauimone
|
r240 | #include "qbarset.h" | ||
sauimone
|
r126 | #include <QDebug> | ||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
sauimone
|
r216 | BarPresenterBase::BarPresenterBase(BarChartModel& model, QGraphicsItem *parent) | ||
sauimone
|
r126 | : ChartItem(parent) | ||
,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready | ||||
,mLayoutSet(false) | ||||
,mLayoutDirty(true) | ||||
sauimone
|
r263 | ,mSeparatorsVisible(false) | ||
sauimone
|
r172 | ,mModel(model) | ||
sauimone
|
r126 | { | ||
sauimone
|
r167 | dataChanged(); | ||
sauimone
|
r126 | } | ||
sauimone
|
r216 | void BarPresenterBase::setSeparatorsVisible(bool visible) | ||
sauimone
|
r126 | { | ||
mSeparatorsVisible = visible; | ||||
} | ||||
sauimone
|
r216 | void BarPresenterBase::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | ||
sauimone
|
r126 | { | ||
if (!mLayoutSet) { | ||||
sauimone
|
r256 | qDebug() << "BarPresenterBase::paint called without layout set. Aborting."; | ||
sauimone
|
r126 | 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 | } | ||
sauimone
|
r216 | QRectF BarPresenterBase::boundingRect() const | ||
sauimone
|
r126 | { | ||
return QRectF(0,0,mWidth,mHeight); | ||||
} | ||||
sauimone
|
r165 | |||
sauimone
|
r216 | void BarPresenterBase::setBarWidth( int w ) | ||
sauimone
|
r126 | { | ||
mBarDefaultWidth = w; | ||||
} | ||||
sauimone
|
r183 | |||
sauimone
|
r216 | void BarPresenterBase::dataChanged() | ||
sauimone
|
r126 | { | ||
sauimone
|
r172 | // TODO: performance optimizations. Do we really need to delete and create items every time data is changed or can we reuse them? | ||
sauimone
|
r256 | qDebug() << "datachanged"; | ||
sauimone
|
r126 | // Delete old bars | ||
foreach (QGraphicsItem* item, childItems()) { | ||||
delete item; | ||||
} | ||||
sauimone
|
r256 | mBars.clear(); | ||
mLabels.clear(); | ||||
mSeparators.clear(); | ||||
sauimone
|
r263 | mFloatingValues.clear(); | ||
sauimone
|
r256 | |||
sauimone
|
r126 | // Create new graphic items for bars | ||
sauimone
|
r273 | for (int c=0; c<mModel.countCategories(); c++) { | ||
for (int s=0; s<mModel.countSets(); s++) { | ||||
QBarSet *set = mModel.setAt(s); | ||||
sauimone
|
r239 | Bar *bar = new Bar(this); | ||
childItems().append(bar); | ||||
sauimone
|
r256 | mBars.append(bar); | ||
sauimone
|
r273 | connect(bar,SIGNAL(clicked()),set,SLOT(barClicked())); | ||
sauimone
|
r276 | connect(bar,SIGNAL(hoverEntered()),set,SLOT(barHoverEntered())); | ||
connect(bar,SIGNAL(hoverLeaved()),set,SLOT(barHoverLeaved())); | ||||
sauimone
|
r239 | } | ||
sauimone
|
r126 | } | ||
sauimone
|
r263 | // Create labels | ||
sauimone
|
r173 | int count = mModel.countCategories(); | ||
sauimone
|
r126 | for (int i=0; i<count; i++) { | ||
BarLabel* label = new BarLabel(this); | ||||
sauimone
|
r172 | label->set(mModel.label(i)); | ||
sauimone
|
r126 | childItems().append(label); | ||
sauimone
|
r256 | mLabels.append(label); | ||
sauimone
|
r126 | } | ||
sauimone
|
r263 | // Create separators | ||
sauimone
|
r173 | count = mModel.countCategories() - 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); | ||||
sauimone
|
r256 | mSeparators.append(sep); | ||
sauimone
|
r126 | } | ||
sauimone
|
r263 | // Create floating values | ||
sauimone
|
r273 | for (int category=0; category<mModel.countCategories(); category++) { | ||
for (int s=0; s<mModel.countSets(); s++) { | ||||
QBarSet *set = mModel.setAt(s); | ||||
sauimone
|
r267 | BarValue *value = new BarValue(*set, this); | ||
sauimone
|
r263 | childItems().append(value); | ||
mFloatingValues.append(value); | ||||
sauimone
|
r276 | connect(set,SIGNAL(toggleFloatingValues()),value,SLOT(toggleVisible())); | ||
sauimone
|
r263 | } | ||
} | ||||
sauimone
|
r126 | // TODO: if (autolayout) { layoutChanged() } or something | ||
mLayoutDirty = true; | ||||
} | ||||
Michal Klocek
|
r139 | //handlers | ||
sauimone
|
r216 | void BarPresenterBase::handleModelChanged(int index) | ||
Michal Klocek
|
r139 | { | ||
sauimone
|
r256 | // qDebug() << "BarPresenterBase::handleModelChanged" << index; | ||
sauimone
|
r161 | dataChanged(); | ||
Michal Klocek
|
r139 | } | ||
sauimone
|
r216 | void BarPresenterBase::handleDomainChanged(const Domain& domain) | ||
Michal Klocek
|
r139 | { | ||
sauimone
|
r256 | // qDebug() << "BarPresenterBase::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 | } | ||
sauimone
|
r216 | void BarPresenterBase::handleGeometryChanged(const QRectF& rect) | ||
Michal Klocek
|
r139 | { | ||
mWidth = rect.width(); | ||||
mHeight = rect.height(); | ||||
layoutChanged(); | ||||
mLayoutSet = true; | ||||
setPos(rect.topLeft()); | ||||
} | ||||
sauimone
|
r216 | #include "moc_barpresenterbase.cpp" | ||
Michal Klocek
|
r139 | |||
sauimone
|
r126 | QTCOMMERCIALCHART_END_NAMESPACE | ||