forked from paceholder/nodeeditor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongProcessingRandomNumber.hpp
More file actions
125 lines (94 loc) · 3.64 KB
/
LongProcessingRandomNumber.hpp
File metadata and controls
125 lines (94 loc) · 3.64 KB
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
#pragma once
#include <QTimer>
#include <QtCore/QElapsedTimer>
#include <QtCore/QObject>
#include <QtCore/QRandomGenerator64>
#include <QtNodes/NodeDelegateModel>
#include <QtWidgets/QLabel>
#include "DecimalData.hpp"
#include "MathOperationDataModel.hpp"
/// The model generates a random value in a long processing schema, as it should demonstrate
/// the usage of the NodeProcessingStatus and the ProgressValue functionality.
/// The random number is generate in the [n1, n2] interval.
class LongProcessingRandomNumber : public MathOperationDataModel
{
public:
LongProcessingRandomNumber()
{
this->setNodeProcessingStatus(QtNodes::NodeProcessingStatus::Empty);
QObject::connect(this, &NodeDelegateModel::computingStarted, this, [this]() {
this->setNodeProcessingStatus(QtNodes::NodeProcessingStatus::Processing);
setProgressValue(QString{"0%"});
emit requestNodeUpdate();
_elapsedTimer.start();
if (!_progressTimer) {
_progressTimer = new QTimer(this);
connect(_progressTimer, &QTimer::timeout, this, [this]() {
qint64 elapsed = _elapsedTimer.elapsed();
int percent = static_cast<int>((double(elapsed) / _totalDurationMs) * 100.0);
if (percent > 100)
percent = 100;
setProgressValue(QString::number(percent) + "%");
emit requestNodeUpdate();
});
}
_progressTimer->start(_progressUpdateIntervalMs);
emit requestNodeUpdate();
});
QObject::connect(this, &NodeDelegateModel::computingFinished, this, [this]() {
if (_progressTimer) {
_progressTimer->stop();
}
setProgressValue(QString());
this->setNodeProcessingStatus(QtNodes::NodeProcessingStatus::Updated);
emit requestNodeUpdate();
});
}
virtual ~LongProcessingRandomNumber()
{
if (_progressTimer) {
_progressTimer->stop();
delete _progressTimer;
}
}
public:
QString caption() const override { return QStringLiteral("Random Number"); }
QString name() const override { return QStringLiteral("Random Number"); }
private:
void compute() override
{
auto n1 = _number1.lock();
auto n2 = _number2.lock();
if (!n1 || !n2) {
return;
}
Q_EMIT computingStarted();
PortIndex const outPortIndex = 0;
QTimer::singleShot(_totalDurationMs, this, [this, n1, n2]() {
if (n1 && n2) {
double a = n1->number();
double b = n2->number();
if (a > b) {
setNodeProcessingStatus(QtNodes::NodeProcessingStatus::Failed);
if (_progressTimer) {
_progressTimer->stop();
}
setProgressValue(QString());
emit requestNodeUpdate();
return;
}
double upper = std::nextafter(b, std::numeric_limits<double>::max());
double randomValue = QRandomGenerator::global()->generateDouble() * (upper - a) + a;
_result = std::make_shared<DecimalData>(randomValue);
emit computingFinished();
} else {
_result.reset();
}
Q_EMIT dataUpdated(outPortIndex);
});
}
QTimer *_progressTimer = nullptr;
QElapsedTimer _elapsedTimer;
const int _totalDurationMs = 3000;
const int _progressUpdateIntervalMs = 50;
};