summaryrefslogtreecommitdiffstats
path: root/tests/manual/qmlbenchmark2d/datasource.cpp
blob: d03b9179aa55d8674ded9cbf7a13d70aebe3ab97 (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
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include "datasource.h"

#include <QQuickItem>
#include <QQuickView>
#include <QRandomGenerator>
#include <QtGraphs/QBarSeries>
#include <QtGraphs/QBarSet>
#include <QtGraphs/QXYSeries>
#include <QtMath>

DataSource::DataSource(QObject *parent)
    : QObject(parent)
{}

void DataSource::reset(int dataScale)
{
    m_dataScale = dataScale;
    m_testIndex = 0;
    m_timer = QDateTime::currentMSecsSinceEpoch();

    generateData();
}

void DataSource::update(QObject *series)
{
    if (QDateTime::currentMSecsSinceEpoch() - m_timer >= 4000 && m_testIndex + 1 < m_data.size()) {
        m_timer = QDateTime::currentMSecsSinceEpoch();
        m_testIndex++;
    }

    if (series) {
        m_index++;
        if (m_index > m_data.at(m_testIndex).count() - 1)
            m_index = 0;

        QList<QPointF> points = m_data.at(m_testIndex).at(m_index);
        if (auto xySeries = qobject_cast<QXYSeries *>(series)) {
            xySeries->replace(points);
            //xySeries->update(); // Uncomment for 6.8 release test
        } else if (auto barSeries = qobject_cast<QBarSeries *>(series)) {
            if (barSeries->count() <= 0) {
                auto set = new QBarSet("Set", barSeries);
                barSeries->append(set);
            }

            QBarSet *set = barSeries->at(0);
            for (qsizetype i = 0; i < points.size(); i++) {
                if (set->count() <= i)
                    set->append(points[i].y());
                else
                    set->replace(i, points[i].y());
            }
        }
    }
}

void DataSource::generateData()
{
    int type = 0;
    int testCount = 7;
    int rowCount = 5;
    // Remove previous data
    m_data.clear();

    // Append the new data depending on the type
    for (int k(0); k < testCount; k++) {
        int colCount = 0;
        if (k > 0)
            colCount = qPow(2, k - 1) * m_dataScale;

        QList<QList<QPointF>> test;
        for (int i(0); i < rowCount; i++) {
            QList<QPointF> points;
            points.reserve(colCount);
            for (int j(0); j < colCount; j++) {
                qreal x(0);
                qreal y(0);
                switch (type) {
                case 0:
                    // data with sin + random component
                    y = qSin(M_PI / 50 * j) + 5 + QRandomGenerator::global()->generateDouble();
                    x = j;
                    break;
                case 1:
                    // linear data
                    x = j;
                    y = (qreal) i / 10;
                    break;
                default:
                    // unknown, do nothing
                    break;
                }
                points.append(QPointF(x, y));
            }
            test.append(points);
        }
        m_data.append(test);
    }
}