11#pragma once
22
3- #include < QtNodes/NodeDelegateModel>
43#include < QTimer>
4+ #include < QtCore/QElapsedTimer>
55#include < QtCore/QObject>
6- #include < QtWidgets/QLabel>
76#include < QtCore/QRandomGenerator64>
7+ #include < QtNodes/NodeDelegateModel>
8+ #include < QtWidgets/QLabel>
89
9- #include " MathOperationDataModel.hpp"
1010#include " DecimalData.hpp"
11+ #include " MathOperationDataModel.hpp"
1112
12- // / The model generates a random value in a long processing schema,
13- // / as it should demonstrate the usage of the NodeProcessingStatus.
13+ // / The model generates a random value in a long processing schema, as it should demonstrate
14+ // / the usage of the NodeProcessingStatus and the ProgressValue functionality .
1415// / The random number is generate in the [n1, n2] interval.
15- class RandomNumberModel : public MathOperationDataModel
16+ class LongProcessingRandomNumber : public MathOperationDataModel
1617{
1718public:
18- RandomNumberModel ()
19- : _timer(new QTimer(this ))
19+ LongProcessingRandomNumber ()
2020 {
2121 this ->setNodeProcessingStatus (QtNodes::NodeProcessingStatus::Empty);
2222
2323 QObject::connect (this , &NodeDelegateModel::computingStarted, this , [this ]() {
24- if (_number1.lock () && _number2.lock ()) {
25- this ->setNodeProcessingStatus (QtNodes::NodeProcessingStatus::Processing);
24+ this ->setNodeProcessingStatus (QtNodes::NodeProcessingStatus::Processing);
25+
26+ setProgressValue (QString{" 0%" });
27+ emit requestNodeUpdate ();
28+
29+ _elapsedTimer.start ();
30+
31+ if (!_progressTimer) {
32+ _progressTimer = new QTimer (this );
33+ connect (_progressTimer, &QTimer::timeout, this , [this ]() {
34+ qint64 elapsed = _elapsedTimer.elapsed ();
35+ int percent = static_cast <int >((double (elapsed) / _totalDurationMs) * 100.0 );
36+
37+ if (percent > 100 )
38+ percent = 100 ;
39+
40+ setProgressValue (QString::number (percent) + " %" );
41+ emit requestNodeUpdate ();
42+ });
2643 }
44+
45+ _progressTimer->start (_progressUpdateIntervalMs);
46+
2747 emit requestNodeUpdate ();
2848 });
49+
2950 QObject::connect (this , &NodeDelegateModel::computingFinished, this , [this ]() {
51+ if (_progressTimer) {
52+ _progressTimer->stop ();
53+ }
54+
55+ setProgressValue (QString ());
56+
3057 this ->setNodeProcessingStatus (QtNodes::NodeProcessingStatus::Updated);
58+
3159 emit requestNodeUpdate ();
3260 });
3361 }
34- virtual ~RandomNumberModel () {}
62+
63+ virtual ~LongProcessingRandomNumber ()
64+ {
65+ if (_progressTimer) {
66+ _progressTimer->stop ();
67+ delete _progressTimer;
68+ }
69+ }
3570
3671public:
3772 QString caption () const override { return QStringLiteral (" Random Number" ); }
@@ -41,47 +76,50 @@ class RandomNumberModel : public MathOperationDataModel
4176private:
4277 void compute () override
4378 {
44- // Stop any previous computation
45- _timer->stop ();
46- _timer->disconnect ();
79+ auto n1 = _number1.lock ();
80+ auto n2 = _number2.lock ();
81+
82+ if (!n1 || !n2) {
83+ return ;
84+ }
4785
4886 Q_EMIT computingStarted ();
4987 PortIndex const outPortIndex = 0 ;
5088
51- auto n1 = _number1.lock ();
52- auto n2 = _number2.lock ();
89+ QTimer::singleShot (_totalDurationMs, this , [this , n1, n2]() {
90+ if (n1 && n2) {
91+ double a = n1->number ();
92+ double b = n2->number ();
93+
94+ if (a > b) {
95+ setNodeProcessingStatus (QtNodes::NodeProcessingStatus::Failed);
5396
54- _secondsRemaining = 3 ;
55- _timer->start (1000 );
56- connect (_timer, &QTimer::timeout, this , [this , n1, n2, outPortIndex]() {
57- if (--_secondsRemaining <= 0 ) {
58- _timer->stop ();
59- if (n1 && n2) {
60- double a = n1->number ();
61- double b = n2->number ();
62-
63- if (a > b) {
64- setNodeProcessingStatus (QtNodes::NodeProcessingStatus::Failed);
65- emit requestNodeUpdate ();
66- return ;
97+ if (_progressTimer) {
98+ _progressTimer->stop ();
6799 }
68100
69- double upper = std::nextafter (b, std::numeric_limits<double >::max ());
70- double randomValue = QRandomGenerator::global ()->generateDouble () * (upper - a)
71- + a;
101+ setProgressValue (QString ());
72102
73- _result = std::make_shared<DecimalData>(randomValue);
74- Q_EMIT computingFinished ();
75- } else {
76- _result.reset ();
103+ emit requestNodeUpdate ();
104+ return ;
77105 }
78106
79- Q_EMIT dataUpdated (outPortIndex);
107+ double upper = std::nextafter (b, std::numeric_limits<double >::max ());
108+ double randomValue = QRandomGenerator::global ()->generateDouble () * (upper - a) + a;
109+
110+ _result = std::make_shared<DecimalData>(randomValue);
111+ emit computingFinished ();
112+ } else {
113+ _result.reset ();
80114 }
115+
116+ Q_EMIT dataUpdated (outPortIndex);
81117 });
82118 }
83119
84- private:
85- QTimer *_timer;
86- int _secondsRemaining = 0 ;
120+ QTimer *_progressTimer = nullptr ;
121+ QElapsedTimer _elapsedTimer;
122+
123+ const int _totalDurationMs = 3000 ;
124+ const int _progressUpdateIntervalMs = 50 ;
87125};
0 commit comments