diff --git a/examples/examples.pro b/examples/examples.pro index 51c74ef..9d0d0cc 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 0000000..a28e48d --- /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 +#include +#include + +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(event)); + return QChart::event(event); +} + +bool Chart::gestureEvent(QGestureEvent* event) +{ + if (QGesture *gesture = event->gesture(Qt::PanGesture)) { + QPanGesture *pan = static_cast(gesture); + QChart::scroll(pan->delta().x(),pan->delta().y()); + } + + if (QGesture *gesture = event->gesture(Qt::PinchGesture)) { + QPinchGesture *pinch = static_cast(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 0000000..0f39663 --- /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 + +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 0000000..9c85ca6 --- /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 +#include + +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 0000000..ea0c33c --- /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 +#include + +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 0000000..5daf6b5 --- /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 +#include +#include +#include +#include +#include +#include +#include + +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 0000000..0e14306 --- /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 diff --git a/tests/chartdesigner/engine.cpp b/tests/chartdesigner/engine.cpp index 6ba2534..bbc61c0 100644 --- a/tests/chartdesigner/engine.cpp +++ b/tests/chartdesigner/engine.cpp @@ -21,9 +21,9 @@ #include "engine.h" #include #include -#include -#include -#include +#include +#include +#include #include #include #include @@ -49,7 +49,6 @@ Engine::~Engine() delete m_chart; delete m_selection; delete m_model; - } void Engine::createModels() @@ -155,6 +154,8 @@ QList Engine::addSeries(QAbstractSeries::SeriesType type) break; } } + + m_chart->createDefaultAxes(); return result; } @@ -255,14 +256,13 @@ bool Engine::load(const QString &filename) void Engine::setupXYSeries(QXYSeries *xyseries, const QList& columns, int column, int minRow, int maxRow) { - xyseries->setModel(m_model); - QXYModelMapper* mapper = new QXYModelMapper(xyseries); - xyseries->setModelMapper(mapper); - mapper->setMapX(columns.first()); - mapper->setMapY(columns.at(column)); - mapper->setOrientation(Qt::Vertical); - mapper->setFirst(minRow); - mapper->setCount(maxRow - minRow + 1); + QVXYModelMapper* mapper = new QVXYModelMapper(xyseries); + mapper->setSeries(xyseries); + mapper->setModel(m_model); + mapper->setXColumn(columns.first()); + mapper->setYColumn(columns.at(column)); + mapper->setFirstRow(minRow); + mapper->setRowCount(maxRow - minRow + 1); m_chart->addSeries(xyseries); xyseries->setName(QString("Series %1").arg(m_chart->series().count())); QObject::connect(xyseries,SIGNAL(clicked(const QPointF&)),this,SIGNAL(selected())); @@ -277,17 +277,15 @@ void Engine::setupXYSeries(QXYSeries *xyseries, const QList& columns, int c m_seriesModelIndex.insert(xyseries,result); } -void Engine::setupBarSeries(QBarSeries *bar, const QList& columns, int minRow, int maxRow) +void Engine::setupBarSeries(QAbstractBarSeries *bar, const QList& columns, int minRow, int maxRow) { - bar->setModel(m_model); - QBarModelMapper* mapper = new QBarModelMapper(bar); - bar->setModelMapper(mapper); - mapper->setMapCategories(columns.first()); - mapper->setMapBarTop(columns.last()); - mapper->setMapBarBottom(columns.at(1)); - mapper->setOrientation(Qt::Vertical); - mapper->setFirst(minRow); - mapper->setCount(maxRow - minRow + 1); + QHBarModelMapper* mapper = new QHBarModelMapper(bar); + mapper->setSeries(bar); + mapper->setModel(m_model); + mapper->setFirstColumn(minRow); + mapper->setColumnCount(maxRow - minRow + 1); + mapper->setFirstBarSetRow(columns.at(1)); + mapper->setLastBarSetRow(columns.last()); m_chart->addSeries(bar); bar->setName(QString("Series %1").arg(m_chart->series().count())); @@ -301,14 +299,13 @@ void Engine::setupBarSeries(QBarSeries *bar, const QList& columns, int minR void Engine::setupPieSeries(QPieSeries *pie, const QList& columns, int minRow, int maxRow) { - pie->setModel(m_model); - QPieModelMapper* mapper = new QPieModelMapper(pie); - pie->setModelMapper(mapper); - mapper->setMapValues(columns.at(1)); - mapper->setMapLabels(columns.first()); - mapper->setOrientation(Qt::Vertical); - mapper->setFirst(minRow); - mapper->setCount(maxRow - minRow + 1); + QVPieModelMapper* mapper = new QVPieModelMapper(pie); + mapper->setSeries(pie); + mapper->setModel(m_model); + mapper->setValuesColumn(columns.at(1)); + mapper->setLabelsColumn(columns.first()); + mapper->setFirstRow(minRow); + mapper->setRowCount(maxRow - minRow + 1); m_chart->addSeries(pie); pie->setName(QString("Series %1").arg(m_chart->series().count())); @@ -320,22 +317,22 @@ void Engine::setupPieSeries(QPieSeries *pie, const QList& columns, int minR void Engine::setupAreaSeries(QAreaSeries *series, const QList& columns, int minRow, int maxRow) { - series->lowerSeries()->setModel(m_model); - series->upperSeries()->setModel(m_model); - QXYModelMapper* umapper = new QXYModelMapper(series); - umapper->setMapX(columns.first()); - umapper->setMapY(columns.at(1)); - umapper->setOrientation(Qt::Vertical); - umapper->setFirst(minRow); - umapper->setCount(maxRow - minRow + 1); - QXYModelMapper* lmapper = new QXYModelMapper(series); - lmapper->setMapX(columns.first()); - lmapper->setMapY(columns.at(2)); - lmapper->setOrientation(Qt::Vertical); - lmapper->setFirst(minRow); - lmapper->setCount(maxRow - minRow + 1); - series->upperSeries()->setModelMapper(umapper); - series->lowerSeries()->setModelMapper(lmapper); + QVXYModelMapper* umapper = new QVXYModelMapper(series); + umapper->setSeries(series->upperSeries()); + umapper->setModel(m_model); + umapper->setXColumn(columns.first()); + umapper->setYColumn(columns.at(1)); + umapper->setFirstRow(minRow); + umapper->setRowCount(maxRow - minRow + 1); + + QVXYModelMapper* lmapper = new QVXYModelMapper(series); + lmapper->setSeries(series->lowerSeries()); + lmapper->setModel(m_model); + lmapper->setXColumn(columns.first()); + lmapper->setYColumn(columns.at(2)); + lmapper->setFirstRow(minRow); + lmapper->setRowCount(maxRow - minRow + 1); + m_chart->addSeries(series); series->setName(QString("Series %1").arg(m_chart->series().count())); diff --git a/tests/chartdesigner/engine.h b/tests/chartdesigner/engine.h index 26e64f0..d5f8fe6 100644 --- a/tests/chartdesigner/engine.h +++ b/tests/chartdesigner/engine.h @@ -32,7 +32,7 @@ class QItemSelectionModel; QTCOMMERCIALCHART_BEGIN_NAMESPACE class QChart; class QXYSeries; -class QBarSeries; +class QAbstractBarSeries; class QPieSeries; class QAreaSeries; QTCOMMERCIALCHART_END_NAMESPACE @@ -61,7 +61,7 @@ signals: private: void createModels(); void setupXYSeries(QXYSeries *xyseries, const QList& columns, int column, int minRow, int maxRow); - void setupBarSeries(QBarSeries *series, const QList& columns, int minRow, int maxRow); + void setupBarSeries(QAbstractBarSeries *series, const QList& columns, int minRow, int maxRow); void setupPieSeries(QPieSeries *pie, const QList& columns, int minRow, int maxRow); void setupAreaSeries(QAreaSeries *series, const QList& columns, int minRow, int maxRow); diff --git a/tests/tests.pro b/tests/tests.pro index 11b1f33..ba01eb3 100644 --- a/tests/tests.pro +++ b/tests/tests.pro @@ -6,7 +6,8 @@ TEMPLATE = subdirs SUBDIRS += \ auto \ chartwidgettest \ - qmlchartproperties + qmlchartproperties \ + chartdesigner !linux-arm*: { SUBDIRS += \