qchartview.cpp
226 lines
| 6.8 KiB
| text/x-c
|
CppLexer
/ src / qchartview.cpp
Michal Klocek
|
r969 | /**************************************************************************** | ||
** | ||||
** 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$ | ||||
** | ||||
****************************************************************************/ | ||||
#include "qchartview.h" | ||||
#include "qchartview_p.h" | ||||
#include "qchart_p.h" | ||||
#include <QGraphicsScene> | ||||
#include <QRubberBand> | ||||
/*! | ||||
\enum QChartView::RubberBand | ||||
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 | ||||
*/ | ||||
/*! | ||||
\class QChartView | ||||
\brief Standalone charting widget. | ||||
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 | ||||
Jani Honkonen
|
r1023 | series and other chart related objects like QAxis and QLegend. If you want to | ||
Michal Klocek
|
r969 | display a chart in your existing QGraphicsScene, you can use the QChart class instead. | ||
\sa QChart | ||||
*/ | ||||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
Michal Klocek
|
r1290 | /*! | ||
Constructs a chartView object with parent \a parent. | ||||
*/ | ||||
QChartView::QChartView(QWidget *parent) : | ||||
QGraphicsView(parent), | ||||
d_ptr(new QChartViewPrivate(this)) | ||||
{ | ||||
} | ||||
Michal Klocek
|
r969 | /*! | ||
Tero Ahola
|
r1002 | Constructs a chartView object with parent \a parent to display a \a chart. | ||
Michal Klocek
|
r969 | */ | ||
Michal Klocek
|
r1290 | |||
Michal Klocek
|
r969 | QChartView::QChartView(QChart *chart,QWidget *parent) : | ||
QGraphicsView(parent), | ||||
Michal Klocek
|
r1290 | d_ptr(new QChartViewPrivate(this,chart)) | ||
Michal Klocek
|
r969 | { | ||
Michal Klocek
|
r1290 | |||
Michal Klocek
|
r969 | } | ||
/*! | ||||
Jani Honkonen
|
r1023 | Destroys the object and it's children, like series and axis objects added to it. | ||
Michal Klocek
|
r969 | */ | ||
QChartView::~QChartView() | ||||
{ | ||||
} | ||||
/*! | ||||
Returns the pointer to the associated chart | ||||
*/ | ||||
QChart* QChartView::chart() const | ||||
{ | ||||
return d_ptr->m_chart; | ||||
} | ||||
/*! | ||||
Sets the RubberBandPlicy to \a rubberBand. Selected policy determines the way zooming is performed. | ||||
*/ | ||||
void QChartView::setRubberBand(const RubberBands& rubberBand) | ||||
{ | ||||
d_ptr->m_rubberBandFlags=rubberBand; | ||||
if (!d_ptr->m_rubberBandFlags) { | ||||
delete d_ptr->m_rubberBand; | ||||
d_ptr->m_rubberBand=0; | ||||
return; | ||||
} | ||||
if (!d_ptr->m_rubberBand) { | ||||
d_ptr->m_rubberBand = new QRubberBand(QRubberBand::Rectangle, this); | ||||
d_ptr->m_rubberBand->setEnabled(true); | ||||
} | ||||
} | ||||
/*! | ||||
Returns the RubberBandPolicy that is currently being used by the widget. | ||||
*/ | ||||
QChartView::RubberBands QChartView::rubberBand() const | ||||
{ | ||||
return d_ptr->m_rubberBandFlags; | ||||
} | ||||
/*! | ||||
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. | ||||
If different mouse button is pressed and/or the RubberBandPolicy is disabled then the \a event is passed to QGraphicsView::mousePressEvent() implementation. | ||||
*/ | ||||
void QChartView::mousePressEvent(QMouseEvent *event) | ||||
{ | ||||
if(d_ptr->m_rubberBand && d_ptr->m_rubberBand->isEnabled() && event->button() == Qt::LeftButton) { | ||||
Michal Klocek
|
r1883 | QRectF plotArea = d_ptr->m_chart->plotArea(); | ||
Michal Klocek
|
r969 | |||
Michal Klocek
|
r1883 | if (plotArea.contains(event->pos())) { | ||
Michal Klocek
|
r969 | d_ptr->m_rubberBandOrigin = event->pos(); | ||
d_ptr->m_rubberBand->setGeometry(QRect(d_ptr->m_rubberBandOrigin, QSize())); | ||||
d_ptr->m_rubberBand->show(); | ||||
event->accept(); | ||||
} | ||||
} | ||||
else { | ||||
QGraphicsView::mousePressEvent(event); | ||||
} | ||||
} | ||||
/*! | ||||
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. | ||||
*/ | ||||
void QChartView::mouseMoveEvent(QMouseEvent *event) | ||||
{ | ||||
if(d_ptr->m_rubberBand && d_ptr->m_rubberBand->isVisible()) { | ||||
Michal Klocek
|
r1883 | QRectF rect = d_ptr->m_chart->plotArea(); | ||
Michal Klocek
|
r969 | 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()); | ||||
height = rect.height(); | ||||
} | ||||
if (!d_ptr->m_rubberBandFlags.testFlag(HorizonalRubberBand)) { | ||||
d_ptr->m_rubberBandOrigin.setX(rect.left()); | ||||
width= rect.width(); | ||||
} | ||||
d_ptr->m_rubberBand->setGeometry(QRect(d_ptr->m_rubberBandOrigin.x(),d_ptr->m_rubberBandOrigin.y(), width,height).normalized()); | ||||
} | ||||
else { | ||||
QGraphicsView::mouseMoveEvent(event); | ||||
} | ||||
} | ||||
/*! | ||||
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. | ||||
*/ | ||||
void QChartView::mouseReleaseEvent(QMouseEvent *event) | ||||
{ | ||||
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); | ||||
event->accept(); | ||||
} | ||||
if(event->button()==Qt::RightButton){ | ||||
d_ptr->m_chart->zoomOut(); | ||||
event->accept(); | ||||
} | ||||
} | ||||
else { | ||||
QGraphicsView::mouseReleaseEvent(event); | ||||
} | ||||
} | ||||
/*! | ||||
Resizes and updates the chart area using the \a event data | ||||
*/ | ||||
void QChartView::resizeEvent(QResizeEvent *event) | ||||
{ | ||||
QGraphicsView::resizeEvent(event); | ||||
d_ptr->m_chart->resize(size()); | ||||
setMinimumSize(d_ptr->m_chart->minimumSize().toSize()); | ||||
setSceneRect(d_ptr->m_chart->geometry()); | ||||
} | ||||
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||||
Michal Klocek
|
r1290 | QChartViewPrivate::QChartViewPrivate(QChartView *q,QChart* chart): | ||
q_ptr(q), | ||||
m_scene(new QGraphicsScene(q)), | ||||
m_chart(chart), | ||||
Michal Klocek
|
r969 | m_presenter(0), | ||
m_rubberBand(0), | ||||
m_rubberBandFlags(QChartView::NoRubberBand) | ||||
{ | ||||
Michal Klocek
|
r1290 | q_ptr->setFrameShape(QFrame::NoFrame); | ||
q_ptr->setBackgroundRole(QPalette::Window); | ||||
q_ptr->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); | ||||
q_ptr->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); | ||||
q_ptr->setScene(m_scene); | ||||
q_ptr->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); | ||||
if(!m_chart) m_chart = new QChart(); | ||||
m_scene->addItem(m_chart); | ||||
Michal Klocek
|
r969 | } | ||
QChartViewPrivate::~QChartViewPrivate() | ||||
{ | ||||
} | ||||
#include "moc_qchartview.cpp" | ||||
QTCOMMERCIALCHART_END_NAMESPACE | ||||