chartdataset.cpp
464 lines
| 12.4 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" | ||
|
r1314 | #include "qchart.h" | ||
|
r1541 | #include "qvaluesaxis.h" | ||
|
r1613 | #include "qbarcategoriesaxis.h" | ||
|
r1541 | #include "qvaluesaxis_p.h" | ||
|
r988 | #include "qabstractseries_p.h" | ||
|
r1586 | #include "qabstractbarseries.h" | ||
|
r338 | #include "qstackedbarseries.h" | ||
#include "qpercentbarseries.h" | ||||
|
r146 | #include "qpieseries.h" | ||
|
r131 | |||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
|
r1314 | ChartDataSet::ChartDataSet(QChart *parent):QObject(parent), | ||
|
r1556 | m_domainIndex(0) | ||
|
r131 | { | ||
|
r1556 | |||
|
r131 | } | ||
ChartDataSet::~ChartDataSet() | ||||
{ | ||||
|
r1062 | removeAllSeries(); | ||
|
r131 | } | ||
|
r1541 | void ChartDataSet::addSeries(QAbstractSeries* series) | ||
|
r131 | { | ||
|
r1577 | Domain* domain = m_seriesDomainMap.value(series); | ||
|
r910 | |||
|
r1577 | if(domain) { | ||
|
r439 | qWarning() << "Can not add series. Series already on the chart"; | ||
return; | ||||
} | ||||
|
r223 | |||
|
r974 | series->setParent(this); // take ownership | ||
|
r223 | |||
|
r1577 | domain = new Domain(series); | ||
|
r421 | |||
|
r1556 | m_seriesDomainMap.insert(series,domain); | ||
|
r421 | |||
|
r943 | series->d_ptr->scaleDomain(*domain); | ||
|
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); | ||||
|
r1314 | series->d_ptr->m_chart = qobject_cast<QChart*>(parent()); | ||
series->d_ptr->m_dataset = this; | ||||
|
r1062 | |||
|
r910 | emit seriesAdded(series,domain); | ||
|
r139 | |||
|
r439 | } | ||
|
r142 | |||
|
r1577 | void ChartDataSet::createDefaultAxes() | ||
{ | ||||
|
r1588 | if(m_seriesDomainMap.isEmpty()) return; | ||
QAbstractAxis::AxisTypes typeX(0); | ||||
QAbstractAxis::AxisTypes typeY(0); | ||||
QMapIterator<QAbstractSeries*, Domain*> i(m_seriesDomainMap); | ||||
|
r1577 | while (i.hasNext()) { | ||
|
r1588 | i.next(); | ||
removeAxes(i.key()); | ||||
} | ||||
|
r1577 | |||
|
r1588 | i.toFront(); | ||
|
r1577 | |||
|
r1588 | while (i.hasNext()) { | ||
i.next(); | ||||
QAbstractAxis* axisX = m_seriesAxisXMap.value(i.key()); | ||||
QAbstractAxis* axisY = m_seriesAxisYMap.value(i.key()); | ||||
if(axisX) typeX&=axisX->type(); | ||||
else typeX|=i.key()->d_ptr->defaultAxisXType(); | ||||
if(axisY) typeY&=axisY->type(); | ||||
else typeY|=i.key()->d_ptr->defaultAxisYType(); | ||||
} | ||||
|
r1577 | |||
|
r1588 | if(typeX.testFlag(QAbstractAxis::AxisTypeValues) && typeX.testFlag(QAbstractAxis::AxisTypeCategories)) | ||
{ | ||||
i.toFront(); | ||||
while (i.hasNext()) { | ||||
i.next(); | ||||
QAbstractAxis* axis = createAxis(i.key()->d_ptr->defaultAxisXType()); | ||||
|
r1616 | if(!axis) continue; | ||
|
r1588 | i.key()->d_ptr->initializeAxisX(axis); | ||
addAxisX(axis,i.key()); | ||||
emit axisAdded(axis,i.value()); | ||||
|
r1577 | } | ||
|
r1588 | }else if(!typeY.testFlag(QAbstractAxis::AxisTypeNoAxis)){ | ||
QAbstractAxis* axis = createAxis(QAbstractAxis::AxisType(int(typeX))); | ||||
i.toFront(); | ||||
while (i.hasNext()) { | ||||
i.next(); | ||||
i.key()->d_ptr->initializeAxisX(axis); | ||||
addAxisX(axis,i.key()); | ||||
} | ||||
emit axisAdded(axis,i.value()); | ||||
} | ||||
if(typeY.testFlag(QAbstractAxis::AxisTypeValues) && typeY.testFlag(QAbstractAxis::AxisTypeCategories)) | ||||
{ | ||||
i.toFront(); | ||||
while (i.hasNext()) { | ||||
i.next(); | ||||
QAbstractAxis* axis = createAxis(i.key()->d_ptr->defaultAxisYType()); | ||||
i.key()->d_ptr->initializeAxisY(axis); | ||||
addAxisY(axis,i.key()); | ||||
emit axisAdded(axis,i.value()); | ||||
|
r1577 | } | ||
|
r1588 | |||
}else if(!typeY.testFlag(QAbstractAxis::AxisTypeNoAxis)){ | ||||
QAbstractAxis* axis = createAxis(QAbstractAxis::AxisType(int(typeY))); | ||||
i.toFront(); | ||||
while (i.hasNext()) { | ||||
i.next(); | ||||
i.key()->d_ptr->initializeAxisY(axis); | ||||
addAxisY(axis,i.key()); | ||||
} | ||||
emit axisAdded(axis,i.value()); | ||||
|
r1577 | } | ||
} | ||||
|
r1588 | QAbstractAxis* ChartDataSet::createAxis(QAbstractAxis::AxisType type) | ||
{ | ||||
QAbstractAxis* axis =0; | ||||
switch(type) { | ||||
case QAbstractAxis::AxisTypeValues: | ||||
axis = new QValuesAxis(this); | ||||
break; | ||||
case QAbstractAxis::AxisTypeCategories: | ||||
|
r1612 | axis = new QBarCategoriesAxis(this); | ||
|
r1588 | break; | ||
default: | ||||
axis = 0; | ||||
break; | ||||
} | ||||
return axis; | ||||
} | ||||
void ChartDataSet::addAxisX(QAbstractAxis* axis,QAbstractSeries* series) { | ||||
Domain* domain = m_seriesDomainMap.value(series); | ||||
QObject::connect(axis->d_ptr.data(),SIGNAL(changed(qreal,qreal,int,bool)),domain,SLOT(handleAxisXChanged(qreal,qreal,int,bool))); | ||||
QObject::connect(domain,SIGNAL(rangeXChanged(qreal,qreal,int)),axis->d_ptr.data(),SLOT(handleAxisRangeChanged(qreal,qreal,int))); | ||||
axis->d_ptr->m_orientation=Qt::Horizontal; | ||||
m_seriesAxisXMap.insert(series,axis); | ||||
} | ||||
void ChartDataSet::addAxisY(QAbstractAxis* axis,QAbstractSeries* series) { | ||||
Domain* domain = m_seriesDomainMap.value(series); | ||||
QObject::connect(axis->d_ptr.data(),SIGNAL(changed(qreal,qreal,int,bool)),domain,SLOT(handleAxisYChanged(qreal,qreal,int,bool))); | ||||
QObject::connect(domain,SIGNAL(rangeYChanged(qreal,qreal,int)),axis->d_ptr.data(),SLOT(handleAxisRangeChanged(qreal,qreal,int))); | ||||
axis->d_ptr->m_orientation=Qt::Vertical; | ||||
m_seriesAxisYMap.insert(series,axis); | ||||
} | ||||
|
r1556 | void ChartDataSet::removeSeries(QAbstractSeries* series) | ||
|
r439 | { | ||
|
r1556 | Domain* domain = m_seriesDomainMap.take(series); | ||
|
r910 | |||
|
r1556 | if(!domain) { | ||
|
r910 | qWarning()<<"Can not remove series. Series not found on the chart."; | ||
|
r1556 | } | ||
|
r974 | |||
|
r910 | emit seriesRemoved(series); | ||
|
r974 | |||
|
r1558 | delete domain; | ||
domain = 0; | ||||
|
r910 | int key = seriesIndex(series); | ||
Q_ASSERT(key!=-1); | ||||
|
r139 | |||
|
r910 | m_indexSeriesMap.remove(key); | ||
|
r1314 | |||
|
r974 | series->setParent(0); | ||
|
r1314 | series->d_ptr->m_chart = 0; | ||
series->d_ptr->m_dataset = 0; | ||||
|
r139 | |||
|
r1588 | removeAxes(series); | ||
} | ||||
void ChartDataSet::removeAxes(QAbstractSeries* series) | ||||
{ | ||||
|
r1564 | QAbstractAxis* axisX = m_seriesAxisXMap.take(series); | ||
|
r910 | |||
|
r1564 | if(axisX) { | ||
QList<QAbstractAxis*> axesX = m_seriesAxisXMap.values(); | ||||
int x = axesX.indexOf(axisX); | ||||
|
r223 | |||
|
r1564 | if(x==-1) { | ||
emit axisRemoved(axisX); | ||||
|
r1569 | axisX->deleteLater(); | ||
|
r1564 | } | ||
|
r439 | } | ||
|
r223 | |||
|
r1564 | QAbstractAxis* axisY = m_seriesAxisYMap.take(series); | ||
if(axisY) { | ||||
QList<QAbstractAxis*> axesY = m_seriesAxisYMap.values(); | ||||
int y = axesY.indexOf(axisY); | ||||
if(y==-1) { | ||||
emit axisRemoved(axisY); | ||||
|
r1569 | axisY->deleteLater(); | ||
|
r1564 | } | ||
|
r223 | } | ||
|
r139 | } | ||
|
r258 | void ChartDataSet::removeAllSeries() | ||
{ | ||||
|
r1556 | QList<QAbstractSeries*> series = m_seriesDomainMap.keys(); | ||
|
r988 | foreach(QAbstractSeries *s , series) { | ||
|
r1556 | removeSeries(s); | ||
|
r139 | } | ||
|
r1554 | Q_ASSERT(m_seriesAxisXMap.count()==0); | ||
|
r1556 | Q_ASSERT(m_seriesAxisXMap.count()==0); | ||
Q_ASSERT(m_seriesDomainMap.count()==0); | ||||
|
r910 | |||
|
r974 | qDeleteAll(series); | ||
|
r139 | } | ||
|
r439 | void ChartDataSet::zoomInDomain(const QRectF& rect, const QSizeF& size) | ||
|
r139 | { | ||
|
r1593 | //for performance reasons block, signals and scale "full" domain one by one. Gives twice less screen updates | ||
blockAxisSignals(true); | ||||
|
r1556 | QMapIterator<QAbstractSeries*, Domain*> i(m_seriesDomainMap); | ||
|
r1593 | |||
|
r439 | while (i.hasNext()) { | ||
i.next(); | ||||
i.value()->zoomIn(rect,size); | ||||
|
r223 | } | ||
|
r1593 | |||
blockAxisSignals(false); | ||||
|
r439 | } | ||
|
r223 | |||
|
r439 | void ChartDataSet::zoomOutDomain(const QRectF& rect, const QSizeF& size) | ||
{ | ||||
|
r1593 | //for performance reasons block, signals and scale "full" domain one by one. Gives twice less screen updates | ||
blockAxisSignals(true); | ||||
|
r1556 | QMapIterator<QAbstractSeries*, Domain*> i(m_seriesDomainMap); | ||
|
r1593 | |||
|
r439 | while (i.hasNext()) { | ||
i.next(); | ||||
i.value()->zoomOut(rect,size); | ||||
} | ||||
|
r1593 | |||
blockAxisSignals(false); | ||||
} | ||||
void ChartDataSet::blockAxisSignals(bool enabled) | ||||
{ | ||||
QMapIterator<QAbstractSeries*, Domain*> i(m_seriesDomainMap); | ||||
while (i.hasNext()) { | ||||
i.next(); | ||||
QAbstractAxis* axisX = m_seriesAxisXMap.value(i.key()); | ||||
QAbstractAxis* axisY = m_seriesAxisYMap.value(i.key()); | ||||
if(axisX) axisX->blockSignals(enabled); | ||||
if(axisY) axisY->blockSignals(enabled); | ||||
} | ||||
|
r223 | } | ||
|
r1107 | int ChartDataSet::seriesCount(QAbstractSeries::SeriesType type) | ||
|
r910 | { | ||
int count=0; | ||||
|
r1577 | QMapIterator<QAbstractSeries*, Domain*> i(m_seriesDomainMap); | ||
|
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 | } | ||
|
r1554 | QAbstractAxis* ChartDataSet::axisX(QAbstractSeries *series) const | ||
{ | ||||
|
r1641 | if(series == 0) { | ||
QMapIterator<QAbstractSeries*, QAbstractAxis *> i(m_seriesAxisXMap); | ||||
while (i.hasNext()) { | ||||
i.next(); | ||||
if(i.value()->isVisible()) return i.value(); | ||||
} | ||||
return 0; | ||||
} | ||||
|
r1554 | return m_seriesAxisXMap.value(series); | ||
} | ||||
|
r1541 | QAbstractAxis* ChartDataSet::axisY(QAbstractSeries *series) const | ||
|
r223 | { | ||
|
r1641 | if(series == 0) { | ||
QMapIterator<QAbstractSeries*, QAbstractAxis *> i(m_seriesAxisYMap); | ||||
while (i.hasNext()) { | ||||
i.next(); | ||||
if(i.value()->isVisible()) return i.value(); | ||||
} | ||||
return 0; | ||||
} | ||||
|
r1554 | return m_seriesAxisYMap.value(series); | ||
|
r223 | } | ||
|
r1554 | void ChartDataSet::setAxisX(QAbstractSeries *series, QAbstractAxis *axis) | ||
{ | ||||
|
r1564 | Q_ASSERT(axis); | ||
|
r1577 | Domain* domain = m_seriesDomainMap.value(series); | ||
|
r1564 | |||
|
r1577 | if(!domain) { | ||
qWarning() << "Series not found on the chart."; | ||||
return; | ||||
|
r1564 | } | ||
if(axis->d_ptr->m_orientation==Qt::Vertical) { | ||||
|
r1577 | qWarning()<<"Axis already defined as axis Y"; | ||
return; | ||||
|
r1564 | } | ||
|
r1577 | QAbstractAxis *oldAxis = m_seriesAxisXMap.take(series); | ||
QList<QAbstractAxis*> axesX = m_seriesAxisXMap.values(); | ||||
|
r1564 | |||
|
r1577 | if(oldAxis) { | ||
int x = axesX.indexOf(oldAxis); | ||||
if(x==-1) { | ||||
emit axisRemoved(oldAxis); | ||||
oldAxis->deleteLater(); | ||||
} | ||||
} | ||||
|
r1564 | |||
QObject::connect(axis->d_ptr.data(),SIGNAL(changed(qreal,qreal,int,bool)),domain,SLOT(handleAxisXChanged(qreal,qreal,int,bool))); | ||||
QObject::connect(domain,SIGNAL(rangeXChanged(qreal,qreal,int)),axis->d_ptr.data(),SLOT(handleAxisRangeChanged(qreal,qreal,int))); | ||||
|
r1577 | int x = axesX.indexOf(axis); | ||
|
r1564 | if(x==-1) { | ||
|
r1577 | axis->d_ptr->m_orientation=Qt::Horizontal; | ||
emit axisAdded(axis,domain); | ||||
|
r1564 | } | ||
m_seriesAxisXMap.insert(series,axis); | ||||
|
r1611 | axis->d_ptr->emitRange(); | ||
|
r1554 | } | ||
void ChartDataSet::setAxisY(QAbstractSeries *series, QAbstractAxis *axis) | ||||
|
r1542 | { | ||
|
r1577 | Q_ASSERT(axis); | ||
Domain* domain = m_seriesDomainMap.value(series); | ||||
|
r1564 | |||
|
r1577 | if(!domain) { | ||
qWarning() << "Series not found on the chart."; | ||||
return; | ||||
} | ||||
|
r1564 | |||
if(axis->d_ptr->m_orientation==Qt::Horizontal) { | ||||
qWarning()<<"Axis already defined as axis X"; | ||||
return; | ||||
} | ||||
|
r1577 | QAbstractAxis *oldAxis = m_seriesAxisYMap.take(series); | ||
QList<QAbstractAxis*> axesY = m_seriesAxisYMap.values(); | ||||
|
r1564 | |||
|
r1577 | if(oldAxis) { | ||
int y = axesY.indexOf(oldAxis); | ||||
if(y==-1) { | ||||
emit axisRemoved(oldAxis); | ||||
oldAxis->deleteLater(); | ||||
} | ||||
} | ||||
|
r1564 | |||
|
r1577 | QObject::connect(axis->d_ptr.data(),SIGNAL(changed(qreal,qreal,int,bool)),domain,SLOT(handleAxisYChanged(qreal,qreal,int,bool))); | ||
QObject::connect(domain,SIGNAL(rangeYChanged(qreal,qreal,int)),axis->d_ptr.data(),SLOT(handleAxisRangeChanged(qreal,qreal,int))); | ||||
|
r1564 | |||
|
r1577 | int y = axesY.indexOf(axis); | ||
|
r1564 | if(y==-1) { | ||
axis->d_ptr->m_orientation=Qt::Vertical; | ||||
emit axisAdded(axis,domain); | ||||
} | ||||
|
r1577 | m_seriesAxisYMap.insert(series,axis); | ||
|
r1611 | axis->d_ptr->emitRange(); | ||
|
r1564 | } | ||
Domain* ChartDataSet::domain(QAbstractSeries *series) const | ||||
{ | ||||
return m_seriesDomainMap.value(series); | ||||
|
r1542 | } | ||
|
r1267 | void ChartDataSet::scrollDomain(qreal dx,qreal dy,const QSizeF& size) | ||
|
r531 | { | ||
|
r1593 | blockAxisSignals(true); | ||
|
r1556 | QMapIterator<QAbstractSeries*, Domain*> i(m_seriesDomainMap); | ||
|
r1078 | while (i.hasNext()) { | ||
i.next(); | ||||
i.value()->move(dx,dy,size); | ||||
} | ||||
|
r1593 | blockAxisSignals(false); | ||
|
r1078 | } | ||
QList<QAbstractSeries*> ChartDataSet::series() const | ||||
{ | ||||
|
r1554 | return m_seriesAxisXMap.keys(); | ||
|
r531 | } | ||
|
r1263 | void ChartDataSet::updateSeries(QAbstractSeries *series) | ||
{ | ||||
emit seriesUpdated(series); | ||||
} | ||||
|
r131 | #include "moc_chartdataset_p.cpp" | ||
QTCOMMERCIALCHART_END_NAMESPACE | ||||