##// END OF EJS Templates
Adds scrollchart, fixes chartdesigner
Michal Klocek -
r1722:4a02b714acfa
parent child
Show More
@@ -0,0 +1,63
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 #include "chart.h"
22 #include <QGesture>
23 #include <QGraphicsScene>
24 #include <QGraphicsView>
25
26 Chart::Chart(QGraphicsItem *parent, Qt::WindowFlags wFlags)
27 :QChart(parent, wFlags)
28 {
29 // Seems that QGraphicsView (QChartView) does not grab gestures.
30 // They can only be grabbed here in the QGraphicsWidget (QChart).
31 grabGesture(Qt::PanGesture);
32 grabGesture(Qt::PinchGesture);
33 }
34
35 Chart::~Chart()
36 {
37
38 }
39
40 //![1]
41 bool Chart::sceneEvent(QEvent *event)
42 {
43 if (event->type() == QEvent::Gesture)
44 return gestureEvent(static_cast<QGestureEvent*>(event));
45 return QChart::event(event);
46 }
47
48 bool Chart::gestureEvent(QGestureEvent* event)
49 {
50 if (QGesture *gesture = event->gesture(Qt::PanGesture)) {
51 QPanGesture *pan = static_cast<QPanGesture *>(gesture);
52 QChart::scroll(pan->delta().x(),pan->delta().y());
53 }
54
55 if (QGesture *gesture = event->gesture(Qt::PinchGesture)) {
56 QPinchGesture *pinch = static_cast<QPinchGesture *>(gesture);
57 if (pinch->changeFlags() & QPinchGesture::ScaleFactorChanged)
58 QChart::zoom(pinch->scaleFactor());
59 }
60
61 return true;
62 }
63 //![1]
@@ -0,0 +1,46
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 #ifndef CHART_H
22 #define CHART_H
23
24 #include <QChart>
25
26 QTCOMMERCIALCHART_USE_NAMESPACE
27
28 //![1]
29 class Chart : public QChart
30 //![1]
31 {
32 public:
33 explicit Chart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0);
34 ~Chart();
35
36 protected:
37 bool sceneEvent(QEvent *event);
38
39 private:
40 bool gestureEvent(QGestureEvent* event);
41
42 private:
43
44 };
45
46 #endif // CHART_H
@@ -0,0 +1,111
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 #include "chartview.h"
22 #include <QMouseEvent>
23 #include <QDebug>
24
25 ChartView::ChartView(QChart *chart, QWidget *parent) :
26 QChartView(chart, parent), m_rubberBand(QRubberBand::Rectangle, this), m_isScrolling(
27 false)
28 {
29 // setRubberBand(QChartView::RectangleRubberBand);
30 this->chart()->setAnimationOptions(QChart::NoAnimation);
31 }
32
33 void ChartView::mousePressEvent(QMouseEvent *event)
34 {
35 if (event->button() == Qt::RightButton) {
36
37 QRectF rect = chart()->plotArea();
38
39 m_origin = event->pos();
40
41 if (!rect.contains(m_origin))
42 return;
43
44 m_rubberBand.setGeometry(QRect(m_origin, QSize()));
45 m_rubberBand.show();
46 }
47
48 if (event->button() == Qt::LeftButton) {
49 m_origin = event->pos();
50 m_isScrolling = true;
51 }
52
53 event->accept();
54 }
55
56 void ChartView::mouseMoveEvent(QMouseEvent *event)
57 {
58 if (m_rubberBand.isVisible())
59 m_rubberBand.setGeometry(QRect(m_origin, event->pos()).normalized());
60
61 if (m_isScrolling) {
62 QPointF delta = m_origin - event->pos();
63 chart()->scroll(delta.x(),-delta.y());
64 m_origin = event->pos();
65 }
66
67 }
68
69 void ChartView::mouseReleaseEvent(QMouseEvent *event)
70 {
71 if (event->button() == Qt::RightButton && m_rubberBand.isVisible()) {
72 m_rubberBand.hide();
73
74 QRect rect = m_rubberBand.geometry();
75 chart()->zoomIn(rect);
76 event->accept();
77 }
78
79 if (event->button() == Qt::LeftButton) {
80 m_isScrolling = false;
81 }
82 }
83
84 //![1]
85 void ChartView::keyPressEvent(QKeyEvent *event)
86 {
87 switch (event->key()) {
88 case Qt::Key_Plus:
89 chart()->zoomIn();
90 break;
91 case Qt::Key_Minus:
92 chart()->zoomOut();
93 break;
94 //![1]
95 case Qt::Key_Left:
96 chart()->scroll(-10, 0);
97 break;
98 case Qt::Key_Right:
99 chart()->scroll(10, 0);
100 break;
101 case Qt::Key_Up:
102 chart()->scroll(0, 10);
103 break;
104 case Qt::Key_Down:
105 chart()->scroll(0, -10);
106 break;
107 default:
108 QGraphicsView::keyPressEvent(event);
109 break;
110 }
111 }
@@ -0,0 +1,46
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 #ifndef CHARTVIEW_H
22 #define CHARTVIEW_H
23
24 #include <QChartView>
25 #include <QRubberBand>
26
27 QTCOMMERCIALCHART_USE_NAMESPACE
28
29 class ChartView : public QChartView
30 {
31 public:
32 ChartView(QChart *chart, QWidget *parent = 0);
33 protected:
34 void mousePressEvent(QMouseEvent *event);
35 void mouseMoveEvent(QMouseEvent *event);
36 void mouseReleaseEvent(QMouseEvent *event);
37 void keyPressEvent(QKeyEvent *event);
38
39 private:
40 bool m_isScrolling;
41 bool m_isRubberBandShown;
42 QRubberBand m_rubberBand;
43 QPoint m_origin;
44 };
45
46 #endif
@@ -0,0 +1,89
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 #include "chart.h"
22 #include "chartview.h"
23 #include <QApplication>
24 #include <QMainWindow>
25 #include <qmath.h>
26 #include <QBarSeries>
27 #include <QBarSet>
28 #include <QBarCategoriesAxis>
29 #include <QLineSeries>
30 #include <QHorizontalBarSeries>
31
32 QTCOMMERCIALCHART_USE_NAMESPACE
33
34 int main(int argc, char *argv[])
35 {
36 QApplication a(argc, argv);
37
38 QLineSeries* series = new QLineSeries();
39 for (int i = 0; i < 500; i++) {
40 QPointF p((qreal) i, qSin(M_PI / 50 * i) * 100);
41 p.ry() += qrand() % 20;
42 *series << p;
43 }
44
45 QBarSet *set0 = new QBarSet("Jane");
46 QBarSet *set1 = new QBarSet("John");
47 QBarSet *set2 = new QBarSet("Axel");
48 QBarSet *set3 = new QBarSet("Mary");
49 QBarSet *set4 = new QBarSet("Samantha");
50
51 *set0 << 1 << 2 << 3 << 4 << 5 << 6;
52 *set1 << 5 << 0 << 0 << 4 << 0 << 7;
53 *set2 << 3 << 5 << 8 << 13 << 8 << 5;
54 *set3 << 5 << 6 << 7 << 3 << 4 << 5;
55 *set4 << 9 << 7 << 5 << 3 << 1 << 2;
56
57 //QHorizontalBarSeries* series2 = new QHorizontalBarSeries();
58 QBarSeries* series2 = new QBarSeries();
59 series2->append(set0);
60 series2->append(set1);
61 series2->append(set2);
62 series2->append(set3);
63 series2->append(set4);
64
65 Chart* chart = new Chart();
66 // chart->addSeries(series);
67 chart->addSeries(series2);
68 chart->setTitle("Scroll in/out example");
69 chart->legend()->hide();
70 chart->createDefaultAxes();
71
72 QStringList categories;
73 categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun";
74 QBarCategoriesAxis* axis = new QBarCategoriesAxis();
75 axis->append(categories);
76 chart->setAxisX(axis, series2);
77
78 ChartView* chartView = new ChartView(chart);
79 chartView->setRenderHint(QPainter::Antialiasing);
80
81 QMainWindow window;
82 window.setCentralWidget(chartView);
83 window.resize(400, 300);
84 window.grabGesture(Qt::PanGesture);
85 window.grabGesture(Qt::PinchGesture);
86 window.show();
87
88 return a.exec();
89 }
@@ -0,0 +1,7
1 !include( ../examples.pri ) {
2 error( "Couldn't find the examples.pri file!" )
3 }
4 TARGET = scrollchart
5 HEADERS += chart.h chartview.h
6
7 SOURCES += main.cpp chart.cpp chartview.cpp
@@ -1,31 +1,32
1 CURRENTLY_BUILDING_COMPONENTS = "examples"
1 CURRENTLY_BUILDING_COMPONENTS = "examples"
2 !include( ../config.pri ) {
2 !include( ../config.pri ) {
3 error( "Couldn't find the config.pri file!" )
3 error( "Couldn't find the config.pri file!" )
4 }
4 }
5
5
6 TEMPLATE = subdirs
6 TEMPLATE = subdirs
7 SUBDIRS += \
7 SUBDIRS += \
8 areachart \
8 areachart \
9 customchart \
9 customchart \
10 linechart \
10 linechart \
11 percentbarchart \
11 percentbarchart \
12 piechart \
12 piechart \
13 piechartdrilldown \
13 piechartdrilldown \
14 presenterchart \
14 presenterchart \
15 scatterchart \
15 scatterchart \
16 scatterinteractions \
16 scatterinteractions \
17 splinechart \
17 splinechart \
18 stackedbarchart \
18 stackedbarchart \
19 stackedbarchartdrilldown \
19 stackedbarchartdrilldown \
20 zoomlinechart \
20 zoomlinechart \
21 modeldata \
21 modeldata \
22 barchart \
22 barchart \
23 legend \
23 legend \
24 barmodelmapper \
24 barmodelmapper \
25 qmlpiechart \
25 qmlpiechart \
26 lineandbar \
26 lineandbar \
27 horizontalbarchart \
27 horizontalbarchart \
28 horizontalstackedbarchart \
28 horizontalstackedbarchart \
29 horizontalpercentbarchart \
29 horizontalpercentbarchart \
30 donut \
30 donut \
31 donutdrilldown
31 donutdrilldown \
32 scrollchart
@@ -1,347 +1,344
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "engine.h"
21 #include "engine.h"
22 #include <QItemSelectionModel>
22 #include <QItemSelectionModel>
23 #include <QStandardItemModel>
23 #include <QStandardItemModel>
24 #include <QXYModelMapper>
24 #include <QVXYModelMapper>
25 #include <QBarModelMapper>
25 #include <QHBarModelMapper>
26 #include <QPieModelMapper>
26 #include <QVPieModelMapper>
27 #include <QLineSeries>
27 #include <QLineSeries>
28 #include <QSplineSeries>
28 #include <QSplineSeries>
29 #include <QScatterSeries>
29 #include <QScatterSeries>
30 #include <QBarSeries>
30 #include <QBarSeries>
31 #include <QPercentBarSeries>
31 #include <QPercentBarSeries>
32 #include <QStackedBarSeries>
32 #include <QStackedBarSeries>
33 #include <QAreaSeries>
33 #include <QAreaSeries>
34 #include <QPieSeries>
34 #include <QPieSeries>
35 #include <QChart>
35 #include <QChart>
36 #include <QBarSet>
36 #include <QBarSet>
37
37
38
38
39 const qint32 MAGIC_NUMBER = 0x66666666;
39 const qint32 MAGIC_NUMBER = 0x66666666;
40
40
41 Engine::Engine(QObject* parent) :
41 Engine::Engine(QObject* parent) :
42 QObject(parent), m_count(10), m_chart(new QChart()), m_model(0), m_selection(0)
42 QObject(parent), m_count(10), m_chart(new QChart()), m_model(0), m_selection(0)
43 {
43 {
44 createModels();
44 createModels();
45 }
45 }
46
46
47 Engine::~Engine()
47 Engine::~Engine()
48 {
48 {
49 delete m_chart;
49 delete m_chart;
50 delete m_selection;
50 delete m_selection;
51 delete m_model;
51 delete m_model;
52
53 }
52 }
54
53
55 void Engine::createModels()
54 void Engine::createModels()
56 {
55 {
57 m_model = new QStandardItemModel(m_count, m_count);
56 m_model = new QStandardItemModel(m_count, m_count);
58 m_model->setHorizontalHeaderLabels(
57 m_model->setHorizontalHeaderLabels(
59 QStringList() << "A" << "B" << "C" << "D" << "E" << "F" << "G" << "H" << "I" << "J");
58 QStringList() << "A" << "B" << "C" << "D" << "E" << "F" << "G" << "H" << "I" << "J");
60 m_model->setVerticalHeaderLabels(
59 m_model->setVerticalHeaderLabels(
61 QStringList() << "1" << "2" << "3" << "4" << "5" << "6" << "7" << "8" << "9" << "10");
60 QStringList() << "1" << "2" << "3" << "4" << "5" << "6" << "7" << "8" << "9" << "10");
62 m_selection = new QItemSelectionModel(m_model);
61 m_selection = new QItemSelectionModel(m_model);
63
62
64 }
63 }
65
64
66 QList<QAbstractSeries*> Engine::addSeries(QAbstractSeries::SeriesType type)
65 QList<QAbstractSeries*> Engine::addSeries(QAbstractSeries::SeriesType type)
67 {
66 {
68 const QModelIndexList& list = m_selection->selectedIndexes();
67 const QModelIndexList& list = m_selection->selectedIndexes();
69
68
70 QMap<int, QModelIndex> columns;
69 QMap<int, QModelIndex> columns;
71
70
72 foreach(const QModelIndex& index, list) {
71 foreach(const QModelIndex& index, list) {
73 columns.insertMulti(index.column(), index);
72 columns.insertMulti(index.column(), index);
74 }
73 }
75
74
76 QList<int> keys = columns.uniqueKeys();
75 QList<int> keys = columns.uniqueKeys();
77
76
78 QModelIndexList rows = columns.values(keys.first());
77 QModelIndexList rows = columns.values(keys.first());
79
78
80 int minRow = m_count + 1;
79 int minRow = m_count + 1;
81 int maxRow = -1;
80 int maxRow = -1;
82
81
83 foreach(const QModelIndex& index, rows) {
82 foreach(const QModelIndex& index, rows) {
84 minRow = qMin(index.row(), minRow);
83 minRow = qMin(index.row(), minRow);
85 maxRow = qMax(index.row(), maxRow);
84 maxRow = qMax(index.row(), maxRow);
86 }
85 }
87
86
88 QList<QAbstractSeries*> result;
87 QList<QAbstractSeries*> result;
89 QColor color;
88 QColor color;
90
89
91 switch (type) {
90 switch (type) {
92
91
93 case QAbstractSeries::SeriesTypeLine:
92 case QAbstractSeries::SeriesTypeLine:
94 {
93 {
95 for (int i = 1; i < keys.count(); ++i) {
94 for (int i = 1; i < keys.count(); ++i) {
96 QLineSeries *line = new QLineSeries();
95 QLineSeries *line = new QLineSeries();
97 setupXYSeries(line, keys, i, minRow, maxRow);
96 setupXYSeries(line, keys, i, minRow, maxRow);
98 result << line;
97 result << line;
99 }
98 }
100 break;
99 break;
101 }
100 }
102 case QAbstractSeries::SeriesTypeSpline:
101 case QAbstractSeries::SeriesTypeSpline:
103 {
102 {
104 for (int i = 1; i < keys.count(); ++i) {
103 for (int i = 1; i < keys.count(); ++i) {
105 QSplineSeries *line = new QSplineSeries();
104 QSplineSeries *line = new QSplineSeries();
106 setupXYSeries(line, keys, i, minRow, maxRow);
105 setupXYSeries(line, keys, i, minRow, maxRow);
107 result << line;
106 result << line;
108 }
107 }
109 break;
108 break;
110 }
109 }
111 case QAbstractSeries::SeriesTypeScatter:
110 case QAbstractSeries::SeriesTypeScatter:
112 {
111 {
113 for (int i = 1; i < keys.count(); ++i) {
112 for (int i = 1; i < keys.count(); ++i) {
114 QScatterSeries *line = new QScatterSeries();
113 QScatterSeries *line = new QScatterSeries();
115 setupXYSeries(line, keys, i, minRow, maxRow);
114 setupXYSeries(line, keys, i, minRow, maxRow);
116 result << line;
115 result << line;
117 }
116 }
118 break;
117 break;
119 }
118 }
120 case QAbstractSeries::SeriesTypeBar:
119 case QAbstractSeries::SeriesTypeBar:
121 {
120 {
122 //TODO: fix me
121 //TODO: fix me
123 QBarSeries *bar = new QBarSeries();
122 QBarSeries *bar = new QBarSeries();
124 setupBarSeries(bar,keys,minRow,maxRow);
123 setupBarSeries(bar,keys,minRow,maxRow);
125 result << bar;
124 result << bar;
126 break;
125 break;
127 }
126 }
128 case QAbstractSeries::SeriesTypePercentBar:
127 case QAbstractSeries::SeriesTypePercentBar:
129 {
128 {
130 QPercentBarSeries *bar = new QPercentBarSeries();
129 QPercentBarSeries *bar = new QPercentBarSeries();
131 setupBarSeries(bar,keys,minRow,maxRow);
130 setupBarSeries(bar,keys,minRow,maxRow);
132 result << bar;
131 result << bar;
133 break;
132 break;
134 }
133 }
135 case QAbstractSeries::SeriesTypeStackedBar:
134 case QAbstractSeries::SeriesTypeStackedBar:
136 {
135 {
137 QStackedBarSeries *bar = new QStackedBarSeries();
136 QStackedBarSeries *bar = new QStackedBarSeries();
138 setupBarSeries(bar,keys,minRow,maxRow);
137 setupBarSeries(bar,keys,minRow,maxRow);
139 result << bar;
138 result << bar;
140 break;
139 break;
141 }
140 }
142 case QAbstractSeries::SeriesTypePie:
141 case QAbstractSeries::SeriesTypePie:
143 {
142 {
144
143
145 QPieSeries *pie = new QPieSeries();
144 QPieSeries *pie = new QPieSeries();
146 setupPieSeries(pie,keys,minRow,maxRow);
145 setupPieSeries(pie,keys,minRow,maxRow);
147 result << pie;
146 result << pie;
148 break;
147 break;
149 }
148 }
150 case QAbstractSeries::SeriesTypeArea:
149 case QAbstractSeries::SeriesTypeArea:
151 {
150 {
152 QAreaSeries *area = new QAreaSeries( new QLineSeries(), new QLineSeries());
151 QAreaSeries *area = new QAreaSeries( new QLineSeries(), new QLineSeries());
153 setupAreaSeries(area,keys,minRow,maxRow);
152 setupAreaSeries(area,keys,minRow,maxRow);
154 result << area;
153 result << area;
155 break;
154 break;
156 }
155 }
157 }
156 }
157
158 m_chart->createDefaultAxes();
158 return result;
159 return result;
159 }
160 }
160
161
161 void Engine::removeSeries(QAbstractSeries* series)
162 void Engine::removeSeries(QAbstractSeries* series)
162 {
163 {
163 m_chart->removeSeries(series);
164 m_chart->removeSeries(series);
164
165
165 foreach(const QModelIndex& index, m_seriesModelIndex.value(series)) {
166 foreach(const QModelIndex& index, m_seriesModelIndex.value(series)) {
166 m_model->setData(index, Qt::white, Qt::BackgroundRole);
167 m_model->setData(index, Qt::white, Qt::BackgroundRole);
167 }
168 }
168 }
169 }
169
170
170 void Engine::clearModels()
171 void Engine::clearModels()
171 {
172 {
172 delete m_selection;
173 delete m_selection;
173 m_selection = 0;
174 m_selection = 0;
174 delete m_model;
175 delete m_model;
175 m_model = 0;
176 m_model = 0;
176 createModels();
177 createModels();
177 }
178 }
178
179
179 bool Engine::save(const QString &filename) const
180 bool Engine::save(const QString &filename) const
180 {
181 {
181 if (filename.isEmpty())
182 if (filename.isEmpty())
182 return false;
183 return false;
183
184
184 QFile file(filename);
185 QFile file(filename);
185
186
186 if (!file.open(QIODevice::WriteOnly)) {
187 if (!file.open(QIODevice::WriteOnly)) {
187 return false;
188 return false;
188 }
189 }
189
190
190 QDataStream out(&file);
191 QDataStream out(&file);
191 out << MAGIC_NUMBER;
192 out << MAGIC_NUMBER;
192 out.setVersion(QDataStream::Qt_4_8);
193 out.setVersion(QDataStream::Qt_4_8);
193 out << m_model->rowCount();
194 out << m_model->rowCount();
194 out << m_model->columnCount();
195 out << m_model->columnCount();
195
196
196 for (int row = 0; row < m_model->rowCount(); ++row) {
197 for (int row = 0; row < m_model->rowCount(); ++row) {
197 for (int column = 0; column < m_model->columnCount(); ++column) {
198 for (int column = 0; column < m_model->columnCount(); ++column) {
198 QStandardItem *item = m_model->item(row, column);
199 QStandardItem *item = m_model->item(row, column);
199 if (item) {
200 if (item) {
200 out << row;
201 out << row;
201 out << column;
202 out << column;
202 out << item->data(Qt::EditRole).toString();
203 out << item->data(Qt::EditRole).toString();
203 }
204 }
204 }
205 }
205 }
206 }
206 return true;
207 return true;
207 }
208 }
208
209
209 bool Engine::load(const QString &filename)
210 bool Engine::load(const QString &filename)
210 {
211 {
211 clearModels();
212 clearModels();
212
213
213 if (filename.isEmpty())
214 if (filename.isEmpty())
214 return false;
215 return false;
215
216
216 QFile file(filename);
217 QFile file(filename);
217
218
218 if (!file.open(QIODevice::ReadOnly)) {
219 if (!file.open(QIODevice::ReadOnly)) {
219 return false;
220 return false;
220 }
221 }
221
222
222 QDataStream in(&file);
223 QDataStream in(&file);
223
224
224 qint32 magicNumber;
225 qint32 magicNumber;
225 in >> magicNumber;
226 in >> magicNumber;
226
227
227 if (magicNumber != MAGIC_NUMBER)
228 if (magicNumber != MAGIC_NUMBER)
228 return false;
229 return false;
229
230
230 in.setVersion(QDataStream::Qt_4_8);
231 in.setVersion(QDataStream::Qt_4_8);
231
232
232 int rowCount;
233 int rowCount;
233 in >> rowCount;
234 in >> rowCount;
234
235
235 int columnCount;
236 int columnCount;
236 in >> columnCount;
237 in >> columnCount;
237
238
238 while (!in.atEnd()) {
239 while (!in.atEnd()) {
239 int row;
240 int row;
240 int column;
241 int column;
241 QString value;
242 QString value;
242 in >> row >> column >> value;
243 in >> row >> column >> value;
243 QStandardItem *item = new QStandardItem();
244 QStandardItem *item = new QStandardItem();
244 bool ok;
245 bool ok;
245 double result = value.toDouble(&ok);
246 double result = value.toDouble(&ok);
246 if(ok)
247 if(ok)
247 item->setData(result, Qt::EditRole);
248 item->setData(result, Qt::EditRole);
248 else
249 else
249 item->setData(value, Qt::EditRole);
250 item->setData(value, Qt::EditRole);
250 m_model->setItem(row, column, item);
251 m_model->setItem(row, column, item);
251 }
252 }
252
253
253 return true;
254 return true;
254 }
255 }
255
256
256 void Engine::setupXYSeries(QXYSeries *xyseries, const QList<int>& columns, int column, int minRow, int maxRow)
257 void Engine::setupXYSeries(QXYSeries *xyseries, const QList<int>& columns, int column, int minRow, int maxRow)
257 {
258 {
258 xyseries->setModel(m_model);
259 QVXYModelMapper* mapper = new QVXYModelMapper(xyseries);
259 QXYModelMapper* mapper = new QXYModelMapper(xyseries);
260 mapper->setSeries(xyseries);
260 xyseries->setModelMapper(mapper);
261 mapper->setModel(m_model);
261 mapper->setMapX(columns.first());
262 mapper->setXColumn(columns.first());
262 mapper->setMapY(columns.at(column));
263 mapper->setYColumn(columns.at(column));
263 mapper->setOrientation(Qt::Vertical);
264 mapper->setFirstRow(minRow);
264 mapper->setFirst(minRow);
265 mapper->setRowCount(maxRow - minRow + 1);
265 mapper->setCount(maxRow - minRow + 1);
266 m_chart->addSeries(xyseries);
266 m_chart->addSeries(xyseries);
267 xyseries->setName(QString("Series %1").arg(m_chart->series().count()));
267 xyseries->setName(QString("Series %1").arg(m_chart->series().count()));
268 QObject::connect(xyseries,SIGNAL(clicked(const QPointF&)),this,SIGNAL(selected()));
268 QObject::connect(xyseries,SIGNAL(clicked(const QPointF&)),this,SIGNAL(selected()));
269 const QModelIndexList& list = m_selection->selectedIndexes();
269 const QModelIndexList& list = m_selection->selectedIndexes();
270 QModelIndexList result;
270 QModelIndexList result;
271 foreach(const QModelIndex& index, list) {
271 foreach(const QModelIndex& index, list) {
272 if (index.column() ==columns.at(column)){
272 if (index.column() ==columns.at(column)){
273 m_model->setData(index, xyseries->pen().color(), Qt::BackgroundRole);
273 m_model->setData(index, xyseries->pen().color(), Qt::BackgroundRole);
274 result << index;
274 result << index;
275 }
275 }
276 }
276 }
277 m_seriesModelIndex.insert(xyseries,result);
277 m_seriesModelIndex.insert(xyseries,result);
278 }
278 }
279
279
280 void Engine::setupBarSeries(QBarSeries *bar, const QList<int>& columns, int minRow, int maxRow)
280 void Engine::setupBarSeries(QAbstractBarSeries *bar, const QList<int>& columns, int minRow, int maxRow)
281 {
281 {
282 bar->setModel(m_model);
282 QHBarModelMapper* mapper = new QHBarModelMapper(bar);
283 QBarModelMapper* mapper = new QBarModelMapper(bar);
283 mapper->setSeries(bar);
284 bar->setModelMapper(mapper);
284 mapper->setModel(m_model);
285 mapper->setMapCategories(columns.first());
285 mapper->setFirstColumn(minRow);
286 mapper->setMapBarTop(columns.last());
286 mapper->setColumnCount(maxRow - minRow + 1);
287 mapper->setMapBarBottom(columns.at(1));
287 mapper->setFirstBarSetRow(columns.at(1));
288 mapper->setOrientation(Qt::Vertical);
288 mapper->setLastBarSetRow(columns.last());
289 mapper->setFirst(minRow);
290 mapper->setCount(maxRow - minRow + 1);
291 m_chart->addSeries(bar);
289 m_chart->addSeries(bar);
292 bar->setName(QString("Series %1").arg(m_chart->series().count()));
290 bar->setName(QString("Series %1").arg(m_chart->series().count()));
293
291
294 const QModelIndexList& list = m_selection->selectedIndexes();
292 const QModelIndexList& list = m_selection->selectedIndexes();
295 foreach(const QModelIndex& index, list) {
293 foreach(const QModelIndex& index, list) {
296 if (index.column() >= columns.at(1) && index.column()<= columns.last()) {
294 if (index.column() >= columns.at(1) && index.column()<= columns.last()) {
297 //m_model->setData(index, bar->barSets().at(index.column())->brush().color(), Qt::BackgroundRole);
295 //m_model->setData(index, bar->barSets().at(index.column())->brush().color(), Qt::BackgroundRole);
298 }
296 }
299 }
297 }
300 }
298 }
301
299
302 void Engine::setupPieSeries(QPieSeries *pie, const QList<int>& columns, int minRow, int maxRow)
300 void Engine::setupPieSeries(QPieSeries *pie, const QList<int>& columns, int minRow, int maxRow)
303 {
301 {
304 pie->setModel(m_model);
302 QVPieModelMapper* mapper = new QVPieModelMapper(pie);
305 QPieModelMapper* mapper = new QPieModelMapper(pie);
303 mapper->setSeries(pie);
306 pie->setModelMapper(mapper);
304 mapper->setModel(m_model);
307 mapper->setMapValues(columns.at(1));
305 mapper->setValuesColumn(columns.at(1));
308 mapper->setMapLabels(columns.first());
306 mapper->setLabelsColumn(columns.first());
309 mapper->setOrientation(Qt::Vertical);
307 mapper->setFirstRow(minRow);
310 mapper->setFirst(minRow);
308 mapper->setRowCount(maxRow - minRow + 1);
311 mapper->setCount(maxRow - minRow + 1);
312 m_chart->addSeries(pie);
309 m_chart->addSeries(pie);
313 pie->setName(QString("Series %1").arg(m_chart->series().count()));
310 pie->setName(QString("Series %1").arg(m_chart->series().count()));
314
311
315 const QModelIndexList& list = m_selection->selectedIndexes();
312 const QModelIndexList& list = m_selection->selectedIndexes();
316 foreach(const QModelIndex& index, list) {
313 foreach(const QModelIndex& index, list) {
317 // m_model->setData(index, bar->barSets()pen().color(), Qt::BackgroundRole);
314 // m_model->setData(index, bar->barSets()pen().color(), Qt::BackgroundRole);
318 }
315 }
319 }
316 }
320
317
321 void Engine::setupAreaSeries(QAreaSeries *series, const QList<int>& columns, int minRow, int maxRow)
318 void Engine::setupAreaSeries(QAreaSeries *series, const QList<int>& columns, int minRow, int maxRow)
322 {
319 {
323 series->lowerSeries()->setModel(m_model);
320 QVXYModelMapper* umapper = new QVXYModelMapper(series);
324 series->upperSeries()->setModel(m_model);
321 umapper->setSeries(series->upperSeries());
325 QXYModelMapper* umapper = new QXYModelMapper(series);
322 umapper->setModel(m_model);
326 umapper->setMapX(columns.first());
323 umapper->setXColumn(columns.first());
327 umapper->setMapY(columns.at(1));
324 umapper->setYColumn(columns.at(1));
328 umapper->setOrientation(Qt::Vertical);
325 umapper->setFirstRow(minRow);
329 umapper->setFirst(minRow);
326 umapper->setRowCount(maxRow - minRow + 1);
330 umapper->setCount(maxRow - minRow + 1);
327
331 QXYModelMapper* lmapper = new QXYModelMapper(series);
328 QVXYModelMapper* lmapper = new QVXYModelMapper(series);
332 lmapper->setMapX(columns.first());
329 lmapper->setSeries(series->lowerSeries());
333 lmapper->setMapY(columns.at(2));
330 lmapper->setModel(m_model);
334 lmapper->setOrientation(Qt::Vertical);
331 lmapper->setXColumn(columns.first());
335 lmapper->setFirst(minRow);
332 lmapper->setYColumn(columns.at(2));
336 lmapper->setCount(maxRow - minRow + 1);
333 lmapper->setFirstRow(minRow);
337 series->upperSeries()->setModelMapper(umapper);
334 lmapper->setRowCount(maxRow - minRow + 1);
338 series->lowerSeries()->setModelMapper(lmapper);
335
339 m_chart->addSeries(series);
336 m_chart->addSeries(series);
340 series->setName(QString("Series %1").arg(m_chart->series().count()));
337 series->setName(QString("Series %1").arg(m_chart->series().count()));
341
338
342 const QModelIndexList& list = m_selection->selectedIndexes();
339 const QModelIndexList& list = m_selection->selectedIndexes();
343 foreach(const QModelIndex& index, list) {
340 foreach(const QModelIndex& index, list) {
344 //if (index.column() ==columns.at(column))
341 //if (index.column() ==columns.at(column))
345 // m_model->setData(index, xyseries->pen().color(), Qt::BackgroundRole);
342 // m_model->setData(index, xyseries->pen().color(), Qt::BackgroundRole);
346 }
343 }
347 }
344 }
@@ -1,76 +1,76
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #ifndef ENGINE_H_
21 #ifndef ENGINE_H_
22 #define ENGINE_H_
22 #define ENGINE_H_
23
23
24 #include <QObject>
24 #include <QObject>
25 #include <QAbstractSeries>
25 #include <QAbstractSeries>
26 #include <QModelIndex>
26 #include <QModelIndex>
27
27
28 class QStandardItemModel;
28 class QStandardItemModel;
29 class QItemSelectionModel;
29 class QItemSelectionModel;
30
30
31
31
32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33 class QChart;
33 class QChart;
34 class QXYSeries;
34 class QXYSeries;
35 class QBarSeries;
35 class QAbstractBarSeries;
36 class QPieSeries;
36 class QPieSeries;
37 class QAreaSeries;
37 class QAreaSeries;
38 QTCOMMERCIALCHART_END_NAMESPACE
38 QTCOMMERCIALCHART_END_NAMESPACE
39
39
40 QTCOMMERCIALCHART_USE_NAMESPACE
40 QTCOMMERCIALCHART_USE_NAMESPACE
41
41
42 class Engine : public QObject
42 class Engine : public QObject
43 {
43 {
44 Q_OBJECT
44 Q_OBJECT
45 public:
45 public:
46 explicit Engine(QObject *parent = 0);
46 explicit Engine(QObject *parent = 0);
47 ~Engine();
47 ~Engine();
48
48
49 int modelCount() { return m_count; }
49 int modelCount() { return m_count; }
50 QStandardItemModel *model() const { return m_model; }
50 QStandardItemModel *model() const { return m_model; }
51 QItemSelectionModel *selectionModel() const { return m_selection; }
51 QItemSelectionModel *selectionModel() const { return m_selection; }
52 QChart* chart() const { return m_chart; }
52 QChart* chart() const { return m_chart; }
53 void clearModels();
53 void clearModels();
54 QList<QAbstractSeries*> addSeries(QAbstractSeries::SeriesType type);
54 QList<QAbstractSeries*> addSeries(QAbstractSeries::SeriesType type);
55 void removeSeries(QAbstractSeries* series);
55 void removeSeries(QAbstractSeries* series);
56 bool save(const QString &filename) const;
56 bool save(const QString &filename) const;
57 bool load(const QString &filename);
57 bool load(const QString &filename);
58 signals:
58 signals:
59 void selected();
59 void selected();
60
60
61 private:
61 private:
62 void createModels();
62 void createModels();
63 void setupXYSeries(QXYSeries *xyseries, const QList<int>& columns, int column, int minRow, int maxRow);
63 void setupXYSeries(QXYSeries *xyseries, const QList<int>& columns, int column, int minRow, int maxRow);
64 void setupBarSeries(QBarSeries *series, const QList<int>& columns, int minRow, int maxRow);
64 void setupBarSeries(QAbstractBarSeries *series, const QList<int>& columns, int minRow, int maxRow);
65 void setupPieSeries(QPieSeries *pie, const QList<int>& columns, int minRow, int maxRow);
65 void setupPieSeries(QPieSeries *pie, const QList<int>& columns, int minRow, int maxRow);
66 void setupAreaSeries(QAreaSeries *series, const QList<int>& columns, int minRow, int maxRow);
66 void setupAreaSeries(QAreaSeries *series, const QList<int>& columns, int minRow, int maxRow);
67
67
68 private:
68 private:
69 int m_count;
69 int m_count;
70 QChart *m_chart;
70 QChart *m_chart;
71 QStandardItemModel *m_model;
71 QStandardItemModel *m_model;
72 QItemSelectionModel *m_selection;
72 QItemSelectionModel *m_selection;
73 QMap<QAbstractSeries*, QList<QModelIndex> > m_seriesModelIndex;
73 QMap<QAbstractSeries*, QList<QModelIndex> > m_seriesModelIndex;
74 };
74 };
75
75
76 #endif /* ENGINE_H_ */
76 #endif /* ENGINE_H_ */
@@ -1,14 +1,15
1 !include( ../config.pri ) {
1 !include( ../config.pri ) {
2 error( "Couldn't find the config.pri file!" )
2 error( "Couldn't find the config.pri file!" )
3 }
3 }
4
4
5 TEMPLATE = subdirs
5 TEMPLATE = subdirs
6 SUBDIRS += \
6 SUBDIRS += \
7 auto \
7 auto \
8 chartwidgettest \
8 chartwidgettest \
9 qmlchartproperties
9 qmlchartproperties \
10 chartdesigner
10
11
11 !linux-arm*: {
12 !linux-arm*: {
12 SUBDIRS += \
13 SUBDIRS += \
13 wavechart
14 wavechart
14 }
15 }
General Comments 0
You need to be logged in to leave comments. Login now