chartdataset.cpp
286 lines
| 7.8 KiB
| text/x-c
|
CppLexer
/ src / chartdataset.cpp
Michal Klocek
|
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$ | ||||
** | ||||
****************************************************************************/ | ||||
Michal Klocek
|
r131 | #include "chartdataset_p.h" | ||
Jani Honkonen
|
r1314 | #include "qchart.h" | ||
Michal Klocek
|
r1541 | #include "qvaluesaxis.h" | ||
#include "qvaluesaxis_p.h" | ||||
Tero Ahola
|
r988 | #include "qabstractseries_p.h" | ||
sauimone
|
r338 | #include "qbarseries.h" | ||
#include "qstackedbarseries.h" | ||||
#include "qpercentbarseries.h" | ||||
Jani Honkonen
|
r146 | #include "qpieseries.h" | ||
Michal Klocek
|
r131 | |||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
Jani Honkonen
|
r1314 | ChartDataSet::ChartDataSet(QChart *parent):QObject(parent), | ||
Michal Klocek
|
r1541 | m_axisX(new QValuesAxis(this)), | ||
m_axisY(new QValuesAxis(this)), | ||||
Marek Rosa
|
r401 | m_domainIndex(0), | ||
Michal Klocek
|
r1078 | m_axisXInitialized(false), | ||
m_axisYInitialized(false) | ||||
Michal Klocek
|
r131 | { | ||
Michal Klocek
|
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))); | ||||
Michal Klocek
|
r131 | } | ||
ChartDataSet::~ChartDataSet() | ||||
{ | ||||
Michal Klocek
|
r1062 | removeAllSeries(); | ||
Michal Klocek
|
r131 | } | ||
Michal Klocek
|
r1541 | void ChartDataSet::addSeries(QAbstractSeries* series) | ||
Michal Klocek
|
r131 | { | ||
Michal Klocek
|
r1541 | //TODO: if(axisY==0) axisY = m_axisY; | ||
QAbstractAxis* axisY = m_axisY; | ||||
Michal Klocek
|
r139 | |||
Michal Klocek
|
r1541 | QAbstractAxis* axis = m_seriesAxisMap.value(series); | ||
Michal Klocek
|
r910 | |||
if(axis) { | ||||
Michal Klocek
|
r439 | qWarning() << "Can not add series. Series already on the chart"; | ||
return; | ||||
} | ||||
Michal Klocek
|
r223 | |||
Michal Klocek
|
r974 | series->setParent(this); // take ownership | ||
axisY->setParent(this); // take ownership | ||||
Michal Klocek
|
r223 | |||
Michal Klocek
|
r439 | Domain* domain = m_axisDomainMap.value(axisY); | ||
Michal Klocek
|
r910 | |||
if(!domain) { | ||||
Michal Klocek
|
r787 | domain = new Domain(axisY); | ||
Michal Klocek
|
r963 | QObject::connect(axisY->d_ptr.data(),SIGNAL(changed(qreal,qreal,int,bool)),domain,SLOT(handleAxisYChanged(qreal,qreal,int,bool))); | ||
Michal Klocek
|
r1078 | QObject::connect(axisX()->d_ptr.data(),SIGNAL(changed(qreal,qreal,int,bool)),domain,SLOT(handleAxisXChanged(qreal,qreal,int))); | ||
Michal Klocek
|
r963 | QObject::connect(domain,SIGNAL(rangeYChanged(qreal,qreal,int)),axisY->d_ptr.data(),SLOT(handleAxisRangeChanged(qreal,qreal,int))); | ||
Michal Klocek
|
r442 | //initialize | ||
Michal Klocek
|
r439 | m_axisDomainMap.insert(axisY,domain); | ||
emit axisAdded(axisY,domain); | ||||
Marek Rosa
|
r401 | } | ||
Michal Klocek
|
r421 | |||
Michal Klocek
|
r910 | if(!m_axisXInitialized){ | ||
m_axisXInitialized=true; | ||||
Michal Klocek
|
r1078 | emit axisAdded(m_axisX,domain); | ||
} | ||||
if(!m_axisYInitialized && axisY==m_axisY){ | ||||
m_axisYInitialized=true; | ||||
emit axisAdded(m_axisY,domain); | ||||
Michal Klocek
|
r439 | } | ||
Michal Klocek
|
r421 | |||
Michal Klocek
|
r943 | series->d_ptr->scaleDomain(*domain); | ||
Tero Ahola
|
r988 | if (series->type()== QAbstractSeries::SeriesTypePie && m_seriesAxisMap.count() == 0) { | ||
Michal Klocek
|
r943 | axisX()->hide(); | ||
this->axisY()->hide(); | ||||
} | ||||
Michal Klocek
|
r421 | |||
Michal Klocek
|
r910 | m_seriesAxisMap.insert(series,axisY); | ||
Tero Ahola
|
r988 | QMapIterator<int, QAbstractSeries*> i(m_indexSeriesMap); | ||
Michal Klocek
|
r910 | |||
int key=0; | ||||
while (i.hasNext()) { | ||||
i.next(); | ||||
if(i.key()!=key) { | ||||
break; | ||||
} | ||||
key++; | ||||
} | ||||
m_indexSeriesMap.insert(key,series); | ||||
Jani Honkonen
|
r1314 | series->d_ptr->m_chart = qobject_cast<QChart*>(parent()); | ||
series->d_ptr->m_dataset = this; | ||||
Michal Klocek
|
r1062 | |||
Michal Klocek
|
r910 | emit seriesAdded(series,domain); | ||
Michal Klocek
|
r139 | |||
Michal Klocek
|
r439 | } | ||
Jani Honkonen
|
r142 | |||
Michal Klocek
|
r1541 | QAbstractAxis* ChartDataSet::removeSeries(QAbstractSeries* series) | ||
Michal Klocek
|
r439 | { | ||
Michal Klocek
|
r1541 | QAbstractAxis* axis = m_seriesAxisMap.value(series); | ||
Michal Klocek
|
r910 | |||
if(!axis){ | ||||
qWarning()<<"Can not remove series. Series not found on the chart."; | ||||
Michal Klocek
|
r974 | return 0; | ||
Marek Rosa
|
r401 | } | ||
Michal Klocek
|
r974 | |||
Michal Klocek
|
r910 | emit seriesRemoved(series); | ||
Michal Klocek
|
r974 | |||
Michal Klocek
|
r910 | m_seriesAxisMap.remove(series); | ||
int key = seriesIndex(series); | ||||
Q_ASSERT(key!=-1); | ||||
Michal Klocek
|
r139 | |||
Michal Klocek
|
r910 | m_indexSeriesMap.remove(key); | ||
Jani Honkonen
|
r1314 | |||
Michal Klocek
|
r974 | series->setParent(0); | ||
Jani Honkonen
|
r1314 | series->d_ptr->m_chart = 0; | ||
series->d_ptr->m_dataset = 0; | ||||
Michal Klocek
|
r139 | |||
Michal Klocek
|
r1541 | QList<QAbstractAxis*> axes = m_seriesAxisMap.values(); | ||
Michal Klocek
|
r910 | |||
int i = axes.indexOf(axis); | ||||
Michal Klocek
|
r223 | |||
Michal Klocek
|
r910 | if(i==-1){ | ||
Michal Klocek
|
r439 | Domain* domain = m_axisDomainMap.take(axis); | ||
emit axisRemoved(axis); | ||||
Michal Klocek
|
r1078 | if(axis!=m_axisY){ | ||
Michal Klocek
|
r974 | axis->setParent(0); | ||
Michal Klocek
|
r1078 | delete domain; | ||
}else{ | ||||
m_axisYInitialized=false; | ||||
m_axisDomainMap.insert(m_axisY,domain); | ||||
Michal Klocek
|
r444 | } | ||
Michal Klocek
|
r439 | } | ||
Michal Klocek
|
r223 | |||
Michal Klocek
|
r910 | if(m_seriesAxisMap.values().size()==0) | ||
{ | ||||
m_axisXInitialized=false; | ||||
Michal Klocek
|
r439 | emit axisRemoved(axisX()); | ||
Michal Klocek
|
r223 | } | ||
Michal Klocek
|
r974 | |||
return axis; | ||||
Michal Klocek
|
r139 | } | ||
Michal Klocek
|
r258 | void ChartDataSet::removeAllSeries() | ||
{ | ||||
Tero Ahola
|
r988 | QList<QAbstractSeries*> series = m_seriesAxisMap.keys(); | ||
Michal Klocek
|
r1541 | QList<QAbstractAxis*> axes; | ||
Tero Ahola
|
r988 | foreach(QAbstractSeries *s , series) { | ||
Michal Klocek
|
r1541 | QAbstractAxis* axis = removeSeries(s); | ||
Michal Klocek
|
r974 | if(axis==axisY()) continue; | ||
int i = axes.indexOf(axis); | ||||
if(i==-1){ | ||||
axes<<axis; | ||||
} | ||||
Michal Klocek
|
r139 | } | ||
Michal Klocek
|
r910 | Q_ASSERT(m_seriesAxisMap.count()==0); | ||
Michal Klocek
|
r1078 | Q_ASSERT(m_axisDomainMap.count()==1); | ||
Michal Klocek
|
r910 | |||
Michal Klocek
|
r974 | qDeleteAll(series); | ||
qDeleteAll(axes); | ||||
Michal Klocek
|
r139 | } | ||
Michal Klocek
|
r439 | void ChartDataSet::zoomInDomain(const QRectF& rect, const QSizeF& size) | ||
Michal Klocek
|
r139 | { | ||
Michal Klocek
|
r1541 | QMapIterator<QAbstractAxis*, Domain*> i(m_axisDomainMap); | ||
Michal Klocek
|
r1078 | //main domain has to be the last one; | ||
Domain *domain = m_axisDomainMap.value(axisY()); | ||||
Q_ASSERT(domain); | ||||
Michal Klocek
|
r439 | while (i.hasNext()) { | ||
i.next(); | ||||
Michal Klocek
|
r1078 | if(i.value()==domain) continue; | ||
Michal Klocek
|
r439 | i.value()->zoomIn(rect,size); | ||
Michal Klocek
|
r223 | } | ||
Michal Klocek
|
r1078 | domain->zoomIn(rect,size); | ||
Michal Klocek
|
r439 | } | ||
Michal Klocek
|
r223 | |||
Michal Klocek
|
r439 | void ChartDataSet::zoomOutDomain(const QRectF& rect, const QSizeF& size) | ||
{ | ||||
Michal Klocek
|
r1541 | QMapIterator<QAbstractAxis*, Domain*> i(m_axisDomainMap); | ||
Michal Klocek
|
r1078 | //main domain has to be the last one; | ||
Domain *domain = m_axisDomainMap.value(axisY()); | ||||
Q_ASSERT(domain); | ||||
Michal Klocek
|
r439 | while (i.hasNext()) { | ||
i.next(); | ||||
Michal Klocek
|
r1078 | if(i.value()==domain) continue; | ||
Michal Klocek
|
r439 | i.value()->zoomOut(rect,size); | ||
} | ||||
Michal Klocek
|
r1078 | domain->zoomOut(rect,size); | ||
Michal Klocek
|
r223 | } | ||
Michal Klocek
|
r1107 | int ChartDataSet::seriesCount(QAbstractSeries::SeriesType type) | ||
Michal Klocek
|
r910 | { | ||
int count=0; | ||||
Michal Klocek
|
r1541 | QMapIterator<QAbstractSeries*, QAbstractAxis*> i(m_seriesAxisMap); | ||
Michal Klocek
|
r910 | while (i.hasNext()) { | ||
i.next(); | ||||
if(i.key()->type()==type) count++; | ||||
} | ||||
return count; | ||||
} | ||||
Tero Ahola
|
r988 | int ChartDataSet::seriesIndex(QAbstractSeries *series) | ||
Tero Ahola
|
r538 | { | ||
Tero Ahola
|
r988 | QMapIterator<int, QAbstractSeries*> i(m_indexSeriesMap); | ||
Michal Klocek
|
r910 | while (i.hasNext()) { | ||
i.next(); | ||||
if (i.value() == series) | ||||
return i.key(); | ||||
Tero Ahola
|
r538 | } | ||
Tero Ahola
|
r825 | return -1; | ||
Tero Ahola
|
r538 | } | ||
Michal Klocek
|
r1541 | QAbstractAxis* ChartDataSet::axisY(QAbstractSeries *series) const | ||
Michal Klocek
|
r223 | { | ||
Michal Klocek
|
r910 | if(series == 0) return m_axisY; | ||
return m_seriesAxisMap.value(series); | ||||
Michal Klocek
|
r223 | } | ||
Tero Ahola
|
r988 | Domain* ChartDataSet::domain(QAbstractSeries *series) const | ||
Michal Klocek
|
r223 | { | ||
Michal Klocek
|
r1541 | QAbstractAxis* axis = m_seriesAxisMap.value(series); | ||
Michal Klocek
|
r910 | if(axis){ | ||
Michal Klocek
|
r439 | return m_axisDomainMap.value(axis); | ||
Michal Klocek
|
r910 | }else | ||
return 0; | ||||
Michal Klocek
|
r223 | } | ||
Michal Klocek
|
r1541 | Domain* ChartDataSet::domain(QAbstractAxis* axis) const | ||
Michal Klocek
|
r223 | { | ||
Michal Klocek
|
r771 | if(!axis || axis==axisX()) { | ||
Michal Klocek
|
r444 | return m_axisDomainMap.value(axisY()); | ||
} | ||||
else { | ||||
return m_axisDomainMap.value(axis); | ||||
} | ||||
Michal Klocek
|
r223 | } | ||
Michal Klocek
|
r1267 | void ChartDataSet::scrollDomain(qreal dx,qreal dy,const QSizeF& size) | ||
Michal Klocek
|
r531 | { | ||
Michal Klocek
|
r1541 | QMapIterator<QAbstractAxis*, Domain*> i( m_axisDomainMap); | ||
Michal Klocek
|
r1078 | //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(); | ||||
Michal Klocek
|
r531 | } | ||
sauimone
|
r1263 | void ChartDataSet::updateSeries(QAbstractSeries *series) | ||
{ | ||||
emit seriesUpdated(series); | ||||
} | ||||
Michal Klocek
|
r131 | #include "moc_chartdataset_p.cpp" | ||
QTCOMMERCIALCHART_END_NAMESPACE | ||||