barpresenterbase.cpp
180 lines
| 4.9 KiB
| text/x-c
|
CppLexer
sauimone
|
r381 | #include "barpresenterbase_p.h" | ||
sauimone
|
r126 | #include "bar_p.h" | ||
sauimone
|
r263 | #include "barvalue_p.h" | ||
sauimone
|
r126 | #include "separator_p.h" | ||
sauimone
|
r240 | #include "qbarset.h" | ||
sauimone
|
r338 | #include "qbarseries.h" | ||
sauimone
|
r487 | #include "qchart.h" | ||
#include "qchartaxis.h" | ||||
#include "qchartaxiscategories.h" | ||||
sauimone
|
r126 | #include <QDebug> | ||
sauimone
|
r288 | #include <QToolTip> | ||
sauimone
|
r126 | |||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
Tero Ahola
|
r518 | BarPresenterBase::BarPresenterBase(QBarSeries *series, QChart *parent) : | ||
ChartItem(parent), | ||||
mLayoutSet(false), | ||||
mSeries(series), | ||||
mChart(parent), | ||||
mWidth(0), | ||||
mHeight(0) | ||||
sauimone
|
r126 | { | ||
sauimone
|
r288 | connect(series,SIGNAL(showToolTip(QPoint,QString)),this,SLOT(showToolTip(QPoint,QString))); | ||
sauimone
|
r505 | connect(series,SIGNAL(enableSeparators(bool)),this,SLOT(enableSeparators(bool))); | ||
enableSeparators(series->separatorsVisible()); | ||||
sauimone
|
r487 | initAxisLabels(); | ||
sauimone
|
r167 | dataChanged(); | ||
sauimone
|
r126 | } | ||
sauimone
|
r288 | BarPresenterBase::~BarPresenterBase() | ||
sauimone
|
r126 | { | ||
sauimone
|
r288 | disconnect(this,SLOT(showToolTip(QPoint,QString))); | ||
sauimone
|
r505 | disconnect(this,SLOT(enableSeparators(bool))); | ||
sauimone
|
r126 | } | ||
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
|
r473 | foreach(QGraphicsItem* i, childItems()) { | ||
i->paint(painter,option,widget); | ||||
} | ||||
sauimone
|
r126 | } | ||
sauimone
|
r216 | QRectF BarPresenterBase::boundingRect() const | ||
sauimone
|
r126 | { | ||
Tero Ahola
|
r518 | return QRectF(0, 0, mWidth, mHeight); | ||
sauimone
|
r126 | } | ||
sauimone
|
r165 | |||
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
|
r126 | // Delete old bars | ||
foreach (QGraphicsItem* item, childItems()) { | ||||
delete item; | ||||
} | ||||
sauimone
|
r256 | mBars.clear(); | ||
mSeparators.clear(); | ||||
sauimone
|
r263 | mFloatingValues.clear(); | ||
sauimone
|
r256 | |||
sauimone
|
r126 | // Create new graphic items for bars | ||
sauimone
|
r366 | for (int c=0; c<mSeries->categoryCount(); c++) { | ||
sauimone
|
r425 | QString category = mSeries->categoryName(c); | ||
sauimone
|
r366 | for (int s=0; s<mSeries->barsetCount(); s++) { | ||
sauimone
|
r357 | QBarSet *set = mSeries->barsetAt(s); | ||
sauimone
|
r425 | Bar *bar = new Bar(category,this); | ||
sauimone
|
r239 | childItems().append(bar); | ||
sauimone
|
r256 | mBars.append(bar); | ||
sauimone
|
r425 | connect(bar,SIGNAL(clicked(QString)),set,SIGNAL(clicked(QString))); | ||
connect(bar,SIGNAL(rightClicked(QString)),set,SIGNAL(rightClicked(QString))); | ||||
sauimone
|
r412 | connect(bar,SIGNAL(hoverEntered(QPoint)),set,SLOT(barHoverEnterEvent(QPoint))); | ||
connect(bar,SIGNAL(hoverLeaved()),set,SLOT(barHoverLeaveEvent())); | ||||
sauimone
|
r239 | } | ||
sauimone
|
r126 | } | ||
sauimone
|
r263 | // Create separators | ||
sauimone
|
r487 | int count = mSeries->categoryCount() - 1; // There is one less separator than columns | ||
sauimone
|
r126 | for (int i=0; i<count; i++) { | ||
Separator* sep = new Separator(this); | ||||
sauimone
|
r505 | sep->setVisible(mSeries->separatorsVisible()); | ||
sauimone
|
r126 | childItems().append(sep); | ||
sauimone
|
r256 | mSeparators.append(sep); | ||
sauimone
|
r126 | } | ||
sauimone
|
r263 | // Create floating values | ||
sauimone
|
r366 | for (int category=0; category<mSeries->categoryCount(); category++) { | ||
for (int s=0; s<mSeries->barsetCount(); s++) { | ||||
sauimone
|
r357 | QBarSet *set = mSeries->barsetAt(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 | } | ||
sauimone
|
r487 | void BarPresenterBase::initAxisLabels() | ||
{ | ||||
int count = mSeries->categoryCount(); | ||||
if (0 == count) { | ||||
return; | ||||
} | ||||
Michal Klocek
|
r502 | mChart->axisX()->setTicksCount(count+2); | ||
sauimone
|
r487 | |||
qreal min = 0; | ||||
Michal Klocek
|
r502 | qreal max = count+1; | ||
sauimone
|
r487 | |||
mChart->axisX()->setMin(min); | ||||
mChart->axisX()->setMax(max); | ||||
Michal Klocek
|
r502 | |||
Michal Klocek
|
r497 | QChartAxisCategories* categories = mChart->axisX()->categories(); | ||
categories->clear(); | ||||
Michal Klocek
|
r502 | for (int i=0; i<count; i++) { | ||
categories->insert(i+1,mSeries->categoryName(i)); | ||||
sauimone
|
r487 | } | ||
Michal Klocek
|
r502 | |||
sauimone
|
r487 | mChart->axisX()->setLabelsVisible(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
|
r487 | qDebug() << "BarPresenterBase::handleDomainChanged"; | ||
/* | ||||
int count = mSeries->categoryCount(); | ||||
if (0 == count) { | ||||
return; | ||||
} | ||||
// Position labels to domain | ||||
qreal min = domain.minX(); | ||||
qreal max = domain.maxX(); | ||||
qreal step = (max-min)/count; | ||||
QChartAxisCategories& categories = mChart->axisX()->categories(); | ||||
categories.clear(); | ||||
for (int i=0; i<count; i++) { | ||||
categories.insert(min,mSeries->categoryName(i)); | ||||
min += step; | ||||
} | ||||
*/ | ||||
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
|
r296 | void BarPresenterBase::showToolTip(QPoint pos, QString tip) | ||
sauimone
|
r288 | { | ||
sauimone
|
r296 | // TODO: cool tooltip instead of default | ||
QToolTip::showText(pos,tip); | ||||
sauimone
|
r288 | } | ||
sauimone
|
r280 | |||
sauimone
|
r288 | void BarPresenterBase::enableSeparators(bool enabled) | ||
sauimone
|
r280 | { | ||
sauimone
|
r296 | for (int i=0; i<mSeparators.count(); i++) { | ||
mSeparators.at(i)->setVisible(enabled); | ||||
sauimone
|
r288 | } | ||
sauimone
|
r280 | } | ||
sauimone
|
r381 | #include "moc_barpresenterbase_p.cpp" | ||
Michal Klocek
|
r139 | |||
sauimone
|
r126 | QTCOMMERCIALCHART_END_NAMESPACE | ||