qchart.cpp
436 lines
| 11.4 KiB
| text/x-c
|
CppLexer
/ src / qchart.cpp
|
r746 | /**************************************************************************** | ||
** | ||||
** 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$ | ||||
** | ||||
****************************************************************************/ | ||||
|
r12 | #include "qchart.h" | ||
|
r740 | #include "qchart_p.h" | ||
|
r950 | #include "legendscroller_p.h" | ||
#include "qlegend_p.h" | ||||
|
r1006 | #include "chartbackground_p.h" | ||
#include "qaxis.h" | ||||
|
r42 | #include <QGraphicsScene> | ||
|
r115 | #include <QGraphicsSceneResizeEvent> | ||
|
r12 | |||
|
r277 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||
/*! | ||||
|
r740 | \enum QChart::ChartTheme | ||
|
r277 | |||
|
r740 | This enum describes the theme used by the chart. | ||
|
r277 | |||
|
r853 | \value ChartThemeLight The default theme | ||
|
r740 | \value ChartThemeBlueCerulean | ||
\value ChartThemeDark | ||||
\value ChartThemeBrownSand | ||||
\value ChartThemeBlueNcs | ||||
|
r757 | \value ChartThemeHighContrast | ||
\value ChartThemeBlueIcy | ||||
|
r740 | */ | ||
|
r277 | |||
|
r302 | /*! | ||
|
r740 | \enum QChart::AnimationOption | ||
|
r302 | |||
|
r740 | For enabling/disabling animations. Defaults to NoAnimation. | ||
|
r302 | |||
|
r740 | \value NoAnimation | ||
\value GridAxisAnimations | ||||
\value SeriesAnimations | ||||
\value AllAnimations | ||||
*/ | ||||
|
r302 | |||
|
r264 | /*! | ||
|
r740 | \class QChart | ||
\brief QtCommercial chart API. | ||||
|
r264 | |||
|
r740 | QChart is a QGraphicsWidget that you can show in a QGraphicsScene. It manages the graphical | ||
representation of different types of QChartSeries and other chart related objects like | ||||
QChartAxis and QChartLegend. If you simply want to show a chart in a layout, you can use the | ||||
convenience class QChartView instead of QChart. | ||||
\sa QChartView | ||||
*/ | ||||
|
r264 | |||
/*! | ||||
|
r740 | Constructs a chart object which is a child of a\a parent. Parameter \a wFlags is passed to the QGraphicsWidget constructor. | ||
*/ | ||||
|
r115 | QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags), | ||
|
r924 | d_ptr(new QChartPrivate()) | ||
|
r12 | { | ||
|
r740 | d_ptr->m_dataset = new ChartDataSet(this); | ||
d_ptr->m_presenter = new ChartPresenter(this,d_ptr->m_dataset); | ||||
|
r871 | d_ptr->createConnections(); | ||
|
r950 | d_ptr->m_legend = new LegendScroller(this); | ||
d_ptr->m_presenter->setTheme(QChart::ChartThemeLight, false); | ||||
|
r12 | } | ||
|
r264 | /*! | ||
|
r740 | Destroys the object and it's children, like QChartSeries and QChartAxis object added to it. | ||
*/ | ||||
|
r264 | QChart::~QChart() | ||
{ | ||||
|
r686 | //delete first presenter , since this is a root of all the graphical items | ||
|
r740 | delete d_ptr->m_presenter; | ||
d_ptr->m_presenter=0; | ||||
|
r264 | } | ||
|
r12 | |||
|
r264 | /*! | ||
|
r740 | Adds the \a series and optional \a axisY onto the chart and takes the ownership of the objects. | ||
If auto scaling is enabled, re-scales the axes the series is bound to (both the x axis and | ||||
the y axis). | ||||
*/ | ||||
|
r1006 | void QChart::addSeries(QAbstractSeries *series, QAxis *axisY) | ||
|
r223 | { | ||
|
r895 | Q_ASSERT(series); | ||
|
r740 | d_ptr->m_dataset->addSeries(series, axisY); | ||
|
r223 | } | ||
|
r42 | |||
|
r274 | /*! | ||
|
r740 | Removes the \a series specified in a perameter from the QChartView. | ||
It releses its ownership of the specified QChartSeries object. | ||||
It does not delete the pointed QChartSeries data object | ||||
\sa addSeries(), removeAllSeries() | ||||
*/ | ||||
|
r988 | void QChart::removeSeries(QAbstractSeries *series) | ||
|
r223 | { | ||
|
r895 | Q_ASSERT(series); | ||
|
r740 | d_ptr->m_dataset->removeSeries(series); | ||
|
r42 | } | ||
|
r48 | |||
|
r274 | /*! | ||
|
r740 | Removes all the QChartSeries that have been added to the QChartView | ||
It also deletes the pointed QChartSeries data objects | ||||
\sa addSeries(), removeSeries() | ||||
*/ | ||||
|
r258 | void QChart::removeAllSeries() | ||
{ | ||||
|
r740 | d_ptr->m_dataset->removeAllSeries(); | ||
|
r258 | } | ||
|
r287 | /*! | ||
|
r740 | Sets the \a brush that is used for painting the background of the chart area. | ||
*/ | ||||
|
r645 | void QChart::setBackgroundBrush(const QBrush& brush) | ||
|
r122 | { | ||
|
r872 | //TODO: refactor me | ||
|
r855 | d_ptr->m_presenter->createChartBackgroundItem(); | ||
d_ptr->m_presenter->m_backgroundItem->setBrush(brush); | ||||
d_ptr->m_presenter->m_backgroundItem->update(); | ||||
|
r122 | } | ||
|
r924 | /*! | ||
Gets the brush that is used for painting the background of the chart area. | ||||
*/ | ||||
|
r645 | QBrush QChart::backgroundBrush() const | ||
{ | ||||
|
r872 | //TODO: refactor me | ||
|
r855 | if (!d_ptr->m_presenter->m_backgroundItem) return QBrush(); | ||
return (d_ptr->m_presenter->m_backgroundItem)->brush(); | ||||
|
r645 | } | ||
|
r287 | /*! | ||
|
r740 | Sets the \a pen that is used for painting the background of the chart area. | ||
*/ | ||||
|
r645 | void QChart::setBackgroundPen(const QPen& pen) | ||
|
r122 | { | ||
|
r872 | //TODO: refactor me | ||
|
r855 | d_ptr->m_presenter->createChartBackgroundItem(); | ||
d_ptr->m_presenter->m_backgroundItem->setPen(pen); | ||||
d_ptr->m_presenter->m_backgroundItem->update(); | ||||
|
r122 | } | ||
|
r924 | /*! | ||
Gets the pen that is used for painting the background of the chart area. | ||||
*/ | ||||
|
r645 | QPen QChart::backgroundPen() const | ||
{ | ||||
|
r872 | //TODO: refactor me | ||
|
r855 | if (!d_ptr->m_presenter->m_backgroundItem) return QPen(); | ||
return d_ptr->m_presenter->m_backgroundItem->pen(); | ||||
|
r645 | } | ||
|
r274 | /*! | ||
|
r740 | Sets the chart \a title. The description text that is drawn above the chart. | ||
*/ | ||||
|
r645 | void QChart::setTitle(const QString& title) | ||
|
r69 | { | ||
|
r872 | //TODO: refactor me | ||
|
r855 | d_ptr->m_presenter->createChartTitleItem(); | ||
d_ptr->m_presenter->m_titleItem->setText(title); | ||||
d_ptr->m_presenter->updateLayout(); | ||||
|
r476 | } | ||
/*! | ||||
|
r740 | Returns the chart title. The description text that is drawn above the chart. | ||
*/ | ||||
|
r645 | QString QChart::title() const | ||
|
r476 | { | ||
|
r872 | //TODO: refactor me | ||
|
r855 | if (d_ptr->m_presenter->m_titleItem) | ||
|
r895 | return d_ptr->m_presenter->m_titleItem->text(); | ||
|
r476 | else | ||
|
r895 | return QString(); | ||
|
r192 | } | ||
|
r274 | /*! | ||
|
r924 | Sets the \a font that is used for drawing the chart description text that is rendered above the chart. | ||
|
r740 | */ | ||
|
r645 | void QChart::setTitleFont(const QFont& font) | ||
|
r192 | { | ||
|
r872 | //TODO: refactor me | ||
|
r855 | d_ptr->m_presenter->createChartTitleItem(); | ||
d_ptr->m_presenter->m_titleItem->setFont(font); | ||||
d_ptr->m_presenter->updateLayout(); | ||||
|
r69 | } | ||
|
r924 | /*! | ||
Gets the font that is used for drawing the chart description text that is rendered above the chart. | ||||
*/ | ||||
|
r895 | QFont QChart::titleFont() const | ||
{ | ||||
if (d_ptr->m_presenter->m_titleItem) | ||||
return d_ptr->m_presenter->m_titleItem->font(); | ||||
else | ||||
return QFont(); | ||||
} | ||||
|
r495 | /*! | ||
|
r740 | Sets the \a brush used for rendering the title text. | ||
*/ | ||||
|
r645 | void QChart::setTitleBrush(const QBrush &brush) | ||
|
r495 | { | ||
|
r872 | //TODO: refactor me | ||
|
r855 | d_ptr->m_presenter->createChartTitleItem(); | ||
d_ptr->m_presenter->m_titleItem->setBrush(brush); | ||||
d_ptr->m_presenter->updateLayout(); | ||||
|
r495 | } | ||
/*! | ||||
|
r740 | Returns the brush used for rendering the title text. | ||
*/ | ||||
|
r645 | QBrush QChart::titleBrush() const | ||
|
r495 | { | ||
|
r872 | //TODO: refactor me | ||
|
r855 | if (!d_ptr->m_presenter->m_titleItem) return QBrush(); | ||
return d_ptr->m_presenter->m_titleItem->brush(); | ||||
|
r223 | } | ||
|
r277 | /*! | ||
|
r740 | Sets the \a theme used by the chart for rendering the graphical representation of the data | ||
|
r909 | \sa theme() | ||
|
r740 | */ | ||
void QChart::setTheme(QChart::ChartTheme theme) | ||||
|
r64 | { | ||
|
r740 | d_ptr->m_presenter->setTheme(theme); | ||
|
r64 | } | ||
|
r277 | /*! | ||
|
r740 | Returns the theme enum used by the chart. | ||
|
r909 | \sa ChartTheme, setTheme() | ||
|
r740 | */ | ||
QChart::ChartTheme QChart::theme() const | ||||
|
r120 | { | ||
|
r740 | return d_ptr->m_presenter->theme(); | ||
|
r120 | } | ||
|
r285 | /*! | ||
|
r740 | Zooms in the view by a factor of 2 | ||
*/ | ||||
|
r223 | void QChart::zoomIn() | ||
|
r67 | { | ||
|
r740 | d_ptr->m_presenter->zoomIn(); | ||
|
r67 | } | ||
|
r285 | /*! | ||
|
r740 | Zooms in the view to a maximum level at which \a rect is still fully visible. | ||
*/ | ||||
|
r223 | void QChart::zoomIn(const QRectF& rect) | ||
|
r67 | { | ||
|
r742 | if (!rect.isValid()) return; | ||
|
r740 | d_ptr->m_presenter->zoomIn(rect); | ||
|
r67 | } | ||
|
r285 | /*! | ||
|
r740 | Restores the view zoom level to the previous one. | ||
*/ | ||||
|
r67 | void QChart::zoomOut() | ||
{ | ||||
|
r740 | d_ptr->m_presenter->zoomOut(); | ||
|
r67 | } | ||
|
r277 | /*! | ||
|
r740 | Returns the pointer to the x axis object of the chart | ||
*/ | ||||
|
r1006 | QAxis* QChart::axisX() const | ||
|
r176 | { | ||
|
r740 | return d_ptr->m_dataset->axisX(); | ||
|
r176 | } | ||
|
r277 | /*! | ||
|
r924 | Returns the pointer to the y axis object of the \a series | ||
If no \a series is provided then default Y axis of the chart is returned. | ||||
|
r740 | */ | ||
|
r1006 | QAxis* QChart::axisY(QAbstractSeries *series) const | ||
|
r176 | { | ||
|
r899 | return d_ptr->m_dataset->axisY(series); | ||
|
r85 | } | ||
|
r524 | /*! | ||
|
r740 | Returns the legend object of the chart. Ownership stays in chart. | ||
*/ | ||||
|
r783 | QLegend* QChart::legend() const | ||
|
r524 | { | ||
|
r783 | return d_ptr->m_legend; | ||
|
r524 | } | ||
|
r924 | /*! | ||
Returns the rect that contains information about margins (distance between chart widget edge and axes). | ||||
Individual margins can be obtained by calling left, top, right, bottom on the returned rect. | ||||
*/ | ||||
|
r874 | QRectF QChart::margins() const | ||
|
r803 | { | ||
|
r855 | return d_ptr->m_presenter->margins(); | ||
|
r803 | } | ||
|
r287 | /*! | ||
|
r740 | Resizes and updates the chart area using the \a event data | ||
*/ | ||||
|
r115 | void QChart::resizeEvent(QGraphicsSceneResizeEvent *event) | ||
{ | ||||
|
r740 | d_ptr->m_rect = QRectF(QPoint(0,0),event->newSize()); | ||
|
r117 | QGraphicsWidget::resizeEvent(event); | ||
|
r855 | d_ptr->m_presenter->setGeometry(d_ptr->m_rect); | ||
|
r115 | } | ||
|
r298 | /*! | ||
|
r740 | Sets animation \a options for the chart | ||
*/ | ||||
|
r298 | void QChart::setAnimationOptions(AnimationOptions options) | ||
{ | ||||
|
r740 | d_ptr->m_presenter->setAnimationOptions(options); | ||
|
r298 | } | ||
/*! | ||||
|
r740 | Returns animation options for the chart | ||
*/ | ||||
|
r298 | QChart::AnimationOptions QChart::animationOptions() const | ||
{ | ||||
|
r740 | return d_ptr->m_presenter->animationOptions(); | ||
|
r298 | } | ||
|
r924 | /*! | ||
Scrolls the visible area of the chart to the left by the distance between two x axis ticks | ||||
*/ | ||||
|
r600 | void QChart::scrollLeft() | ||
{ | ||||
|
r855 | d_ptr->m_presenter->scroll(-d_ptr->m_presenter->chartGeometry().width()/(axisX()->ticksCount()-1),0); | ||
|
r600 | } | ||
|
r924 | /*! | ||
Scrolls the visible area of the chart to the right by the distance between two x axis ticks | ||||
*/ | ||||
|
r600 | void QChart::scrollRight() | ||
{ | ||||
|
r855 | d_ptr->m_presenter->scroll(d_ptr->m_presenter->chartGeometry().width()/(axisX()->ticksCount()-1),0); | ||
|
r600 | } | ||
|
r740 | |||
|
r924 | /*! | ||
Scrolls the visible area of the chart up by the distance between two y axis ticks | ||||
*/ | ||||
|
r600 | void QChart::scrollUp() | ||
{ | ||||
|
r855 | d_ptr->m_presenter->scroll(0,d_ptr->m_presenter->chartGeometry().width()/(axisY()->ticksCount()-1)); | ||
|
r600 | } | ||
|
r740 | |||
|
r924 | /*! | ||
Scrolls the visible area of the chart down by the distance between two y axis ticks | ||||
*/ | ||||
|
r600 | void QChart::scrollDown() | ||
{ | ||||
|
r855 | d_ptr->m_presenter->scroll(0,-d_ptr->m_presenter->chartGeometry().width()/(axisY()->ticksCount()-1)); | ||
|
r531 | } | ||
|
r924 | /*! | ||
Sets the chart background visibility state to \a visible | ||||
*/ | ||||
|
r740 | void QChart::setBackgroundVisible(bool visible) | ||
{ | ||||
|
r872 | //TODO: refactor me | ||
|
r855 | d_ptr->m_presenter->createChartBackgroundItem(); | ||
d_ptr->m_presenter->m_backgroundItem->setVisible(visible); | ||||
|
r740 | } | ||
|
r716 | |||
|
r924 | /*! | ||
Returns the chart's background visibility state | ||||
*/ | ||||
|
r740 | bool QChart::isBackgroundVisible() const | ||
{ | ||||
|
r872 | //TODO: refactor me | ||
|
r1001 | if (!d_ptr->m_presenter->m_backgroundItem) | ||
return false; | ||||
|
r855 | return d_ptr->m_presenter->m_backgroundItem->isVisible(); | ||
|
r716 | } | ||
|
r1002 | /*! | ||
Sets the background drop shadow effect state to \a enabled. | ||||
*/ | ||||
|
r1001 | void QChart::setBackgroundDropShadowEnabled(bool enabled) | ||
{ | ||||
d_ptr->m_presenter->createChartBackgroundItem(); | ||||
d_ptr->m_presenter->m_backgroundItem->setDropShadowEnabled(enabled); | ||||
} | ||||
|
r1002 | /*! | ||
Returns true if the drop shadow effect is enabled for the chart background. | ||||
*/ | ||||
|
r1001 | bool QChart::isBackgroundDropShadowEnabled() const | ||
{ | ||||
if (!d_ptr->m_presenter->m_backgroundItem) | ||||
return false; | ||||
return d_ptr->m_presenter->m_backgroundItem->isDropShadowEnabled(); | ||||
} | ||||
|
r740 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||
|
r639 | |||
|
r855 | QChartPrivate::QChartPrivate(): | ||
|
r924 | m_legend(0), | ||
m_dataset(0), | ||||
m_presenter(0) | ||||
|
r639 | { | ||
|
r740 | |||
|
r639 | } | ||
|
r740 | QChartPrivate::~QChartPrivate() | ||
|
r639 | { | ||
|
r740 | |||
|
r639 | } | ||
|
r871 | void QChartPrivate::createConnections() | ||
{ | ||||
|
r950 | |||
|
r988 | QObject::connect(m_dataset,SIGNAL(seriesAdded(QAbstractSeries *, Domain *)),m_presenter,SLOT(handleSeriesAdded(QAbstractSeries *, Domain *))); | ||
QObject::connect(m_dataset,SIGNAL(seriesRemoved(QAbstractSeries *)),m_presenter,SLOT(handleSeriesRemoved(QAbstractSeries *))); | ||||
|
r1006 | QObject::connect(m_dataset,SIGNAL(axisAdded(QAxis*,Domain*)),m_presenter,SLOT(handleAxisAdded(QAxis*,Domain*))); | ||
QObject::connect(m_dataset,SIGNAL(axisRemoved(QAxis*)),m_presenter,SLOT(handleAxisRemoved(QAxis*))); | ||||
|
r871 | } | ||
|
r64 | #include "moc_qchart.cpp" | ||
|
r48 | |||
|
r30 | QTCOMMERCIALCHART_END_NAMESPACE | ||