@@ -1,91 +1,94 | |||||
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 "datasource.h" |
|
21 | #include "datasource.h" | |
22 | #include <QXYSeries> |
|
22 | #include <QXYSeries> | |
23 | #include <QAreaSeries> |
|
23 | #include <QAreaSeries> | |
24 | #include <QDeclarativeView> |
|
24 | #include <QDeclarativeView> | |
25 | #include <QGLWidget> |
|
25 | #include <QGLWidget> | |
26 | #include <QDebug> |
|
26 | #include <QDebug> | |
27 | #include <cmath> |
|
27 | #include <cmath> | |
28 |
|
28 | |||
29 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
29 | QTCOMMERCIALCHART_USE_NAMESPACE | |
30 |
|
30 | |||
|
31 | Q_DECLARE_METATYPE(QAbstractSeries *) | |||
|
32 | ||||
31 | DataSource::DataSource(QDeclarativeView *appViewer, QObject *parent) : |
|
33 | DataSource::DataSource(QDeclarativeView *appViewer, QObject *parent) : | |
32 | QObject(parent), |
|
34 | QObject(parent), | |
33 | m_appViewer(appViewer), |
|
35 | m_appViewer(appViewer), | |
34 | m_index(-1) |
|
36 | m_index(-1) | |
35 | { |
|
37 | { | |
36 | // generate |
|
38 | qRegisterMetaType<QAbstractSeries*>(); | |
|
39 | ||||
37 | generateData(0, 5, 1024); |
|
40 | generateData(0, 5, 1024); | |
38 | } |
|
41 | } | |
39 |
|
42 | |||
40 | void DataSource::update(QAbstractSeries *series) |
|
43 | void DataSource::update(QAbstractSeries *series) | |
41 | { |
|
44 | { | |
42 | QXYSeries *xySeries = qobject_cast<QXYSeries *>(series); |
|
45 | QXYSeries *xySeries = qobject_cast<QXYSeries *>(series); | |
43 | Q_ASSERT(xySeries); |
|
46 | Q_ASSERT(xySeries); | |
44 |
|
47 | |||
45 | m_index++; |
|
48 | m_index++; | |
46 | if (m_index > m_data.count() - 1) |
|
49 | if (m_index > m_data.count() - 1) | |
47 | m_index = 0; |
|
50 | m_index = 0; | |
48 |
|
51 | |||
49 | QList<QPointF> points = m_data.at(m_index); |
|
52 | QList<QPointF> points = m_data.at(m_index); | |
50 | // Use replace instead of clear + append, it's optimized for performance |
|
53 | // Use replace instead of clear + append, it's optimized for performance | |
51 | xySeries->replace(points); |
|
54 | xySeries->replace(points); | |
52 | } |
|
55 | } | |
53 |
|
56 | |||
54 | void DataSource::generateData(int type, int rowCount, int colCount) |
|
57 | void DataSource::generateData(int type, int rowCount, int colCount) | |
55 | { |
|
58 | { | |
56 | // Remove previous data |
|
59 | // Remove previous data | |
57 | foreach (QList<QPointF> row, m_data) |
|
60 | foreach (QList<QPointF> row, m_data) | |
58 | row.clear(); |
|
61 | row.clear(); | |
59 | m_data.clear(); |
|
62 | m_data.clear(); | |
60 |
|
63 | |||
61 | // Append the new data depending on the type |
|
64 | // Append the new data depending on the type | |
62 | for (int i(0); i < rowCount; i++) { |
|
65 | for (int i(0); i < rowCount; i++) { | |
63 | QList<QPointF> points; |
|
66 | QList<QPointF> points; | |
64 | for (int j(0); j < colCount; j++) { |
|
67 | for (int j(0); j < colCount; j++) { | |
65 | qreal x(0); |
|
68 | qreal x(0); | |
66 | qreal y(0); |
|
69 | qreal y(0); | |
67 | switch (type) { |
|
70 | switch (type) { | |
68 | case 0: |
|
71 | case 0: | |
69 | // data with sin + random component |
|
72 | // data with sin + random component | |
70 | y = sin(3.14159265358979 / 50 * j) + 0.5 + (qreal) rand() / (qreal) RAND_MAX; |
|
73 | y = sin(3.14159265358979 / 50 * j) + 0.5 + (qreal) rand() / (qreal) RAND_MAX; | |
71 | x = j; |
|
74 | x = j; | |
72 | break; |
|
75 | break; | |
73 | case 1: |
|
76 | case 1: | |
74 | // linear data |
|
77 | // linear data | |
75 | x = j; |
|
78 | x = j; | |
76 | y = (qreal) i / 10; |
|
79 | y = (qreal) i / 10; | |
77 | break; |
|
80 | break; | |
78 | default: |
|
81 | default: | |
79 | // unknown, do nothing |
|
82 | // unknown, do nothing | |
80 | break; |
|
83 | break; | |
81 | } |
|
84 | } | |
82 | points.append(QPointF(x, y)); |
|
85 | points.append(QPointF(x, y)); | |
83 | } |
|
86 | } | |
84 | m_data.append(points); |
|
87 | m_data.append(points); | |
85 | } |
|
88 | } | |
86 | } |
|
89 | } | |
87 |
|
90 | |||
88 | void DataSource::setAntialiasing(bool enabled) |
|
91 | void DataSource::setAntialiasing(bool enabled) | |
89 | { |
|
92 | { | |
90 | m_appViewer->setRenderHint(QPainter::Antialiasing, enabled); |
|
93 | m_appViewer->setRenderHint(QPainter::Antialiasing, enabled); | |
91 | } |
|
94 | } |
General Comments 0
You need to be logged in to leave comments.
Login now