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