|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include "DecimalData.hpp" |
| 4 | +#include "MathOperationDataModel.hpp" |
| 5 | + |
| 6 | +#include <QtNodes/NodeDelegateModel> |
| 7 | + |
| 8 | +#include <QtCore/QObject> |
| 9 | +#include <QtWidgets/QLabel> |
| 10 | + |
| 11 | +/// The model dictates the number of inputs and outputs for the Node. |
| 12 | +/// In this example it has no logic. |
| 13 | +class DivisionModel : public MathOperationDataModel |
| 14 | +{ |
| 15 | +public: |
| 16 | + virtual ~DivisionModel() {} |
| 17 | + |
| 18 | +public: |
| 19 | + QString caption() const override { return QStringLiteral("Division"); } |
| 20 | + |
| 21 | + bool portCaptionVisible(PortType portType, PortIndex portIndex) const override |
| 22 | + { |
| 23 | + Q_UNUSED(portType); |
| 24 | + Q_UNUSED(portIndex); |
| 25 | + return true; |
| 26 | + } |
| 27 | + |
| 28 | + QString portCaption(PortType portType, PortIndex portIndex) const override |
| 29 | + { |
| 30 | + switch (portType) { |
| 31 | + case PortType::In: |
| 32 | + if (portIndex == 0) |
| 33 | + return QStringLiteral("Dividend"); |
| 34 | + else if (portIndex == 1) |
| 35 | + return QStringLiteral("Divisor"); |
| 36 | + |
| 37 | + break; |
| 38 | + |
| 39 | + case PortType::Out: |
| 40 | + return QStringLiteral("Result"); |
| 41 | + |
| 42 | + default: |
| 43 | + break; |
| 44 | + } |
| 45 | + return QString(); |
| 46 | + } |
| 47 | + |
| 48 | + QString name() const override { return QStringLiteral("Division"); } |
| 49 | + |
| 50 | +private: |
| 51 | + void compute() override |
| 52 | + { |
| 53 | + PortIndex const outPortIndex = 0; |
| 54 | + |
| 55 | + auto n1 = _number1.lock(); |
| 56 | + auto n2 = _number2.lock(); |
| 57 | + |
| 58 | + if (n2 && (n2->number() == 0.0)) { |
| 59 | + //modelValidationState = NodeValidationState::Error; |
| 60 | + //modelValidationError = QStringLiteral("Division by zero error"); |
| 61 | + _result.reset(); |
| 62 | + } else if (n1 && n2) { |
| 63 | + //modelValidationState = NodeValidationState::Valid; |
| 64 | + //modelValidationError = QString(); |
| 65 | + _result = std::make_shared<DecimalData>(n1->number() / n2->number()); |
| 66 | + } else { |
| 67 | + //modelValidationState = NodeValidationState::Warning; |
| 68 | + //modelValidationError = QStringLiteral("Missing or incorrect inputs"); |
| 69 | + _result.reset(); |
| 70 | + } |
| 71 | + |
| 72 | + Q_EMIT dataUpdated(outPortIndex); |
| 73 | + } |
| 74 | +}; |
0 commit comments