chartpresenter.cpp
390 lines
| 13.0 KiB
| text/x-c
|
CppLexer
/ src / chartpresenter.cpp
|
r139 | #include "qchart.h" | ||
|
r140 | #include "qchartaxis.h" | ||
|
r131 | #include "chartpresenter_p.h" | ||
#include "chartdataset_p.h" | ||||
|
r143 | #include "charttheme_p.h" | ||
|
r530 | #include "chartanimator_p.h" | ||
|
r139 | //series | ||
|
r338 | #include "qbarseries.h" | ||
#include "qstackedbarseries.h" | ||||
#include "qpercentbarseries.h" | ||||
|
r349 | #include "qlineseries.h" | ||
|
r421 | #include "qareaseries.h" | ||
|
r142 | #include "qpieseries.h" | ||
|
r158 | #include "qscatterseries.h" | ||
|
r295 | #include "qsplineseries.h" | ||
|
r139 | //items | ||
|
r140 | #include "axisitem_p.h" | ||
|
r421 | #include "areachartitem_p.h" | ||
|
r381 | #include "barpresenter_p.h" | ||
#include "stackedbarpresenter_p.h" | ||||
#include "percentbarpresenter_p.h" | ||||
|
r145 | #include "linechartitem_p.h" | ||
|
r353 | #include "piepresenter_p.h" | ||
|
r470 | #include "scatterchartitem_p.h" | ||
|
r460 | #include "splinechartitem_p.h" | ||
|
r131 | |||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(chart), | ||||
|
r401 | m_chart(chart), | ||
|
r530 | m_animator(0), | ||
|
r401 | m_dataset(dataset), | ||
m_chartTheme(0), | ||||
|
r439 | m_zoomIndex(0), | ||
|
r401 | m_marginSize(0), | ||
|
r418 | m_rect(QRectF(QPoint(0,0),m_chart->size())), | ||
|
r439 | m_options(QChart::NoAnimation) | ||
|
r131 | { | ||
|
r401 | createConnections(); | ||
|
r153 | setChartTheme(QChart::ChartThemeDefault); | ||
|
r131 | } | ||
ChartPresenter::~ChartPresenter() | ||||
{ | ||||
|
r531 | delete m_chartTheme; | ||
|
r131 | } | ||
|
r140 | void ChartPresenter::createConnections() | ||
|
r131 | { | ||
QObject::connect(m_chart,SIGNAL(geometryChanged()),this,SLOT(handleGeometryChanged())); | ||||
|
r439 | QObject::connect(m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),this,SLOT(handleSeriesAdded(QSeries*,Domain*))); | ||
|
r360 | QObject::connect(m_dataset,SIGNAL(seriesRemoved(QSeries*)),this,SLOT(handleSeriesRemoved(QSeries*))); | ||
|
r439 | QObject::connect(m_dataset,SIGNAL(axisAdded(QChartAxis*,Domain*)),this,SLOT(handleAxisAdded(QChartAxis*,Domain*))); | ||
|
r223 | QObject::connect(m_dataset,SIGNAL(axisRemoved(QChartAxis*)),this,SLOT(handleAxisRemoved(QChartAxis*))); | ||
} | ||||
QRectF ChartPresenter::geometry() const | ||||
{ | ||||
return m_rect; | ||||
|
r131 | } | ||
void ChartPresenter::handleGeometryChanged() | ||||
{ | ||||
|
r439 | QRectF rect(QPoint(0,0),m_chart->size()); | ||
rect.adjust(m_marginSize,m_marginSize, -m_marginSize, -m_marginSize); | ||||
//rewrite zoom stack | ||||
for(int i=0;i<m_zoomStack.count();i++){ | ||||
QRectF r = m_zoomStack[i]; | ||||
qreal w = rect.width()/m_rect.width(); | ||||
qreal h = rect.height()/m_rect.height(); | ||||
QPointF tl = r.topLeft(); | ||||
tl.setX(tl.x()*w); | ||||
tl.setY(tl.y()*h); | ||||
QPointF br = r.bottomRight(); | ||||
br.setX(br.x()*w); | ||||
br.setY(br.y()*h); | ||||
r.setTopLeft(tl); | ||||
r.setBottomRight(br); | ||||
m_zoomStack[i]=r; | ||||
} | ||||
m_rect = rect; | ||||
|
r147 | Q_ASSERT(m_rect.isValid()); | ||
|
r139 | emit geometryChanged(m_rect); | ||
} | ||||
|
r131 | |||
|
r139 | int ChartPresenter::margin() const | ||
{ | ||||
return m_marginSize; | ||||
} | ||||
|
r131 | |||
|
r139 | void ChartPresenter::setMargin(int margin) | ||
{ | ||||
m_marginSize = margin; | ||||
|
r131 | } | ||
|
r439 | void ChartPresenter::handleAxisAdded(QChartAxis* axis,Domain* domain) | ||
|
r223 | { | ||
|
r530 | AxisItem* item = new AxisItem(axis,this,axis==m_dataset->axisX()?AxisItem::X_AXIS : AxisItem::Y_AXIS,m_chart); | ||
|
r298 | |||
|
r530 | if(m_options.testFlag(QChart::GridAxisAnimations)){ | ||
m_animator->addAnimation(item); | ||||
|
r439 | } | ||
|
r530 | |||
|
r439 | if(axis==m_dataset->axisX()){ | ||
|
r531 | QObject::connect(domain,SIGNAL(rangeXChanged(qreal,qreal,int)),item,SLOT(handleRangeChanged(qreal,qreal,int))); | ||
|
r439 | //initialize | ||
|
r531 | item->handleRangeChanged(domain->minX(),domain->maxX(),domain->tickXCount()); | ||
|
r439 | } | ||
else{ | ||||
|
r531 | QObject::connect(domain,SIGNAL(rangeYChanged(qreal,qreal,int)),item,SLOT(handleRangeChanged(qreal,qreal,int))); | ||
|
r439 | //initialize | ||
|
r531 | item->handleRangeChanged(domain->minY(),domain->maxY(),domain->tickYCount()); | ||
|
r223 | } | ||
|
r298 | |||
|
r223 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&))); | ||
|
r439 | //initialize | ||
|
r259 | item->handleGeometryChanged(m_rect); | ||
|
r223 | m_chartTheme->decorate(axis,item); | ||
m_axisItems.insert(axis,item); | ||||
|
r530 | |||
|
r223 | } | ||
void ChartPresenter::handleAxisRemoved(QChartAxis* axis) | ||||
{ | ||||
AxisItem* item = m_axisItems.take(axis); | ||||
Q_ASSERT(item); | ||||
|
r530 | if(m_animator) m_animator->removeAnimation(item); | ||
|
r223 | delete item; | ||
} | ||||
|
r439 | void ChartPresenter::handleSeriesAdded(QSeries* series,Domain* domain) | ||
|
r131 | { | ||
|
r531 | ChartItem *item = 0 ; | ||
|
r131 | switch(series->type()) | ||
{ | ||||
|
r418 | case QSeries::SeriesTypeLine: { | ||
|
r426 | |||
|
r418 | QLineSeries* lineSeries = static_cast<QLineSeries*>(series); | ||
|
r531 | LineChartItem* line = new LineChartItem(lineSeries,m_chart); | ||
if(m_options.testFlag(QChart::SeriesAnimations)) { | ||||
m_animator->addAnimation(line); | ||||
|
r139 | } | ||
|
r538 | m_chartTheme->decorate(line, lineSeries, m_dataset->seriesIndex(series)); | ||
|
r531 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),line,SLOT(handleGeometryChanged(const QRectF&))); | ||
QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),line,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal))); | ||||
item = line; | ||||
|
r426 | break; | ||
} | ||||
|
r421 | |||
|
r426 | case QSeries::SeriesTypeArea: { | ||
|
r139 | |||
|
r426 | QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series); | ||
|
r531 | AreaChartItem* area = new AreaChartItem(areaSeries,m_chart); | ||
|
r426 | if(m_options.testFlag(QChart::SeriesAnimations)) { | ||
|
r531 | // m_animator->addAnimation(area); | ||
|
r131 | } | ||
|
r538 | m_chartTheme->decorate(area, areaSeries, m_dataset->seriesIndex(series)); | ||
|
r531 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),area,SLOT(handleGeometryChanged(const QRectF&))); | ||
QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),area,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal))); | ||||
item=area; | ||||
|
r401 | break; | ||
} | ||||
|
r139 | |||
|
r418 | case QSeries::SeriesTypeBar: { | ||
QBarSeries* barSeries = static_cast<QBarSeries*>(series); | ||||
|
r531 | BarPresenter* bar = new BarPresenter(barSeries,m_chart); | ||
if(m_options.testFlag(QChart::SeriesAnimations)) { | ||||
// m_animator->addAnimation(bar); | ||||
} | ||||
|
r538 | m_chartTheme->decorate(bar, barSeries, m_dataset->seriesIndex(barSeries)); | ||
|
r531 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),bar,SLOT(handleGeometryChanged(const QRectF&))); | ||
QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),bar,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal))); | ||||
item=bar; | ||||
|
r401 | break; | ||
} | ||||
|
r131 | |||
|
r418 | case QSeries::SeriesTypeStackedBar: { | ||
QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series); | ||||
|
r531 | StackedBarPresenter* bar = new StackedBarPresenter(stackedBarSeries,m_chart); | ||
if(m_options.testFlag(QChart::SeriesAnimations)) { | ||||
// m_animator->addAnimation(bar); | ||||
} | ||||
|
r538 | m_chartTheme->decorate(bar, stackedBarSeries, m_dataset->seriesIndex(stackedBarSeries)); | ||
|
r531 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),bar,SLOT(handleGeometryChanged(const QRectF&))); | ||
QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),bar,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal))); | ||||
item=bar; | ||||
|
r401 | break; | ||
} | ||||
|
r139 | |||
|
r418 | case QSeries::SeriesTypePercentBar: { | ||
QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series); | ||||
|
r531 | PercentBarPresenter* bar = new PercentBarPresenter(percentBarSeries,m_chart); | ||
if(m_options.testFlag(QChart::SeriesAnimations)) { | ||||
// m_animator->addAnimation(bar); | ||||
} | ||||
|
r538 | m_chartTheme->decorate(bar, percentBarSeries, m_dataset->seriesIndex(percentBarSeries)); | ||
|
r531 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),bar,SLOT(handleGeometryChanged(const QRectF&))); | ||
QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),bar,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal))); | ||||
item=bar; | ||||
|
r401 | break; | ||
} | ||||
|
r476 | |||
|
r531 | case QSeries::SeriesTypeScatter: { | ||
QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series); | ||||
|
r538 | ScatterChartItem *scatter = new ScatterChartItem(scatterSeries, m_chart); | ||
if(m_options.testFlag(QChart::SeriesAnimations)) { | ||||
m_animator->addAnimation(scatter); | ||||
} | ||||
m_chartTheme->decorate(scatter, scatterSeries, m_dataset->seriesIndex(series)); | ||||
QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),scatter,SLOT(handleGeometryChanged(const QRectF&))); | ||||
QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),scatter,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal))); | ||||
item = scatter; | ||||
break; | ||||
|
r531 | } | ||
|
r418 | |||
|
r531 | case QSeries::SeriesTypePie: { | ||
QPieSeries *pieSeries = static_cast<QPieSeries *>(series); | ||||
PiePresenter* pie = new PiePresenter(m_chart, pieSeries); | ||||
if(m_options.testFlag(QChart::SeriesAnimations)) { | ||||
// m_animator->addAnimation(pie); | ||||
} | ||||
|
r538 | m_chartTheme->decorate(pie, pieSeries, m_dataset->seriesIndex(series)); | ||
|
r531 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),pie,SLOT(handleGeometryChanged(const QRectF&))); | ||
QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),pie,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal))); | ||||
|
r418 | // Hide all from background when there is only piechart | ||
// TODO: refactor this ugly code... should be one setting for this | ||||
if (m_chartItems.count() == 0) { | ||||
|
r534 | m_chart->axisX()->hide(); | ||
m_chart->axisY()->hide(); | ||||
|
r418 | m_chart->setChartBackgroundBrush(Qt::transparent); | ||
|
r131 | } | ||
|
r531 | item=pie; | ||
|
r401 | break; | ||
} | ||||
|
r427 | |||
|
r419 | case QSeries::SeriesTypeSpline: { | ||
|
r538 | QSplineSeries* splineSeries = static_cast<QSplineSeries*>(series); | ||
SplineChartItem* spline = new SplineChartItem(splineSeries, m_chart); | ||||
if(m_options.testFlag(QChart::SeriesAnimations)) { | ||||
m_animator->addAnimation(spline); | ||||
} | ||||
m_chartTheme->decorate(spline, splineSeries, m_dataset->seriesIndex(series)); | ||||
QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),spline,SLOT(handleGeometryChanged(const QRectF&))); | ||||
QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),spline,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal))); | ||||
item=spline; | ||||
break; | ||||
} | ||||
|
r401 | default: { | ||
qDebug()<< "Series type" << series->type() << "not implemented."; | ||||
|
r295 | break; | ||
} | ||||
|
r131 | } | ||
|
r439 | |||
|
r531 | //initialize | ||
item->handleDomainChanged(domain->minX(),domain->maxX(),domain->minY(),domain->maxY()); | ||||
if(m_rect.isValid()) item->handleGeometryChanged(m_rect); | ||||
m_chartItems.insert(series,item); | ||||
|
r439 | zoomReset(); | ||
|
r131 | } | ||
|
r360 | void ChartPresenter::handleSeriesRemoved(QSeries* series) | ||
|
r139 | { | ||
|
r401 | ChartItem* item = m_chartItems.take(series); | ||
|
r530 | Q_ASSERT(item); | ||
if(m_animator) m_animator->removeAnimation(item); | ||||
|
r401 | delete item; | ||
|
r139 | } | ||
|
r131 | |||
|
r153 | void ChartPresenter::setChartTheme(QChart::ChartTheme theme) | ||
|
r143 | { | ||
|
r531 | if(m_chartTheme && m_chartTheme->id() == theme) return; | ||
|
r143 | delete m_chartTheme; | ||
m_chartTheme = ChartTheme::createTheme(theme); | ||||
|
r532 | m_chartTheme->decorate(m_chart); | ||
|
r540 | m_chartTheme->decorate(m_chart->legend()); | ||
|
r531 | resetAllElements(); | ||
|
r176 | } | ||
|
r153 | QChart::ChartTheme ChartPresenter::chartTheme() | ||
|
r143 | { | ||
|
r153 | return m_chartTheme->id(); | ||
} | ||||
|
r298 | void ChartPresenter::setAnimationOptions(QChart::AnimationOptions options) | ||
{ | ||||
if(m_options!=options) { | ||||
m_options=options; | ||||
|
r530 | if(m_options!=QChart::NoAnimation && !m_animator) { | ||
m_animator= new ChartAnimator(this); | ||||
|
r298 | } | ||
|
r531 | resetAllElements(); | ||
|
r298 | } | ||
|
r530 | |||
|
r298 | } | ||
|
r531 | void ChartPresenter::resetAllElements() | ||
{ | ||||
QList<QChartAxis*> axisList = m_axisItems.uniqueKeys(); | ||||
QList<QSeries*> seriesList = m_chartItems.uniqueKeys(); | ||||
foreach(QChartAxis* axis, axisList) { | ||||
handleAxisRemoved(axis); | ||||
handleAxisAdded(axis,m_dataset->domain(axis)); | ||||
} | ||||
foreach(QSeries* series, seriesList) { | ||||
handleSeriesRemoved(series); | ||||
handleSeriesAdded(series,m_dataset->domain(series)); | ||||
} | ||||
} | ||||
|
r439 | void ChartPresenter::zoomIn() | ||
{ | ||||
QRectF rect = geometry(); | ||||
rect.setWidth(rect.width()/2); | ||||
rect.setHeight(rect.height()/2); | ||||
rect.moveCenter(geometry().center()); | ||||
zoomIn(rect); | ||||
} | ||||
void ChartPresenter::zoomIn(const QRectF& rect) | ||||
{ | ||||
QRectF r = rect.normalized(); | ||||
r.translate(-m_marginSize, -m_marginSize); | ||||
|
r531 | if(m_animator) { | ||
QPointF point(r.center().x()/geometry().width(),r.center().y()/geometry().height()); | ||||
m_animator->setState(ChartAnimator::ZoomInState,point); | ||||
} | ||||
|
r439 | m_dataset->zoomInDomain(r,geometry().size()); | ||
m_zoomStack<<r; | ||||
m_zoomIndex++; | ||||
|
r531 | if(m_animator) { | ||
m_animator->setState(ChartAnimator::ShowState); | ||||
} | ||||
|
r439 | } | ||
void ChartPresenter::zoomOut() | ||||
{ | ||||
if(m_zoomIndex==0) return; | ||||
|
r531 | if(m_animator) | ||
{ | ||||
m_animator->setState(ChartAnimator::ZoomOutState); | ||||
} | ||||
|
r439 | m_dataset->zoomOutDomain(m_zoomStack[m_zoomIndex-1],geometry().size()); | ||
m_zoomIndex--; | ||||
m_zoomStack.resize(m_zoomIndex); | ||||
|
r531 | if(m_animator){ | ||
m_animator->setState(ChartAnimator::ShowState); | ||||
} | ||||
|
r439 | } | ||
void ChartPresenter::zoomReset() | ||||
{ | ||||
m_zoomIndex=0; | ||||
m_zoomStack.resize(m_zoomIndex); | ||||
} | ||||
|
r531 | void ChartPresenter::scroll(int dx,int dy) | ||
{ | ||||
if(m_animator){ | ||||
if(dx<0) m_animator->setState(ChartAnimator::ScrollLeftState,QPointF()); | ||||
if(dx>0) m_animator->setState(ChartAnimator::ScrollRightState,QPointF()); | ||||
if(dy<0) m_animator->setState(ChartAnimator::ScrollUpState,QPointF()); | ||||
if(dy>0) m_animator->setState(ChartAnimator::ScrollDownState,QPointF()); | ||||
} | ||||
m_dataset->scrollDomain(dx,dy,geometry().size()); | ||||
|
r547 | if(m_animator){ | ||
|
r531 | m_animator->setState(ChartAnimator::ShowState); | ||
} | ||||
} | ||||
|
r298 | QChart::AnimationOptions ChartPresenter::animationOptions() const | ||
{ | ||||
return m_options; | ||||
} | ||||
|
r143 | |||
|
r131 | #include "moc_chartpresenter_p.cpp" | ||
QTCOMMERCIALCHART_END_NAMESPACE | ||||