chartpresenter.cpp
373 lines
| 12.9 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" | ||
|
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" | ||
|
r298 | #include "axisanimationitem_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" | ||
|
r131 | #include "linechartanimationitem_p.h" | ||
|
r353 | #include "piepresenter_p.h" | ||
|
r194 | #include "scatterpresenter_p.h" | ||
|
r295 | #include "splinepresenter_p.h" | ||
|
r131 | |||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(chart), | ||||
|
r401 | m_chart(chart), | ||
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() | ||||
{ | ||||
} | ||||
|
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 | { | ||
|
r298 | |||
|
r223 | AxisItem* item ; | ||
|
r298 | if(!m_options.testFlag(QChart::GridAxisAnimations)) | ||
{ | ||||
|
r439 | item = new AxisItem(axis,axis==m_dataset->axisX()?AxisItem::X_AXIS : AxisItem::Y_AXIS,m_chart); | ||
|
r223 | }else{ | ||
|
r439 | item = new AxisAnimationItem(axis,axis==m_dataset->axisX()?AxisItem::X_AXIS : AxisItem::Y_AXIS,m_chart); | ||
} | ||||
if(axis==m_dataset->axisX()){ | ||||
QObject::connect(domain,SIGNAL(rangeXChanged(qreal,qreal)),item,SLOT(handleRangeChanged(qreal,qreal))); | ||||
//initialize | ||||
item->handleRangeChanged(domain->minX(),domain->maxX()); | ||||
|
r452 | item->handleTicksCountChanged(4); | ||
|
r439 | } | ||
else{ | ||||
QObject::connect(domain,SIGNAL(rangeYChanged(qreal,qreal)),item,SLOT(handleRangeChanged(qreal,qreal))); | ||||
//initialize | ||||
item->handleRangeChanged(domain->minY(),domain->maxY()); | ||||
|
r452 | item->handleTicksCountChanged(4); | ||
|
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); | ||||
} | ||||
void ChartPresenter::handleAxisRemoved(QChartAxis* axis) | ||||
{ | ||||
AxisItem* item = m_axisItems.take(axis); | ||||
Q_ASSERT(item); | ||||
delete item; | ||||
} | ||||
|
r439 | void ChartPresenter::handleSeriesAdded(QSeries* series,Domain* domain) | ||
|
r131 | { | ||
switch(series->type()) | ||||
{ | ||||
|
r418 | case QSeries::SeriesTypeLine: { | ||
|
r426 | |||
|
r418 | QLineSeries* lineSeries = static_cast<QLineSeries*>(series); | ||
LineChartItem* item; | ||||
if(m_options.testFlag(QChart::SeriesAnimations)){ | ||||
|
r439 | item = new LineChartAnimationItem(lineSeries,m_chart); | ||
|
r418 | }else{ | ||
|
r439 | item = new LineChartItem(lineSeries,m_chart); | ||
|
r139 | } | ||
|
r439 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&))); | ||
QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),item,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal))); | ||||
//initialize | ||||
item->handleDomainChanged(domain->minX(),domain->maxX(),domain->minY(),domain->maxY()); | ||||
if(m_rect.isValid()) item->handleGeometryChanged(m_rect); | ||||
//decorate | ||||
|
r401 | m_chartTheme->decorate(item,lineSeries,m_chartItems.count()); | ||
m_chartItems.insert(series,item); | ||||
|
r426 | break; | ||
} | ||||
|
r421 | |||
|
r426 | case QSeries::SeriesTypeArea: { | ||
|
r139 | |||
|
r426 | QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series); | ||
AreaChartItem* item; | ||||
if(m_options.testFlag(QChart::SeriesAnimations)) { | ||||
|
r439 | item = new AreaChartItem(areaSeries,m_chart); | ||
|
r426 | } | ||
else { | ||||
|
r439 | item = new AreaChartItem(areaSeries,m_chart); | ||
|
r131 | } | ||
|
r439 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&))); | ||
QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),item,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal))); | ||||
//initialize | ||||
item->handleDomainChanged(domain->minX(),domain->maxX(),domain->minY(),domain->maxY()); | ||||
if(m_rect.isValid()) item->handleGeometryChanged(m_rect); | ||||
//decorate | ||||
|
r426 | m_chartTheme->decorate(item,areaSeries,m_chartItems.count()); | ||
m_chartItems.insert(series,item); | ||||
|
r401 | break; | ||
} | ||||
|
r139 | |||
|
r418 | case QSeries::SeriesTypeBar: { | ||
QBarSeries* barSeries = static_cast<QBarSeries*>(series); | ||||
|
r401 | BarPresenter* item = new BarPresenter(barSeries,m_chart); | ||
m_chartTheme->decorate(item,barSeries,m_chartItems.count()); | ||||
QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&))); | ||||
|
r427 | // QObject::connect(barSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int))); | ||
|
r401 | m_chartItems.insert(series,item); | ||
// m_axisXItem->setVisible(false); | ||||
|
r418 | if(m_rect.isValid()) item->handleGeometryChanged(m_rect); | ||
|
r401 | break; | ||
} | ||||
|
r131 | |||
|
r418 | case QSeries::SeriesTypeStackedBar: { | ||
|
r139 | |||
|
r418 | QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series); | ||
|
r401 | StackedBarPresenter* item = new StackedBarPresenter(stackedBarSeries,m_chart); | ||
m_chartTheme->decorate(item,stackedBarSeries,m_chartItems.count()); | ||||
QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&))); | ||||
|
r427 | // QObject::connect(stackedBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int))); | ||
|
r401 | m_chartItems.insert(series,item); | ||
|
r418 | if(m_rect.isValid()) item->handleGeometryChanged(m_rect); | ||
|
r401 | break; | ||
} | ||||
|
r139 | |||
|
r418 | case QSeries::SeriesTypePercentBar: { | ||
|
r139 | |||
|
r418 | QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series); | ||
|
r401 | PercentBarPresenter* item = new PercentBarPresenter(percentBarSeries,m_chart); | ||
m_chartTheme->decorate(item,percentBarSeries ,m_chartItems.count()); | ||||
QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&))); | ||||
|
r427 | // QObject::connect(percentBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int))); | ||
|
r401 | m_chartItems.insert(series,item); | ||
|
r418 | if(m_rect.isValid()) item->handleGeometryChanged(m_rect); | ||
|
r401 | break; | ||
} | ||||
|
r418 | case QSeries::SeriesTypeScatter: { | ||
|
r401 | QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series); | ||
ScatterPresenter *scatterPresenter = new ScatterPresenter(scatterSeries, m_chart); | ||||
|
r418 | QObject::connect(scatterPresenter, SIGNAL(clicked(QPointF)), | ||
scatterSeries, SIGNAL(clicked(QPointF))); | ||||
|
r401 | QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)), | ||
scatterPresenter, SLOT(handleGeometryChanged(const QRectF&))); | ||||
|
r447 | QObject::connect(domain, SIGNAL(domainChanged(qreal,qreal,qreal,qreal)), | ||
scatterPresenter, SLOT(handleDomainChanged(qreal,qreal,qreal,qreal))); | ||||
|
r401 | m_chartTheme->decorate(scatterPresenter, scatterSeries, m_chartItems.count()); | ||
m_chartItems.insert(scatterSeries, scatterPresenter); | ||||
|
r447 | if (m_rect.isValid()) | ||
scatterPresenter->handleGeometryChanged(m_rect); | ||||
scatterPresenter->handleDomainChanged(domain->minX(), domain->maxX(), domain->minY(), domain->maxY()); | ||||
|
r401 | break; | ||
} | ||||
|
r418 | case QSeries::SeriesTypePie: { | ||
|
r401 | QPieSeries *s = qobject_cast<QPieSeries *>(series); | ||
PiePresenter* pie = new PiePresenter(m_chart, s); | ||||
m_chartTheme->decorate(pie, s, m_chartItems.count()); | ||||
QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)), pie, SLOT(handleGeometryChanged(const QRectF&))); | ||||
|
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) { | ||||
m_chart->axisX()->setAxisVisible(false); | ||||
m_chart->axisY()->setAxisVisible(false); | ||||
m_chart->axisX()->setGridVisible(false); | ||||
m_chart->axisY()->setGridVisible(false); | ||||
m_chart->axisX()->setLabelsVisible(false); | ||||
m_chart->axisY()->setLabelsVisible(false); | ||||
m_chart->axisX()->setShadesVisible(false); | ||||
m_chart->axisY()->setShadesVisible(false); | ||||
m_chart->setChartBackgroundBrush(Qt::transparent); | ||||
|
r131 | } | ||
|
r418 | |||
|
r401 | m_chartItems.insert(series, pie); | ||
|
r418 | pie->handleGeometryChanged(m_rect); | ||
|
r401 | break; | ||
} | ||||
|
r427 | |||
|
r419 | case QSeries::SeriesTypeSpline: { | ||
|
r295 | QSplineSeries* splineSeries = qobject_cast<QSplineSeries*>(series); | ||
|
r401 | SplinePresenter* splinePresenter = new SplinePresenter(splineSeries, m_chart); | ||
QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)), splinePresenter, SLOT(handleGeometryChanged(const QRectF&))); | ||||
|
r458 | QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),splinePresenter,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal))); | ||
|
r450 | //initialize | ||
splinePresenter->handleDomainChanged(domain->minX(),domain->maxX(),domain->minY(),domain->maxY()); | ||||
|
r401 | m_chartTheme->decorate(splinePresenter, splineSeries, m_chartItems.count()); | ||
m_chartItems.insert(splineSeries, splinePresenter); | ||||
break; | ||||
} | ||||
default: { | ||||
qDebug()<< "Series type" << series->type() << "not implemented."; | ||||
|
r295 | break; | ||
} | ||||
|
r131 | } | ||
|
r439 | |||
zoomReset(); | ||||
|
r131 | } | ||
|
r360 | void ChartPresenter::handleSeriesRemoved(QSeries* series) | ||
|
r139 | { | ||
|
r401 | ChartItem* item = m_chartItems.take(series); | ||
delete item; | ||||
|
r139 | } | ||
|
r131 | |||
|
r153 | void ChartPresenter::setChartTheme(QChart::ChartTheme theme) | ||
|
r143 | { | ||
delete m_chartTheme; | ||||
m_chartTheme = ChartTheme::createTheme(theme); | ||||
m_chartTheme->decorate(m_chart); | ||||
|
r360 | QMapIterator<QSeries*,ChartItem*> i(m_chartItems); | ||
|
r143 | |||
int index=0; | ||||
while (i.hasNext()) { | ||||
i.next(); | ||||
m_chartTheme->decorate(i.value(),i.key(),index); | ||||
|
r312 | index++; | ||
|
r143 | } | ||
|
r223 | QMapIterator<QChartAxis*,AxisItem*> j(m_axisItems); | ||
while (j.hasNext()) { | ||||
|
r401 | j.next(); | ||
m_chartTheme->decorate(j.key(),j.value()); | ||||
|
r223 | } | ||
|
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; | ||||
//recreate elements | ||||
QList<QChartAxis*> axisList = m_axisItems.uniqueKeys(); | ||||
|
r360 | QList<QSeries*> seriesList = m_chartItems.uniqueKeys(); | ||
|
r298 | |||
foreach(QChartAxis* axis, axisList) { | ||||
handleAxisRemoved(axis); | ||||
|
r439 | handleAxisAdded(axis,m_dataset->domain(axis)); | ||
|
r298 | } | ||
|
r360 | foreach(QSeries* series, seriesList) { | ||
|
r298 | handleSeriesRemoved(series); | ||
|
r439 | handleSeriesAdded(series,m_dataset->domain(series)); | ||
|
r298 | } | ||
} | ||||
} | ||||
|
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); | ||||
m_dataset->zoomInDomain(r,geometry().size()); | ||||
m_zoomStack<<r; | ||||
m_zoomIndex++; | ||||
} | ||||
void ChartPresenter::zoomOut() | ||||
{ | ||||
if(m_zoomIndex==0) return; | ||||
m_dataset->zoomOutDomain(m_zoomStack[m_zoomIndex-1],geometry().size()); | ||||
m_zoomIndex--; | ||||
m_zoomStack.resize(m_zoomIndex); | ||||
} | ||||
void ChartPresenter::zoomReset() | ||||
{ | ||||
m_zoomIndex=0; | ||||
m_zoomStack.resize(m_zoomIndex); | ||||
} | ||||
|
r298 | QChart::AnimationOptions ChartPresenter::animationOptions() const | ||
{ | ||||
return m_options; | ||||
} | ||||
|
r143 | |||
|
r131 | #include "moc_chartpresenter_p.cpp" | ||
QTCOMMERCIALCHART_END_NAMESPACE | ||||