diff options
author | Michal Klocek <michal.klocek@digia.com> | 2012-07-19 17:32:01 +0300 |
---|---|---|
committer | Michal Klocek <michal.klocek@digia.com> | 2012-07-19 17:32:01 +0300 |
commit | 4a02b714acfaee5ba8de2683e6346b5181a7f068 (patch) | |
tree | ddb19739ca67913d65efe032ab33b7a8f85c2474 /examples | |
parent | c93d43dcbcb81fa19c9f691d0b20f7c3d384b27c (diff) |
Adds scrollchart, fixes chartdesigner
Diffstat (limited to 'examples')
-rw-r--r-- | examples/examples.pro | 3 | ||||
-rw-r--r-- | examples/scrollchart/chart.cpp | 63 | ||||
-rw-r--r-- | examples/scrollchart/chart.h | 46 | ||||
-rw-r--r-- | examples/scrollchart/chartview.cpp | 111 | ||||
-rw-r--r-- | examples/scrollchart/chartview.h | 46 | ||||
-rw-r--r-- | examples/scrollchart/main.cpp | 89 | ||||
-rw-r--r-- | examples/scrollchart/scrollchart.pro | 7 |
7 files changed, 364 insertions, 1 deletions
diff --git a/examples/examples.pro b/examples/examples.pro index 51c74eff..9d0d0cc3 100644 --- a/examples/examples.pro +++ b/examples/examples.pro @@ -28,4 +28,5 @@ SUBDIRS += \ horizontalstackedbarchart \ horizontalpercentbarchart \ donut \ - donutdrilldown + donutdrilldown \ + scrollchart diff --git a/examples/scrollchart/chart.cpp b/examples/scrollchart/chart.cpp new file mode 100644 index 00000000..a28e48da --- /dev/null +++ b/examples/scrollchart/chart.cpp @@ -0,0 +1,63 @@ +/**************************************************************************** +** +** 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 "chart.h" +#include <QGesture> +#include <QGraphicsScene> +#include <QGraphicsView> + +Chart::Chart(QGraphicsItem *parent, Qt::WindowFlags wFlags) + :QChart(parent, wFlags) +{ + // Seems that QGraphicsView (QChartView) does not grab gestures. + // They can only be grabbed here in the QGraphicsWidget (QChart). + grabGesture(Qt::PanGesture); + grabGesture(Qt::PinchGesture); +} + +Chart::~Chart() +{ + +} + +//![1] +bool Chart::sceneEvent(QEvent *event) +{ + if (event->type() == QEvent::Gesture) + return gestureEvent(static_cast<QGestureEvent*>(event)); + return QChart::event(event); +} + +bool Chart::gestureEvent(QGestureEvent* event) +{ + if (QGesture *gesture = event->gesture(Qt::PanGesture)) { + QPanGesture *pan = static_cast<QPanGesture *>(gesture); + QChart::scroll(pan->delta().x(),pan->delta().y()); + } + + if (QGesture *gesture = event->gesture(Qt::PinchGesture)) { + QPinchGesture *pinch = static_cast<QPinchGesture *>(gesture); + if (pinch->changeFlags() & QPinchGesture::ScaleFactorChanged) + QChart::zoom(pinch->scaleFactor()); + } + + return true; +} +//![1] diff --git a/examples/scrollchart/chart.h b/examples/scrollchart/chart.h new file mode 100644 index 00000000..0f396633 --- /dev/null +++ b/examples/scrollchart/chart.h @@ -0,0 +1,46 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +#ifndef CHART_H +#define CHART_H + +#include <QChart> + +QTCOMMERCIALCHART_USE_NAMESPACE + +//![1] +class Chart : public QChart +//![1] +{ +public: + explicit Chart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0); + ~Chart(); + +protected: + bool sceneEvent(QEvent *event); + +private: + bool gestureEvent(QGestureEvent* event); + +private: + +}; + +#endif // CHART_H diff --git a/examples/scrollchart/chartview.cpp b/examples/scrollchart/chartview.cpp new file mode 100644 index 00000000..9c85ca66 --- /dev/null +++ b/examples/scrollchart/chartview.cpp @@ -0,0 +1,111 @@ +/**************************************************************************** + ** + ** 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 "chartview.h" +#include <QMouseEvent> +#include <QDebug> + +ChartView::ChartView(QChart *chart, QWidget *parent) : + QChartView(chart, parent), m_rubberBand(QRubberBand::Rectangle, this), m_isScrolling( + false) +{ + // setRubberBand(QChartView::RectangleRubberBand); + this->chart()->setAnimationOptions(QChart::NoAnimation); +} + +void ChartView::mousePressEvent(QMouseEvent *event) +{ + if (event->button() == Qt::RightButton) { + + QRectF rect = chart()->plotArea(); + + m_origin = event->pos(); + + if (!rect.contains(m_origin)) + return; + + m_rubberBand.setGeometry(QRect(m_origin, QSize())); + m_rubberBand.show(); + } + + if (event->button() == Qt::LeftButton) { + m_origin = event->pos(); + m_isScrolling = true; + } + + event->accept(); +} + +void ChartView::mouseMoveEvent(QMouseEvent *event) +{ + if (m_rubberBand.isVisible()) + m_rubberBand.setGeometry(QRect(m_origin, event->pos()).normalized()); + + if (m_isScrolling) { + QPointF delta = m_origin - event->pos(); + chart()->scroll(delta.x(),-delta.y()); + m_origin = event->pos(); + } + +} + +void ChartView::mouseReleaseEvent(QMouseEvent *event) +{ + if (event->button() == Qt::RightButton && m_rubberBand.isVisible()) { + m_rubberBand.hide(); + + QRect rect = m_rubberBand.geometry(); + chart()->zoomIn(rect); + event->accept(); + } + + if (event->button() == Qt::LeftButton) { + m_isScrolling = false; + } +} + +//![1] +void ChartView::keyPressEvent(QKeyEvent *event) +{ + switch (event->key()) { + case Qt::Key_Plus: + chart()->zoomIn(); + break; + case Qt::Key_Minus: + chart()->zoomOut(); + break; +//![1] + case Qt::Key_Left: + chart()->scroll(-10, 0); + break; + case Qt::Key_Right: + chart()->scroll(10, 0); + break; + case Qt::Key_Up: + chart()->scroll(0, 10); + break; + case Qt::Key_Down: + chart()->scroll(0, -10); + break; + default: + QGraphicsView::keyPressEvent(event); + break; + } +} diff --git a/examples/scrollchart/chartview.h b/examples/scrollchart/chartview.h new file mode 100644 index 00000000..ea0c33c8 --- /dev/null +++ b/examples/scrollchart/chartview.h @@ -0,0 +1,46 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +#ifndef CHARTVIEW_H +#define CHARTVIEW_H + +#include <QChartView> +#include <QRubberBand> + +QTCOMMERCIALCHART_USE_NAMESPACE + +class ChartView : public QChartView +{ +public: + ChartView(QChart *chart, QWidget *parent = 0); +protected: + void mousePressEvent(QMouseEvent *event); + void mouseMoveEvent(QMouseEvent *event); + void mouseReleaseEvent(QMouseEvent *event); + void keyPressEvent(QKeyEvent *event); + +private: + bool m_isScrolling; + bool m_isRubberBandShown; + QRubberBand m_rubberBand; + QPoint m_origin; +}; + +#endif diff --git a/examples/scrollchart/main.cpp b/examples/scrollchart/main.cpp new file mode 100644 index 00000000..5daf6b5a --- /dev/null +++ b/examples/scrollchart/main.cpp @@ -0,0 +1,89 @@ +/**************************************************************************** + ** + ** 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 "chart.h" +#include "chartview.h" +#include <QApplication> +#include <QMainWindow> +#include <qmath.h> +#include <QBarSeries> +#include <QBarSet> +#include <QBarCategoriesAxis> +#include <QLineSeries> +#include <QHorizontalBarSeries> + +QTCOMMERCIALCHART_USE_NAMESPACE + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + + QLineSeries* series = new QLineSeries(); + for (int i = 0; i < 500; i++) { + QPointF p((qreal) i, qSin(M_PI / 50 * i) * 100); + p.ry() += qrand() % 20; + *series << p; + } + + QBarSet *set0 = new QBarSet("Jane"); + QBarSet *set1 = new QBarSet("John"); + QBarSet *set2 = new QBarSet("Axel"); + QBarSet *set3 = new QBarSet("Mary"); + QBarSet *set4 = new QBarSet("Samantha"); + + *set0 << 1 << 2 << 3 << 4 << 5 << 6; + *set1 << 5 << 0 << 0 << 4 << 0 << 7; + *set2 << 3 << 5 << 8 << 13 << 8 << 5; + *set3 << 5 << 6 << 7 << 3 << 4 << 5; + *set4 << 9 << 7 << 5 << 3 << 1 << 2; + + //QHorizontalBarSeries* series2 = new QHorizontalBarSeries(); + QBarSeries* series2 = new QBarSeries(); + series2->append(set0); + series2->append(set1); + series2->append(set2); + series2->append(set3); + series2->append(set4); + + Chart* chart = new Chart(); + // chart->addSeries(series); + chart->addSeries(series2); + chart->setTitle("Scroll in/out example"); + chart->legend()->hide(); + chart->createDefaultAxes(); + + QStringList categories; + categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun"; + QBarCategoriesAxis* axis = new QBarCategoriesAxis(); + axis->append(categories); + chart->setAxisX(axis, series2); + + ChartView* chartView = new ChartView(chart); + chartView->setRenderHint(QPainter::Antialiasing); + + QMainWindow window; + window.setCentralWidget(chartView); + window.resize(400, 300); + window.grabGesture(Qt::PanGesture); + window.grabGesture(Qt::PinchGesture); + window.show(); + + return a.exec(); +} diff --git a/examples/scrollchart/scrollchart.pro b/examples/scrollchart/scrollchart.pro new file mode 100644 index 00000000..0e143064 --- /dev/null +++ b/examples/scrollchart/scrollchart.pro @@ -0,0 +1,7 @@ +!include( ../examples.pri ) { + error( "Couldn't find the examples.pri file!" ) +} +TARGET = scrollchart +HEADERS += chart.h chartview.h + +SOURCES += main.cpp chart.cpp chartview.cpp |