qchartview.cpp
375 lines
| 10.3 KiB
| text/x-c
|
CppLexer
/ src / qchartview.cpp
|
r58 | #include "qchartview.h" | ||
#include "qchart.h" | ||||
|
r176 | #include "qchartaxis.h" | ||
|
r58 | #include <QGraphicsView> | ||
#include <QGraphicsScene> | ||||
#include <QRubberBand> | ||||
#include <QResizeEvent> | ||||
#include <QDebug> | ||||
|
r287 | /*! | ||
\enum QChartView::RubberBandPolicy | ||||
This enum describes the different types of rubber bands that can be used for zoom rect selection | ||||
\value NoRubberBand | ||||
\value VerticalRubberBand | ||||
\value HorizonalRubberBand | ||||
\value RectangleRubberBand | ||||
*/ | ||||
|
r233 | /*! | ||
\class QChartView | ||||
|
r287 | \brief Standalone charting widget. | ||
|
r274 | |||
|
r321 | QChartView is a standalone widget that can display charts. It does not require separate | ||
QGraphicsScene to work. It manages the graphical representation of different types of | ||||
QChartSeries and other chart related objects like QChartAxis and QChartLegend. If you want to | ||||
display a chart in your existing QGraphicsScene, you can use the QChart class instead. | ||||
|
r274 | |||
\sa QChart | ||||
|
r233 | */ | ||
|
r58 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||
|
r285 | /*! | ||
Constructs a chartView object which is a child of a\a parent. | ||||
*/ | ||||
|
r58 | QChartView::QChartView(QWidget *parent) : | ||
|
r176 | QGraphicsView(parent), | ||
|
r204 | m_scene(new QGraphicsScene(this)), | ||
|
r176 | m_chart(new QChart()), | ||
m_rubberBand(0), | ||||
m_verticalRubberBand(false), | ||||
m_horizonalRubberBand(false) | ||||
{ | ||||
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); | ||||
|
r58 | setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); | ||
setScene(m_scene); | ||||
m_chart->setMargin(50); | ||||
m_scene->addItem(m_chart); | ||||
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); | ||||
} | ||||
|
r277 | |||
/*! | ||||
Destroys the object and it's children, like QChartSeries and QChartAxis object added to it. | ||||
*/ | ||||
|
r58 | QChartView::~QChartView() | ||
{ | ||||
} | ||||
|
r287 | /*! | ||
Resizes and updates the chart area using the \a event data | ||||
*/ | ||||
|
r58 | void QChartView::resizeEvent(QResizeEvent *event) | ||
{ | ||||
m_scene->setSceneRect(0,0,size().width(),size().height()); | ||||
|
r115 | m_chart->resize(size()); | ||
|
r58 | QWidget::resizeEvent(event); | ||
} | ||||
|
r274 | /*! | ||
|
r285 | Adds the \a series and optional \a axisY onto the chart and takes the ownership of the objects. | ||
|
r274 | If auto scaling is enabled, re-scales the axes the series is bound to (both the x axis and | ||
the y axis). | ||||
|
r285 | \sa removeSeries(), removeAllSeries() | ||
|
r274 | */ | ||
|
r360 | void QChartView::addSeries(QSeries* series,QChartAxis *axisY) | ||
|
r58 | { | ||
|
r223 | m_chart->addSeries(series,axisY); | ||
|
r58 | } | ||
|
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 QChartView::removeSeries(QSeries* series) | ||
|
r245 | { | ||
m_chart->removeSeries(series); | ||||
} | ||||
|
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 QChartView::removeAllSeries() | ||
{ | ||||
m_chart->removeAllSeries(); | ||||
} | ||||
|
r285 | /*! | ||
Zooms in the view by a factor of 2 | ||||
*/ | ||||
|
r223 | void QChartView::zoomIn() | ||
|
r58 | { | ||
|
r223 | m_chart->zoomIn(); | ||
|
r58 | } | ||
|
r285 | /*! | ||
Zooms in the view to a maximum level at which \a rect is still fully visible. | ||||
*/ | ||||
|
r223 | void QChartView::zoomIn(const QRect& rect) | ||
|
r58 | { | ||
|
r223 | m_chart->zoomIn(rect); | ||
|
r58 | } | ||
|
r285 | /*! | ||
Restores the view zoom level to the previous one. | ||||
*/ | ||||
|
r67 | void QChartView::zoomOut() | ||
|
r58 | { | ||
|
r67 | m_chart->zoomOut(); | ||
|
r58 | } | ||
|
r274 | /*! | ||
|
r277 | Returns the chart margin, which is the distance between the widget edge and the part of the chart where the actual data can be displayed. | ||
|
r274 | */ | ||
|
r67 | int QChartView::margin() const | ||
{ | ||||
return m_chart->margin(); | ||||
} | ||||
|
r69 | |||
|
r274 | /*! | ||
|
r481 | Sets the chart \a title. A description text that is drawn above the chart. | ||
|
r274 | */ | ||
|
r192 | void QChartView::setChartTitle(const QString& title) | ||
|
r69 | { | ||
|
r192 | m_chart->setChartTitle(title); | ||
} | ||||
|
r476 | /*! | ||
|
r481 | Returns the chart's title. A description text that is drawn above the chart. | ||
|
r476 | */ | ||
QString QChartView::chartTitle() const | ||||
{ | ||||
return m_chart->chartTitle(); | ||||
} | ||||
|
r274 | /*! | ||
|
r277 | Sets the \a font that is used for rendering the description text that is rendered above the chart. | ||
|
r274 | */ | ||
|
r192 | void QChartView::setChartTitleFont(const QFont& font) | ||
{ | ||||
m_chart->setChartTitleFont(font); | ||||
|
r69 | } | ||
|
r495 | /*! | ||
Sets the \a brush used for rendering the title text. | ||||
*/ | ||||
void QChartView::setChartTitleBrush(const QBrush &brush) | ||||
{ | ||||
m_chart->setChartTitleBrush(brush); | ||||
} | ||||
/*! | ||||
Returns the brush used for rendering the title text. | ||||
*/ | ||||
QBrush QChartView::chartTitleBrush() | ||||
{ | ||||
return m_chart->chartTitleBrush(); | ||||
} | ||||
|
r277 | /*! | ||
Sets the \a brush that is used for painting the background of the chart area of the QChartView widget. | ||||
*/ | ||||
|
r122 | void QChartView::setChartBackgroundBrush(const QBrush& brush) | ||
{ | ||||
|
r176 | m_chart->setChartBackgroundBrush(brush); | ||
|
r122 | } | ||
|
r277 | |||
/*! | ||||
Sets the \a pen that is used for painting the background of the chart area of the QChartView widget. | ||||
*/ | ||||
|
r122 | void QChartView::setChartBackgroundPen(const QPen& pen) | ||
{ | ||||
|
r176 | m_chart->setChartBackgroundPen(pen); | ||
|
r122 | } | ||
|
r136 | |||
|
r285 | /*! | ||
Sets the RubberBandPlicy to \a policy. Selected policy determines the way zooming is performed. | ||||
*/ | ||||
|
r136 | void QChartView::setRubberBandPolicy(const RubberBandPolicy policy) | ||
{ | ||||
|
r176 | switch(policy) { | ||
|
r136 | case VerticalRubberBand: | ||
|
r176 | m_verticalRubberBand = true; | ||
m_horizonalRubberBand = false; | ||||
break; | ||||
|
r136 | case HorizonalRubberBand: | ||
|
r176 | m_verticalRubberBand = false; | ||
m_horizonalRubberBand = true; | ||||
break; | ||||
|
r136 | case RectangleRubberBand: | ||
|
r176 | m_verticalRubberBand = true; | ||
m_horizonalRubberBand = true; | ||||
break; | ||||
|
r136 | case NoRubberBand: | ||
default: | ||||
|
r176 | delete m_rubberBand; | ||
m_rubberBand=0; | ||||
m_horizonalRubberBand = false; | ||||
m_verticalRubberBand = false; | ||||
return; | ||||
|
r136 | } | ||
|
r176 | if(!m_rubberBand) { | ||
m_rubberBand = new QRubberBand(QRubberBand::Rectangle, this); | ||||
m_rubberBand->setEnabled(true); | ||||
|
r138 | } | ||
|
r136 | } | ||
|
r285 | /*! | ||
Returns the RubberBandPolicy that is currently being used by the widget. | ||||
*/ | ||||
|
r136 | QChartView::RubberBandPolicy QChartView::rubberBandPolicy() const | ||
{ | ||||
if(m_horizonalRubberBand && m_verticalRubberBand) return RectangleRubberBand; | ||||
if(m_horizonalRubberBand) return HorizonalRubberBand; | ||||
if(m_verticalRubberBand) return VerticalRubberBand; | ||||
return NoRubberBand; | ||||
} | ||||
|
r285 | /*! | ||
If Left mouse button is pressed and the RubberBandPolicy is enabled the \a event is accepted and the rubber band is displayed on the screen allowing the user to select the zoom area. | ||||
|
r328 | If different mouse button is pressed and/or the RubberBandPolicy is disabled then the \a event is passed to QGraphicsView::mousePressEvent() implementation. | ||
|
r285 | */ | ||
|
r136 | void QChartView::mousePressEvent(QMouseEvent *event) | ||
{ | ||||
if(m_rubberBand && m_rubberBand->isEnabled() && event->button() == Qt::LeftButton) { | ||||
int margin = m_chart->margin(); | ||||
QRect rect(margin, margin, width() - 2 * margin, height() - 2 * margin); | ||||
if (rect.contains(event->pos())) { | ||||
m_rubberBandOrigin = event->pos(); | ||||
m_rubberBand->setGeometry(QRect(m_rubberBandOrigin, QSize())); | ||||
m_rubberBand->show(); | ||||
event->accept(); | ||||
} | ||||
} | ||||
|
r285 | else { | ||
QGraphicsView::mousePressEvent(event); | ||||
} | ||||
|
r136 | } | ||
|
r287 | /*! | ||
If RubberBand rectange specification has been initiated in pressEvent then \a event data is used to update RubberBand geometry. | ||||
In other case the defualt QGraphicsView::mouseMoveEvent implementation is called. | ||||
*/ | ||||
|
r136 | void QChartView::mouseMoveEvent(QMouseEvent *event) | ||
{ | ||||
|
r176 | if(m_rubberBand && m_rubberBand->isVisible()) { | ||
|
r136 | int margin = m_chart->margin(); | ||
QRect rect(margin, margin, width() - 2 * margin, height() - 2 * margin); | ||||
int width = event->pos().x() - m_rubberBandOrigin.x(); | ||||
int height = event->pos().y() - m_rubberBandOrigin.y(); | ||||
if(!m_verticalRubberBand) { | ||||
m_rubberBandOrigin.setY(rect.top()); | ||||
height = rect.height(); | ||||
} | ||||
if(!m_horizonalRubberBand) { | ||||
m_rubberBandOrigin.setX(rect.left()); | ||||
width= rect.width(); | ||||
} | ||||
m_rubberBand->setGeometry(QRect(m_rubberBandOrigin.x(),m_rubberBandOrigin.y(), width,height).normalized()); | ||||
|
r176 | } | ||
else { | ||||
|
r136 | QGraphicsView::mouseMoveEvent(event); | ||
} | ||||
} | ||||
|
r285 | /*! | ||
If left mouse button is release and RubberBand is enabled then \a event is accepted and the view is zoomed in to rect specified by RubberBand | ||||
If it is the right mouse button \a event then RubberBand is dissmissed and zoom is canceled. | ||||
*/ | ||||
|
r136 | void QChartView::mouseReleaseEvent(QMouseEvent *event) | ||
{ | ||||
|
r176 | if(m_rubberBand) { | ||
if (event->button() == Qt::LeftButton && m_rubberBand->isVisible()) { | ||||
m_rubberBand->hide(); | ||||
QRect rect = m_rubberBand->geometry(); | ||||
|
r223 | m_chart->zoomIn(rect); | ||
|
r176 | event->accept(); | ||
} | ||||
|
r136 | |||
|
r176 | if(event->button()==Qt::RightButton) | ||
|
r136 | m_chart->zoomReset(); | ||
|
r176 | } | ||
else { | ||||
|
r136 | QGraphicsView::mouseReleaseEvent(event); | ||
} | ||||
} | ||||
|
r287 | /*! | ||
Pressing + and - keys performs zoomIn() and zoomOut() respectivly. | ||||
In other \a event is passed to the QGraphicsView::keyPressEvent() implementation | ||||
*/ | ||||
|
r136 | void QChartView::keyPressEvent(QKeyEvent *event) | ||
{ | ||||
switch (event->key()) { | ||||
|
r176 | case Qt::Key_Plus: | ||
|
r136 | zoomIn(); | ||
break; | ||||
|
r176 | case Qt::Key_Minus: | ||
|
r136 | zoomOut(); | ||
break; | ||||
|
r176 | default: | ||
|
r136 | QGraphicsView::keyPressEvent(event); | ||
break; | ||||
} | ||||
} | ||||
|
r277 | /*! | ||
Sets the \a theme used by the chart for rendering the graphical representation of the data | ||||
\sa QChart::ChartTheme, chartTheme() | ||||
*/ | ||||
|
r153 | void QChartView::setChartTheme(QChart::ChartTheme theme) | ||
|
r136 | { | ||
|
r153 | m_chart->setChartTheme(theme); | ||
|
r136 | } | ||
|
r277 | /*! | ||
Returns the theme enum used by the chart. | ||||
\sa setChartTheme() | ||||
*/ | ||||
|
r153 | QChart::ChartTheme QChartView::chartTheme() const | ||
{ | ||||
return m_chart->chartTheme(); | ||||
} | ||||
|
r136 | |||
|
r274 | /*! | ||
Returns the pointer to the x axis object of the chart | ||||
*/ | ||||
|
r223 | QChartAxis* QChartView::axisX() const | ||
|
r155 | { | ||
|
r223 | return m_chart->axisX(); | ||
|
r155 | } | ||
|
r274 | /*! | ||
Returns the pointer to the y axis object of the chart | ||||
*/ | ||||
|
r223 | QChartAxis* QChartView::axisY() const | ||
|
r155 | { | ||
|
r223 | return m_chart->axisY(); | ||
|
r155 | } | ||
|
r298 | /*! | ||
|
r302 | Sets animation \a options for the chart | ||
|
r298 | */ | ||
void QChartView::setAnimationOptions(QChart::AnimationOptions options) | ||||
{ | ||||
m_chart->setAnimationOptions(options); | ||||
} | ||||
/*! | ||||
Returns animation options for the chart | ||||
*/ | ||||
QChart::AnimationOptions QChartView::animationOptions() const | ||||
{ | ||||
return m_chart->animationOptions(); | ||||
} | ||||
|
r58 | QTCOMMERCIALCHART_END_NAMESPACE | ||