blob: 3e7b27345411aa5008cd997cf77eee2442783980 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
// Copyright (C) 2025 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include "randomGenerator.h"
#include <iterator>
RandomGenerator::RandomGenerator(QObject *parent)
: QObject(parent)
, m_rowCount(0)
{}
int RandomGenerator::rowCount() const
{
return m_rowCount;
}
void RandomGenerator::setRowCount(int rowCount)
{
if (m_rowCount == rowCount)
return;
m_rowCount = rowCount;
emit rowCountChanged(rowCount);
generateCaches();
}
int RandomGenerator::activeSeriesIndex() const
{
return m_activeSeriesIndex;
}
void RandomGenerator::setActiveSeriesIndex(int index)
{
if (m_activeSeriesIndex == index)
return;
m_activeSeriesIndex = index;
emit activeSeriesIndexChanged(index);
nextCache();
}
void RandomGenerator::nextCache()
{
m_currentCache = (m_currentCache + 1) % 60;
if (m_activeSeriesIndex == 0)
updateSurface3D();
else if (m_activeSeriesIndex < 3)
updateScatter3D(m_activeSeriesIndex);
else
update2DGraph(m_activeSeriesIndex);
}
void RandomGenerator::generateCaches()
{
m_data.clear();
m_data.resize(m_cacheCount);
for (int i = 0; i < m_cacheCount; i++) {
QList<qreal> &array = m_data[i];
array.reserve(m_rowCount);
for (int j = 0; j < m_rowCount; j++) {
qreal y = float(QRandomGenerator::global()->bounded(100)) / (100.0);
array.append(y);
}
}
}
void RandomGenerator::clearCaches()
{
for (auto data : m_data) {
data.clear();
}
m_data.clear();
}
void RandomGenerator::updateSurface3D()
{
if (!m_surface3D)
return;
QSurfaceDataArray dataArray;
dataArray.resize(m_rowCount);
for (int i = 0; i < m_rowCount; i++) {
//append two points
QVector3D pos = QVector3D(i, m_data[m_currentCache][i], 0);
dataArray[i].append(QSurfaceDataItem(pos));
}
m_surface3D->dataProxy()->resetArray(dataArray);
}
void RandomGenerator::updateScatter3D(int index)
{
QScatter3DSeries *series = nullptr;
if (index == 1)
series = m_scatter3D;
else
series = m_spline3D;
if (!series)
return;
QScatterDataArray dataArray;
dataArray.reserve(m_rowCount);
for (int i = 0; i < m_rowCount; i++) {
QVector3D pos = QVector3D(i, m_data[m_currentCache][i], 0);
dataArray.append(QScatterDataItem(pos));
}
series->dataProxy()->resetArray(dataArray);
}
void RandomGenerator::update2DGraph(int index)
{
QXYSeries *series = nullptr;
if (index == 3)
series = m_line2D;
else if (index == 4)
series = m_spline2D;
else
series = m_scatter2D;
if (!series)
return;
series->clear();
for (int i = 0; i < m_rowCount; i++) {
QPointF point = QPointF(i, m_data[m_currentCache][i]);
series->append(point);
}
}
|