##// END OF EJS Templates
QChart returns first axis if null series
QChart returns first axis if null series

File last commit:

r1556:10e43766cad7
r1557:9904688c7b99
Show More
chartpresenter.cpp
420 lines | 10.9 KiB | text/x-c | CppLexer
/ src / chartpresenter.cpp
Jani Honkonen
Add license headers
r794 /****************************************************************************
Michal Klocek
Fixes header guard style issues
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
Refactored for MVP...
r139 #include "qchart.h"
Michal Klocek
Refactor qledgend handling...
r855 #include "qchart_p.h"
Michal Klocek
Refactors QAxis to QAbstractAxis...
r1541 #include "qabstractaxis.h"
Michal Klocek
Refactors core to support mulitpile axis and domains...
r1556 #include "qabstractaxis_p.h"
Michal Klocek
Refactors qchart , adds line animation...
r131 #include "chartdataset_p.h"
Michal Klocek
Refactor themes...
r143 #include "charttheme_p.h"
Michal Klocek
Animation refactor...
r530 #include "chartanimator_p.h"
Michal Klocek
Refactors axis animation, line animations
r1241 #include "chartanimation_p.h"
Tero Ahola
Renamed QSeries to QAbstractSeries
r988 #include "qabstractseries_p.h"
Michal Klocek
Adds area chart...
r421 #include "qareaseries.h"
Michal Klocek
Changes QChartAxis -> QAxis
r1006 #include "chartaxis_p.h"
Michal Klocek
Refactors axis animation, line animations
r1241 #include "chartaxisx_p.h"
#include "chartaxisy_p.h"
Michal Klocek
Adds area chart...
r421 #include "areachartitem_p.h"
Michal Klocek
Changes QChartAxis -> QAxis
r1006 #include "chartbackground_p.h"
Michal Klocek
Refactors layout managment...
r1534 #include "chartlayout_p.h"
Michal Klocek
Refactors axis animation, line animations
r1241 #include <QTimer>
Michal Klocek
Refactors qchart , adds line animation...
r131
QTCOMMERCIALCHART_BEGIN_NAMESPACE
ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(chart),
Michal Klocek
Fixes header guard style issues
r969 m_chart(chart),
m_animator(0),
m_dataset(dataset),
m_chartTheme(0),
m_options(QChart::NoAnimation),
Michal Klocek
Refactors axis animation, line animations
r1241 m_state(ShowState),
Michal Klocek
Refactors layout managment...
r1534 m_layout(new ChartLayout(this)),
Michal Klocek
Fixes header guard style issues
r969 m_backgroundItem(0),
Michal Klocek
Refactors layout managment...
r1534 m_titleItem(0)
Michal Klocek
Refactors qchart , adds line animation...
r131 {
Michal Klocek
Refactors axis animation, line animations
r1241
Michal Klocek
Refactors qchart , adds line animation...
r131 }
ChartPresenter::~ChartPresenter()
{
Michal Klocek
Fixes header guard style issues
r969 delete m_chartTheme;
Michal Klocek
Refactors qchart , adds line animation...
r131 }
Michal Klocek
Refactor qledgend handling...
r855 void ChartPresenter::setGeometry(const QRectF& rect)
Michal Klocek
Refactors qchart , adds line animation...
r131 {
Michal Klocek
Refactor qledgend handling...
r855
Michal Klocek
Refactors layout managment...
r1534 Q_ASSERT(rect.isValid());
Michal Klocek
Refactor qledgend handling...
r855
Michal Klocek
Refactors layout managment...
r1534 if(m_rect!=rect) {
m_rect=rect;
emit geometryChanged(m_rect);
Michal Klocek
Refactor qledgend handling...
r855 }
Michal Klocek
Refactored for MVP...
r139 }
Michal Klocek
Refactors qchart , adds line animation...
r131
Michal Klocek
Refactors QAxis to QAbstractAxis...
r1541 void ChartPresenter::handleAxisAdded(QAbstractAxis* axis,Domain* domain)
Michal Klocek
Refactors axis handling...
r223 {
Michal Klocek
Refactors core to support mulitpile axis and domains...
r1556 ChartAxis* item = axis->d_ptr->createGraphics(this);
item->setDomain(domain);
Michal Klocek
Adds animation settings handling
r298
Michal Klocek
Animation refactor...
r530 if(m_options.testFlag(QChart::GridAxisAnimations)){
Michal Klocek
Refactors axis animation, line animations
r1241 item->setAnimator(m_animator);
item->setAnimation(new AxisAnimation(item));
Michal Klocek
Refactor domain model...
r439 }
Michal Klocek
Animation refactor...
r530
Michal Klocek
Refactors core to support mulitpile axis and domains...
r1556 if(item->axisType()==ChartAxis::X_AXIS){
Michal Klocek
Refactor to use qseries private for implmentation interface...
r943 m_chartTheme->decorate(axis,true);
Michal Klocek
Adds scroll support...
r531 QObject::connect(domain,SIGNAL(rangeXChanged(qreal,qreal,int)),item,SLOT(handleRangeChanged(qreal,qreal,int)));
Michal Klocek
Refactor domain model...
r439 //initialize
Michal Klocek
Adds scroll support...
r531 item->handleRangeChanged(domain->minX(),domain->maxX(),domain->tickXCount());
Michal Klocek
Fix theme decoration calls on all xyseries
r562
Michal Klocek
Refactor domain model...
r439 }
else{
Michal Klocek
Refactor to use qseries private for implmentation interface...
r943 m_chartTheme->decorate(axis,false);
Michal Klocek
Adds scroll support...
r531 QObject::connect(domain,SIGNAL(rangeYChanged(qreal,qreal,int)),item,SLOT(handleRangeChanged(qreal,qreal,int)));
Michal Klocek
Refactor domain model...
r439 //initialize
Michal Klocek
Adds scroll support...
r531 item->handleRangeChanged(domain->minY(),domain->maxY(),domain->tickYCount());
Michal Klocek
Refactors axis handling...
r223 }
Michal Klocek
Adds animation settings handling
r298
Michal Klocek
Normalizes signal slots connections
r967 QObject::connect(this,SIGNAL(geometryChanged(QRectF)),item,SLOT(handleGeometryChanged(QRectF)));
Michal Klocek
Refactor domain model...
r439 //initialize
Michal Klocek
Refactors layout managment...
r1534 if(m_rect.isValid()) item->handleGeometryChanged(m_rect);
Tero Ahola
Now using either vertical or horizontal grid shades
r561 m_axisItems.insert(axis, item);
Michal Klocek
Refactors axis handling...
r223 }
Michal Klocek
Refactors QAxis to QAbstractAxis...
r1541 void ChartPresenter::handleAxisRemoved(QAbstractAxis* axis)
Michal Klocek
Refactors axis handling...
r223 {
Michal Klocek
Changes QChartAxis -> QAxis
r1006 ChartAxis* item = m_axisItems.take(axis);
Michal Klocek
Refactors axis handling...
r223 Q_ASSERT(item);
Michal Klocek
Animation refactor...
r530 if(m_animator) m_animator->removeAnimation(item);
Michal Klocek
Refactors axis handling...
r223 delete item;
}
Tero Ahola
Renamed QSeries to QAbstractSeries
r988 void ChartPresenter::handleSeriesAdded(QAbstractSeries* series,Domain* domain)
Michal Klocek
Refactors qchart , adds line animation...
r131 {
Michal Klocek
Fixes header guard style issues
r969 Chart *item = series->d_ptr->createGraphics(this);
Q_ASSERT(item);
Michal Klocek
Refactors core to support mulitpile axis and domains...
r1556 item->setDomain(domain);
Michal Klocek
Fixes header guard style issues
r969 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());
Michal Klocek
Refactors layout managment...
r1534 if(m_rect.isValid()) item->handleGeometryChanged(m_rect);
Michal Klocek
Fixes header guard style issues
r969 m_chartItems.insert(series,item);
Michal Klocek
Refactors qchart , adds line animation...
r131 }
Tero Ahola
Renamed QSeries to QAbstractSeries
r988 void ChartPresenter::handleSeriesRemoved(QAbstractSeries* series)
Michal Klocek
Refactored for MVP...
r139 {
Michal Klocek
Refactors chartitem...
r677 Chart* item = m_chartItems.take(series);
Michal Klocek
Animation refactor...
r530 Q_ASSERT(item);
Michal Klocek
Adds area chart animations...
r560 if(m_animator) {
//small hack to handle area animations
Tero Ahola
Renamed QSeries to QAbstractSeries
r988 if(series->type() == QAbstractSeries::SeriesTypeArea){
Michal Klocek
Fixes header guard style issues
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
Adds area chart animations...
r560 }else
Michal Klocek
Fixes header guard style issues
r969 m_animator->removeAnimation(item);
Michal Klocek
Adds area chart animations...
r560 }
Marek Rosa
Spline working somewhat
r401 delete item;
Michal Klocek
Refactored for MVP...
r139 }
Michal Klocek
Refactors qchart , adds line animation...
r131
Michal Klocek
Adds PIMPL to qchart
r740 void ChartPresenter::setTheme(QChart::ChartTheme theme,bool force)
Michal Klocek
Refactor themes...
r143 {
Michal Klocek
Fixes header guard style issues
r969 if(m_chartTheme && m_chartTheme->id() == theme) return;
Michal Klocek
Refactor themes...
r143 delete m_chartTheme;
m_chartTheme = ChartTheme::createTheme(theme);
Michal Klocek
Refactor to use qseries private for implmentation interface...
r943 m_chartTheme->setForced(force);
m_chartTheme->decorate(m_chart);
m_chartTheme->decorate(m_chart->legend());
Michal Klocek
Adds scroll support...
r531 resetAllElements();
Jani Honkonen
Bugfix: Adding/removing slices erased users colors....
r1093
// We do not want "force" to stay on.
// Bar/pie are calling decorate when adding/removing slices/bars which means
// that to preserve users colors "force" must not be on.
m_chartTheme->setForced(false);
Michal Klocek
Adds more axis handling...
r176 }
Michal Klocek
Adds PIMPL to qchart
r740 QChart::ChartTheme ChartPresenter::theme()
Michal Klocek
Refactor themes...
r143 {
Michal Klocek
Adds missing ids to theme classes
r153 return m_chartTheme->id();
}
Michal Klocek
Adds animation settings handling
r298 void ChartPresenter::setAnimationOptions(QChart::AnimationOptions options)
{
if(m_options!=options) {
m_options=options;
Michal Klocek
Animation refactor...
r530 if(m_options!=QChart::NoAnimation && !m_animator) {
m_animator= new ChartAnimator(this);
Michal Klocek
Adds animation settings handling
r298 }
Michal Klocek
Adds scroll support...
r531 resetAllElements();
Michal Klocek
Adds animation settings handling
r298 }
Michal Klocek
Animation refactor...
r530
Michal Klocek
Adds animation settings handling
r298 }
Michal Klocek
Adds scroll support...
r531 void ChartPresenter::resetAllElements()
{
Michal Klocek
Refactors core to support mulitpile axis and domains...
r1556 QMapIterator<QAbstractAxis*, ChartAxis*> i(m_axisItems);
while (i.hasNext()) {
i.next();
Domain* domain = i.value()->domain();
QAbstractAxis* axis = i.key();
handleAxisRemoved(axis);
handleAxisAdded(axis,domain);
Michal Klocek
Fixes header guard style issues
r969 }
Michal Klocek
Refactors core to support mulitpile axis and domains...
r1556
QMapIterator<QAbstractSeries*, Chart*> j(m_chartItems);
while (j.hasNext()) {
j.next();
Domain* domain = j.value()->domain();
QAbstractSeries* series = j.key();
handleSeriesRemoved(series);
handleSeriesAdded(series,domain);
}
Michal Klocek
Adds scroll support...
r531 }
Jani Honkonen
Add gestures support for zoomlinechart example...
r1187 void ChartPresenter::zoomIn(qreal factor)
Michal Klocek
Refactor domain model...
r439 {
Michal Klocek
Refactors layout managment...
r1534 QRectF rect = geometry();
Jani Honkonen
Add gestures support for zoomlinechart example...
r1187 rect.setWidth(rect.width()/factor);
rect.setHeight(rect.height()/factor);
Michal Klocek
Refactors layout managment...
r1534 rect.moveCenter(geometry().center());
Michal Klocek
Fixes header guard style issues
r969 zoomIn(rect);
Michal Klocek
Refactor domain model...
r439 }
void ChartPresenter::zoomIn(const QRectF& rect)
{
Michal Klocek
Fixes header guard style issues
r969 QRectF r = rect.normalized();
Michal Klocek
Refactors layout managment...
r1534 r.translate(-geometry().topLeft());
Jani Honkonen
Add gestures support for zoomlinechart example...
r1187 if (!r.isValid())
return;
Michal Klocek
Fixes header guard style issues
r969
Michal Klocek
Refactors axis animation, line animations
r1241 m_state = ZoomInState;
Michal Klocek
Refactors layout managment...
r1534 m_statePoint = QPointF(r.center().x()/geometry().width(),r.center().y()/geometry().height());
m_dataset->zoomInDomain(r,geometry().size());
Michal Klocek
Refactors axis animation, line animations
r1241 m_state = ShowState;
Michal Klocek
Refactor domain model...
r439 }
Jani Honkonen
Add gestures support for zoomlinechart example...
r1187 void ChartPresenter::zoomOut(qreal factor)
Michal Klocek
Refactor domain model...
r439 {
Michal Klocek
Refactors axis animation, line animations
r1241 m_state = ZoomOutState;
Michal Klocek
Adds loosenumber algorithm...
r678
Jani Honkonen
Add gestures support for zoomlinechart example...
r1187 QRectF chartRect;
Michal Klocek
Refactors layout managment...
r1534 chartRect.setSize(geometry().size());
Michal Klocek
Adds loosenumber algorithm...
r678
Jani Honkonen
Add gestures support for zoomlinechart example...
r1187 QRectF rect;
rect.setSize(chartRect.size()/factor);
rect.moveCenter(chartRect.center());
if (!rect.isValid())
return;
Michal Klocek
Refactors layout managment...
r1534 m_statePoint = QPointF(rect.center().x()/geometry().width(),rect.center().y()/geometry().height());
Jani Honkonen
Add gestures support for zoomlinechart example...
r1187 m_dataset->zoomOutDomain(rect, chartRect.size());
Michal Klocek
Refactors axis animation, line animations
r1241 m_state = ShowState;
Michal Klocek
Refactor domain model...
r439 }
Michal Klocek
Fix precision issue when scrolling domain
r1267 void ChartPresenter::scroll(qreal dx,qreal dy)
Michal Klocek
Adds scroll support...
r531 {
Michal Klocek
Refactors axis animation, line animations
r1241 if(dx<0) m_state=ScrollLeftState;
if(dx>0) m_state=ScrollRightState;
if(dy<0) m_state=ScrollUpState;
if(dy>0) m_state=ScrollDownState;
Michal Klocek
Adds scroll support...
r531
Michal Klocek
Refactors layout managment...
r1534 m_dataset->scrollDomain(dx,dy,geometry().size());
Michal Klocek
Refactors axis animation, line animations
r1241 m_state = ShowState;
Michal Klocek
Adds scroll support...
r531 }
Michal Klocek
Adds animation settings handling
r298 QChart::AnimationOptions ChartPresenter::animationOptions() const
{
return m_options;
}
Michal Klocek
Refactors layout managment...
r1534 void ChartPresenter::createBackgroundItem()
Michal Klocek
Refactor qledgend handling...
r855 {
Michal Klocek
Refactors layout managment...
r1534 if (!m_backgroundItem) {
m_backgroundItem = new ChartBackground(rootItem());
m_backgroundItem->setPen(Qt::NoPen);
m_backgroundItem->setZValue(ChartPresenter::BackgroundZValue);
}
}
Michal Klocek
Refactor qledgend handling...
r855
Michal Klocek
Refactors layout managment...
r1534 void ChartPresenter::createTitleItem()
{
if (!m_titleItem) {
m_titleItem = new QGraphicsSimpleTextItem(rootItem());
m_titleItem->setZValue(ChartPresenter::BackgroundZValue);
}
}
Tero Ahola
Added property definitions to QChart
r1524
Michal Klocek
Refactor qledgend handling...
r855
Michal Klocek
Refactors layout managment...
r1534 void ChartPresenter::handleAnimationFinished()
{
m_animations.removeAll(qobject_cast<ChartAnimation*>(sender()));
if(m_animations.empty()) emit animationsFinished();
}
Michal Klocek
Refactor qledgend handling...
r855
Michal Klocek
Refactors layout managment...
r1534 void ChartPresenter::startAnimation(ChartAnimation* animation)
{
if (animation->state() != QAbstractAnimation::Stopped) animation->stop();
QObject::connect(animation, SIGNAL(finished()),this,SLOT(handleAnimationFinished()),Qt::UniqueConnection);
if(!m_animations.isEmpty()){
m_animations.append(animation);
Michal Klocek
Refactor qledgend handling...
r855 }
Michal Klocek
Refactors layout managment...
r1534 QTimer::singleShot(0, animation, SLOT(start()));
}
Michal Klocek
Refactor qledgend handling...
r855
Michal Klocek
Refactors layout managment...
r1534 QGraphicsRectItem* ChartPresenter::backgroundItem()
{
return m_backgroundItem;
}
Michal Klocek
Refactor qledgend handling...
r855
Michal Klocek
Refactors layout managment...
r1534 void ChartPresenter::setBackgroundBrush(const QBrush& brush)
{
createBackgroundItem();
m_backgroundItem->setBrush(brush);
m_layout->invalidate();
}
Michal Klocek
Refactor qledgend handling...
r855
Michal Klocek
Refactors layout managment...
r1534 QBrush ChartPresenter::backgroundBrush() const
{
if (!m_backgroundItem) return QBrush();
return m_backgroundItem->brush();
}
Michal Klocek
Refactor qledgend handling...
r855
Michal Klocek
Refactors layout managment...
r1534 void ChartPresenter::setBackgroundPen(const QPen& pen)
{
createBackgroundItem();
m_backgroundItem->setPen(pen);
m_layout->invalidate();
}
Michal Klocek
Refactor qledgend handling...
r855
Michal Klocek
Refactors layout managment...
r1534 QPen ChartPresenter::backgroundPen() const
{
if (!m_backgroundItem) return QPen();
return m_backgroundItem->pen();
}
Michal Klocek
Refactor qledgend handling...
r855
Michal Klocek
Refactors layout managment...
r1534 QGraphicsItem* ChartPresenter::titleItem()
{
return m_titleItem;
}
Michal Klocek
Add minimum size back to chartview and qchart
r913
Michal Klocek
Refactors layout managment...
r1534 void ChartPresenter::setTitle(const QString& title)
{
createTitleItem();
m_titleItem->setText(title);
m_layout->invalidate();
}
Michal Klocek
Add minimum size back to chartview and qchart
r913
Michal Klocek
Refactors layout managment...
r1534 QString ChartPresenter::title() const
{
if (!m_titleItem) return QString();
return m_titleItem->text();
}
Michal Klocek
Refactor qledgend handling...
r855
Michal Klocek
Refactors layout managment...
r1534 void ChartPresenter::setTitleFont(const QFont& font)
{
createTitleItem();
m_titleItem->setFont(font);
m_layout->invalidate();
}
Michal Klocek
Refactor qledgend handling...
r855
Michal Klocek
Refactors layout managment...
r1534 QFont ChartPresenter::titleFont() const
{
if (!m_titleItem) return QFont();
return m_titleItem->font();
}
Michal Klocek
Add minimum size back to chartview and qchart
r913
Michal Klocek
Refactors layout managment...
r1534 void ChartPresenter::setTitleBrush(const QBrush &brush)
{
createTitleItem();
m_titleItem->setBrush(brush);
m_layout->invalidate();
}
Michal Klocek
Bugfixes for unnesery geometry changes
r869
Michal Klocek
Refactors layout managment...
r1534 QBrush ChartPresenter::titleBrush() const
{
if (!m_titleItem) return QBrush();
return m_titleItem->brush();
}
void ChartPresenter::setBackgroundVisible(bool visible)
{
createBackgroundItem();
m_backgroundItem->setVisible(visible);
}
Michal Klocek
Refactor , move dataset legend presentr connections to one place
r871
Michal Klocek
Refactor qledgend handling...
r855
Michal Klocek
Refactors layout managment...
r1534 bool ChartPresenter::isBackgroundVisible() const
{
if (!m_backgroundItem) return false;
return m_backgroundItem->isVisible();
Michal Klocek
Refactor qledgend handling...
r855 }
Michal Klocek
Refactors layout managment...
r1534 void ChartPresenter::setBackgroundDropShadowEnabled(bool enabled)
Michal Klocek
Refactor qledgend handling...
r855 {
Michal Klocek
Refactors layout managment...
r1534 createBackgroundItem();
m_backgroundItem->setDropShadowEnabled(enabled);
Michal Klocek
Refactor qledgend handling...
r855 }
Michal Klocek
Refactors layout managment...
r1534 bool ChartPresenter::isBackgroundDropShadowEnabled() const
Michal Klocek
Refactor qledgend handling...
r855 {
Michal Klocek
Refactors layout managment...
r1534 if (!m_backgroundItem) return false;
return m_backgroundItem->isDropShadowEnabled();
Michal Klocek
Refactor qledgend handling...
r855 }
Michal Klocek
Refactor themes...
r143
Michal Klocek
Refactors layout managment...
r1534
QGraphicsLayout* ChartPresenter::layout()
Michal Klocek
Refactors axis animation, line animations
r1241 {
Michal Klocek
Refactors layout managment...
r1534 return m_layout;
Michal Klocek
Refactors axis animation, line animations
r1241 }
Michal Klocek
Refactors layout managment...
r1534 void ChartPresenter::setMarginsMinimum(const QRectF& margins)
Michal Klocek
Refactors axis animation, line animations
r1241 {
Michal Klocek
Refactors layout managment...
r1534 Q_UNUSED(margins);
// m_layout->setMarginsMinimum(margins);
}
QRectF ChartPresenter::margins() const
{
return QRectF();//m_layout->margins();
}
QLegend* ChartPresenter::legend()
{
return m_chart->legend();
}
QList<ChartAxis*> ChartPresenter::axisItems() const
{
return m_axisItems.values();
}
void ChartPresenter::setVisible(bool visible)
{
m_chart->setVisible(visible);
Michal Klocek
Refactors axis animation, line animations
r1241 }
Michal Klocek
Refactors qchart , adds line animation...
r131 #include "moc_chartpresenter_p.cpp"
QTCOMMERCIALCHART_END_NAMESPACE