blob: 3da8febbe0a7f69bb59bea943e33d5ca086edecb (
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
|
// Copyright (C) 2025 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include "randomModel.h"
#include <qabstractitemmodel.h>
#include <qnamespace.h>
#include <QRandomGenerator>
RandomModel::RandomModel(QObject *parent)
: QAbstractTableModel(parent)
{
m_columnCount = 2;
m_rowCount = 0;
}
int RandomModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return m_data[m_currentCache].count();
}
void RandomModel::setRowCount(int rowCount)
{
if (m_rowCount == rowCount)
return;
m_rowCount = rowCount;
emit rowCountChanged(rowCount);
generateCaches();
}
int RandomModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return m_columnCount;
}
QVariant RandomModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role != Qt::DisplayRole)
return QVariant();
if (orientation != Qt::Horizontal)
return section == 0 ? "X" : "Y";
return QVariant();
}
QVariant RandomModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
auto data = m_data[m_currentCache];
switch (role) {
case Qt::DisplayRole:
return data[index.row()].at(index.column());
case XRole:
return data[index.row()].at(0);
case YRole:
return data[index.row()].at(1);
default:
return QVariant();
}
}
bool RandomModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (index.isValid() && role == Qt::EditRole) {
for (auto data : m_data)
data[index.row()].replace(index.column(), value.toReal());
Q_EMIT dataChanged(index, index);
return true;
}
return false;
}
Qt::ItemFlags RandomModel::flags(const QModelIndex &index) const
{
return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
}
QHash<int, QByteArray> RandomModel::roleNames() const
{
QHash<int, QByteArray> roles = QAbstractItemModel::roleNames();
roles[Roles::XRole] = "xData";
roles[Roles::YRole] = "yData";
return roles;
}
void RandomModel::nextCache()
{
beginResetModel();
m_currentCache = (m_currentCache + 1) % 60;
endResetModel();
}
void RandomModel::generateCaches()
{
beginResetModel();
m_data.clear();
m_data.resize(m_cacheCount);
for (int i = 0; i < m_cacheCount; i++) {
QList<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);
auto dataList = QList<qreal>{qreal(j), y};
array.append(dataList);
}
}
endResetModel();
}
void RandomModel::clearData()
{
for (auto data : m_data) {
for (auto l : data)
l.clear();
data.clear();
}
m_data.clear();
}
|