-
Notifications
You must be signed in to change notification settings - Fork 769
/
DivisionModel.hpp
116 lines (94 loc) · 2.16 KB
/
DivisionModel.hpp
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
#pragma once
#include <QtCore/QObject>
#include <QtWidgets/QLabel>
#include <nodes/NodeDataModel>
#include "MathOperationDataModel.hpp"
#include "NumberData.hpp"
/// The model dictates the number of inputs and outputs for the Node.
/// In this example it has no logic.
class DivisionModel : public MathOperationDataModel
{
private:
bool isModelValid = true;
QString modelValidationError = QString("");
public:
virtual
~DivisionModel() {}
public:
QString
caption() const override
{ return QString("Division"); }
virtual bool
portCaptionVisible(PortType portType, PortIndex portIndex) const override
{ return true; }
virtual QString
portCaption(PortType portType, PortIndex portIndex) const override
{
switch (portType)
{
case PortType::In:
if (portIndex == 0)
return QString("Dividend");
else if (portIndex == 1)
return QString("Divisor");
break;
case PortType::Out:
return QString("Result");
default:
break;
}
return QString("");
}
QString
name() const override
{ return QString("Division"); }
std::unique_ptr<NodeDataModel>
clone() const override
{ return std::make_unique<DivisionModel>(); }
virtual
bool
isValid() const { return isModelValid; }
virtual
QString
errorMessage() const { return modelValidationError; }
public:
void
save(Properties &p) const override
{
p.put("model_name", DivisionModel::name());
}
private:
void
setValidationState(bool isValid, const QString &msg)
{
isModelValid = isValid;
modelValidationError = msg;
}
void
compute() override
{
PortIndex const outPortIndex = 0;
auto n1 = _number1.lock();
auto n2 = _number2.lock();
if (n2 && (n2->number() == 0.0))
{
setValidationState(false,
QString("Division by zero error"));
}
else
{
setValidationState(true,
QString(""));
if (n1 && n2 && (n2->number() != 0.0))
{
_result = std::make_shared<NumberData>(n1->number() /
n2->number());
}
else
{
_result.reset();
}
}
emit dataUpdated(outPortIndex);
}
};