chartpresenter.cpp
251 lines
| 8.3 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 | ||
#include "barchartseries.h" | ||||
#include "stackedbarchartseries.h" | ||||
#include "percentbarchartseries.h" | ||||
|
r131 | #include "qxychartseries.h" | ||
|
r142 | #include "qpieseries.h" | ||
|
r139 | //items | ||
|
r140 | #include "axisitem_p.h" | ||
|
r139 | #include "bargroup.h" | ||
#include "stackedbargroup.h" | ||||
|
r131 | #include "xylinechartitem_p.h" | ||
|
r139 | #include "percentbargroup.h" | ||
|
r131 | #include "linechartanimationitem_p.h" | ||
|
r142 | #include "piepresentation.h" | ||
|
r131 | |||
#include <QAbstractAnimation> | ||||
#include <QPropertyAnimation> | ||||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(chart), | ||||
m_chart(chart), | ||||
m_dataset(dataset), | ||||
|
r143 | m_chartTheme(0), | ||
|
r139 | m_domainIndex(0), | ||
m_marginSize(0), | ||||
|
r131 | m_rect(QRectF(QPoint(0,0),m_chart->size())) | ||
{ | ||||
|
r143 | setTheme(QChart::ChartThemeDefault); | ||
|
r140 | createConnections(); | ||
createDeafultAxis(); | ||||
|
r131 | } | ||
ChartPresenter::~ChartPresenter() | ||||
{ | ||||
} | ||||
|
r140 | void ChartPresenter::createDeafultAxis() | ||
{ | ||||
//default axis | ||||
QChartAxis* axisX = new QChartAxis(this); | ||||
QChartAxis* axisY = new QChartAxis(this); | ||||
m_axis << new AxisItem(axisX,AxisItem::X_AXIS,m_chart); | ||||
m_axis << new AxisItem(axisY,AxisItem::Y_AXIS,m_chart); | ||||
foreach(AxisItem* item, m_axis) { | ||||
QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&))); | ||||
QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&))); | ||||
} | ||||
} | ||||
void ChartPresenter::createConnections() | ||||
|
r131 | { | ||
QObject::connect(m_chart,SIGNAL(geometryChanged()),this,SLOT(handleGeometryChanged())); | ||||
QObject::connect(m_dataset,SIGNAL(seriesAdded(QChartSeries*)),this,SLOT(handleSeriesAdded(QChartSeries*))); | ||||
} | ||||
void ChartPresenter::handleGeometryChanged() | ||||
{ | ||||
m_rect = QRectF(QPoint(0,0),m_chart->size()); | ||||
|
r139 | m_rect.adjust(m_marginSize,m_marginSize, -m_marginSize, -m_marginSize); | ||
emit geometryChanged(m_rect); | ||||
} | ||||
|
r131 | |||
|
r139 | int ChartPresenter::margin() const | ||
{ | ||||
return m_marginSize; | ||||
} | ||||
|
r131 | |||
|
r139 | void ChartPresenter::setMargin(int margin) | ||
{ | ||||
m_marginSize = margin; | ||||
|
r131 | } | ||
void ChartPresenter::handleSeriesAdded(QChartSeries* series) | ||||
{ | ||||
switch(series->type()) | ||||
{ | ||||
case QChartSeries::SeriesTypeLine: { | ||||
|
r139 | QXYChartSeries* lineSeries = static_cast<QXYChartSeries*>(series); | ||
XYLineChartItem* item = new LineChartAnimationItem(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(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&))); | ||||
QObject::connect(lineSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int))); | ||||
m_chartItems.insert(series,item); | ||||
break; | ||||
} | ||||
case QChartSeries::SeriesTypeBar: { | ||||
BarChartSeries* barSeries = static_cast<BarChartSeries*>(series); | ||||
BarGroup* item = new BarGroup(*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(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&))); | ||||
QObject::connect(barSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int))); | ||||
|
r131 | m_chartItems.insert(series,item); | ||
|
r139 | // m_axisXItem->setVisible(false); | ||
|
r131 | break; | ||
} | ||||
|
r139 | case QChartSeries::SeriesTypeStackedBar: { | ||
StackedBarChartSeries* stackedBarSeries = static_cast<StackedBarChartSeries*>(series); | ||||
StackedBarGroup* item = new StackedBarGroup(*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(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&))); | ||||
QObject::connect(stackedBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int))); | ||||
m_chartItems.insert(series,item); | ||||
break; | ||||
} | ||||
case QChartSeries::SeriesTypePercentBar: { | ||||
PercentBarChartSeries* percentBarSeries = static_cast<PercentBarChartSeries*>(series); | ||||
PercentBarGroup* item = new PercentBarGroup(*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(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&))); | ||||
QObject::connect(percentBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int))); | ||||
m_chartItems.insert(series,item); | ||||
break; | ||||
|
r140 | } | ||
|
r139 | /* | ||
case QChartSeries::SeriesTypeScatter: { | ||||
|
r140 | QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series); | ||
scatterSeries->d->m_theme = m_chartTheme->themeForSeries(); | ||||
scatterSeries->d->setParentItem(this); | ||||
scatterSeries->d->m_boundingRect = m_rect.adjusted(margin(),margin(), -margin(), -margin()); | ||||
m_chartItems << scatterSeries->d; | ||||
m_chartTheme->addObserver(scatterSeries->d); | ||||
|
r139 | |||
foreach (qreal x, scatterSeries->d->m_x) { | ||||
domain.m_minX = qMin(domain.m_minX, x); | ||||
domain.m_maxX = qMax(domain.m_maxX, x); | ||||
} | ||||
foreach (qreal y, scatterSeries->d->m_y) { | ||||
domain.m_minY = qMin(domain.m_minY, y); | ||||
domain.m_maxY = qMax(domain.m_maxY, y); | ||||
} | ||||
break; | ||||
} | ||||
*/ | ||||
|
r142 | case QChartSeries::SeriesTypePie: { | ||
QPieSeries *pieSeries = qobject_cast<QPieSeries *>(series); | ||||
PiePresentation* pieChart = new PiePresentation(m_chart, pieSeries); | ||||
pieSeries->m_piePresentation = pieChart; // TODO: remove this pointer passing use signals&slots | ||||
QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)), pieChart, SLOT(handleGeometryChanged(const QRectF&))); | ||||
QObject::connect(m_dataset, SIGNAL(domainChanged(const Domain&)), pieChart, SLOT(handleDomainChanged(const Domain&))); | ||||
m_chartItems.insert(series, pieChart); | ||||
break; | ||||
} | ||||
|
r131 | default: { | ||
|
r139 | qDebug()<< "Series type" << series->type() << "not implemented."; | ||
break; | ||||
|
r131 | } | ||
} | ||||
} | ||||
void ChartPresenter::handleSeriesChanged(QChartSeries* series) | ||||
{ | ||||
|
r140 | //TODO: | ||
|
r139 | } | ||
void ChartPresenter::zoomInToRect(const QRectF& rect) | ||||
{ | ||||
if(!rect.isValid()) return; | ||||
QRectF r = rect.normalized(); | ||||
r.translate(-m_marginSize, -m_marginSize); | ||||
Domain domain (m_dataset->domain().subDomain(rect,m_rect.width(),m_rect.height())); | ||||
m_dataset->addDomain(domain); | ||||
} | ||||
|
r131 | |||
|
r139 | void ChartPresenter::zoomIn() | ||
{ | ||||
if (!m_dataset->nextDomain()) { | ||||
QRectF rect = m_rect; | ||||
rect.setWidth(rect.width()/2); | ||||
rect.setHeight(rect.height()/2); | ||||
rect.moveCenter(m_rect.center()); | ||||
Domain domain (m_dataset->domain().subDomain(rect,m_rect.width(),m_rect.height())); | ||||
m_dataset->addDomain(domain); | ||||
} | ||||
} | ||||
|
r131 | |||
|
r139 | void ChartPresenter::zoomOut() | ||
{ | ||||
m_dataset->previousDomain(); | ||||
|
r131 | } | ||
|
r139 | void ChartPresenter::zoomReset() | ||
{ | ||||
m_dataset->clearDomains(); | ||||
} | ||||
|
r143 | void ChartPresenter::setTheme(QChart::ChartThemeId theme) | ||
{ | ||||
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); | ||||
} | ||||
} | ||||
QChart::ChartThemeId ChartPresenter::theme() | ||||
{ | ||||
return (QChart::ChartThemeId) 0; | ||||
} | ||||
|
r139 | /* | ||
|
r140 | void ChartPresenter::setAxisX(const QChartAxis& axis) | ||
{ | ||||
setAxis(m_axisXItem,axis); | ||||
} | ||||
void ChartPresenter::setAxisY(const QChartAxis& axis) | ||||
{ | ||||
setAxis(m_axisYItem.at(0),axis); | ||||
} | ||||
void ChartPresenter::setAxisY(const QList<QChartAxis>& axis) | ||||
{ | ||||
//TODO not implemented | ||||
} | ||||
void ChartPresenter::setAxis(AxisItem *item, const QChartAxis& axis) | ||||
{ | ||||
item->setVisible(axis.isAxisVisible()); | ||||
} | ||||
*/ | ||||
|
r131 | #include "moc_chartpresenter_p.cpp" | ||
QTCOMMERCIALCHART_END_NAMESPACE | ||||