qchart.cpp
388 lines
| 8.9 KiB
| text/x-c
|
CppLexer
/ src / qchart.cpp
|
r12 | #include "qchart.h" | ||
|
r85 | #include "qchartaxis.h" | ||
|
r524 | #include "qlegend.h" | ||
|
r131 | #include "chartpresenter_p.h" | ||
#include "chartdataset_p.h" | ||||
|
r639 | #include "chartbackground_p.h" | ||
|
r42 | #include <QGraphicsScene> | ||
|
r115 | #include <QGraphicsSceneResizeEvent> | ||
|
r21 | #include <QDebug> | ||
|
r12 | |||
|
r277 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||
/*! | ||||
\enum QChart::ChartTheme | ||||
This enum describes the theme used by the chart. | ||||
|
r581 | \value ChartThemeDefault Follows the GUI style of the Operating System | ||
|
r277 | \value ChartThemeVanilla | ||
\value ChartThemeIcy | ||||
\value ChartThemeGrayscale | ||||
\value ChartThemeScientific | ||||
|
r581 | \value ChartThemeBlueCerulean | ||
|
r584 | \value ChartThemeLight | ||
|
r581 | \value ChartThemeCount Not really a theme; the total count of themes. | ||
|
r277 | */ | ||
|
r302 | /*! | ||
\enum QChart::AnimationOption | ||||
For enabling/disabling animations. Defaults to NoAnimation. | ||||
\value NoAnimation | ||||
\value GridAxisAnimations | ||||
\value SeriesAnimations | ||||
\value AllAnimations | ||||
*/ | ||||
|
r264 | /*! | ||
\class QChart | ||||
\brief QtCommercial chart API. | ||||
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. | ||||
|
r274 | \sa QChartView | ||
|
r264 | */ | ||
/*! | ||||
|
r287 | Constructs a chart object which is a child of a\a parent. Parameter \a wFlags is passed to the QGraphicsWidget constructor. | ||
|
r264 | */ | ||
|
r115 | QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags), | ||
|
r264 | m_backgroundItem(0), | ||
m_titleItem(0), | ||||
|
r550 | m_legend(new QLegend(this)), | ||
|
r264 | m_dataset(new ChartDataSet(this)), | ||
|
r639 | m_presenter(new ChartPresenter(this,m_dataset)), | ||
m_padding(50), | ||||
m_backgroundPadding(10) | ||||
|
r12 | { | ||
|
r550 | connect(m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),m_legend,SLOT(handleSeriesAdded(QSeries*,Domain*))); | ||
connect(m_dataset,SIGNAL(seriesRemoved(QSeries*)),m_legend,SLOT(handleSeriesRemoved(QSeries*))); | ||||
|
r12 | } | ||
|
r264 | /*! | ||
Destroys the object and it's children, like QChartSeries and QChartAxis object added to it. | ||||
*/ | ||||
QChart::~QChart() | ||||
{ | ||||
} | ||||
|
r12 | |||
|
r264 | /*! | ||
|
r285 | Adds the \a series and optional \a axisY onto the chart and takes the ownership of the objects. | ||
|
r264 | If auto scaling is enabled, re-scales the axes the series is bound to (both the x axis and | ||
the y axis). | ||||
*/ | ||||
|
r360 | void QChart::addSeries(QSeries* series, QChartAxis* axisY) | ||
|
r223 | { | ||
|
r264 | m_dataset->addSeries(series, axisY); | ||
|
r223 | } | ||
|
r42 | |||
|
r274 | /*! | ||
|
r285 | Removes the \a series specified in a perameter from the QChartView. | ||
|
r274 | It releses its ownership of the specified QChartSeries object. | ||
It does not delete the pointed QChartSeries data object | ||||
|
r285 | \sa addSeries(), removeAllSeries() | ||
|
r274 | */ | ||
|
r360 | void QChart::removeSeries(QSeries* series) | ||
|
r223 | { | ||
m_dataset->removeSeries(series); | ||||
|
r42 | } | ||
|
r48 | |||
|
r274 | /*! | ||
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() | ||
{ | ||||
m_dataset->removeAllSeries(); | ||||
} | ||||
|
r287 | /*! | ||
Sets the \a brush that is used for painting the background of the chart area. | ||||
*/ | ||||
|
r645 | void QChart::setBackgroundBrush(const QBrush& brush) | ||
|
r122 | { | ||
|
r223 | createChartBackgroundItem(); | ||
|
r122 | m_backgroundItem->setBrush(brush); | ||
m_backgroundItem->update(); | ||||
} | ||||
|
r645 | QBrush QChart::backgroundBrush() const | ||
{ | ||||
if(!m_backgroundItem) return QBrush(); | ||||
return m_backgroundItem->brush(); | ||||
} | ||||
|
r287 | /*! | ||
Sets the \a pen that is used for painting the background of the chart area. | ||||
*/ | ||||
|
r645 | void QChart::setBackgroundPen(const QPen& pen) | ||
|
r122 | { | ||
|
r223 | createChartBackgroundItem(); | ||
|
r122 | m_backgroundItem->setPen(pen); | ||
m_backgroundItem->update(); | ||||
} | ||||
|
r645 | QPen QChart::backgroundPen() const | ||
{ | ||||
if(!m_backgroundItem) return QPen(); | ||||
return m_backgroundItem->pen(); | ||||
} | ||||
|
r274 | /*! | ||
|
r481 | Sets the chart \a title. The description text that is drawn above the chart. | ||
|
r274 | */ | ||
|
r645 | void QChart::setTitle(const QString& title) | ||
|
r69 | { | ||
|
r223 | createChartTitleItem(); | ||
|
r476 | m_titleItem->setText(title); | ||
|
r571 | updateLayout(); | ||
|
r476 | } | ||
/*! | ||||
|
r481 | Returns the chart title. The description text that is drawn above the chart. | ||
|
r476 | */ | ||
|
r645 | QString QChart::title() const | ||
|
r476 | { | ||
if(m_titleItem) | ||||
return m_titleItem->text(); | ||||
else | ||||
return QString(); | ||||
|
r192 | } | ||
|
r274 | /*! | ||
|
r277 | Sets the \a font that is used for rendering the description text that is rendered above the chart. | ||
|
r274 | */ | ||
|
r645 | void QChart::setTitleFont(const QFont& font) | ||
|
r192 | { | ||
|
r223 | createChartTitleItem(); | ||
|
r87 | m_titleItem->setFont(font); | ||
|
r571 | updateLayout(); | ||
|
r69 | } | ||
|
r495 | /*! | ||
Sets the \a brush used for rendering the title text. | ||||
*/ | ||||
|
r645 | void QChart::setTitleBrush(const QBrush &brush) | ||
|
r495 | { | ||
createChartTitleItem(); | ||||
m_titleItem->setBrush(brush); | ||||
|
r571 | updateLayout(); | ||
|
r495 | } | ||
/*! | ||||
Returns the brush used for rendering the title text. | ||||
*/ | ||||
|
r645 | QBrush QChart::titleBrush() const | ||
|
r495 | { | ||
|
r645 | if(!m_titleItem) return QBrush(); | ||
|
r495 | return m_titleItem->brush(); | ||
} | ||||
|
r223 | void QChart::createChartBackgroundItem() | ||
{ | ||||
if(!m_backgroundItem) { | ||||
|
r639 | m_backgroundItem = new ChartBackground(this); | ||
|
r272 | m_backgroundItem->setPen(Qt::NoPen); | ||
|
r262 | m_backgroundItem->setZValue(ChartPresenter::BackgroundZValue); | ||
|
r223 | } | ||
} | ||||
void QChart::createChartTitleItem() | ||||
{ | ||||
|
r262 | if(!m_titleItem) { | ||
|
r476 | m_titleItem = new QGraphicsSimpleTextItem(this); | ||
|
r262 | m_titleItem->setZValue(ChartPresenter::BackgroundZValue); | ||
} | ||||
|
r223 | } | ||
|
r277 | /*! | ||
|
r285 | Sets the \a theme used by the chart for rendering the graphical representation of the data | ||
|
r277 | \sa ChartTheme, chartTheme() | ||
*/ | ||||
|
r153 | void QChart::setChartTheme(QChart::ChartTheme theme) | ||
|
r64 | { | ||
|
r153 | m_presenter->setChartTheme(theme); | ||
|
r64 | } | ||
|
r277 | /*! | ||
Returns the theme enum used by the chart. | ||||
\sa ChartTheme, setChartTheme() | ||||
*/ | ||||
|
r153 | QChart::ChartTheme QChart::chartTheme() const | ||
|
r120 | { | ||
|
r153 | return m_presenter->chartTheme(); | ||
|
r120 | } | ||
|
r285 | /*! | ||
Zooms in the view by a factor of 2 | ||||
*/ | ||||
|
r223 | void QChart::zoomIn() | ||
|
r67 | { | ||
|
r439 | m_presenter->zoomIn(); | ||
|
r67 | } | ||
|
r285 | /*! | ||
Zooms in the view to a maximum level at which \a rect is still fully visible. | ||||
*/ | ||||
|
r223 | void QChart::zoomIn(const QRectF& rect) | ||
|
r67 | { | ||
|
r439 | |||
|
r223 | if(!rect.isValid()) return; | ||
|
r439 | m_presenter->zoomIn(rect); | ||
|
r67 | } | ||
|
r285 | /*! | ||
Restores the view zoom level to the previous one. | ||||
*/ | ||||
|
r67 | void QChart::zoomOut() | ||
{ | ||||
|
r439 | m_presenter->zoomOut(); | ||
|
r67 | } | ||
|
r277 | /*! | ||
Returns the pointer to the x axis object of the chart | ||||
*/ | ||||
|
r223 | QChartAxis* QChart::axisX() const | ||
|
r176 | { | ||
|
r223 | return m_dataset->axisX(); | ||
|
r176 | } | ||
|
r277 | /*! | ||
Returns the pointer to the y axis object of the chart | ||||
*/ | ||||
|
r223 | QChartAxis* QChart::axisY() const | ||
|
r176 | { | ||
|
r223 | return m_dataset->axisY(); | ||
|
r85 | } | ||
|
r524 | /*! | ||
|
r652 | Returns the legend object of the chart. Ownership stays in chart. | ||
|
r524 | */ | ||
|
r652 | QLegend* QChart::legend() const | ||
|
r524 | { | ||
return m_legend; | ||||
} | ||||
|
r287 | /*! | ||
Resizes and updates the chart area using the \a event data | ||||
*/ | ||||
|
r115 | void QChart::resizeEvent(QGraphicsSceneResizeEvent *event) | ||
{ | ||||
m_rect = QRectF(QPoint(0,0),event->newSize()); | ||||
|
r571 | updateLayout(); | ||
|
r117 | QGraphicsWidget::resizeEvent(event); | ||
|
r115 | update(); | ||
} | ||||
|
r298 | /*! | ||
|
r302 | Sets animation \a options for the chart | ||
|
r298 | */ | ||
void QChart::setAnimationOptions(AnimationOptions options) | ||||
{ | ||||
m_presenter->setAnimationOptions(options); | ||||
} | ||||
/*! | ||||
Returns animation options for the chart | ||||
*/ | ||||
QChart::AnimationOptions QChart::animationOptions() const | ||||
{ | ||||
return m_presenter->animationOptions(); | ||||
} | ||||
|
r600 | void QChart::scrollLeft() | ||
{ | ||||
m_presenter->scroll(-m_presenter->geometry().width()/(axisX()->ticksCount()-1),0); | ||||
} | ||||
void QChart::scrollRight() | ||||
{ | ||||
m_presenter->scroll(m_presenter->geometry().width()/(axisX()->ticksCount()-1),0); | ||||
} | ||||
void QChart::scrollUp() | ||||
{ | ||||
m_presenter->scroll(0,m_presenter->geometry().width()/(axisY()->ticksCount()-1)); | ||||
} | ||||
void QChart::scrollDown() | ||||
{ | ||||
|
r531 | m_presenter->scroll(0,-m_presenter->geometry().width()/(axisY()->ticksCount()-1)); | ||
} | ||||
|
r571 | void QChart::updateLayout() | ||
{ | ||||
if(!m_rect.isValid()) return; | ||||
|
r639 | QRectF rect = m_rect.adjusted(m_padding,m_padding, -m_padding, -m_padding); | ||
|
r571 | |||
// recalculate title position | ||||
if (m_titleItem) { | ||||
QPointF center = m_rect.center() -m_titleItem->boundingRect().center(); | ||||
|
r639 | m_titleItem->setPos(center.x(),m_rect.top()/2 + m_padding/2); | ||
|
r571 | } | ||
//recalculate background gradient | ||||
if (m_backgroundItem) { | ||||
|
r639 | m_backgroundItem->setRect(m_rect.adjusted(m_backgroundPadding,m_backgroundPadding, -m_backgroundPadding, -m_backgroundPadding)); | ||
|
r571 | } | ||
|
r582 | |||
// recalculate legend position | ||||
if (m_legend) { | ||||
|
r652 | if (m_legend->parentObject() == this) { | ||
m_legend->setMaximumSize(rect.size()); | ||||
m_legend->setPos(rect.topLeft()); | ||||
} | ||||
|
r582 | } | ||
|
r571 | } | ||
|
r639 | |||
int QChart::padding() const | ||||
{ | ||||
return m_padding; | ||||
} | ||||
void QChart::setPadding(int padding) | ||||
{ | ||||
if(m_padding==padding){ | ||||
m_padding = padding; | ||||
m_presenter->handleGeometryChanged(); | ||||
updateLayout(); | ||||
} | ||||
} | ||||
void QChart::setBackgroundPadding(int padding) | ||||
{ | ||||
if(m_backgroundPadding!=padding){ | ||||
m_backgroundPadding = padding; | ||||
updateLayout(); | ||||
} | ||||
} | ||||
void QChart::setBackgroundDiameter(int diameter) | ||||
{ | ||||
createChartBackgroundItem(); | ||||
m_backgroundItem->setDimeter(diameter); | ||||
m_backgroundItem->update(); | ||||
} | ||||
|
r645 | void QChart::setBackgroundVisible(bool visible) | ||
{ | ||||
createChartBackgroundItem(); | ||||
m_backgroundItem->setVisible(visible); | ||||
} | ||||
bool QChart::isBackgroundVisible() const | ||||
{ | ||||
if(!m_backgroundItem) return false; | ||||
return m_backgroundItem->isVisible(); | ||||
} | ||||
|
r64 | #include "moc_qchart.cpp" | ||
|
r48 | |||
|
r30 | QTCOMMERCIALCHART_END_NAMESPACE | ||