##// END OF EJS Templates
Add possibility to set reverse values to axes...
Add possibility to set reverse values to axes Added support for reverse axis. This works with line, spline, scatter and area series with cartesian chart. QBarCategoryAxis is not supported, nor different bar series. Change-Id: I60f21372ea9cee7b49918d7d99de24671bdc42c3 Reviewed-by: Miikka Heikkinen <miikka.heikkinen@theqtcompany.com>

File last commit:

r2776:bc1f6aa59d42
r2781:7c9f8e5a27d8
Show More
qchartview.cpp
315 lines | 9.9 KiB | text/x-c | CppLexer
Michal Klocek
Fixes header guard style issues
r969 /****************************************************************************
**
Titta Heikkala
Copyright header changes...
r2776 ** Copyright (C) 2015 The Qt Company Ltd
Michal Klocek
Fixes header guard style issues
r969 ** All rights reserved.
Titta Heikkala
Copyright header changes...
r2776 ** For any questions to The Qt Company, please use contact form at http://qt.io
Michal Klocek
Fixes header guard style issues
r969 **
Titta Heikkala
Updated license headers...
r2740 ** This file is part of the Qt Charts module.
Michal Klocek
Fixes header guard style issues
r969 **
Titta Heikkala
Updated license headers...
r2740 ** Licensees holding valid commercial license for Qt may use this file in
** accordance with the Qt License Agreement provided with the Software
** or, alternatively, in accordance with the terms contained in a written
Titta Heikkala
Copyright header changes...
r2776 ** agreement between you and The Qt Company.
Michal Klocek
Fixes header guard style issues
r969 **
** If you have questions regarding the use of this file, please use
Titta Heikkala
Updated license headers...
r2740 ** contact form at http://qt.io
Michal Klocek
Fixes header guard style issues
r969 **
****************************************************************************/
Titta Heikkala
Fix include syntax...
r2714 #include <QtCharts/QChartView>
#include <private/qchartview_p.h>
#include <private/qchart_p.h>
#include <QtWidgets/QGraphicsScene>
#include <QtWidgets/QRubberBand>
Michal Klocek
Fixes header guard style issues
r969
/*!
\enum QChartView::RubberBand
This enum describes the different types of rubber bands that can be used for zoom rect selection
\value NoRubberBand
\value VerticalRubberBand
Miikka Heikkinen
Remove deprecated elements from APIs....
r2742 \value HorizontalRubberBand
Michal Klocek
Fixes header guard style issues
r969 \value RectangleRubberBand
*/
/*!
\class QChartView
Titta Heikkala
Fix Charts documentation...
r2639 \inmodule Qt Charts
Michal Klocek
Fixes header guard style issues
r969 \brief Standalone charting widget.
QChartView is a standalone widget that can display charts. It does not require separate
Miikka Heikkinen
Documentation updates...
r2494 QGraphicsScene to work. If you want to display a chart in your existing QGraphicsScene,
you need to use the QChart (or QPolarChart) class instead.
Michal Klocek
Fixes header guard style issues
r969
Miikka Heikkinen
Documentation updates...
r2494 \sa QChart, QPolarChart
Michal Klocek
Fixes header guard style issues
r969 */
Titta Heikkala
Qt Charts project file structure change...
r2712 QT_CHARTS_BEGIN_NAMESPACE
Michal Klocek
Fixes header guard style issues
r969
Michal Klocek
Adds QChartView empty constructor to be able to promote qchartview in qtdesigner
r1290 /*!
Constructs a chartView object with parent \a parent.
*/
Jani Honkonen
astyle and manual coding style fixes for src-folder
r2097 QChartView::QChartView(QWidget *parent)
: QGraphicsView(parent),
d_ptr(new QChartViewPrivate(this))
Michal Klocek
Adds QChartView empty constructor to be able to promote qchartview in qtdesigner
r1290 {
}
Michal Klocek
Fixes header guard style issues
r969 /*!
Miikka Heikkinen
Documentation updates...
r2494 Constructs a chartview object with parent \a parent to display a \a chart.
Ownership of the \a chart is passed to chartview.
Michal Klocek
Fixes header guard style issues
r969 */
Michal Klocek
Adds QChartView empty constructor to be able to promote qchartview in qtdesigner
r1290
Jani Honkonen
astyle and manual coding style fixes for src-folder
r2097 QChartView::QChartView(QChart *chart, QWidget *parent)
: QGraphicsView(parent),
d_ptr(new QChartViewPrivate(this, chart))
Michal Klocek
Fixes header guard style issues
r969 {
Michal Klocek
Adds QChartView empty constructor to be able to promote qchartview in qtdesigner
r1290
Michal Klocek
Fixes header guard style issues
r969 }
/*!
Miikka Heikkinen
Documentation updates...
r2494 Destroys the chartview object and the associated chart.
Michal Klocek
Fixes header guard style issues
r969 */
QChartView::~QChartView()
{
}
/*!
Miikka Heikkinen
Documentation updates...
r2494 Returns the pointer to the associated chart.
Michal Klocek
Fixes header guard style issues
r969 */
Jani Honkonen
more coding style fixes for src-folder...
r2104 QChart *QChartView::chart() const
Michal Klocek
Fixes header guard style issues
r969 {
return d_ptr->m_chart;
}
Jani Honkonen
Implement QChartView::setChart()
r2056 /*!
Sets the current chart to \a chart. Ownership of the new chart is passed to chartview
and ownership of the previous chart is released.
Miikka Heikkinen
Documentation updates...
r2494 To avoid memory leaks users need to make sure the previous chart is deleted.
Jani Honkonen
Implement QChartView::setChart()
r2056 */
void QChartView::setChart(QChart *chart)
{
d_ptr->setChart(chart);
}
Michal Klocek
Fixes header guard style issues
r969 /*!
Miikka Heikkinen
Documentation updates...
r2494 Sets the rubber band flags to \a rubberBand.
Selected flags determine the way zooming is performed.
\note Rubber band zooming is not supported for polar charts.
Michal Klocek
Fixes header guard style issues
r969 */
Jani Honkonen
more coding style fixes for src-folder...
r2104 void QChartView::setRubberBand(const RubberBands &rubberBand)
Michal Klocek
Fixes header guard style issues
r969 {
Titta Heikkala
Fix Chart build when QT_NO_RUBBERBAND is defined...
r2623 #ifndef QT_NO_RUBBERBAND
Jani Honkonen
astyle and manual coding style fixes for src-folder
r2097 d_ptr->m_rubberBandFlags = rubberBand;
Michal Klocek
Fixes header guard style issues
r969
if (!d_ptr->m_rubberBandFlags) {
delete d_ptr->m_rubberBand;
Jani Honkonen
astyle and manual coding style fixes for src-folder
r2097 d_ptr->m_rubberBand = 0;
Michal Klocek
Fixes header guard style issues
r969 return;
}
if (!d_ptr->m_rubberBand) {
d_ptr->m_rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
d_ptr->m_rubberBand->setEnabled(true);
}
Titta Heikkala
Fix Chart build when QT_NO_RUBBERBAND is defined...
r2623 #else
Q_UNUSED(rubberBand);
qWarning("Unable to set rubber band because Qt is configured without it.");
#endif
Michal Klocek
Fixes header guard style issues
r969 }
/*!
Miikka Heikkinen
Documentation updates...
r2494 Returns the rubber band flags that are currently being used by the widget.
Michal Klocek
Fixes header guard style issues
r969 */
QChartView::RubberBands QChartView::rubberBand() const
{
return d_ptr->m_rubberBandFlags;
}
/*!
Miikka Heikkinen
Documentation updates...
r2494 If Left mouse button is pressed and the rubber band 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 rubber band is disabled then the \a event is passed to QGraphicsView::mousePressEvent() implementation.
Michal Klocek
Fixes header guard style issues
r969 */
void QChartView::mousePressEvent(QMouseEvent *event)
{
Titta Heikkala
Fix Chart build when QT_NO_RUBBERBAND is defined...
r2623 #ifndef QT_NO_RUBBERBAND
Titta Heikkala
Fix mouse event handling with RubberBand...
r2609 QRectF plotArea = d_ptr->m_chart->plotArea();
if (d_ptr->m_rubberBand && d_ptr->m_rubberBand->isEnabled()
&& event->button() == Qt::LeftButton && plotArea.contains(event->pos())) {
d_ptr->m_rubberBandOrigin = event->pos();
d_ptr->m_rubberBand->setGeometry(QRect(d_ptr->m_rubberBandOrigin, QSize()));
d_ptr->m_rubberBand->show();
event->accept();
Jani Honkonen
astyle and manual coding style fixes for src-folder
r2097 } else {
Titta Heikkala
Fix Chart build when QT_NO_RUBBERBAND is defined...
r2623 #endif
Michal Klocek
Fixes header guard style issues
r969 QGraphicsView::mousePressEvent(event);
Titta Heikkala
Fix Chart build when QT_NO_RUBBERBAND is defined...
r2623 #ifndef QT_NO_RUBBERBAND
Michal Klocek
Fixes header guard style issues
r969 }
Titta Heikkala
Fix Chart build when QT_NO_RUBBERBAND is defined...
r2623 #endif
Michal Klocek
Fixes header guard style issues
r969 }
/*!
Miikka Heikkinen
Documentation updates...
r2494 If the rubber band rectange has been displayed in pressEvent then \a event data is used to update the rubber band geometry.
Otherwise the default QGraphicsView::mouseMoveEvent implementation is called.
Michal Klocek
Fixes header guard style issues
r969 */
void QChartView::mouseMoveEvent(QMouseEvent *event)
{
Titta Heikkala
Fix Chart build when QT_NO_RUBBERBAND is defined...
r2623 #ifndef QT_NO_RUBBERBAND
Jani Honkonen
astyle and manual coding style fixes for src-folder
r2097 if (d_ptr->m_rubberBand && d_ptr->m_rubberBand->isVisible()) {
Michal Klocek
minor. fix warning on gcc4.1
r1961 QRect rect = d_ptr->m_chart->plotArea().toRect();
Michal Klocek
Fixes header guard style issues
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();
}
Miikka Heikkinen
Remove deprecated elements from APIs....
r2742 if (!d_ptr->m_rubberBandFlags.testFlag(HorizontalRubberBand)) {
Michal Klocek
Fixes header guard style issues
r969 d_ptr->m_rubberBandOrigin.setX(rect.left());
Jani Honkonen
astyle and manual coding style fixes for src-folder
r2097 width = rect.width();
Michal Klocek
Fixes header guard style issues
r969 }
Jani Honkonen
astyle and manual coding style fixes for src-folder
r2097 d_ptr->m_rubberBand->setGeometry(QRect(d_ptr->m_rubberBandOrigin.x(), d_ptr->m_rubberBandOrigin.y(), width, height).normalized());
} else {
Titta Heikkala
Fix Chart build when QT_NO_RUBBERBAND is defined...
r2623 #endif
Michal Klocek
Fixes header guard style issues
r969 QGraphicsView::mouseMoveEvent(event);
Titta Heikkala
Fix Chart build when QT_NO_RUBBERBAND is defined...
r2623 #ifndef QT_NO_RUBBERBAND
Michal Klocek
Fixes header guard style issues
r969 }
Titta Heikkala
Fix Chart build when QT_NO_RUBBERBAND is defined...
r2623 #endif
Michal Klocek
Fixes header guard style issues
r969 }
/*!
Miikka Heikkinen
Documentation updates...
r2494 If left mouse button is released and the rubber band is enabled then \a event is accepted and
the view is zoomed into the rect specified by the rubber band.
Miikka Heikkinen
Added QChart::zoomReset() and QChart::isZoomed()...
r2545 If it is a right mouse button \a event then the view is zoomed out.
Michal Klocek
Fixes header guard style issues
r969 */
void QChartView::mouseReleaseEvent(QMouseEvent *event)
{
Titta Heikkala
Fix Chart build when QT_NO_RUBBERBAND is defined...
r2623 #ifndef QT_NO_RUBBERBAND
Titta Heikkala
Fix mouse event handling with RubberBand...
r2609 if (d_ptr->m_rubberBand && d_ptr->m_rubberBand->isVisible()) {
if (event->button() == Qt::LeftButton) {
Michal Klocek
Fixes header guard style issues
r969 d_ptr->m_rubberBand->hide();
Miikka Heikkinen
Fix zooming when presenter dimensions are not integers...
r2416 QRectF rect = d_ptr->m_rubberBand->geometry();
// Since plotArea uses QRectF and rubberband uses QRect, we can't just blindly use
// rubberband's dimensions for vertical and horizontal rubberbands, where one
// dimension must match the corresponding plotArea dimension exactly.
if (d_ptr->m_rubberBandFlags == VerticalRubberBand) {
rect.setX(d_ptr->m_chart->plotArea().x());
rect.setWidth(d_ptr->m_chart->plotArea().width());
Miikka Heikkinen
Remove deprecated elements from APIs....
r2742 } else if (d_ptr->m_rubberBandFlags == HorizontalRubberBand) {
Miikka Heikkinen
Fix zooming when presenter dimensions are not integers...
r2416 rect.setY(d_ptr->m_chart->plotArea().y());
rect.setHeight(d_ptr->m_chart->plotArea().height());
}
Michal Klocek
Fixes header guard style issues
r969 d_ptr->m_chart->zoomIn(rect);
event->accept();
}
Titta Heikkala
Fix regression with right click zooming...
r2656 } else if (d_ptr->m_rubberBand && event->button() == Qt::RightButton) {
Miikka Heikkinen
Fixed QChartView right click zoom out when zooming one axis...
r2546 // If vertical or horizontal rubberband mode, restrict zoom out to specified axis.
// Since there is no suitable API for that, use zoomIn with rect bigger than the
// plot area.
if (d_ptr->m_rubberBandFlags == VerticalRubberBand
Miikka Heikkinen
Remove deprecated elements from APIs....
r2742 || d_ptr->m_rubberBandFlags == HorizontalRubberBand) {
Miikka Heikkinen
Fixed QChartView right click zoom out when zooming one axis...
r2546 QRectF rect = d_ptr->m_chart->plotArea();
if (d_ptr->m_rubberBandFlags == VerticalRubberBand) {
qreal adjustment = rect.height() / 2;
rect.adjust(0, -adjustment, 0, adjustment);
Miikka Heikkinen
Remove deprecated elements from APIs....
r2742 } else if (d_ptr->m_rubberBandFlags == HorizontalRubberBand) {
Miikka Heikkinen
Fixed QChartView right click zoom out when zooming one axis...
r2546 qreal adjustment = rect.width() / 2;
rect.adjust(-adjustment, 0, adjustment, 0);
}
d_ptr->m_chart->zoomIn(rect);
} else {
d_ptr->m_chart->zoomOut();
}
Michal Klocek
Fixes header guard style issues
r969 event->accept();
Jani Honkonen
astyle and manual coding style fixes for src-folder
r2097 } else {
Titta Heikkala
Fix Chart build when QT_NO_RUBBERBAND is defined...
r2623 #endif
Michal Klocek
Fixes header guard style issues
r969 QGraphicsView::mouseReleaseEvent(event);
Titta Heikkala
Fix Chart build when QT_NO_RUBBERBAND is defined...
r2623 #ifndef QT_NO_RUBBERBAND
Michal Klocek
Fixes header guard style issues
r969 }
Titta Heikkala
Fix Chart build when QT_NO_RUBBERBAND is defined...
r2623 #endif
Michal Klocek
Fixes header guard style issues
r969 }
/*!
Resizes and updates the chart area using the \a event data
*/
void QChartView::resizeEvent(QResizeEvent *event)
{
QGraphicsView::resizeEvent(event);
Jani Honkonen
Implement QChartView::setChart()
r2056 d_ptr->resize();
Michal Klocek
Fixes header guard style issues
r969 }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Jani Honkonen
more coding style fixes for src-folder...
r2104 QChartViewPrivate::QChartViewPrivate(QChartView *q, QChart *chart)
Jani Honkonen
astyle and manual coding style fixes for src-folder
r2097 : q_ptr(q),
m_scene(new QGraphicsScene(q)),
m_chart(chart),
Titta Heikkala
Fix Chart build when QT_NO_RUBBERBAND is defined...
r2623 #ifndef QT_NO_RUBBERBAND
Jani Honkonen
astyle and manual coding style fixes for src-folder
r2097 m_rubberBand(0),
Titta Heikkala
Fix Chart build when QT_NO_RUBBERBAND is defined...
r2623 #endif
Jani Honkonen
astyle and manual coding style fixes for src-folder
r2097 m_rubberBandFlags(QChartView::NoRubberBand)
Michal Klocek
Fixes header guard style issues
r969 {
Michal Klocek
Adds QChartView empty constructor to be able to promote qchartview in qtdesigner
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);
Jani Honkonen
astyle and manual coding style fixes for src-folder
r2097 if (!m_chart)
m_chart = new QChart();
Michal Klocek
Adds QChartView empty constructor to be able to promote qchartview in qtdesigner
r1290 m_scene->addItem(m_chart);
Michal Klocek
Fixes header guard style issues
r969 }
QChartViewPrivate::~QChartViewPrivate()
{
}
Jani Honkonen
Implement QChartView::setChart()
r2056 void QChartViewPrivate::setChart(QChart *chart)
{
Q_ASSERT(chart);
if (m_chart == chart)
return;
if (m_chart)
m_scene->removeItem(m_chart);
m_chart = chart;
m_scene->addItem(m_chart);
resize();
}
void QChartViewPrivate::resize()
{
Miikka Heikkinen
Fix ChartView rotations for non-90 degree steps....
r2507 // Fit the chart into view if it has been rotated
qreal sinA = qAbs(q_ptr->transform().m21());
qreal cosA = qAbs(q_ptr->transform().m11());
Miikka Heikkinen
Fix chart resize for rotated QChartViews....
r2500 QSize chartSize = q_ptr->size();
Miikka Heikkinen
Fix ChartView rotations for non-90 degree steps....
r2507 if (sinA == 1.0) {
Miikka Heikkinen
Fix chart resize for rotated QChartViews....
r2500 chartSize.setHeight(q_ptr->size().width());
chartSize.setWidth(q_ptr->size().height());
Miikka Heikkinen
Fix ChartView rotations for non-90 degree steps....
r2507 } else if (sinA != 0.0) {
// Non-90 degree rotation, find largest square chart that can fit into the view.
qreal minDimension = qMin(q_ptr->size().width(), q_ptr->size().height());
qreal h = (minDimension - (minDimension / ((sinA / cosA) + 1.0))) / sinA;
chartSize.setHeight(h);
chartSize.setWidth(h);
Miikka Heikkinen
Fix chart resize for rotated QChartViews....
r2500 }
m_chart->resize(chartSize);
Jani Honkonen
Implement QChartView::setChart()
r2056 q_ptr->setMinimumSize(m_chart->minimumSize().toSize());
q_ptr->setSceneRect(m_chart->geometry());
}
Michal Klocek
Fixes header guard style issues
r969 #include "moc_qchartview.cpp"
Titta Heikkala
Qt Charts project file structure change...
r2712 QT_CHARTS_END_NAMESPACE