chartpresenter.cpp
402 lines
| 12.8 KiB
| text/x-c
|
CppLexer
/ src / chartpresenter.cpp
Jani Honkonen
|
r794 | /**************************************************************************** | ||
Michal Klocek
|
r969 | ** | ||
** 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$ | ||||
** | ||||
****************************************************************************/ | ||||
#include "chartpresenter_p.h" | ||||
Michal Klocek
|
r139 | #include "qchart.h" | ||
Michal Klocek
|
r855 | #include "qchart_p.h" | ||
Michal Klocek
|
r1006 | #include "qaxis.h" | ||
Michal Klocek
|
r131 | #include "chartdataset_p.h" | ||
Michal Klocek
|
r143 | #include "charttheme_p.h" | ||
Michal Klocek
|
r530 | #include "chartanimator_p.h" | ||
Tero Ahola
|
r988 | #include "qabstractseries_p.h" | ||
Michal Klocek
|
r421 | #include "qareaseries.h" | ||
Michal Klocek
|
r1006 | #include "chartaxis_p.h" | ||
Michal Klocek
|
r421 | #include "areachartitem_p.h" | ||
Michal Klocek
|
r1006 | #include "chartbackground_p.h" | ||
Michal Klocek
|
r131 | |||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(chart), | ||||
Michal Klocek
|
r969 | m_chart(chart), | ||
m_animator(0), | ||||
m_dataset(dataset), | ||||
m_chartTheme(0), | ||||
m_chartRect(QRectF(QPoint(0,0),m_chart->size())), | ||||
m_options(QChart::NoAnimation), | ||||
m_minLeftMargin(0), | ||||
m_minBottomMargin(0), | ||||
m_backgroundItem(0), | ||||
m_titleItem(0), | ||||
m_marginBig(60), | ||||
m_marginSmall(20), | ||||
m_marginTiny(10), | ||||
m_chartMargins(QRect(m_marginBig,m_marginBig,0,0)) | ||||
Michal Klocek
|
r131 | { | ||
} | ||||
ChartPresenter::~ChartPresenter() | ||||
{ | ||||
Michal Klocek
|
r969 | delete m_chartTheme; | ||
Michal Klocek
|
r131 | } | ||
Michal Klocek
|
r855 | void ChartPresenter::setGeometry(const QRectF& rect) | ||
Michal Klocek
|
r131 | { | ||
Michal Klocek
|
r439 | m_rect = rect; | ||
Michal Klocek
|
r147 | Q_ASSERT(m_rect.isValid()); | ||
Michal Klocek
|
r855 | updateLayout(); | ||
} | ||||
Michal Klocek
|
r1006 | void ChartPresenter::setMinimumMarginWidth(ChartAxis* axis, qreal width) | ||
Michal Klocek
|
r855 | { | ||
switch(axis->axisType()){ | ||||
Michal Klocek
|
r1006 | case ChartAxis::X_AXIS: | ||
Michal Klocek
|
r969 | { | ||
if(width>m_chartRect.width()+ m_chartMargins.left()) { | ||||
m_minLeftMargin= width - m_chartRect.width(); | ||||
updateLayout(); | ||||
Michal Klocek
|
r855 | } | ||
Michal Klocek
|
r969 | break; | ||
} | ||||
Michal Klocek
|
r1006 | case ChartAxis::Y_AXIS: | ||
Michal Klocek
|
r969 | { | ||
Michal Klocek
|
r855 | |||
Michal Klocek
|
r969 | if(m_minLeftMargin!=width){ | ||
m_minLeftMargin= width; | ||||
updateLayout(); | ||||
Michal Klocek
|
r855 | } | ||
Michal Klocek
|
r969 | break; | ||
} | ||||
Michal Klocek
|
r855 | |||
} | ||||
} | ||||
Michal Klocek
|
r1006 | void ChartPresenter::setMinimumMarginHeight(ChartAxis* axis, qreal height) | ||
Michal Klocek
|
r855 | { | ||
switch(axis->axisType()){ | ||||
Michal Klocek
|
r1006 | case ChartAxis::X_AXIS: | ||
Michal Klocek
|
r969 | { | ||
if(m_minBottomMargin!=height) { | ||||
m_minBottomMargin= height; | ||||
updateLayout(); | ||||
Michal Klocek
|
r855 | } | ||
Michal Klocek
|
r969 | break; | ||
} | ||||
Michal Klocek
|
r1006 | case ChartAxis::Y_AXIS: | ||
Michal Klocek
|
r969 | { | ||
Michal Klocek
|
r855 | |||
Michal Klocek
|
r969 | if(height>m_chartMargins.bottom()+m_chartRect.height()){ | ||
m_minBottomMargin= height - m_chartRect.height(); | ||||
updateLayout(); | ||||
Michal Klocek
|
r855 | } | ||
Michal Klocek
|
r969 | break; | ||
} | ||||
Michal Klocek
|
r855 | |||
} | ||||
Michal Klocek
|
r139 | } | ||
Michal Klocek
|
r131 | |||
Michal Klocek
|
r1006 | void ChartPresenter::handleAxisAdded(QAxis* axis,Domain* domain) | ||
Michal Klocek
|
r223 | { | ||
Michal Klocek
|
r1006 | ChartAxis* item = new ChartAxis(axis,this,axis==m_dataset->axisX()?ChartAxis::X_AXIS : ChartAxis::Y_AXIS); | ||
Michal Klocek
|
r298 | |||
Michal Klocek
|
r530 | if(m_options.testFlag(QChart::GridAxisAnimations)){ | ||
m_animator->addAnimation(item); | ||||
Michal Klocek
|
r439 | } | ||
Michal Klocek
|
r530 | |||
Michal Klocek
|
r439 | if(axis==m_dataset->axisX()){ | ||
Michal Klocek
|
r943 | m_chartTheme->decorate(axis,true); | ||
Michal Klocek
|
r531 | QObject::connect(domain,SIGNAL(rangeXChanged(qreal,qreal,int)),item,SLOT(handleRangeChanged(qreal,qreal,int))); | ||
Michal Klocek
|
r439 | //initialize | ||
Michal Klocek
|
r531 | item->handleRangeChanged(domain->minX(),domain->maxX(),domain->tickXCount()); | ||
Michal Klocek
|
r562 | |||
Michal Klocek
|
r439 | } | ||
else{ | ||||
Michal Klocek
|
r943 | m_chartTheme->decorate(axis,false); | ||
Michal Klocek
|
r531 | QObject::connect(domain,SIGNAL(rangeYChanged(qreal,qreal,int)),item,SLOT(handleRangeChanged(qreal,qreal,int))); | ||
Michal Klocek
|
r439 | //initialize | ||
Michal Klocek
|
r531 | item->handleRangeChanged(domain->minY(),domain->maxY(),domain->tickYCount()); | ||
Michal Klocek
|
r223 | } | ||
Michal Klocek
|
r298 | |||
Michal Klocek
|
r967 | QObject::connect(this,SIGNAL(geometryChanged(QRectF)),item,SLOT(handleGeometryChanged(QRectF))); | ||
Michal Klocek
|
r439 | //initialize | ||
Michal Klocek
|
r855 | item->handleGeometryChanged(m_chartRect); | ||
Tero Ahola
|
r561 | m_axisItems.insert(axis, item); | ||
Michal Klocek
|
r223 | } | ||
Michal Klocek
|
r1006 | void ChartPresenter::handleAxisRemoved(QAxis* axis) | ||
Michal Klocek
|
r223 | { | ||
Michal Klocek
|
r1006 | ChartAxis* item = m_axisItems.take(axis); | ||
Michal Klocek
|
r223 | Q_ASSERT(item); | ||
Michal Klocek
|
r530 | if(m_animator) m_animator->removeAnimation(item); | ||
Michal Klocek
|
r223 | delete item; | ||
} | ||||
Tero Ahola
|
r988 | void ChartPresenter::handleSeriesAdded(QAbstractSeries* series,Domain* domain) | ||
Michal Klocek
|
r131 | { | ||
Michal Klocek
|
r969 | Chart *item = series->d_ptr->createGraphics(this); | ||
Q_ASSERT(item); | ||||
QObject::connect(this,SIGNAL(geometryChanged(QRectF)),item,SLOT(handleGeometryChanged(QRectF))); | ||||
QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),item,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal))); | ||||
//initialize | ||||
item->handleDomainChanged(domain->minX(),domain->maxX(),domain->minY(),domain->maxY()); | ||||
if(m_chartRect.isValid()) item->handleGeometryChanged(m_chartRect); | ||||
m_chartItems.insert(series,item); | ||||
Michal Klocek
|
r131 | } | ||
Tero Ahola
|
r988 | void ChartPresenter::handleSeriesRemoved(QAbstractSeries* series) | ||
Michal Klocek
|
r139 | { | ||
Michal Klocek
|
r677 | Chart* item = m_chartItems.take(series); | ||
Michal Klocek
|
r530 | Q_ASSERT(item); | ||
Michal Klocek
|
r560 | if(m_animator) { | ||
//small hack to handle area animations | ||||
Tero Ahola
|
r988 | if(series->type() == QAbstractSeries::SeriesTypeArea){ | ||
Michal Klocek
|
r969 | QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series); | ||
AreaChartItem* area = static_cast<AreaChartItem*>(item); | ||||
m_animator->removeAnimation(area->upperLineItem()); | ||||
if(areaSeries->lowerSeries()) m_animator->removeAnimation(area->lowerLineItem()); | ||||
Michal Klocek
|
r560 | }else | ||
Michal Klocek
|
r969 | m_animator->removeAnimation(item); | ||
Michal Klocek
|
r560 | } | ||
Marek Rosa
|
r401 | delete item; | ||
Michal Klocek
|
r139 | } | ||
Michal Klocek
|
r131 | |||
Michal Klocek
|
r740 | void ChartPresenter::setTheme(QChart::ChartTheme theme,bool force) | ||
Michal Klocek
|
r143 | { | ||
Michal Klocek
|
r969 | if(m_chartTheme && m_chartTheme->id() == theme) return; | ||
Michal Klocek
|
r143 | delete m_chartTheme; | ||
m_chartTheme = ChartTheme::createTheme(theme); | ||||
Michal Klocek
|
r943 | m_chartTheme->setForced(force); | ||
m_chartTheme->decorate(m_chart); | ||||
m_chartTheme->decorate(m_chart->legend()); | ||||
Michal Klocek
|
r531 | resetAllElements(); | ||
Michal Klocek
|
r176 | } | ||
Michal Klocek
|
r740 | QChart::ChartTheme ChartPresenter::theme() | ||
Michal Klocek
|
r143 | { | ||
Michal Klocek
|
r153 | return m_chartTheme->id(); | ||
} | ||||
Michal Klocek
|
r298 | void ChartPresenter::setAnimationOptions(QChart::AnimationOptions options) | ||
{ | ||||
if(m_options!=options) { | ||||
m_options=options; | ||||
Michal Klocek
|
r530 | if(m_options!=QChart::NoAnimation && !m_animator) { | ||
m_animator= new ChartAnimator(this); | ||||
Michal Klocek
|
r298 | } | ||
Michal Klocek
|
r531 | resetAllElements(); | ||
Michal Klocek
|
r298 | } | ||
Michal Klocek
|
r530 | |||
Michal Klocek
|
r298 | } | ||
Michal Klocek
|
r531 | void ChartPresenter::resetAllElements() | ||
{ | ||||
Michal Klocek
|
r1006 | QList<QAxis *> axisList = m_axisItems.uniqueKeys(); | ||
Tero Ahola
|
r988 | QList<QAbstractSeries *> seriesList = m_chartItems.uniqueKeys(); | ||
Michal Klocek
|
r969 | |||
Michal Klocek
|
r1006 | foreach(QAxis *axis, axisList) { | ||
Michal Klocek
|
r969 | handleAxisRemoved(axis); | ||
handleAxisAdded(axis,m_dataset->domain(axis)); | ||||
} | ||||
Tero Ahola
|
r988 | foreach(QAbstractSeries *series, seriesList) { | ||
Michal Klocek
|
r969 | handleSeriesRemoved(series); | ||
handleSeriesAdded(series,m_dataset->domain(series)); | ||||
} | ||||
Michal Klocek
|
r531 | } | ||
Michal Klocek
|
r439 | void ChartPresenter::zoomIn() | ||
{ | ||||
Michal Klocek
|
r969 | QRectF rect = chartGeometry(); | ||
rect.setWidth(rect.width()/2); | ||||
rect.setHeight(rect.height()/2); | ||||
rect.moveCenter(chartGeometry().center()); | ||||
zoomIn(rect); | ||||
Michal Klocek
|
r439 | } | ||
void ChartPresenter::zoomIn(const QRectF& rect) | ||||
{ | ||||
Michal Klocek
|
r969 | QRectF r = rect.normalized(); | ||
Michal Klocek
|
r855 | r.translate(-m_chartMargins.topLeft()); | ||
Michal Klocek
|
r969 | if(m_animator) { | ||
QPointF point(r.center().x()/chartGeometry().width(),r.center().y()/chartGeometry().height()); | ||||
m_animator->setState(ChartAnimator::ZoomInState,point); | ||||
} | ||||
m_dataset->zoomInDomain(r,chartGeometry().size()); | ||||
if(m_animator) { | ||||
m_animator->setState(ChartAnimator::ShowState); | ||||
} | ||||
Michal Klocek
|
r439 | } | ||
void ChartPresenter::zoomOut() | ||||
{ | ||||
Michal Klocek
|
r969 | if(m_animator) | ||
{ | ||||
m_animator->setState(ChartAnimator::ZoomOutState); | ||||
} | ||||
Michal Klocek
|
r678 | |||
Michal Klocek
|
r969 | QSizeF size = chartGeometry().size(); | ||
QRectF rect = chartGeometry(); | ||||
Michal Klocek
|
r855 | rect.translate(-m_chartMargins.topLeft()); | ||
sauimone
|
r803 | m_dataset->zoomOutDomain(rect.adjusted(size.width()/4,size.height()/4,-size.width()/4,-size.height()/4),size); | ||
Michal Klocek
|
r678 | //m_dataset->zoomOutDomain(m_zoomStack[m_zoomIndex-1],geometry().size()); | ||
Michal Klocek
|
r969 | if(m_animator){ | ||
m_animator->setState(ChartAnimator::ShowState); | ||||
} | ||||
Michal Klocek
|
r439 | } | ||
Michal Klocek
|
r531 | void ChartPresenter::scroll(int dx,int dy) | ||
{ | ||||
Michal Klocek
|
r969 | if(m_animator){ | ||
if(dx<0) m_animator->setState(ChartAnimator::ScrollLeftState,QPointF()); | ||||
if(dx>0) m_animator->setState(ChartAnimator::ScrollRightState,QPointF()); | ||||
if(dy<0) m_animator->setState(ChartAnimator::ScrollUpState,QPointF()); | ||||
if(dy>0) m_animator->setState(ChartAnimator::ScrollDownState,QPointF()); | ||||
} | ||||
Michal Klocek
|
r531 | |||
Michal Klocek
|
r969 | m_dataset->scrollDomain(dx,dy,chartGeometry().size()); | ||
Michal Klocek
|
r531 | |||
sauimone
|
r547 | if(m_animator){ | ||
Michal Klocek
|
r969 | m_animator->setState(ChartAnimator::ShowState); | ||
} | ||||
Michal Klocek
|
r531 | } | ||
Michal Klocek
|
r298 | QChart::AnimationOptions ChartPresenter::animationOptions() const | ||
{ | ||||
return m_options; | ||||
} | ||||
Michal Klocek
|
r855 | void ChartPresenter::updateLayout() | ||
{ | ||||
if (!m_rect.isValid()) return; | ||||
// recalculate title size | ||||
QSize titleSize; | ||||
int titlePadding=0; | ||||
if (m_titleItem) { | ||||
titleSize= m_titleItem->boundingRect().size().toSize(); | ||||
} | ||||
//defaults | ||||
m_chartMargins = QRect(QPoint(m_minLeftMargin>m_marginBig?m_minLeftMargin:m_marginBig,m_marginBig),QPoint(m_marginBig,m_minBottomMargin>m_marginBig?m_minBottomMargin:m_marginBig)); | ||||
titlePadding = m_chartMargins.top()/2; | ||||
QLegend* legend = m_chart->d_ptr->m_legend; | ||||
// recalculate legend position | ||||
if (legend->isAttachedToChart() && legend->isEnabled()) { | ||||
QRect legendRect; | ||||
// Reserve some space for legend | ||||
switch (legend->alignment()) { | ||||
Michal Klocek
|
r969 | case QLegend::AlignmentTop: { | ||
int ledgendSize = legend->minHeight(); | ||||
int topPadding = 2*m_marginTiny + titleSize.height() + ledgendSize + m_marginTiny; | ||||
m_chartMargins = QRect(QPoint(m_chartMargins.left(),topPadding),QPoint(m_chartMargins.right(),m_chartMargins.bottom())); | ||||
m_legendMargins = QRect(QPoint(m_chartMargins.left(),topPadding - (ledgendSize + m_marginTiny)),QPoint(m_chartMargins.right(),m_rect.height()-topPadding + m_marginTiny)); | ||||
titlePadding = m_marginTiny + m_marginTiny; | ||||
break; | ||||
} | ||||
case QLegend::AlignmentBottom: { | ||||
int ledgendSize = legend->minHeight(); | ||||
int bottomPadding = m_marginTiny + m_marginSmall + ledgendSize + m_marginTiny + m_minBottomMargin; | ||||
m_chartMargins = QRect(QPoint(m_chartMargins.left(),m_chartMargins.top()),QPoint(m_chartMargins.right(),bottomPadding)); | ||||
m_legendMargins = QRect(QPoint(m_chartMargins.left(),m_rect.height()-bottomPadding + m_marginTiny + m_minBottomMargin),QPoint(m_chartMargins.right(),m_marginTiny + m_marginSmall)); | ||||
titlePadding = m_chartMargins.top()/2; | ||||
break; | ||||
} | ||||
case QLegend::AlignmentLeft: { | ||||
int ledgendSize = legend->minWidth(); | ||||
int leftPadding = m_marginTiny + m_marginSmall + ledgendSize + m_marginTiny + m_minLeftMargin; | ||||
m_chartMargins = QRect(QPoint(leftPadding,m_chartMargins.top()),QPoint(m_chartMargins.right(),m_chartMargins.bottom())); | ||||
m_legendMargins = QRect(QPoint(m_marginTiny + m_marginSmall,m_chartMargins.top()),QPoint(m_rect.width()-leftPadding + m_marginTiny + m_minLeftMargin,m_chartMargins.bottom())); | ||||
titlePadding = m_chartMargins.top()/2; | ||||
break; | ||||
} | ||||
case QLegend::AlignmentRight: { | ||||
int ledgendSize = legend->minWidth(); | ||||
int rightPadding = m_marginTiny + m_marginSmall + ledgendSize + m_marginTiny; | ||||
m_chartMargins = QRect(QPoint(m_chartMargins.left(),m_chartMargins.top()),QPoint(rightPadding,m_chartMargins.bottom())); | ||||
m_legendMargins = QRect(QPoint(m_rect.width()- rightPadding+ m_marginTiny ,m_chartMargins.top()),QPoint(m_marginTiny + m_marginSmall,m_chartMargins.bottom())); | ||||
titlePadding = m_chartMargins.top()/2; | ||||
break; | ||||
} | ||||
default: { | ||||
break; | ||||
} | ||||
Michal Klocek
|
r855 | } | ||
} | ||||
Michal Klocek
|
r913 | if(m_rect.width()<2*(m_chartMargins.top()+m_chartMargins.bottom()) || m_rect.height()< 2*(m_chartMargins.top() + m_chartMargins.bottom())) | ||
{ | ||||
m_chart->setMinimumSize(2*(m_chartMargins.top()+m_chartMargins.bottom()),2*(m_chartMargins.top() + m_chartMargins.bottom())); | ||||
return; | ||||
} | ||||
Michal Klocek
|
r855 | // recalculate title position | ||
if (m_titleItem) { | ||||
QPointF center = m_rect.center() -m_titleItem->boundingRect().center(); | ||||
m_titleItem->setPos(center.x(),titlePadding); | ||||
} | ||||
//recalculate background gradient | ||||
if (m_backgroundItem) { | ||||
m_backgroundItem->setRect(m_rect.adjusted(m_marginTiny,m_marginTiny, -m_marginTiny, -m_marginTiny)); | ||||
} | ||||
Michal Klocek
|
r913 | |||
Michal Klocek
|
r869 | QRectF chartRect = m_rect.adjusted(m_chartMargins.left(),m_chartMargins.top(),-m_chartMargins.right(),-m_chartMargins.bottom()); | ||
Michal Klocek
|
r871 | legend->setGeometry(m_rect.adjusted(m_legendMargins.left(),m_legendMargins.top(),-m_legendMargins.right(),-m_legendMargins.bottom())); | ||
Michal Klocek
|
r969 | if(m_chartRect!=chartRect){ | ||
m_chartRect=chartRect; | ||||
emit geometryChanged(m_chartRect); | ||||
} | ||||
Michal Klocek
|
r855 | |||
Michal Klocek
|
r871 | |||
Michal Klocek
|
r855 | } | ||
void ChartPresenter::createChartBackgroundItem() | ||||
{ | ||||
if (!m_backgroundItem) { | ||||
m_backgroundItem = new ChartBackground(rootItem()); | ||||
m_backgroundItem->setPen(Qt::NoPen); | ||||
m_backgroundItem->setZValue(ChartPresenter::BackgroundZValue); | ||||
} | ||||
} | ||||
void ChartPresenter::createChartTitleItem() | ||||
{ | ||||
if (!m_titleItem) { | ||||
m_titleItem = new QGraphicsSimpleTextItem(rootItem()); | ||||
m_titleItem->setZValue(ChartPresenter::BackgroundZValue); | ||||
} | ||||
} | ||||
Michal Klocek
|
r143 | |||
Michal Klocek
|
r131 | #include "moc_chartpresenter_p.cpp" | ||
QTCOMMERCIALCHART_END_NAMESPACE | ||||