chartpresenter.cpp
388 lines
| 9.4 KiB
| text/x-c
|
CppLexer
/ src / chartpresenter.cpp
|
r794 | /**************************************************************************** | ||
|
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" | ||||
|
r139 | #include "qchart.h" | ||
|
r855 | #include "qchart_p.h" | ||
|
r1541 | #include "qabstractaxis.h" | ||
|
r1556 | #include "qabstractaxis_p.h" | ||
|
r131 | #include "chartdataset_p.h" | ||
|
r143 | #include "charttheme_p.h" | ||
|
r1241 | #include "chartanimation_p.h" | ||
|
r988 | #include "qabstractseries_p.h" | ||
|
r421 | #include "qareaseries.h" | ||
|
r1006 | #include "chartaxis_p.h" | ||
#include "chartbackground_p.h" | ||||
|
r1534 | #include "chartlayout_p.h" | ||
|
r1965 | #include "charttitle_p.h" | ||
|
r1241 | #include <QTimer> | ||
|
r131 | |||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
|
r2131 | ChartPresenter::ChartPresenter(QChart *chart, ChartDataSet *dataset) | ||
: QObject(chart), | ||||
m_chart(chart), | ||||
m_dataset(dataset), | ||||
m_chartTheme(0), | ||||
m_options(QChart::NoAnimation), | ||||
m_state(ShowState), | ||||
m_layout(new ChartLayout(this)), | ||||
m_background(0), | ||||
m_title(0) | ||||
|
r131 | { | ||
|
r1241 | |||
|
r131 | } | ||
ChartPresenter::~ChartPresenter() | ||||
{ | ||||
|
r969 | delete m_chartTheme; | ||
|
r131 | } | ||
|
r2131 | void ChartPresenter::handleAxisAdded(QAbstractAxis *axis, Domain *domain) | ||
|
r131 | { | ||
|
r2131 | ChartAxis *item = axis->d_ptr->createGraphics(this); | ||
|
r1556 | item->setDomain(domain); | ||
|
r298 | |||
|
r2131 | if (m_options.testFlag(QChart::GridAxisAnimations)) | ||
|
r1241 | item->setAnimation(new AxisAnimation(item)); | ||
|
r530 | |||
|
r2131 | QObject::connect(domain, SIGNAL(updated()), item, SLOT(handleDomainUpdated())); | ||
|
r1698 | |||
|
r439 | //initialize | ||
|
r1698 | domain->emitUpdated(); | ||
m_chartTheme->decorate(axis); | ||||
axis->d_ptr->setDirty(false); | ||||
axis->d_ptr->emitUpdated(); | ||||
|
r561 | m_axisItems.insert(axis, item); | ||
|
r2105 | m_layout->invalidate(); | ||
|
r223 | } | ||
|
r2131 | void ChartPresenter::handleAxisRemoved(QAbstractAxis *axis) | ||
|
r223 | { | ||
|
r2131 | ChartAxis *item = m_axisItems.take(axis); | ||
|
r223 | Q_ASSERT(item); | ||
|
r1729 | item->hide(); | ||
item->disconnect(); | ||||
|
r2131 | QObject::disconnect(this, 0, item, 0); | ||
|
r1562 | item->deleteLater(); | ||
|
r2154 | m_layout->invalidate(); | ||
|
r223 | } | ||
|
r2131 | void ChartPresenter::handleSeriesAdded(QAbstractSeries *series, Domain *domain) | ||
|
r131 | { | ||
|
r1736 | ChartElement *item = series->d_ptr->createGraphics(this); | ||
|
r969 | Q_ASSERT(item); | ||
|
r1556 | item->setDomain(domain); | ||
|
r2131 | QObject::connect(domain, SIGNAL(updated()), item, SLOT(handleDomainUpdated())); | ||
|
r969 | //initialize | ||
|
r1698 | item->handleDomainUpdated(); | ||
|
r2131 | m_chartItems.insert(series, item); | ||
|
r2105 | m_layout->invalidate(); | ||
|
r131 | } | ||
|
r2131 | void ChartPresenter::handleSeriesRemoved(QAbstractSeries *series) | ||
|
r139 | { | ||
|
r2131 | ChartElement *item = m_chartItems.take(series); | ||
|
r530 | Q_ASSERT(item); | ||
|
r1562 | item->deleteLater(); | ||
|
r139 | } | ||
|
r131 | |||
|
r2131 | void ChartPresenter::setTheme(QChart::ChartTheme theme, bool force) | ||
|
r143 | { | ||
|
r2131 | if (m_chartTheme && m_chartTheme->id() == theme) | ||
return; | ||||
|
r143 | delete m_chartTheme; | ||
m_chartTheme = ChartTheme::createTheme(theme); | ||||
|
r943 | m_chartTheme->setForced(force); | ||
m_chartTheme->decorate(m_chart); | ||||
m_chartTheme->decorate(m_chart->legend()); | ||||
|
r531 | resetAllElements(); | ||
|
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); | ||||
|
r176 | } | ||
|
r740 | QChart::ChartTheme ChartPresenter::theme() | ||
|
r143 | { | ||
|
r153 | return m_chartTheme->id(); | ||
} | ||||
|
r298 | void ChartPresenter::setAnimationOptions(QChart::AnimationOptions options) | ||
{ | ||||
|
r2131 | if (m_options != options) { | ||
m_options = options; | ||||
|
r531 | resetAllElements(); | ||
|
r298 | } | ||
|
r530 | |||
|
r298 | } | ||
|
r531 | void ChartPresenter::resetAllElements() | ||
{ | ||||
|
r2131 | QMapIterator<QAbstractAxis *, ChartAxis *> i(m_axisItems); | ||
|
r1556 | while (i.hasNext()) { | ||
|
r2131 | i.next(); | ||
Domain *domain = i.value()->domain(); | ||||
QAbstractAxis *axis = i.key(); | ||||
handleAxisRemoved(axis); | ||||
handleAxisAdded(axis, domain); | ||||
|
r969 | } | ||
|
r1556 | |||
|
r2131 | QMapIterator<QAbstractSeries *, ChartElement *> j(m_chartItems); | ||
while (j.hasNext()) { | ||||
j.next(); | ||||
Domain *domain = j.value()->domain(); | ||||
QAbstractSeries *series = j.key(); | ||||
handleSeriesRemoved(series); | ||||
handleSeriesAdded(series, domain); | ||||
} | ||||
|
r1965 | |||
|
r2131 | layout()->invalidate(); | ||
|
r531 | } | ||
|
r1187 | void ChartPresenter::zoomIn(qreal factor) | ||
|
r439 | { | ||
|
r2105 | QRectF rect = m_layout->chartsGeometry(); | ||
|
r2131 | rect.setWidth(rect.width() / factor); | ||
rect.setHeight(rect.height() / factor); | ||||
|
r2105 | rect.moveCenter(m_layout->chartsGeometry().center()); | ||
|
r969 | zoomIn(rect); | ||
|
r439 | } | ||
|
r2131 | void ChartPresenter::zoomIn(const QRectF &rect) | ||
|
r439 | { | ||
|
r969 | QRectF r = rect.normalized(); | ||
|
r2105 | r.translate(-m_layout->chartsGeometry().topLeft()); | ||
|
r1187 | if (!r.isValid()) | ||
return; | ||||
|
r969 | |||
|
r1241 | m_state = ZoomInState; | ||
|
r2131 | m_statePoint = QPointF(r.center().x() / m_layout->chartsGeometry().width(), r.center().y() / m_layout->chartsGeometry().height()); | ||
m_dataset->zoomInDomain(r, m_layout->chartsGeometry().size()); | ||||
|
r1241 | m_state = ShowState; | ||
|
r439 | } | ||
|
r1187 | void ChartPresenter::zoomOut(qreal factor) | ||
|
r439 | { | ||
|
r1241 | m_state = ZoomOutState; | ||
|
r678 | |||
|
r1187 | QRectF chartRect; | ||
|
r2105 | chartRect.setSize(m_layout->chartsGeometry().size()); | ||
|
r678 | |||
|
r1187 | QRectF rect; | ||
|
r2131 | rect.setSize(chartRect.size() / factor); | ||
|
r1187 | rect.moveCenter(chartRect.center()); | ||
if (!rect.isValid()) | ||||
return; | ||||
|
r2131 | m_statePoint = QPointF(rect.center().x() / m_layout->chartsGeometry().width(), rect.center().y() / m_layout->chartsGeometry().height()); | ||
|
r1187 | m_dataset->zoomOutDomain(rect, chartRect.size()); | ||
|
r1241 | m_state = ShowState; | ||
|
r439 | } | ||
|
r2131 | void ChartPresenter::scroll(qreal dx, qreal dy) | ||
|
r531 | { | ||
|
r2131 | if (dx < 0) m_state = ScrollLeftState; | ||
if (dx > 0) m_state = ScrollRightState; | ||||
if (dy < 0) m_state = ScrollUpState; | ||||
if (dy > 0) m_state = ScrollDownState; | ||||
|
r531 | |||
|
r2131 | m_dataset->scrollDomain(dx, dy, m_layout->chartsGeometry().size()); | ||
|
r1241 | m_state = ShowState; | ||
|
r531 | } | ||
|
r298 | QChart::AnimationOptions ChartPresenter::animationOptions() const | ||
{ | ||||
return m_options; | ||||
} | ||||
|
r1534 | void ChartPresenter::createBackgroundItem() | ||
|
r855 | { | ||
|
r1965 | if (!m_background) { | ||
m_background = new ChartBackground(rootItem()); | ||||
m_background->setPen(Qt::NoPen); | ||||
m_background->setZValue(ChartPresenter::BackgroundZValue); | ||||
|
r1534 | } | ||
} | ||||
|
r855 | |||
|
r1534 | void ChartPresenter::createTitleItem() | ||
{ | ||||
|
r1965 | if (!m_title) { | ||
m_title = new ChartTitle(rootItem()); | ||||
m_title->setZValue(ChartPresenter::BackgroundZValue); | ||||
|
r1534 | } | ||
} | ||||
|
r1524 | |||
|
r855 | |||
|
r1534 | void ChartPresenter::handleAnimationFinished() | ||
{ | ||||
|
r2131 | m_animations.removeAll(qobject_cast<ChartAnimation *>(sender())); | ||
if (m_animations.empty()) | ||||
emit animationsFinished(); | ||||
|
r1534 | } | ||
|
r855 | |||
|
r2131 | void ChartPresenter::startAnimation(ChartAnimation *animation) | ||
|
r1534 | { | ||
|
r2105 | if (animation->state() != QAbstractAnimation::Stopped) animation->stop(); | ||
|
r2131 | QObject::connect(animation, SIGNAL(finished()), this, SLOT(handleAnimationFinished()), Qt::UniqueConnection); | ||
if (!m_animations.isEmpty()) | ||||
|
r1534 | m_animations.append(animation); | ||
QTimer::singleShot(0, animation, SLOT(start())); | ||||
} | ||||
|
r855 | |||
|
r2131 | void ChartPresenter::setBackgroundBrush(const QBrush &brush) | ||
|
r1534 | { | ||
createBackgroundItem(); | ||||
|
r1965 | m_background->setBrush(brush); | ||
|
r1534 | m_layout->invalidate(); | ||
} | ||||
|
r855 | |||
|
r1534 | QBrush ChartPresenter::backgroundBrush() const | ||
{ | ||||
|
r2131 | if (!m_background) | ||
return QBrush(); | ||||
|
r1965 | return m_background->brush(); | ||
|
r1534 | } | ||
|
r855 | |||
|
r2131 | void ChartPresenter::setBackgroundPen(const QPen &pen) | ||
|
r1534 | { | ||
createBackgroundItem(); | ||||
|
r1965 | m_background->setPen(pen); | ||
|
r1534 | m_layout->invalidate(); | ||
} | ||||
|
r855 | |||
|
r1534 | QPen ChartPresenter::backgroundPen() const | ||
{ | ||||
|
r2131 | if (!m_background) | ||
return QPen(); | ||||
|
r1965 | return m_background->pen(); | ||
|
r1534 | } | ||
|
r913 | |||
|
r2131 | void ChartPresenter::setTitle(const QString &title) | ||
|
r1534 | { | ||
createTitleItem(); | ||||
|
r1965 | m_title->setText(title); | ||
|
r1534 | m_layout->invalidate(); | ||
} | ||||
|
r913 | |||
|
r1534 | QString ChartPresenter::title() const | ||
{ | ||||
|
r2131 | if (!m_title) | ||
return QString(); | ||||
|
r1965 | return m_title->text(); | ||
|
r1534 | } | ||
|
r855 | |||
|
r2131 | void ChartPresenter::setTitleFont(const QFont &font) | ||
|
r1534 | { | ||
createTitleItem(); | ||||
|
r1965 | m_title->setFont(font); | ||
|
r1534 | m_layout->invalidate(); | ||
} | ||||
|
r855 | |||
|
r1534 | QFont ChartPresenter::titleFont() const | ||
{ | ||||
|
r2131 | if (!m_title) | ||
return QFont(); | ||||
|
r1965 | return m_title->font(); | ||
|
r1534 | } | ||
|
r913 | |||
|
r1534 | void ChartPresenter::setTitleBrush(const QBrush &brush) | ||
{ | ||||
createTitleItem(); | ||||
|
r1965 | m_title->setBrush(brush); | ||
|
r1534 | m_layout->invalidate(); | ||
} | ||||
|
r869 | |||
|
r1534 | QBrush ChartPresenter::titleBrush() const | ||
{ | ||||
|
r2131 | if (!m_title) | ||
return QBrush(); | ||||
|
r1965 | return m_title->brush(); | ||
|
r1534 | } | ||
void ChartPresenter::setBackgroundVisible(bool visible) | ||||
{ | ||||
createBackgroundItem(); | ||||
|
r1965 | m_background->setVisible(visible); | ||
|
r1534 | } | ||
|
r871 | |||
|
r855 | |||
|
r1534 | bool ChartPresenter::isBackgroundVisible() const | ||
{ | ||||
|
r2131 | if (!m_background) | ||
return false; | ||||
|
r1965 | return m_background->isVisible(); | ||
|
r855 | } | ||
|
r1534 | void ChartPresenter::setBackgroundDropShadowEnabled(bool enabled) | ||
|
r855 | { | ||
|
r1534 | createBackgroundItem(); | ||
|
r1965 | m_background->setDropShadowEnabled(enabled); | ||
|
r855 | } | ||
|
r1534 | bool ChartPresenter::isBackgroundDropShadowEnabled() const | ||
|
r855 | { | ||
|
r2131 | if (!m_background) | ||
return false; | ||||
|
r1965 | return m_background->isDropShadowEnabled(); | ||
|
r855 | } | ||
|
r143 | |||
|
r1534 | |||
|
r2131 | ChartLayout *ChartPresenter::layout() | ||
|
r1241 | { | ||
|
r1534 | return m_layout; | ||
|
r1241 | } | ||
|
r2131 | QLegend *ChartPresenter::legend() | ||
|
r1534 | { | ||
return m_chart->legend(); | ||||
} | ||||
|
r1965 | void ChartPresenter::setVisible(bool visible) | ||
{ | ||||
m_chart->setVisible(visible); | ||||
} | ||||
|
r2131 | ChartBackground *ChartPresenter::backgroundElement() | ||
|
r1965 | { | ||
return m_background; | ||||
} | ||||
|
r2131 | QList<ChartAxis *> ChartPresenter::axisItems() const | ||
|
r1534 | { | ||
return m_axisItems.values(); | ||||
} | ||||
|
r2131 | QList<ChartElement *> ChartPresenter::chartItems() const | ||
|
r2105 | { | ||
return m_chartItems.values(); | ||||
} | ||||
|
r2131 | ChartTitle *ChartPresenter::titleElement() | ||
|
r1534 | { | ||
|
r1965 | return m_title; | ||
|
r1241 | } | ||
|
r131 | #include "moc_chartpresenter_p.cpp" | ||
QTCOMMERCIALCHART_END_NAMESPACE | ||||