chartpresenter.cpp
283 lines
| 9.6 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" | ||
|
r142 | #include "qpieseries.h" | ||
|
r158 | #include "qscatterseries.h" | ||
|
r139 | //items | ||
|
r140 | #include "axisitem_p.h" | ||
|
r298 | #include "axisanimationitem_p.h" | ||
|
r216 | #include "barpresenter.h" | ||
#include "stackedbarpresenter.h" | ||||
|
r145 | #include "linechartitem_p.h" | ||
|
r216 | #include "percentbarpresenter.h" | ||
|
r131 | #include "linechartanimationitem_p.h" | ||
|
r353 | #include "piepresenter_p.h" | ||
|
r194 | #include "scatterpresenter_p.h" | ||
|
r131 | |||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(chart), | ||||
m_chart(chart), | ||||
m_dataset(dataset), | ||||
|
r143 | m_chartTheme(0), | ||
|
r139 | m_marginSize(0), | ||
|
r298 | m_rect(QRectF(QPoint(0,0),m_chart->size())), | ||
m_options(0) | ||||
|
r131 | { | ||
|
r223 | createConnections(); | ||
|
r153 | setChartTheme(QChart::ChartThemeDefault); | ||
|
r223 | |||
|
r131 | } | ||
ChartPresenter::~ChartPresenter() | ||||
{ | ||||
} | ||||
|
r140 | void ChartPresenter::createConnections() | ||
|
r131 | { | ||
QObject::connect(m_chart,SIGNAL(geometryChanged()),this,SLOT(handleGeometryChanged())); | ||||
|
r360 | QObject::connect(m_dataset,SIGNAL(seriesAdded(QSeries*)),this,SLOT(handleSeriesAdded(QSeries*))); | ||
QObject::connect(m_dataset,SIGNAL(seriesRemoved(QSeries*)),this,SLOT(handleSeriesRemoved(QSeries*))); | ||||
|
r223 | QObject::connect(m_dataset,SIGNAL(axisAdded(QChartAxis*)),this,SLOT(handleAxisAdded(QChartAxis*))); | ||
QObject::connect(m_dataset,SIGNAL(axisRemoved(QChartAxis*)),this,SLOT(handleAxisRemoved(QChartAxis*))); | ||||
|
r360 | QObject::connect(m_dataset,SIGNAL(seriesDomainChanged(QSeries*,const Domain&)),this,SLOT(handleSeriesDomainChanged(QSeries*,const Domain&))); | ||
|
r223 | QObject::connect(m_dataset,SIGNAL(axisLabelsChanged(QChartAxis*,const QStringList&)),this,SLOT(handleAxisLabelsChanged(QChartAxis*,const QStringList&))); | ||
} | ||||
QRectF ChartPresenter::geometry() const | ||||
{ | ||||
return m_rect; | ||||
|
r131 | } | ||
void ChartPresenter::handleGeometryChanged() | ||||
{ | ||||
m_rect = QRectF(QPoint(0,0),m_chart->size()); | ||||
|
r139 | m_rect.adjust(m_marginSize,m_marginSize, -m_marginSize, -m_marginSize); | ||
|
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 | } | ||
|
r223 | void ChartPresenter::handleAxisAdded(QChartAxis* axis) | ||
{ | ||||
|
r298 | |||
|
r223 | AxisItem* item ; | ||
|
r298 | if(!m_options.testFlag(QChart::GridAxisAnimations)) | ||
{ | ||||
item = new AxisItem(axis==m_dataset->axisX()?AxisItem::X_AXIS : AxisItem::Y_AXIS,m_chart); | ||||
|
r223 | }else{ | ||
|
r298 | item = new AxisAnimationItem(axis==m_dataset->axisX()?AxisItem::X_AXIS : AxisItem::Y_AXIS,m_chart); | ||
|
r223 | } | ||
|
r298 | |||
|
r223 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&))); | ||
QObject::connect(axis,SIGNAL(update(QChartAxis*)),item,SLOT(handleAxisUpdate(QChartAxis*))); | ||||
|
r259 | |||
item->handleAxisUpdate(axis); | ||||
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; | ||||
} | ||||
|
r360 | void ChartPresenter::handleSeriesAdded(QSeries* series) | ||
|
r131 | { | ||
switch(series->type()) | ||||
{ | ||||
|
r360 | case QSeries::SeriesTypeLine: { | ||
|
r349 | QLineSeries* lineSeries = static_cast<QLineSeries*>(series); | ||
|
r298 | LineChartItem* item; | ||
if(m_options.testFlag(QChart::SeriesAnimations)){ | ||||
item = new LineChartAnimationItem(this,lineSeries,m_chart); | ||||
}else{ | ||||
item = new LineChartItem(this,lineSeries,m_chart); | ||||
} | ||||
|
r143 | m_chartTheme->decorate(item,lineSeries,m_chartItems.count()); | ||
|
r139 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&))); | ||
QObject::connect(lineSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int))); | ||||
m_chartItems.insert(series,item); | ||||
break; | ||||
} | ||||
|
r360 | case QSeries::SeriesTypeBar: { | ||
|
r338 | QBarSeries* barSeries = static_cast<QBarSeries*>(series); | ||
|
r288 | BarPresenter* item = new BarPresenter(barSeries,m_chart); | ||
|
r143 | m_chartTheme->decorate(item,barSeries,m_chartItems.count()); | ||
|
r139 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&))); | ||
QObject::connect(barSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int))); | ||||
|
r131 | m_chartItems.insert(series,item); | ||
|
r139 | // m_axisXItem->setVisible(false); | ||
|
r131 | break; | ||
} | ||||
|
r360 | case QSeries::SeriesTypeStackedBar: { | ||
|
r139 | |||
|
r338 | QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series); | ||
|
r288 | StackedBarPresenter* item = new StackedBarPresenter(stackedBarSeries,m_chart); | ||
|
r143 | m_chartTheme->decorate(item,stackedBarSeries,m_chartItems.count()); | ||
|
r139 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&))); | ||
QObject::connect(stackedBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int))); | ||||
m_chartItems.insert(series,item); | ||||
break; | ||||
} | ||||
|
r360 | case QSeries::SeriesTypePercentBar: { | ||
|
r139 | |||
|
r338 | QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series); | ||
|
r288 | PercentBarPresenter* item = new PercentBarPresenter(percentBarSeries,m_chart); | ||
|
r143 | m_chartTheme->decorate(item,percentBarSeries ,m_chartItems.count()); | ||
|
r139 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&))); | ||
QObject::connect(percentBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int))); | ||||
m_chartItems.insert(series,item); | ||||
break; | ||||
|
r140 | } | ||
|
r360 | case QSeries::SeriesTypeScatter: { | ||
|
r158 | QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series); | ||
ScatterPresenter *scatterPresenter = new ScatterPresenter(scatterSeries, m_chart); | ||||
|
r316 | QObject::connect(scatterPresenter, SIGNAL(clicked()), scatterSeries, SIGNAL(clicked())); | ||
|
r158 | QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)), | ||
scatterPresenter, SLOT(handleGeometryChanged(const QRectF&))); | ||||
|
r182 | m_chartTheme->decorate(scatterPresenter, scatterSeries, m_chartItems.count()); | ||
|
r158 | m_chartItems.insert(scatterSeries, scatterPresenter); | ||
break; | ||||
} | ||||
|
r360 | case QSeries::SeriesTypePie: { | ||
|
r163 | QPieSeries *s = qobject_cast<QPieSeries *>(series); | ||
PiePresenter* pie = new PiePresenter(m_chart, s); | ||||
|
r166 | m_chartTheme->decorate(pie, s, m_chartItems.count()); | ||
|
r146 | QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)), pie, SLOT(handleGeometryChanged(const QRectF&))); | ||
|
r293 | |||
// 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); | ||||
} | ||||
|
r146 | m_chartItems.insert(series, pie); | ||
|
r142 | break; | ||
} | ||||
|
r131 | default: { | ||
|
r139 | qDebug()<< "Series type" << series->type() << "not implemented."; | ||
break; | ||||
|
r131 | } | ||
} | ||||
|
r148 | |||
if(m_rect.isValid()) emit geometryChanged(m_rect); | ||||
|
r131 | } | ||
|
r360 | void ChartPresenter::handleSeriesRemoved(QSeries* series) | ||
|
r139 | { | ||
|
r223 | ChartItem* item = m_chartItems.take(series); | ||
delete item; | ||||
|
r139 | } | ||
|
r131 | |||
|
r360 | void ChartPresenter::handleSeriesChanged(QSeries* series) | ||
|
r139 | { | ||
|
r223 | //TODO: | ||
|
r139 | } | ||
|
r131 | |||
|
r360 | void ChartPresenter::handleSeriesDomainChanged(QSeries* series, const Domain& domain) | ||
|
r139 | { | ||
|
r223 | m_chartItems.value(series)->handleDomainChanged(domain); | ||
|
r131 | } | ||
|
r223 | void ChartPresenter::handleAxisLabelsChanged(QChartAxis* axis,const QStringList& labels) | ||
|
r139 | { | ||
|
r241 | m_axisItems.value(axis)->handleLabelsChanged(axis,labels); | ||
|
r139 | } | ||
|
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()) { | ||||
j.next(); | ||||
m_chartTheme->decorate(j.key(),j.value()); | ||||
} | ||||
|
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); | ||||
handleAxisAdded(axis); | ||||
} | ||||
|
r360 | foreach(QSeries* series, seriesList) { | ||
|
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; | ||||
} | ||||
|
r143 | |||
|
r131 | #include "moc_chartpresenter_p.cpp" | ||
QTCOMMERCIALCHART_END_NAMESPACE | ||||