barpresenterbase.cpp
158 lines
| 4.5 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" | ||
|
r338 | #include "qbarseries.h" | ||
|
r126 | #include <QDebug> | ||
|
r288 | #include <QToolTip> | ||
|
r126 | |||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
|
r338 | BarPresenterBase::BarPresenterBase(QBarSeries *series, QGraphicsItem *parent) | ||
|
r126 | : ChartItem(parent) | ||
,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready | ||||
,mLayoutSet(false) | ||||
,mLayoutDirty(true) | ||||
|
r296 | ,mSeparatorsEnabled(false) | ||
|
r288 | ,mSeries(series) | ||
|
r126 | { | ||
|
r288 | connect(series,SIGNAL(showToolTip(QPoint,QString)),this,SLOT(showToolTip(QPoint,QString))); | ||
|
r296 | connect(series,SIGNAL(separatorsEnabled(bool)),this,SLOT(enableSeparators(bool))); | ||
|
r167 | dataChanged(); | ||
|
r126 | } | ||
|
r288 | BarPresenterBase::~BarPresenterBase() | ||
|
r126 | { | ||
|
r288 | disconnect(this,SLOT(showToolTip(QPoint,QString))); | ||
delete mSeries; | ||||
|
r126 | } | ||
|
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 | ||
|
r288 | for (int c=0; c<mSeries->countCategories(); c++) { | ||
for (int s=0; s<mSeries->countSets(); s++) { | ||||
QBarSet *set = mSeries->setAt(s); | ||||
|
r239 | Bar *bar = new Bar(this); | ||
childItems().append(bar); | ||||
|
r256 | mBars.append(bar); | ||
|
r273 | connect(bar,SIGNAL(clicked()),set,SLOT(barClicked())); | ||
|
r283 | connect(bar,SIGNAL(hoverEntered(QPoint)),set,SLOT(barHoverEntered(QPoint))); | ||
connect(bar,SIGNAL(hoverLeaved()),set,SLOT(barHoverLeaved())); | ||||
|
r239 | } | ||
|
r126 | } | ||
|
r263 | // Create labels | ||
|
r288 | int count = mSeries->countCategories(); | ||
|
r126 | for (int i=0; i<count; i++) { | ||
BarLabel* label = new BarLabel(this); | ||||
|
r288 | label->set(mSeries->label(i)); | ||
|
r126 | childItems().append(label); | ||
|
r256 | mLabels.append(label); | ||
|
r126 | } | ||
|
r263 | // Create separators | ||
|
r288 | count = mSeries->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 | ||||
|
r296 | sep->setVisible(mSeparatorsEnabled); | ||
|
r126 | childItems().append(sep); | ||
|
r256 | mSeparators.append(sep); | ||
|
r126 | } | ||
|
r263 | // Create floating values | ||
|
r288 | for (int category=0; category<mSeries->countCategories(); category++) { | ||
for (int s=0; s<mSeries->countSets(); s++) { | ||||
QBarSet *set = mSeries->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()); | ||||
} | ||||
|
r296 | void BarPresenterBase::showToolTip(QPoint pos, QString tip) | ||
|
r288 | { | ||
|
r296 | // TODO: cool tooltip instead of default | ||
QToolTip::showText(pos,tip); | ||||
|
r288 | } | ||
|
r280 | |||
|
r288 | void BarPresenterBase::enableSeparators(bool enabled) | ||
|
r280 | { | ||
|
r296 | for (int i=0; i<mSeparators.count(); i++) { | ||
mSeparators.at(i)->setVisible(enabled); | ||||
|
r288 | } | ||
|
r296 | mSeparatorsEnabled = enabled; | ||
|
r280 | } | ||
|
r216 | #include "moc_barpresenterbase.cpp" | ||
|
r139 | |||
|
r126 | QTCOMMERCIALCHART_END_NAMESPACE | ||