chartdataset.cpp
290 lines
| 7.9 KiB
| text/x-c
|
CppLexer
/ src / chartdataset.cpp
|
r771 | /**************************************************************************** | ||
** | ||||
** Copyright (C) 2012 Digia Plc | ||||
** All rights reserved. | ||||
** For any questions to Digia, please use contact form at http://qt.digia.com | ||||
** | ||||
** This file is part of the Qt Commercial Charts Add-on. | ||||
** | ||||
** $QT_BEGIN_LICENSE$ | ||||
** Licensees holding valid Qt Commercial licenses may use this file in | ||||
** accordance with the Qt Commercial License Agreement provided with the | ||||
** Software or, alternatively, in accordance with the terms contained in | ||||
** a written agreement between you and Digia. | ||||
** | ||||
** If you have questions regarding the use of this file, please use | ||||
** contact form at http://qt.digia.com | ||||
** $QT_END_LICENSE$ | ||||
** | ||||
****************************************************************************/ | ||||
|
r131 | #include "chartdataset_p.h" | ||
|
r1006 | #include "qaxis.h" | ||
#include "qaxis_p.h" | ||||
|
r988 | #include "qabstractseries_p.h" | ||
|
r338 | #include "qbarseries.h" | ||
#include "qstackedbarseries.h" | ||||
#include "qpercentbarseries.h" | ||||
|
r146 | #include "qpieseries.h" | ||
|
r131 | |||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
|
r223 | ChartDataSet::ChartDataSet(QObject *parent):QObject(parent), | ||
|
r1006 | m_axisX(new QAxis(this)), | ||
m_axisY(new QAxis(this)), | ||||
|
r401 | m_domainIndex(0), | ||
|
r1078 | m_axisXInitialized(false), | ||
m_axisYInitialized(false) | ||||
|
r131 | { | ||
|
r1078 | //create main domain | ||
Domain* domain = new Domain(m_axisY); | ||||
m_axisDomainMap.insert(m_axisY,domain); | ||||
QObject::connect(m_axisY->d_ptr.data(),SIGNAL(changed(qreal,qreal,int,bool)),domain,SLOT(handleAxisYChanged(qreal,qreal,int,bool))); | ||||
QObject::connect(m_axisX->d_ptr.data(),SIGNAL(changed(qreal,qreal,int,bool)),domain,SLOT(handleAxisXChanged(qreal,qreal,int,bool))); | ||||
QObject::connect(domain,SIGNAL(rangeYChanged(qreal,qreal,int)),m_axisY->d_ptr.data(),SLOT(handleAxisRangeChanged(qreal,qreal,int))); | ||||
QObject::connect(domain,SIGNAL(rangeXChanged(qreal,qreal,int)),m_axisX->d_ptr.data(),SLOT(handleAxisRangeChanged(qreal,qreal,int))); | ||||
|
r131 | } | ||
ChartDataSet::~ChartDataSet() | ||||
{ | ||||
|
r1062 | removeAllSeries(); | ||
|
r131 | } | ||
|
r1006 | void ChartDataSet::addSeries(QAbstractSeries* series, QAxis *axisY) | ||
|
r131 | { | ||
|
r439 | if(axisY==0) axisY = m_axisY; | ||
|
r139 | |||
|
r1006 | QAxis* axis = m_seriesAxisMap.value(series); | ||
|
r910 | |||
if(axis) { | ||||
|
r439 | qWarning() << "Can not add series. Series already on the chart"; | ||
return; | ||||
} | ||||
|
r223 | |||
|
r974 | series->setParent(this); // take ownership | ||
axisY->setParent(this); // take ownership | ||||
|
r223 | |||
|
r439 | Domain* domain = m_axisDomainMap.value(axisY); | ||
|
r910 | |||
if(!domain) { | ||||
|
r787 | domain = new Domain(axisY); | ||
|
r963 | QObject::connect(axisY->d_ptr.data(),SIGNAL(changed(qreal,qreal,int,bool)),domain,SLOT(handleAxisYChanged(qreal,qreal,int,bool))); | ||
|
r1078 | QObject::connect(axisX()->d_ptr.data(),SIGNAL(changed(qreal,qreal,int,bool)),domain,SLOT(handleAxisXChanged(qreal,qreal,int))); | ||
|
r963 | QObject::connect(domain,SIGNAL(rangeYChanged(qreal,qreal,int)),axisY->d_ptr.data(),SLOT(handleAxisRangeChanged(qreal,qreal,int))); | ||
|
r442 | //initialize | ||
|
r439 | m_axisDomainMap.insert(axisY,domain); | ||
emit axisAdded(axisY,domain); | ||||
|
r401 | } | ||
|
r421 | |||
|
r910 | if(!m_axisXInitialized){ | ||
m_axisXInitialized=true; | ||||
|
r1078 | emit axisAdded(m_axisX,domain); | ||
} | ||||
if(!m_axisYInitialized && axisY==m_axisY){ | ||||
m_axisYInitialized=true; | ||||
emit axisAdded(m_axisY,domain); | ||||
|
r439 | } | ||
|
r421 | |||
|
r943 | series->d_ptr->scaleDomain(*domain); | ||
|
r1167 | if(series->type() == QAbstractSeries::SeriesTypeGroupedBar | ||
|
r988 | || series->type() == QAbstractSeries::SeriesTypeStackedBar | ||
|| series->type() == QAbstractSeries::SeriesTypePercentBar) { | ||||
|
r943 | QBarSeries* barSeries = static_cast<QBarSeries*>(series); | ||
setupCategories(barSeries); | ||||
} | ||||
|
r988 | if (series->type()== QAbstractSeries::SeriesTypePie && m_seriesAxisMap.count() == 0) { | ||
|
r943 | axisX()->hide(); | ||
this->axisY()->hide(); | ||||
} | ||||
|
r421 | |||
|
r910 | m_seriesAxisMap.insert(series,axisY); | ||
|
r988 | QMapIterator<int, QAbstractSeries*> i(m_indexSeriesMap); | ||
|
r910 | |||
int key=0; | ||||
while (i.hasNext()) { | ||||
i.next(); | ||||
if(i.key()!=key) { | ||||
break; | ||||
} | ||||
key++; | ||||
} | ||||
m_indexSeriesMap.insert(key,series); | ||||
|
r1062 | series->d_ptr->m_dataset=this; | ||
|
r910 | emit seriesAdded(series,domain); | ||
|
r139 | |||
|
r439 | } | ||
|
r142 | |||
|
r1006 | QAxis* ChartDataSet::removeSeries(QAbstractSeries* series) | ||
|
r439 | { | ||
|
r1006 | QAxis* axis = m_seriesAxisMap.value(series); | ||
|
r910 | |||
if(!axis){ | ||||
qWarning()<<"Can not remove series. Series not found on the chart."; | ||||
|
r974 | return 0; | ||
|
r401 | } | ||
|
r974 | |||
|
r910 | emit seriesRemoved(series); | ||
|
r974 | |||
|
r910 | m_seriesAxisMap.remove(series); | ||
int key = seriesIndex(series); | ||||
Q_ASSERT(key!=-1); | ||||
|
r139 | |||
|
r910 | m_indexSeriesMap.remove(key); | ||
|
r974 | series->setParent(0); | ||
|
r1062 | series->d_ptr->m_dataset=0; | ||
|
r139 | |||
|
r1006 | QList<QAxis*> axes = m_seriesAxisMap.values(); | ||
|
r910 | |||
int i = axes.indexOf(axis); | ||||
|
r223 | |||
|
r910 | if(i==-1){ | ||
|
r439 | Domain* domain = m_axisDomainMap.take(axis); | ||
emit axisRemoved(axis); | ||||
|
r1078 | if(axis!=m_axisY){ | ||
|
r974 | axis->setParent(0); | ||
|
r1078 | delete domain; | ||
}else{ | ||||
m_axisYInitialized=false; | ||||
m_axisDomainMap.insert(m_axisY,domain); | ||||
|
r444 | } | ||
|
r439 | } | ||
|
r223 | |||
|
r910 | if(m_seriesAxisMap.values().size()==0) | ||
{ | ||||
m_axisXInitialized=false; | ||||
|
r439 | emit axisRemoved(axisX()); | ||
|
r223 | } | ||
|
r974 | |||
return axis; | ||||
|
r139 | } | ||
|
r258 | void ChartDataSet::removeAllSeries() | ||
{ | ||||
|
r988 | QList<QAbstractSeries*> series = m_seriesAxisMap.keys(); | ||
|
r1006 | QList<QAxis*> axes; | ||
|
r988 | foreach(QAbstractSeries *s , series) { | ||
|
r1006 | QAxis* axis = removeSeries(s); | ||
|
r974 | if(axis==axisY()) continue; | ||
int i = axes.indexOf(axis); | ||||
if(i==-1){ | ||||
axes<<axis; | ||||
} | ||||
|
r139 | } | ||
|
r910 | Q_ASSERT(m_seriesAxisMap.count()==0); | ||
|
r1078 | Q_ASSERT(m_axisDomainMap.count()==1); | ||
|
r910 | |||
|
r974 | qDeleteAll(series); | ||
qDeleteAll(axes); | ||||
|
r139 | } | ||
|
r701 | void ChartDataSet::setupCategories(QBarSeries* series) | ||
{ | ||||
|
r1032 | QAxisCategories* categories = axisX()->categories(); | ||
|
r701 | categories->clear(); | ||
|
r703 | categories->insert(series->categories()); | ||
|
r139 | } | ||
|
r439 | void ChartDataSet::zoomInDomain(const QRectF& rect, const QSizeF& size) | ||
|
r139 | { | ||
|
r1006 | QMapIterator<QAxis*, Domain*> i(m_axisDomainMap); | ||
|
r1078 | //main domain has to be the last one; | ||
Domain *domain = m_axisDomainMap.value(axisY()); | ||||
Q_ASSERT(domain); | ||||
|
r439 | while (i.hasNext()) { | ||
i.next(); | ||||
|
r1078 | if(i.value()==domain) continue; | ||
|
r439 | i.value()->zoomIn(rect,size); | ||
|
r223 | } | ||
|
r1078 | domain->zoomIn(rect,size); | ||
|
r439 | } | ||
|
r223 | |||
|
r439 | void ChartDataSet::zoomOutDomain(const QRectF& rect, const QSizeF& size) | ||
{ | ||||
|
r1006 | QMapIterator<QAxis*, Domain*> i(m_axisDomainMap); | ||
|
r1078 | //main domain has to be the last one; | ||
Domain *domain = m_axisDomainMap.value(axisY()); | ||||
Q_ASSERT(domain); | ||||
|
r439 | while (i.hasNext()) { | ||
i.next(); | ||||
|
r1078 | if(i.value()==domain) continue; | ||
|
r439 | i.value()->zoomOut(rect,size); | ||
} | ||||
|
r1078 | domain->zoomOut(rect,size); | ||
|
r223 | } | ||
|
r1107 | int ChartDataSet::seriesCount(QAbstractSeries::SeriesType type) | ||
|
r910 | { | ||
int count=0; | ||||
|
r1006 | QMapIterator<QAbstractSeries*, QAxis*> i(m_seriesAxisMap); | ||
|
r910 | while (i.hasNext()) { | ||
i.next(); | ||||
if(i.key()->type()==type) count++; | ||||
} | ||||
return count; | ||||
} | ||||
|
r988 | int ChartDataSet::seriesIndex(QAbstractSeries *series) | ||
|
r538 | { | ||
|
r988 | QMapIterator<int, QAbstractSeries*> i(m_indexSeriesMap); | ||
|
r910 | while (i.hasNext()) { | ||
i.next(); | ||||
if (i.value() == series) | ||||
return i.key(); | ||||
|
r538 | } | ||
|
r825 | return -1; | ||
|
r538 | } | ||
|
r1006 | QAxis* ChartDataSet::axisY(QAbstractSeries *series) const | ||
|
r223 | { | ||
|
r910 | if(series == 0) return m_axisY; | ||
return m_seriesAxisMap.value(series); | ||||
|
r223 | } | ||
|
r988 | Domain* ChartDataSet::domain(QAbstractSeries *series) const | ||
|
r223 | { | ||
|
r1006 | QAxis* axis = m_seriesAxisMap.value(series); | ||
|
r910 | if(axis){ | ||
|
r439 | return m_axisDomainMap.value(axis); | ||
|
r910 | }else | ||
return 0; | ||||
|
r223 | } | ||
|
r1006 | Domain* ChartDataSet::domain(QAxis* axis) const | ||
|
r223 | { | ||
|
r771 | if(!axis || axis==axisX()) { | ||
|
r444 | return m_axisDomainMap.value(axisY()); | ||
} | ||||
else { | ||||
return m_axisDomainMap.value(axis); | ||||
} | ||||
|
r223 | } | ||
|
r531 | void ChartDataSet::scrollDomain(int dx,int dy,const QSizeF& size) | ||
{ | ||||
|
r1078 | QMapIterator<QAxis*, Domain*> i( m_axisDomainMap); | ||
//main domain has to be the last one; | ||||
Domain *domain = m_axisDomainMap.value(axisY()); | ||||
while (i.hasNext()) { | ||||
i.next(); | ||||
if(i.value()==domain) continue; | ||||
i.value()->move(dx,dy,size); | ||||
} | ||||
domain->move(dx,dy,size); | ||||
} | ||||
QList<QAbstractSeries*> ChartDataSet::series() const | ||||
{ | ||||
return m_seriesAxisMap.keys(); | ||||
|
r531 | } | ||
|
r131 | #include "moc_chartdataset_p.cpp" | ||
QTCOMMERCIALCHART_END_NAMESPACE | ||||