chartpresenter.cpp
311 lines
| 10.6 KiB
| text/x-c
|
CppLexer
/ src / chartpresenter.cpp
Michal Klocek
|
r139 | #include "qchart.h" | ||
Michal Klocek
|
r140 | #include "qchartaxis.h" | ||
Michal Klocek
|
r131 | #include "chartpresenter_p.h" | ||
#include "chartdataset_p.h" | ||||
Michal Klocek
|
r143 | #include "charttheme_p.h" | ||
Michal Klocek
|
r139 | //series | ||
sauimone
|
r338 | #include "qbarseries.h" | ||
#include "qstackedbarseries.h" | ||||
#include "qpercentbarseries.h" | ||||
Michal Klocek
|
r349 | #include "qlineseries.h" | ||
Michal Klocek
|
r421 | #include "qareaseries.h" | ||
Jani Honkonen
|
r142 | #include "qpieseries.h" | ||
Tero Ahola
|
r158 | #include "qscatterseries.h" | ||
Marek Rosa
|
r295 | #include "qsplineseries.h" | ||
Michal Klocek
|
r139 | //items | ||
Michal Klocek
|
r140 | #include "axisitem_p.h" | ||
Michal Klocek
|
r298 | #include "axisanimationitem_p.h" | ||
Michal Klocek
|
r421 | #include "areachartitem_p.h" | ||
sauimone
|
r381 | #include "barpresenter_p.h" | ||
#include "stackedbarpresenter_p.h" | ||||
#include "percentbarpresenter_p.h" | ||||
Michal Klocek
|
r145 | #include "linechartitem_p.h" | ||
Michal Klocek
|
r131 | #include "linechartanimationitem_p.h" | ||
Jani Honkonen
|
r353 | #include "piepresenter_p.h" | ||
Tero Ahola
|
r194 | #include "scatterpresenter_p.h" | ||
Marek Rosa
|
r295 | #include "splinepresenter_p.h" | ||
Michal Klocek
|
r131 | |||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(chart), | ||||
Marek Rosa
|
r401 | m_chart(chart), | ||
m_dataset(dataset), | ||||
m_chartTheme(0), | ||||
m_marginSize(0), | ||||
Marek Rosa
|
r418 | m_rect(QRectF(QPoint(0,0),m_chart->size())), | ||
m_options(0) | ||||
Michal Klocek
|
r131 | { | ||
Marek Rosa
|
r401 | createConnections(); | ||
Michal Klocek
|
r153 | setChartTheme(QChart::ChartThemeDefault); | ||
Michal Klocek
|
r223 | |||
Michal Klocek
|
r131 | } | ||
ChartPresenter::~ChartPresenter() | ||||
{ | ||||
} | ||||
Michal Klocek
|
r140 | void ChartPresenter::createConnections() | ||
Michal Klocek
|
r131 | { | ||
QObject::connect(m_chart,SIGNAL(geometryChanged()),this,SLOT(handleGeometryChanged())); | ||||
Michal Klocek
|
r360 | QObject::connect(m_dataset,SIGNAL(seriesAdded(QSeries*)),this,SLOT(handleSeriesAdded(QSeries*))); | ||
QObject::connect(m_dataset,SIGNAL(seriesRemoved(QSeries*)),this,SLOT(handleSeriesRemoved(QSeries*))); | ||||
Michal Klocek
|
r223 | QObject::connect(m_dataset,SIGNAL(axisAdded(QChartAxis*)),this,SLOT(handleAxisAdded(QChartAxis*))); | ||
QObject::connect(m_dataset,SIGNAL(axisRemoved(QChartAxis*)),this,SLOT(handleAxisRemoved(QChartAxis*))); | ||||
Michal Klocek
|
r360 | QObject::connect(m_dataset,SIGNAL(seriesDomainChanged(QSeries*,const Domain&)),this,SLOT(handleSeriesDomainChanged(QSeries*,const Domain&))); | ||
Michal Klocek
|
r399 | QObject::connect(m_dataset,SIGNAL(axisRangeChanged(QChartAxis*,const QStringList&)),this,SLOT(handleAxisRangeChanged(QChartAxis*,const QStringList&))); | ||
Michal Klocek
|
r223 | } | ||
QRectF ChartPresenter::geometry() const | ||||
{ | ||||
return m_rect; | ||||
Michal Klocek
|
r131 | } | ||
void ChartPresenter::handleGeometryChanged() | ||||
{ | ||||
m_rect = QRectF(QPoint(0,0),m_chart->size()); | ||||
Marek Rosa
|
r419 | m_rect.adjust(m_marginSize,m_marginSize, -m_marginSize, -m_marginSize); | ||
Michal Klocek
|
r147 | Q_ASSERT(m_rect.isValid()); | ||
Michal Klocek
|
r139 | emit geometryChanged(m_rect); | ||
} | ||||
Michal Klocek
|
r131 | |||
Michal Klocek
|
r139 | int ChartPresenter::margin() const | ||
{ | ||||
return m_marginSize; | ||||
} | ||||
Michal Klocek
|
r131 | |||
Michal Klocek
|
r139 | void ChartPresenter::setMargin(int margin) | ||
{ | ||||
m_marginSize = margin; | ||||
Michal Klocek
|
r131 | } | ||
Michal Klocek
|
r223 | void ChartPresenter::handleAxisAdded(QChartAxis* axis) | ||
{ | ||||
Michal Klocek
|
r298 | |||
Michal Klocek
|
r223 | AxisItem* item ; | ||
Michal Klocek
|
r298 | if(!m_options.testFlag(QChart::GridAxisAnimations)) | ||
{ | ||||
item = new AxisItem(axis==m_dataset->axisX()?AxisItem::X_AXIS : AxisItem::Y_AXIS,m_chart); | ||||
Michal Klocek
|
r223 | }else{ | ||
Michal Klocek
|
r298 | item = new AxisAnimationItem(axis==m_dataset->axisX()?AxisItem::X_AXIS : AxisItem::Y_AXIS,m_chart); | ||
Michal Klocek
|
r223 | } | ||
Michal Klocek
|
r298 | |||
Michal Klocek
|
r223 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&))); | ||
QObject::connect(axis,SIGNAL(update(QChartAxis*)),item,SLOT(handleAxisUpdate(QChartAxis*))); | ||||
Michal Klocek
|
r259 | |||
item->handleAxisUpdate(axis); | ||||
item->handleGeometryChanged(m_rect); | ||||
Michal Klocek
|
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; | ||||
} | ||||
Michal Klocek
|
r360 | void ChartPresenter::handleSeriesAdded(QSeries* series) | ||
Michal Klocek
|
r131 | { | ||
switch(series->type()) | ||||
{ | ||||
Marek Rosa
|
r418 | case QSeries::SeriesTypeLine: { | ||
Marek Rosa
|
r426 | |||
Marek Rosa
|
r418 | QLineSeries* lineSeries = static_cast<QLineSeries*>(series); | ||
LineChartItem* item; | ||||
if(m_options.testFlag(QChart::SeriesAnimations)){ | ||||
item = new LineChartAnimationItem(this,lineSeries,m_chart); | ||||
}else{ | ||||
item = new LineChartItem(this,lineSeries,m_chart); | ||||
Michal Klocek
|
r139 | } | ||
Marek Rosa
|
r401 | m_chartTheme->decorate(item,lineSeries,m_chartItems.count()); | ||
m_chartItems.insert(series,item); | ||||
Marek Rosa
|
r418 | if(m_rect.isValid()) item->handleGeometryChanged(m_rect); | ||
Marek Rosa
|
r426 | break; | ||
} | ||||
Michal Klocek
|
r421 | |||
Marek Rosa
|
r426 | case QSeries::SeriesTypeArea: { | ||
Michal Klocek
|
r139 | |||
Marek Rosa
|
r426 | QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series); | ||
AreaChartItem* item; | ||||
if(m_options.testFlag(QChart::SeriesAnimations)) { | ||||
item = new AreaChartItem(this,areaSeries,m_chart); | ||||
} | ||||
else { | ||||
item = new AreaChartItem(this,areaSeries,m_chart); | ||||
Michal Klocek
|
r131 | } | ||
Marek Rosa
|
r426 | m_chartTheme->decorate(item,areaSeries,m_chartItems.count()); | ||
m_chartItems.insert(series,item); | ||||
if(m_rect.isValid()) item->handleGeometryChanged(m_rect); | ||||
Marek Rosa
|
r401 | break; | ||
} | ||||
Michal Klocek
|
r139 | |||
Marek Rosa
|
r418 | case QSeries::SeriesTypeBar: { | ||
QBarSeries* barSeries = static_cast<QBarSeries*>(series); | ||||
Marek Rosa
|
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&))); | ||||
Marek Rosa
|
r427 | // QObject::connect(barSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int))); | ||
Marek Rosa
|
r401 | m_chartItems.insert(series,item); | ||
// m_axisXItem->setVisible(false); | ||||
Marek Rosa
|
r418 | if(m_rect.isValid()) item->handleGeometryChanged(m_rect); | ||
Marek Rosa
|
r401 | break; | ||
} | ||||
Michal Klocek
|
r131 | |||
Marek Rosa
|
r418 | case QSeries::SeriesTypeStackedBar: { | ||
Michal Klocek
|
r139 | |||
Marek Rosa
|
r418 | QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series); | ||
Marek Rosa
|
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&))); | ||||
Marek Rosa
|
r427 | // QObject::connect(stackedBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int))); | ||
Marek Rosa
|
r401 | m_chartItems.insert(series,item); | ||
Marek Rosa
|
r418 | if(m_rect.isValid()) item->handleGeometryChanged(m_rect); | ||
Marek Rosa
|
r401 | break; | ||
} | ||||
Michal Klocek
|
r139 | |||
Marek Rosa
|
r418 | case QSeries::SeriesTypePercentBar: { | ||
Michal Klocek
|
r139 | |||
Marek Rosa
|
r418 | QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series); | ||
Marek Rosa
|
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&))); | ||||
Marek Rosa
|
r427 | // QObject::connect(percentBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int))); | ||
Marek Rosa
|
r401 | m_chartItems.insert(series,item); | ||
Marek Rosa
|
r418 | if(m_rect.isValid()) item->handleGeometryChanged(m_rect); | ||
Marek Rosa
|
r401 | break; | ||
} | ||||
Marek Rosa
|
r418 | case QSeries::SeriesTypeScatter: { | ||
Marek Rosa
|
r401 | QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series); | ||
ScatterPresenter *scatterPresenter = new ScatterPresenter(scatterSeries, m_chart); | ||||
Marek Rosa
|
r418 | QObject::connect(scatterPresenter, SIGNAL(clicked(QPointF)), | ||
scatterSeries, SIGNAL(clicked(QPointF))); | ||||
Marek Rosa
|
r401 | QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)), | ||
scatterPresenter, SLOT(handleGeometryChanged(const QRectF&))); | ||||
m_chartTheme->decorate(scatterPresenter, scatterSeries, m_chartItems.count()); | ||||
m_chartItems.insert(scatterSeries, scatterPresenter); | ||||
Marek Rosa
|
r418 | if(m_rect.isValid()) scatterPresenter->handleGeometryChanged(m_rect); | ||
Marek Rosa
|
r401 | break; | ||
} | ||||
Marek Rosa
|
r418 | case QSeries::SeriesTypePie: { | ||
Marek Rosa
|
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&))); | ||||
Marek Rosa
|
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); | ||||
Michal Klocek
|
r131 | } | ||
Marek Rosa
|
r418 | |||
Marek Rosa
|
r401 | m_chartItems.insert(series, pie); | ||
Marek Rosa
|
r418 | pie->handleGeometryChanged(m_rect); | ||
Marek Rosa
|
r401 | break; | ||
} | ||||
Marek Rosa
|
r427 | |||
Marek Rosa
|
r419 | case QSeries::SeriesTypeSpline: { | ||
Marek Rosa
|
r295 | QSplineSeries* splineSeries = qobject_cast<QSplineSeries*>(series); | ||
Marek Rosa
|
r401 | SplinePresenter* splinePresenter = new SplinePresenter(splineSeries, m_chart); | ||
QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)), splinePresenter, SLOT(handleGeometryChanged(const QRectF&))); | ||||
m_chartTheme->decorate(splinePresenter, splineSeries, m_chartItems.count()); | ||||
m_chartItems.insert(splineSeries, splinePresenter); | ||||
break; | ||||
} | ||||
default: { | ||||
qDebug()<< "Series type" << series->type() << "not implemented."; | ||||
Marek Rosa
|
r295 | break; | ||
} | ||||
Michal Klocek
|
r131 | } | ||
} | ||||
Michal Klocek
|
r360 | void ChartPresenter::handleSeriesRemoved(QSeries* series) | ||
Michal Klocek
|
r139 | { | ||
Marek Rosa
|
r401 | ChartItem* item = m_chartItems.take(series); | ||
delete item; | ||||
Michal Klocek
|
r139 | } | ||
Michal Klocek
|
r131 | |||
Michal Klocek
|
r360 | void ChartPresenter::handleSeriesDomainChanged(QSeries* series, const Domain& domain) | ||
Michal Klocek
|
r139 | { | ||
Michal Klocek
|
r223 | m_chartItems.value(series)->handleDomainChanged(domain); | ||
Michal Klocek
|
r131 | } | ||
Michal Klocek
|
r399 | void ChartPresenter::handleAxisRangeChanged(QChartAxis* axis,const QStringList& labels) | ||
Michal Klocek
|
r139 | { | ||
Michal Klocek
|
r399 | m_axisItems.value(axis)->handleRangeChanged(axis,labels); | ||
Michal Klocek
|
r139 | } | ||
Michal Klocek
|
r153 | void ChartPresenter::setChartTheme(QChart::ChartTheme theme) | ||
Michal Klocek
|
r143 | { | ||
delete m_chartTheme; | ||||
m_chartTheme = ChartTheme::createTheme(theme); | ||||
m_chartTheme->decorate(m_chart); | ||||
Michal Klocek
|
r360 | QMapIterator<QSeries*,ChartItem*> i(m_chartItems); | ||
Michal Klocek
|
r143 | |||
int index=0; | ||||
while (i.hasNext()) { | ||||
i.next(); | ||||
m_chartTheme->decorate(i.value(),i.key(),index); | ||||
Tero Ahola
|
r312 | index++; | ||
Michal Klocek
|
r143 | } | ||
Michal Klocek
|
r223 | QMapIterator<QChartAxis*,AxisItem*> j(m_axisItems); | ||
while (j.hasNext()) { | ||||
Marek Rosa
|
r401 | j.next(); | ||
m_chartTheme->decorate(j.key(),j.value()); | ||||
Michal Klocek
|
r223 | } | ||
Michal Klocek
|
r176 | } | ||
Michal Klocek
|
r153 | QChart::ChartTheme ChartPresenter::chartTheme() | ||
Michal Klocek
|
r143 | { | ||
Michal Klocek
|
r153 | return m_chartTheme->id(); | ||
} | ||||
Michal Klocek
|
r298 | void ChartPresenter::setAnimationOptions(QChart::AnimationOptions options) | ||
{ | ||||
if(m_options!=options) { | ||||
m_options=options; | ||||
//recreate elements | ||||
QList<QChartAxis*> axisList = m_axisItems.uniqueKeys(); | ||||
Michal Klocek
|
r360 | QList<QSeries*> seriesList = m_chartItems.uniqueKeys(); | ||
Michal Klocek
|
r298 | |||
foreach(QChartAxis* axis, axisList) { | ||||
handleAxisRemoved(axis); | ||||
handleAxisAdded(axis); | ||||
} | ||||
Michal Klocek
|
r360 | foreach(QSeries* series, seriesList) { | ||
Michal Klocek
|
r298 | handleSeriesRemoved(series); | ||
handleSeriesAdded(series); | ||||
} | ||||
//now reintialize view data | ||||
//TODO: make it more nice | ||||
m_dataset->setDomain(m_dataset->domainIndex()); | ||||
} | ||||
} | ||||
QChart::AnimationOptions ChartPresenter::animationOptions() const | ||||
{ | ||||
return m_options; | ||||
} | ||||
Michal Klocek
|
r143 | |||
Michal Klocek
|
r131 | #include "moc_chartpresenter_p.cpp" | ||
QTCOMMERCIALCHART_END_NAMESPACE | ||||