qchartview.cpp
245 lines
| 7.8 KiB
| text/x-c
|
CppLexer
/ src / qchartview.cpp
|
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$ | ||||
** | ||||
****************************************************************************/ | ||||
|
r58 | #include "qchartview.h" | ||
|
r742 | #include "qchart_p.h" | ||
|
r746 | #include "qchartview_p.h" | ||
|
r58 | #include <QGraphicsScene> | ||
#include <QRubberBand> | ||||
|
r746 | |||
|
r58 | |||
|
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. | ||||
*/ | ||||
|
r746 | QChartView::QChartView(QChart *chart,QWidget *parent) : | ||
|
r584 | QGraphicsView(parent), | ||
|
r746 | d_ptr(new QChartViewPrivate()) | ||
|
r176 | { | ||
|
r746 | d_ptr->m_scene = new QGraphicsScene(this); | ||
d_ptr->m_chart = chart; | ||||
|
r700 | setFrameShape(QFrame::NoFrame); | ||
|
r664 | setBackgroundRole(QPalette::Window); | ||
|
r176 | setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); | ||
|
r58 | setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); | ||
|
r746 | setScene(d_ptr->m_scene); | ||
d_ptr->m_scene->addItem(chart); | ||||
|
r58 | setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); | ||
} | ||||
|
r277 | |||
/*! | ||||
Destroys the object and it's children, like QChartSeries and QChartAxis object added to it. | ||||
*/ | ||||
|
r58 | QChartView::~QChartView() | ||
{ | ||||
} | ||||
|
r746 | QChart* QChartView::chart() const | ||
|
r122 | { | ||
|
r746 | return d_ptr->m_chart; | ||
|
r122 | } | ||
|
r136 | |||
|
r285 | /*! | ||
Sets the RubberBandPlicy to \a policy. Selected policy determines the way zooming is performed. | ||||
*/ | ||||
|
r746 | void QChartView::setRubberBand(const RubberBands& rubberBand) | ||
|
r136 | { | ||
|
r746 | d_ptr->m_rubberBandFlags=rubberBand; | ||
if (!d_ptr->m_rubberBandFlags) { | ||||
delete d_ptr->m_rubberBand; | ||||
d_ptr->m_rubberBand=0; | ||||
|
r176 | return; | ||
|
r136 | } | ||
|
r746 | |||
if (!d_ptr->m_rubberBand) { | ||||
d_ptr->m_rubberBand = new QRubberBand(QRubberBand::Rectangle, this); | ||||
d_ptr->m_rubberBand->setEnabled(true); | ||||
|
r138 | } | ||
|
r136 | } | ||
|
r285 | /*! | ||
Returns the RubberBandPolicy that is currently being used by the widget. | ||||
*/ | ||||
|
r746 | QChartView::RubberBands QChartView::rubberBand() const | ||
|
r136 | { | ||
|
r746 | return d_ptr->m_rubberBandFlags; | ||
|
r136 | } | ||
|
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) | ||
{ | ||||
|
r746 | if(d_ptr->m_rubberBand && d_ptr->m_rubberBand->isEnabled() && event->button() == Qt::LeftButton) { | ||
|
r136 | |||
|
r855 | int padding = d_ptr->m_chart->margins().top(); | ||
|
r639 | QRect rect(padding, padding, width() - 2 * padding, height() - 2 * padding); | ||
|
r136 | |||
if (rect.contains(event->pos())) { | ||||
|
r746 | d_ptr->m_rubberBandOrigin = event->pos(); | ||
d_ptr->m_rubberBand->setGeometry(QRect(d_ptr->m_rubberBandOrigin, QSize())); | ||||
d_ptr->m_rubberBand->show(); | ||||
|
r136 | 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) | ||
{ | ||||
|
r746 | if(d_ptr->m_rubberBand && d_ptr->m_rubberBand->isVisible()) { | ||
|
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()); | ||||
|
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()); | ||||
|
r136 | height = rect.height(); | ||
} | ||||
|
r746 | if (!d_ptr->m_rubberBandFlags.testFlag(HorizonalRubberBand)) { | ||
d_ptr->m_rubberBandOrigin.setX(rect.left()); | ||||
|
r136 | width= rect.width(); | ||
} | ||||
|
r746 | d_ptr->m_rubberBand->setGeometry(QRect(d_ptr->m_rubberBandOrigin.x(),d_ptr->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) | ||
{ | ||||
|
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); | ||||
|
r176 | event->accept(); | ||
} | ||||
|
r136 | |||
|
r678 | if(event->button()==Qt::RightButton){ | ||
|
r746 | d_ptr->m_chart->zoomOut(); | ||
|
r678 | event->accept(); | ||
} | ||||
|
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: | ||
|
r804 | d_ptr->m_chart->zoomIn(); | ||
break; | ||||
|
r176 | case Qt::Key_Minus: | ||
|
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; | ||||
|
r176 | default: | ||
|
r804 | QGraphicsView::keyPressEvent(event); | ||
break; | ||||
|
r136 | } | ||
} | ||||
|
r277 | /*! | ||
|
r746 | Resizes and updates the chart area using the \a event data | ||
|
r298 | */ | ||
|
r746 | void QChartView::resizeEvent(QResizeEvent *event) | ||
|
r298 | { | ||
|
r746 | QGraphicsView::resizeEvent(event); | ||
|
r782 | d_ptr->m_chart->resize(size()); | ||
setSceneRect(d_ptr->m_chart->geometry()); | ||||
|
r298 | } | ||
|
r746 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||
|
r298 | |||
|
r746 | QChartViewPrivate::QChartViewPrivate(): | ||
m_scene(0), | ||||
m_chart(0), | ||||
m_presenter(0), | ||||
m_rubberBand(0), | ||||
m_rubberBandFlags(QChartView::NoRubberBand) | ||||
|
r531 | { | ||
|
r600 | } | ||
|
r746 | QChartViewPrivate::~QChartViewPrivate() | ||
|
r600 | { | ||
} | ||||
|
r584 | #include "moc_qchartview.cpp" | ||
|
r58 | QTCOMMERCIALCHART_END_NAMESPACE | ||