|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <QtNodes/NodeDelegateModel> |
| 4 | +#include <QTimer> |
| 5 | +#include <QtCore/QObject> |
| 6 | +#include <QtWidgets/QLabel> |
| 7 | +#include <QtCore/QRandomGenerator64> |
| 8 | + |
| 9 | +#include "MathOperationDataModel.hpp" |
| 10 | +#include "DecimalData.hpp" |
| 11 | + |
| 12 | +/// The model generates a random value in a long processing schema, |
| 13 | +/// as it should demonstrate the usage of the NodeProcessingStatus. |
| 14 | +/// The random number is generate in the [n1, n2] interval. |
| 15 | +class RandomNumberModel : public MathOperationDataModel |
| 16 | +{ |
| 17 | +public: |
| 18 | + RandomNumberModel() { |
| 19 | + setFrozenMenu(true); |
| 20 | + |
| 21 | + this->setNodeProcessingStatus(QtNodes::NodeProcessingStatus::Empty); |
| 22 | + |
| 23 | + |
| 24 | + QObject::connect(this, &NodeDelegateModel::computingStarted, this, [this]() { |
| 25 | + if (_number1.lock() && _number2.lock()) { |
| 26 | + this->setNodeProcessingStatus( |
| 27 | + QtNodes::NodeProcessingStatus::Processing); |
| 28 | + } |
| 29 | + |
| 30 | + emit requestNodeUpdate(); |
| 31 | + }); |
| 32 | + QObject::connect(this, &NodeDelegateModel::computingFinished, this, [this]() { |
| 33 | + this->setNodeProcessingStatus( |
| 34 | + QtNodes::NodeProcessingStatus::Updated); |
| 35 | + |
| 36 | + emit requestNodeUpdate(); |
| 37 | + }); |
| 38 | + } |
| 39 | + virtual ~RandomNumberModel() {} |
| 40 | + |
| 41 | +public: |
| 42 | + QString caption() const override { return QStringLiteral("Random Number"); } |
| 43 | + |
| 44 | + QString name() const override { return QStringLiteral("Random Number"); } |
| 45 | + |
| 46 | +private: |
| 47 | + void compute() override |
| 48 | + { |
| 49 | + if (frozen()) |
| 50 | + return; |
| 51 | + |
| 52 | + Q_EMIT computingStarted(); |
| 53 | + PortIndex const outPortIndex = 0; |
| 54 | + |
| 55 | + auto n1 = _number1.lock(); |
| 56 | + auto n2 = _number2.lock(); |
| 57 | + |
| 58 | + QTimer *timer = new QTimer(this); |
| 59 | + timer->start(1000); |
| 60 | + int secondsRemaining = 3; |
| 61 | + connect(timer, &QTimer::timeout, this, [=]() mutable { |
| 62 | + if (--secondsRemaining <= 0) { |
| 63 | + timer->stop(); |
| 64 | + if (n1 && n2) { |
| 65 | + double a = n1->number(); |
| 66 | + double b = n2->number(); |
| 67 | + |
| 68 | + if (a > b) { |
| 69 | + setNodeProcessingStatus(QtNodes::NodeProcessingStatus::Failed); |
| 70 | + |
| 71 | + emit requestNodeUpdate(); |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + double upper = std::nextafter(b, std::numeric_limits<double>::max()); |
| 76 | + double randomValue = QRandomGenerator::global()->generateDouble() * (upper - a) + a; |
| 77 | + |
| 78 | + _result = std::make_shared<DecimalData>(randomValue); |
| 79 | + Q_EMIT computingFinished(); |
| 80 | + } else { |
| 81 | + _result.reset(); |
| 82 | + } |
| 83 | + |
| 84 | + Q_EMIT dataUpdated(outPortIndex); |
| 85 | + } |
| 86 | + }); |
| 87 | + } |
| 88 | +}; |
0 commit comments