qchartview.cpp
250 lines
| 7.9 KiB
| text/x-c
|
CppLexer
/ src / qchartview.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
|
r58 | #include "qchartview.h" | ||
Michal Klocek
|
r742 | #include "qchart_p.h" | ||
Michal Klocek
|
r746 | #include "qchartview_p.h" | ||
Michal Klocek
|
r58 | #include <QGraphicsScene> | ||
#include <QRubberBand> | ||||
Michal Klocek
|
r746 | |||
Michal Klocek
|
r58 | |||
Marek Rosa
|
r287 | /*! | ||
Marek Rosa
|
r935 | \enum QChartView::RubberBand | ||
Marek Rosa
|
r287 | |||
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 | ||||
*/ | ||||
Marek Rosa
|
r233 | /*! | ||
\class QChartView | ||||
Marek Rosa
|
r287 | \brief Standalone charting widget. | ||
Marek Rosa
|
r274 | |||
Tero Ahola
|
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. | ||||
Marek Rosa
|
r274 | |||
\sa QChart | ||||
Marek Rosa
|
r233 | */ | ||
Michal Klocek
|
r58 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||
Marek Rosa
|
r285 | /*! | ||
Constructs a chartView object which is a child of a\a parent. | ||||
*/ | ||||
Michal Klocek
|
r746 | QChartView::QChartView(QChart *chart,QWidget *parent) : | ||
Tero Ahola
|
r584 | QGraphicsView(parent), | ||
Michal Klocek
|
r746 | d_ptr(new QChartViewPrivate()) | ||
Michal Klocek
|
r176 | { | ||
Michal Klocek
|
r895 | Q_ASSERT(chart); | ||
Michal Klocek
|
r746 | d_ptr->m_scene = new QGraphicsScene(this); | ||
d_ptr->m_chart = chart; | ||||
Tero Ahola
|
r700 | setFrameShape(QFrame::NoFrame); | ||
Tero Ahola
|
r664 | setBackgroundRole(QPalette::Window); | ||
Michal Klocek
|
r176 | setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); | ||
Michal Klocek
|
r58 | setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); | ||
Michal Klocek
|
r746 | setScene(d_ptr->m_scene); | ||
d_ptr->m_scene->addItem(chart); | ||||
Michal Klocek
|
r58 | setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); | ||
} | ||||
Marek Rosa
|
r277 | |||
/*! | ||||
Destroys the object and it's children, like QChartSeries and QChartAxis object added to it. | ||||
*/ | ||||
Michal Klocek
|
r58 | QChartView::~QChartView() | ||
{ | ||||
} | ||||
Marek Rosa
|
r909 | /*! | ||
Returns the pointer to the associated chart | ||||
*/ | ||||
Michal Klocek
|
r746 | QChart* QChartView::chart() const | ||
Michal Klocek
|
r122 | { | ||
Michal Klocek
|
r746 | return d_ptr->m_chart; | ||
Michal Klocek
|
r122 | } | ||
Michal Klocek
|
r136 | |||
Marek Rosa
|
r285 | /*! | ||
Marek Rosa
|
r909 | Sets the RubberBandPlicy to \a rubberBand. Selected policy determines the way zooming is performed. | ||
Marek Rosa
|
r285 | */ | ||
Michal Klocek
|
r746 | void QChartView::setRubberBand(const RubberBands& rubberBand) | ||
Michal Klocek
|
r136 | { | ||
Michal Klocek
|
r746 | d_ptr->m_rubberBandFlags=rubberBand; | ||
if (!d_ptr->m_rubberBandFlags) { | ||||
delete d_ptr->m_rubberBand; | ||||
d_ptr->m_rubberBand=0; | ||||
Michal Klocek
|
r176 | return; | ||
Michal Klocek
|
r136 | } | ||
Michal Klocek
|
r746 | |||
if (!d_ptr->m_rubberBand) { | ||||
d_ptr->m_rubberBand = new QRubberBand(QRubberBand::Rectangle, this); | ||||
d_ptr->m_rubberBand->setEnabled(true); | ||||
Michal Klocek
|
r138 | } | ||
Michal Klocek
|
r136 | } | ||
Marek Rosa
|
r285 | /*! | ||
Returns the RubberBandPolicy that is currently being used by the widget. | ||||
*/ | ||||
Michal Klocek
|
r746 | QChartView::RubberBands QChartView::rubberBand() const | ||
Michal Klocek
|
r136 | { | ||
Michal Klocek
|
r746 | return d_ptr->m_rubberBandFlags; | ||
Michal Klocek
|
r136 | } | ||
Marek Rosa
|
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. | ||||
Marek Rosa
|
r328 | If different mouse button is pressed and/or the RubberBandPolicy is disabled then the \a event is passed to QGraphicsView::mousePressEvent() implementation. | ||
Marek Rosa
|
r285 | */ | ||
Michal Klocek
|
r136 | void QChartView::mousePressEvent(QMouseEvent *event) | ||
{ | ||||
Michal Klocek
|
r746 | if(d_ptr->m_rubberBand && d_ptr->m_rubberBand->isEnabled() && event->button() == Qt::LeftButton) { | ||
Michal Klocek
|
r136 | |||
Michal Klocek
|
r855 | int padding = d_ptr->m_chart->margins().top(); | ||
Michal Klocek
|
r639 | QRect rect(padding, padding, width() - 2 * padding, height() - 2 * padding); | ||
Michal Klocek
|
r136 | |||
if (rect.contains(event->pos())) { | ||||
Michal Klocek
|
r746 | d_ptr->m_rubberBandOrigin = event->pos(); | ||
d_ptr->m_rubberBand->setGeometry(QRect(d_ptr->m_rubberBandOrigin, QSize())); | ||||
d_ptr->m_rubberBand->show(); | ||||
Michal Klocek
|
r136 | event->accept(); | ||
} | ||||
} | ||||
Marek Rosa
|
r285 | else { | ||
QGraphicsView::mousePressEvent(event); | ||||
} | ||||
Michal Klocek
|
r136 | } | ||
Marek Rosa
|
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. | ||||
*/ | ||||
Michal Klocek
|
r136 | void QChartView::mouseMoveEvent(QMouseEvent *event) | ||
{ | ||||
Michal Klocek
|
r746 | if(d_ptr->m_rubberBand && d_ptr->m_rubberBand->isVisible()) { | ||
Michal Klocek
|
r874 | QRectF margins = d_ptr->m_chart->margins(); | ||
QRectF geometry = d_ptr->m_chart->geometry(); | ||||
QRectF rect =geometry.adjusted(margins.left(),margins.top(),-margins.right(),-margins.bottom()); | ||||
Michal Klocek
|
r746 | int width = event->pos().x() - d_ptr->m_rubberBandOrigin.x(); | ||
int height = event->pos().y() - d_ptr->m_rubberBandOrigin.y(); | ||||
if (!d_ptr->m_rubberBandFlags.testFlag(VerticalRubberBand)) { | ||||
d_ptr->m_rubberBandOrigin.setY(rect.top()); | ||||
Michal Klocek
|
r136 | height = rect.height(); | ||
} | ||||
Michal Klocek
|
r746 | if (!d_ptr->m_rubberBandFlags.testFlag(HorizonalRubberBand)) { | ||
d_ptr->m_rubberBandOrigin.setX(rect.left()); | ||||
Michal Klocek
|
r136 | width= rect.width(); | ||
} | ||||
Michal Klocek
|
r746 | d_ptr->m_rubberBand->setGeometry(QRect(d_ptr->m_rubberBandOrigin.x(),d_ptr->m_rubberBandOrigin.y(), width,height).normalized()); | ||
Michal Klocek
|
r176 | } | ||
else { | ||||
Michal Klocek
|
r136 | QGraphicsView::mouseMoveEvent(event); | ||
} | ||||
} | ||||
Marek Rosa
|
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. | ||||
*/ | ||||
Michal Klocek
|
r136 | void QChartView::mouseReleaseEvent(QMouseEvent *event) | ||
{ | ||||
Michal Klocek
|
r746 | if(d_ptr->m_rubberBand) { | ||
if (event->button() == Qt::LeftButton && d_ptr->m_rubberBand->isVisible()) { | ||||
d_ptr->m_rubberBand->hide(); | ||||
QRect rect = d_ptr->m_rubberBand->geometry(); | ||||
d_ptr->m_chart->zoomIn(rect); | ||||
Michal Klocek
|
r176 | event->accept(); | ||
} | ||||
Michal Klocek
|
r136 | |||
Michal Klocek
|
r678 | if(event->button()==Qt::RightButton){ | ||
Michal Klocek
|
r746 | d_ptr->m_chart->zoomOut(); | ||
Michal Klocek
|
r678 | event->accept(); | ||
} | ||||
Michal Klocek
|
r176 | } | ||
else { | ||||
Michal Klocek
|
r136 | QGraphicsView::mouseReleaseEvent(event); | ||
} | ||||
} | ||||
Marek Rosa
|
r287 | /*! | ||
Pressing + and - keys performs zoomIn() and zoomOut() respectivly. | ||||
In other \a event is passed to the QGraphicsView::keyPressEvent() implementation | ||||
*/ | ||||
Michal Klocek
|
r136 | void QChartView::keyPressEvent(QKeyEvent *event) | ||
{ | ||||
switch (event->key()) { | ||||
Michal Klocek
|
r176 | case Qt::Key_Plus: | ||
Michal Klocek
|
r804 | d_ptr->m_chart->zoomIn(); | ||
break; | ||||
Michal Klocek
|
r176 | case Qt::Key_Minus: | ||
Michal Klocek
|
r804 | d_ptr->m_chart->zoomOut(); | ||
break; | ||||
case Qt::Key_Left: | ||||
d_ptr->m_chart->scrollLeft(); | ||||
break; | ||||
case Qt::Key_Right: | ||||
d_ptr->m_chart->scrollRight(); | ||||
break; | ||||
case Qt::Key_Up: | ||||
d_ptr->m_chart->scrollUp(); | ||||
break; | ||||
case Qt::Key_Down: | ||||
d_ptr->m_chart->scrollDown(); | ||||
break; | ||||
Michal Klocek
|
r176 | default: | ||
Michal Klocek
|
r804 | QGraphicsView::keyPressEvent(event); | ||
break; | ||||
Michal Klocek
|
r136 | } | ||
} | ||||
Marek Rosa
|
r277 | /*! | ||
Michal Klocek
|
r746 | Resizes and updates the chart area using the \a event data | ||
Michal Klocek
|
r298 | */ | ||
Michal Klocek
|
r746 | void QChartView::resizeEvent(QResizeEvent *event) | ||
Michal Klocek
|
r298 | { | ||
Michal Klocek
|
r746 | QGraphicsView::resizeEvent(event); | ||
Michal Klocek
|
r782 | d_ptr->m_chart->resize(size()); | ||
Michal Klocek
|
r913 | setMinimumSize(d_ptr->m_chart->minimumSize().toSize()); | ||
Michal Klocek
|
r782 | setSceneRect(d_ptr->m_chart->geometry()); | ||
Michal Klocek
|
r298 | } | ||
Michal Klocek
|
r746 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||
Michal Klocek
|
r298 | |||
Michal Klocek
|
r746 | QChartViewPrivate::QChartViewPrivate(): | ||
m_scene(0), | ||||
m_chart(0), | ||||
m_presenter(0), | ||||
m_rubberBand(0), | ||||
m_rubberBandFlags(QChartView::NoRubberBand) | ||||
Michal Klocek
|
r531 | { | ||
Michal Klocek
|
r600 | } | ||
Michal Klocek
|
r746 | QChartViewPrivate::~QChartViewPrivate() | ||
Michal Klocek
|
r600 | { | ||
} | ||||
Tero Ahola
|
r584 | #include "moc_qchartview.cpp" | ||
Michal Klocek
|
r58 | QTCOMMERCIALCHART_END_NAMESPACE | ||