chartpresenter.cpp
223 lines
| 7.7 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
|
r216 | #include "qbarchartseries.h" | ||
#include "qstackedbarchartseries.h" | ||||
#include "qpercentbarchartseries.h" | ||||
Michal Klocek
|
r145 | #include "qlinechartseries.h" | ||
Jani Honkonen
|
r142 | #include "qpieseries.h" | ||
Tero Ahola
|
r158 | #include "qscatterseries.h" | ||
Michal Klocek
|
r139 | //items | ||
Michal Klocek
|
r140 | #include "axisitem_p.h" | ||
sauimone
|
r216 | #include "barpresenter.h" | ||
#include "stackedbarpresenter.h" | ||||
Michal Klocek
|
r145 | #include "linechartitem_p.h" | ||
sauimone
|
r216 | #include "percentbarpresenter.h" | ||
Michal Klocek
|
r131 | #include "linechartanimationitem_p.h" | ||
Jani Honkonen
|
r146 | #include "piepresenter.h" | ||
Tero Ahola
|
r194 | #include "scatterpresenter_p.h" | ||
Michal Klocek
|
r131 | |||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(chart), | ||||
m_chart(chart), | ||||
m_dataset(dataset), | ||||
Michal Klocek
|
r143 | m_chartTheme(0), | ||
Michal Klocek
|
r139 | m_marginSize(0), | ||
Michal Klocek
|
r131 | m_rect(QRectF(QPoint(0,0),m_chart->size())) | ||
{ | ||||
Michal Klocek
|
r223 | 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())); | ||||
QObject::connect(m_dataset,SIGNAL(seriesAdded(QChartSeries*)),this,SLOT(handleSeriesAdded(QChartSeries*))); | ||||
Michal Klocek
|
r223 | QObject::connect(m_dataset,SIGNAL(seriesRemoved(QChartSeries*)),this,SLOT(handleSeriesRemoved(QChartSeries*))); | ||
QObject::connect(m_dataset,SIGNAL(axisAdded(QChartAxis*)),this,SLOT(handleAxisAdded(QChartAxis*))); | ||||
QObject::connect(m_dataset,SIGNAL(axisRemoved(QChartAxis*)),this,SLOT(handleAxisRemoved(QChartAxis*))); | ||||
QObject::connect(m_dataset,SIGNAL(seriesDomainChanged(QChartSeries*,const Domain&)),this,SLOT(handleSeriesDomainChanged(QChartSeries*,const Domain&))); | ||||
QObject::connect(m_dataset,SIGNAL(axisLabelsChanged(QChartAxis*,const QStringList&)),this,SLOT(handleAxisLabelsChanged(QChartAxis*,const QStringList&))); | ||||
} | ||||
QRectF ChartPresenter::geometry() const | ||||
{ | ||||
return m_rect; | ||||
Michal Klocek
|
r131 | } | ||
void ChartPresenter::handleGeometryChanged() | ||||
{ | ||||
m_rect = QRectF(QPoint(0,0),m_chart->size()); | ||||
Michal Klocek
|
r139 | 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) | ||
{ | ||||
AxisItem* item ; | ||||
if(axis==m_dataset->axisX()){ | ||||
item = new AxisItem(AxisItem::X_AXIS,m_chart); | ||||
}else{ | ||||
item = new AxisItem(AxisItem::Y_AXIS,m_chart); | ||||
} | ||||
QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&))); | ||||
QObject::connect(axis,SIGNAL(update(QChartAxis*)),item,SLOT(handleAxisUpdate(QChartAxis*))); | ||||
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
|
r131 | void ChartPresenter::handleSeriesAdded(QChartSeries* series) | ||
{ | ||||
switch(series->type()) | ||||
{ | ||||
case QChartSeries::SeriesTypeLine: { | ||||
Michal Klocek
|
r145 | QLineChartSeries* lineSeries = static_cast<QLineChartSeries*>(series); | ||
LineChartItem* item = new LineChartAnimationItem(this,lineSeries,m_chart); | ||||
Michal Klocek
|
r143 | m_chartTheme->decorate(item,lineSeries,m_chartItems.count()); | ||
Michal Klocek
|
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; | ||||
} | ||||
case QChartSeries::SeriesTypeBar: { | ||||
sauimone
|
r216 | QBarChartSeries* barSeries = static_cast<QBarChartSeries*>(series); | ||
BarPresenter* item = new BarPresenter(barSeries->model(),m_chart); | ||||
Michal Klocek
|
r143 | m_chartTheme->decorate(item,barSeries,m_chartItems.count()); | ||
Michal Klocek
|
r139 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&))); | ||
QObject::connect(barSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int))); | ||||
Michal Klocek
|
r131 | m_chartItems.insert(series,item); | ||
Michal Klocek
|
r139 | // m_axisXItem->setVisible(false); | ||
Michal Klocek
|
r131 | break; | ||
} | ||||
Michal Klocek
|
r139 | case QChartSeries::SeriesTypeStackedBar: { | ||
sauimone
|
r216 | QStackedBarChartSeries* stackedBarSeries = static_cast<QStackedBarChartSeries*>(series); | ||
StackedBarPresenter* item = new StackedBarPresenter(stackedBarSeries->model(),m_chart); | ||||
Michal Klocek
|
r143 | m_chartTheme->decorate(item,stackedBarSeries,m_chartItems.count()); | ||
Michal Klocek
|
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; | ||||
} | ||||
case QChartSeries::SeriesTypePercentBar: { | ||||
sauimone
|
r216 | QPercentBarChartSeries* percentBarSeries = static_cast<QPercentBarChartSeries*>(series); | ||
PercentBarPresenter* item = new PercentBarPresenter(percentBarSeries->model(),m_chart); | ||||
Michal Klocek
|
r143 | m_chartTheme->decorate(item,percentBarSeries ,m_chartItems.count()); | ||
Michal Klocek
|
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; | ||||
Michal Klocek
|
r140 | } | ||
Tero Ahola
|
r158 | case QChartSeries::SeriesTypeScatter: { | ||
QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series); | ||||
ScatterPresenter *scatterPresenter = new ScatterPresenter(scatterSeries, m_chart); | ||||
QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)), | ||||
scatterPresenter, SLOT(handleGeometryChanged(const QRectF&))); | ||||
Tero Ahola
|
r182 | m_chartTheme->decorate(scatterPresenter, scatterSeries, m_chartItems.count()); | ||
Tero Ahola
|
r158 | m_chartItems.insert(scatterSeries, scatterPresenter); | ||
break; | ||||
} | ||||
Jani Honkonen
|
r142 | case QChartSeries::SeriesTypePie: { | ||
Jani Honkonen
|
r163 | QPieSeries *s = qobject_cast<QPieSeries *>(series); | ||
PiePresenter* pie = new PiePresenter(m_chart, s); | ||||
Jani Honkonen
|
r166 | m_chartTheme->decorate(pie, s, m_chartItems.count()); | ||
Jani Honkonen
|
r146 | QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)), pie, SLOT(handleGeometryChanged(const QRectF&))); | ||
m_chartItems.insert(series, pie); | ||||
Jani Honkonen
|
r142 | break; | ||
} | ||||
Michal Klocek
|
r131 | default: { | ||
Michal Klocek
|
r139 | qDebug()<< "Series type" << series->type() << "not implemented."; | ||
break; | ||||
Michal Klocek
|
r131 | } | ||
} | ||||
Michal Klocek
|
r148 | |||
if(m_rect.isValid()) emit geometryChanged(m_rect); | ||||
Michal Klocek
|
r131 | } | ||
Michal Klocek
|
r223 | void ChartPresenter::handleSeriesRemoved(QChartSeries* series) | ||
Michal Klocek
|
r139 | { | ||
Michal Klocek
|
r223 | ChartItem* item = m_chartItems.take(series); | ||
delete item; | ||||
Michal Klocek
|
r139 | } | ||
Michal Klocek
|
r131 | |||
Michal Klocek
|
r223 | void ChartPresenter::handleSeriesChanged(QChartSeries* series) | ||
Michal Klocek
|
r139 | { | ||
Michal Klocek
|
r223 | //TODO: | ||
Michal Klocek
|
r139 | } | ||
Michal Klocek
|
r131 | |||
Michal Klocek
|
r223 | void ChartPresenter::handleSeriesDomainChanged(QChartSeries* series, const Domain& domain) | ||
Michal Klocek
|
r139 | { | ||
Michal Klocek
|
r223 | m_chartItems.value(series)->handleDomainChanged(domain); | ||
Michal Klocek
|
r131 | } | ||
Michal Klocek
|
r223 | void ChartPresenter::handleAxisLabelsChanged(QChartAxis* axis,const QStringList& labels) | ||
Michal Klocek
|
r139 | { | ||
Michal Klocek
|
r241 | m_axisItems.value(axis)->handleLabelsChanged(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); | ||||
QMapIterator<QChartSeries*,ChartItem*> i(m_chartItems); | ||||
int index=0; | ||||
while (i.hasNext()) { | ||||
i.next(); | ||||
index++; | ||||
m_chartTheme->decorate(i.value(),i.key(),index); | ||||
} | ||||
Michal Klocek
|
r223 | QMapIterator<QChartAxis*,AxisItem*> j(m_axisItems); | ||
while (j.hasNext()) { | ||||
j.next(); | ||||
m_chartTheme->decorate(j.key(),j.value()); | ||||
} | ||||
Michal Klocek
|
r176 | } | ||
Michal Klocek
|
r153 | QChart::ChartTheme ChartPresenter::chartTheme() | ||
Michal Klocek
|
r143 | { | ||
Michal Klocek
|
r153 | return m_chartTheme->id(); | ||
} | ||||
Michal Klocek
|
r143 | |||
Michal Klocek
|
r131 | #include "moc_chartpresenter_p.cpp" | ||
QTCOMMERCIALCHART_END_NAMESPACE | ||||