qchart.cpp
435 lines
| 11.1 KiB
| text/x-c
|
CppLexer
/ src / qchart.cpp
Michal Klocek
|
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$ | ||||
** | ||||
****************************************************************************/ | ||||
Michal Klocek
|
r12 | #include "qchart.h" | ||
Michal Klocek
|
r740 | #include "qchart_p.h" | ||
Michal Klocek
|
r950 | #include "legendscroller_p.h" | ||
#include "qlegend_p.h" | ||||
Michal Klocek
|
r1006 | #include "chartbackground_p.h" | ||
Michal Klocek
|
r1541 | #include "qabstractaxis.h" | ||
Tero Ahola
|
r42 | #include <QGraphicsScene> | ||
Michal Klocek
|
r115 | #include <QGraphicsSceneResizeEvent> | ||
Michal Klocek
|
r1534 | #include <QGraphicsLayout> | ||
Michal Klocek
|
r12 | |||
Marek Rosa
|
r277 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||
/*! | ||||
Michal Klocek
|
r740 | \enum QChart::ChartTheme | ||
Marek Rosa
|
r277 | |||
Michal Klocek
|
r740 | This enum describes the theme used by the chart. | ||
Marek Rosa
|
r277 | |||
Tero Ahola
|
r853 | \value ChartThemeLight The default theme | ||
Michal Klocek
|
r740 | \value ChartThemeBlueCerulean | ||
\value ChartThemeDark | ||||
\value ChartThemeBrownSand | ||||
\value ChartThemeBlueNcs | ||||
Tero Ahola
|
r757 | \value ChartThemeHighContrast | ||
\value ChartThemeBlueIcy | ||||
Michal Klocek
|
r740 | */ | ||
Marek Rosa
|
r277 | |||
Tero Ahola
|
r302 | /*! | ||
Michal Klocek
|
r740 | \enum QChart::AnimationOption | ||
Tero Ahola
|
r302 | |||
Michal Klocek
|
r740 | For enabling/disabling animations. Defaults to NoAnimation. | ||
Tero Ahola
|
r302 | |||
Michal Klocek
|
r740 | \value NoAnimation | ||
\value GridAxisAnimations | ||||
\value SeriesAnimations | ||||
\value AllAnimations | ||||
*/ | ||||
Tero Ahola
|
r302 | |||
Tero Ahola
|
r264 | /*! | ||
Michal Klocek
|
r740 | \class QChart | ||
\brief QtCommercial chart API. | ||||
Tero Ahola
|
r264 | |||
Michal Klocek
|
r740 | QChart is a QGraphicsWidget that you can show in a QGraphicsScene. It manages the graphical | ||
Jani Honkonen
|
r1023 | representation of different types of series and other chart related objects like | ||
QAxis and QLegend. If you simply want to show a chart in a layout, you can use the | ||||
Michal Klocek
|
r740 | convenience class QChartView instead of QChart. | ||
\sa QChartView | ||||
*/ | ||||
Tero Ahola
|
r264 | |||
Tero Ahola
|
r1526 | /*! | ||
\property QChart::animationOptions | ||||
The animation \a options for the chart. Animations are enabled/disabled based on this setting. | ||||
*/ | ||||
/*! | ||||
\property QChart::backgroundVisible | ||||
Whether the chart background is visible or not. | ||||
\sa setBackgroundBrush(), setBackgroundPen() | ||||
*/ | ||||
/*! | ||||
\property QChart::dropShadowEnabled | ||||
If set to true, the background drop shadow effect is enabled. If set to false, it is disabled. Note that the drop | ||||
shadow effect depends on theme, which means the setting may be changed if you switch to another theme. | ||||
*/ | ||||
/*! | ||||
\property QChart::margins | ||||
Margins around the plot area. Note that the margin area is used for drawing chart title, legend and axes. | ||||
*/ | ||||
/*! | ||||
\property QChart::theme | ||||
Theme is a built-in collection of UI style related settings applied for all visual elements of a chart, like colors, | ||||
pens, brushes and fonts of series, axes, title and legend. \l {Chart themes demo} shows an example with a few | ||||
different themes. | ||||
Note: changing the theme will overwrite all customizations previously applied to the series. | ||||
*/ | ||||
/*! | ||||
\property QChart::title | ||||
Title is the name (label) of a chart. It is shown as a headline on top of the chart. | ||||
*/ | ||||
/*! | ||||
\fn void QChart::marginsChanged(QRectF newMargins) | ||||
The margins around plot area have changed to \a newMargins. This may happen for example if you change title font size, | ||||
modify axes or hide/show legend. | ||||
*/ | ||||
Tero Ahola
|
r264 | /*! | ||
Michal Klocek
|
r740 | Constructs a chart object which is a child of a\a parent. Parameter \a wFlags is passed to the QGraphicsWidget constructor. | ||
*/ | ||||
Michal Klocek
|
r115 | QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags), | ||
Marek Rosa
|
r924 | d_ptr(new QChartPrivate()) | ||
Michal Klocek
|
r12 | { | ||
Michal Klocek
|
r740 | d_ptr->m_dataset = new ChartDataSet(this); | ||
d_ptr->m_presenter = new ChartPresenter(this,d_ptr->m_dataset); | ||||
Michal Klocek
|
r871 | d_ptr->createConnections(); | ||
Michal Klocek
|
r950 | d_ptr->m_legend = new LegendScroller(this); | ||
d_ptr->m_presenter->setTheme(QChart::ChartThemeLight, false); | ||||
Michal Klocek
|
r1534 | //connect(d_ptr->m_presenter, SIGNAL(marginsChanged(QRectF)), this, SIGNAL(marginsChanged(QRectF))); | ||
setLayout(d_ptr->m_presenter->layout()); | ||||
Michal Klocek
|
r12 | } | ||
Tero Ahola
|
r264 | /*! | ||
Jani Honkonen
|
r1023 | Destroys the object and it's children, like series and axis objects added to it. | ||
Michal Klocek
|
r740 | */ | ||
Tero Ahola
|
r264 | QChart::~QChart() | ||
{ | ||||
Michal Klocek
|
r686 | //delete first presenter , since this is a root of all the graphical items | ||
Michal Klocek
|
r1534 | setLayout(0); | ||
Michal Klocek
|
r740 | delete d_ptr->m_presenter; | ||
d_ptr->m_presenter=0; | ||||
Tero Ahola
|
r264 | } | ||
Michal Klocek
|
r12 | |||
Tero Ahola
|
r264 | /*! | ||
sauimone
|
r1575 | Adds the \a series onto the chart and takes the ownership of the object. | ||
Michal Klocek
|
r740 | If auto scaling is enabled, re-scales the axes the series is bound to (both the x axis and | ||
the y axis). | ||||
Jani Honkonen
|
r1316 | |||
\sa removeSeries(), removeAllSeries() | ||||
Michal Klocek
|
r740 | */ | ||
Michal Klocek
|
r1541 | void QChart::addSeries(QAbstractSeries *series) | ||
Michal Klocek
|
r223 | { | ||
Michal Klocek
|
r895 | Q_ASSERT(series); | ||
Michal Klocek
|
r1541 | d_ptr->m_dataset->addSeries(series); | ||
Michal Klocek
|
r223 | } | ||
Tero Ahola
|
r42 | |||
Marek Rosa
|
r274 | /*! | ||
Michal Klocek
|
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() | ||||
*/ | ||||
Tero Ahola
|
r988 | void QChart::removeSeries(QAbstractSeries *series) | ||
Michal Klocek
|
r223 | { | ||
Michal Klocek
|
r895 | Q_ASSERT(series); | ||
Michal Klocek
|
r740 | d_ptr->m_dataset->removeSeries(series); | ||
Tero Ahola
|
r42 | } | ||
Tero Ahola
|
r48 | |||
Marek Rosa
|
r274 | /*! | ||
Michal Klocek
|
r740 | Removes all the QChartSeries that have been added to the QChartView | ||
It also deletes the pointed QChartSeries data objects | ||||
\sa addSeries(), removeSeries() | ||||
*/ | ||||
Michal Klocek
|
r258 | void QChart::removeAllSeries() | ||
{ | ||||
Michal Klocek
|
r740 | d_ptr->m_dataset->removeAllSeries(); | ||
Michal Klocek
|
r258 | } | ||
Marek Rosa
|
r287 | /*! | ||
Michal Klocek
|
r740 | Sets the \a brush that is used for painting the background of the chart area. | ||
*/ | ||||
Michal Klocek
|
r645 | void QChart::setBackgroundBrush(const QBrush& brush) | ||
Michal Klocek
|
r122 | { | ||
Michal Klocek
|
r1534 | d_ptr->m_presenter->setBackgroundBrush(brush); | ||
Michal Klocek
|
r122 | } | ||
Marek Rosa
|
r924 | /*! | ||
Gets the brush that is used for painting the background of the chart area. | ||||
*/ | ||||
Michal Klocek
|
r645 | QBrush QChart::backgroundBrush() const | ||
{ | ||||
Michal Klocek
|
r1534 | return d_ptr->m_presenter->backgroundBrush(); | ||
Michal Klocek
|
r645 | } | ||
Marek Rosa
|
r287 | /*! | ||
Michal Klocek
|
r740 | Sets the \a pen that is used for painting the background of the chart area. | ||
*/ | ||||
Michal Klocek
|
r645 | void QChart::setBackgroundPen(const QPen& pen) | ||
Michal Klocek
|
r122 | { | ||
Michal Klocek
|
r1534 | d_ptr->m_presenter->setBackgroundPen(pen); | ||
Michal Klocek
|
r122 | } | ||
Marek Rosa
|
r924 | /*! | ||
Gets the pen that is used for painting the background of the chart area. | ||||
*/ | ||||
Michal Klocek
|
r645 | QPen QChart::backgroundPen() const | ||
{ | ||||
Michal Klocek
|
r1534 | return d_ptr->m_presenter->backgroundPen(); | ||
Michal Klocek
|
r645 | } | ||
Marek Rosa
|
r274 | /*! | ||
Michal Klocek
|
r740 | Sets the chart \a title. The description text that is drawn above the chart. | ||
*/ | ||||
Michal Klocek
|
r645 | void QChart::setTitle(const QString& title) | ||
Michal Klocek
|
r69 | { | ||
Michal Klocek
|
r1534 | d_ptr->m_presenter->setTitle(title); | ||
Michal Klocek
|
r476 | } | ||
/*! | ||||
Michal Klocek
|
r740 | Returns the chart title. The description text that is drawn above the chart. | ||
*/ | ||||
Michal Klocek
|
r645 | QString QChart::title() const | ||
Michal Klocek
|
r476 | { | ||
Michal Klocek
|
r1534 | return d_ptr->m_presenter->title(); | ||
Michal Klocek
|
r192 | } | ||
Marek Rosa
|
r274 | /*! | ||
Marek Rosa
|
r924 | Sets the \a font that is used for drawing the chart description text that is rendered above the chart. | ||
Michal Klocek
|
r740 | */ | ||
Michal Klocek
|
r645 | void QChart::setTitleFont(const QFont& font) | ||
Michal Klocek
|
r192 | { | ||
Michal Klocek
|
r1534 | d_ptr->m_presenter->setTitleFont(font); | ||
Michal Klocek
|
r69 | } | ||
Marek Rosa
|
r924 | /*! | ||
Gets the font that is used for drawing the chart description text that is rendered above the chart. | ||||
*/ | ||||
Michal Klocek
|
r895 | QFont QChart::titleFont() const | ||
{ | ||||
Michal Klocek
|
r1534 | return d_ptr->m_presenter->titleFont(); | ||
Michal Klocek
|
r895 | } | ||
Tero Ahola
|
r495 | /*! | ||
Michal Klocek
|
r740 | Sets the \a brush used for rendering the title text. | ||
*/ | ||||
Michal Klocek
|
r645 | void QChart::setTitleBrush(const QBrush &brush) | ||
Tero Ahola
|
r495 | { | ||
Michal Klocek
|
r1534 | d_ptr->m_presenter->setTitleBrush(brush); | ||
Tero Ahola
|
r495 | } | ||
/*! | ||||
Michal Klocek
|
r740 | Returns the brush used for rendering the title text. | ||
*/ | ||||
Michal Klocek
|
r645 | QBrush QChart::titleBrush() const | ||
Tero Ahola
|
r495 | { | ||
Michal Klocek
|
r1534 | return d_ptr->m_presenter->titleBrush(); | ||
Michal Klocek
|
r223 | } | ||
Michal Klocek
|
r740 | void QChart::setTheme(QChart::ChartTheme theme) | ||
Tero Ahola
|
r64 | { | ||
Michal Klocek
|
r740 | d_ptr->m_presenter->setTheme(theme); | ||
Tero Ahola
|
r64 | } | ||
Michal Klocek
|
r740 | QChart::ChartTheme QChart::theme() const | ||
Tero Ahola
|
r120 | { | ||
Michal Klocek
|
r740 | return d_ptr->m_presenter->theme(); | ||
Tero Ahola
|
r120 | } | ||
Marek Rosa
|
r285 | /*! | ||
Michal Klocek
|
r740 | Zooms in the view by a factor of 2 | ||
*/ | ||||
Michal Klocek
|
r223 | void QChart::zoomIn() | ||
Michal Klocek
|
r67 | { | ||
Jani Honkonen
|
r1187 | d_ptr->m_presenter->zoomIn(2.0); | ||
Michal Klocek
|
r67 | } | ||
Marek Rosa
|
r285 | /*! | ||
Michal Klocek
|
r740 | Zooms in the view to a maximum level at which \a rect is still fully visible. | ||
*/ | ||||
Michal Klocek
|
r223 | void QChart::zoomIn(const QRectF& rect) | ||
Michal Klocek
|
r67 | { | ||
Michal Klocek
|
r742 | if (!rect.isValid()) return; | ||
Michal Klocek
|
r740 | d_ptr->m_presenter->zoomIn(rect); | ||
Michal Klocek
|
r67 | } | ||
Marek Rosa
|
r285 | /*! | ||
Michal Klocek
|
r740 | Restores the view zoom level to the previous one. | ||
*/ | ||||
Michal Klocek
|
r67 | void QChart::zoomOut() | ||
{ | ||||
Jani Honkonen
|
r1187 | d_ptr->m_presenter->zoomOut(2.0); | ||
} | ||||
/*! | ||||
Zooms in the view by a \a factor. | ||||
A factor over 1.0 zooms the view in and factor between 0.0 and 1.0 zooms out. | ||||
*/ | ||||
void QChart::zoom(qreal factor) | ||||
{ | ||||
if (qFuzzyIsNull(factor)) | ||||
return; | ||||
if (qFuzzyCompare(factor, 1.0)) | ||||
return; | ||||
if (factor < 0) | ||||
return; | ||||
if (factor > 1.0) | ||||
d_ptr->m_presenter->zoomIn(factor); | ||||
else | ||||
d_ptr->m_presenter->zoomOut(1.0 / factor); | ||||
Michal Klocek
|
r67 | } | ||
Marek Rosa
|
r277 | /*! | ||
Michal Klocek
|
r740 | Returns the pointer to the x axis object of the chart | ||
*/ | ||||
Michal Klocek
|
r1542 | QAbstractAxis* QChart::axisX(QAbstractSeries* series) const | ||
Michal Klocek
|
r176 | { | ||
Michal Klocek
|
r1559 | return d_ptr->m_dataset->axisX(series); | ||
Michal Klocek
|
r176 | } | ||
Marek Rosa
|
r277 | /*! | ||
Marek Rosa
|
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. | ||||
Michal Klocek
|
r740 | */ | ||
Michal Klocek
|
r1541 | QAbstractAxis* QChart::axisY(QAbstractSeries *series) const | ||
Michal Klocek
|
r176 | { | ||
Michal Klocek
|
r899 | return d_ptr->m_dataset->axisY(series); | ||
Michal Klocek
|
r85 | } | ||
sauimone
|
r524 | /*! | ||
Michal Klocek
|
r740 | Returns the legend object of the chart. Ownership stays in chart. | ||
*/ | ||||
sauimone
|
r783 | QLegend* QChart::legend() const | ||
sauimone
|
r524 | { | ||
sauimone
|
r783 | return d_ptr->m_legend; | ||
sauimone
|
r524 | } | ||
Marek Rosa
|
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. | ||||
*/ | ||||
Michal Klocek
|
r874 | QRectF QChart::margins() const | ||
sauimone
|
r803 | { | ||
Michal Klocek
|
r855 | return d_ptr->m_presenter->margins(); | ||
sauimone
|
r803 | } | ||
Marek Rosa
|
r287 | /*! | ||
Michal Klocek
|
r1534 | Sets animation \a options for the chart | ||
Michal Klocek
|
r740 | */ | ||
Michal Klocek
|
r115 | |||
Michal Klocek
|
r298 | void QChart::setAnimationOptions(AnimationOptions options) | ||
{ | ||||
Michal Klocek
|
r740 | d_ptr->m_presenter->setAnimationOptions(options); | ||
Michal Klocek
|
r298 | } | ||
QChart::AnimationOptions QChart::animationOptions() const | ||||
{ | ||||
Michal Klocek
|
r740 | return d_ptr->m_presenter->animationOptions(); | ||
Michal Klocek
|
r298 | } | ||
Jani Honkonen
|
r1187 | /*! | ||
sauimone
|
r1575 | Scrolls the visible area of the chart by the distance defined in the \a dx and \a dy. | ||
Jani Honkonen
|
r1187 | */ | ||
Michal Klocek
|
r1553 | void QChart::scroll(qreal dx, qreal dy) | ||
Jani Honkonen
|
r1187 | { | ||
Michal Klocek
|
r1553 | d_ptr->m_presenter->scroll(dx, dy); | ||
Jani Honkonen
|
r1187 | } | ||
Michal Klocek
|
r740 | void QChart::setBackgroundVisible(bool visible) | ||
{ | ||||
Michal Klocek
|
r1534 | d_ptr->m_presenter->setBackgroundVisible(visible); | ||
Michal Klocek
|
r740 | } | ||
sauimone
|
r716 | |||
Michal Klocek
|
r740 | bool QChart::isBackgroundVisible() const | ||
{ | ||||
Michal Klocek
|
r1534 | return d_ptr->m_presenter->isBackgroundVisible(); | ||
sauimone
|
r716 | } | ||
Tero Ahola
|
r1462 | void QChart::setDropShadowEnabled(bool enabled) | ||
Tero Ahola
|
r1001 | { | ||
Michal Klocek
|
r1534 | d_ptr->m_presenter->setBackgroundDropShadowEnabled(enabled); | ||
Tero Ahola
|
r1001 | } | ||
Tero Ahola
|
r1462 | bool QChart::isDropShadowEnabled() const | ||
Tero Ahola
|
r1001 | { | ||
Michal Klocek
|
r1534 | return d_ptr->m_presenter->isBackgroundDropShadowEnabled(); | ||
Tero Ahola
|
r1001 | } | ||
Jani Honkonen
|
r1316 | /*! | ||
Returns all the series that are added to the chart. | ||||
\sa addSeries(), removeSeries(), removeAllSeries() | ||||
*/ | ||||
Michal Klocek
|
r1107 | QList<QAbstractSeries*> QChart::series() const | ||
{ | ||||
return d_ptr->m_dataset->series(); | ||||
} | ||||
Michal Klocek
|
r1534 | void QChart::setMarginsMinimum(const QRectF& margins) | ||
{ | ||||
d_ptr->m_presenter->setMarginsMinimum(margins); | ||||
} | ||||
Michal Klocek
|
r1541 | void QChart::setAxisX(QAbstractSeries *series, QAbstractAxis* axis) | ||
{ | ||||
sauimone
|
r1554 | d_ptr->m_dataset->setAxisX(series, axis); | ||
Michal Klocek
|
r1541 | } | ||
void QChart::setAxisY(QAbstractSeries *series, QAbstractAxis* axis) | ||||
{ | ||||
sauimone
|
r1554 | d_ptr->m_dataset->setAxisY(series, axis); | ||
Michal Klocek
|
r1541 | } | ||
Michal Klocek
|
r740 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||
Michal Klocek
|
r639 | |||
Michal Klocek
|
r855 | QChartPrivate::QChartPrivate(): | ||
Marek Rosa
|
r924 | m_legend(0), | ||
m_dataset(0), | ||||
m_presenter(0) | ||||
Michal Klocek
|
r639 | { | ||
Michal Klocek
|
r740 | |||
Michal Klocek
|
r639 | } | ||
Michal Klocek
|
r740 | QChartPrivate::~QChartPrivate() | ||
Michal Klocek
|
r639 | { | ||
Michal Klocek
|
r740 | |||
Michal Klocek
|
r639 | } | ||
Michal Klocek
|
r871 | void QChartPrivate::createConnections() | ||
{ | ||||
Michal Klocek
|
r1033 | 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*))); | ||||
Michal Klocek
|
r1541 | QObject::connect(m_dataset,SIGNAL(axisAdded(QAbstractAxis*,Domain*)),m_presenter,SLOT(handleAxisAdded(QAbstractAxis*,Domain*))); | ||
QObject::connect(m_dataset,SIGNAL(axisRemoved(QAbstractAxis*)),m_presenter,SLOT(handleAxisRemoved(QAbstractAxis*))); | ||||
Michal Klocek
|
r1534 | //QObject::connect(m_presenter, SIGNAL(marginsChanged(QRectF)), q_ptr, SIGNAL(marginsChanged(QRectF))); | ||
Michal Klocek
|
r871 | } | ||
Tero Ahola
|
r64 | #include "moc_qchart.cpp" | ||
Tero Ahola
|
r48 | |||
Tero Ahola
|
r30 | QTCOMMERCIALCHART_END_NAMESPACE | ||