-
Notifications
You must be signed in to change notification settings - Fork 948
Expand file tree
/
Copy pathAdditionModel.hpp
More file actions
152 lines (121 loc) · 3.35 KB
/
AdditionModel.hpp
File metadata and controls
152 lines (121 loc) · 3.35 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#pragma once
#include <QtCore/QObject>
#include <QtWidgets/QLabel>
#include <nodes/NodeDataModel>
#include "MathOperationDataModel.hpp"
#include "DecimalData.hpp"
/// The model dictates the number of inputs and outputs for the Node.
/// In this example it has no logic.
class AdditionModel : public MathOperationDataModel
{
public:
virtual
~AdditionModel() {}
public:
QString
caption() const override
{ return QStringLiteral("Addition"); }
QString
name() const override
{ return QStringLiteral("Addition"); }
private:
void
compute() override
{
PortIndex const outPortIndex = 0;
auto n1 = _number1.lock();
auto n2 = _number2.lock();
if (n1 && n2)
{
modelValidationState = NodeValidationState::Valid;
modelValidationError = QString();
_result = std::make_shared<DecimalData>(n1->number() +
n2->number());
}
else
{
modelValidationState = NodeValidationState::Warning;
modelValidationError = QStringLiteral("Missing or incorrect inputs");
_result.reset();
}
Q_EMIT dataUpdated(outPortIndex);
}
};
class MultiAdditionModel : public NodeDataModel
{
public:
QString
caption() const override
{
return QStringLiteral("Multi Addition");
}
QString
name() const override
{
return QStringLiteral("Multi Addition");
}
unsigned int
nPorts(PortType portType) const override
{
return 1;
}
NodeDataType
dataType(PortType portType,
PortIndex portIndex) const
{
return DecimalData().type();
}
std::shared_ptr<NodeData>
outData(PortIndex port) override
{
return _result;
}
void setInData(std::shared_ptr<NodeData> data, PortIndex portIndex) override
{
}
void setInData(std::vector<std::shared_ptr<NodeData>> data, PortIndex portIndex) override
{
_input.clear();
_input.reserve(data.size());
for (auto& node : data)
{
std::shared_ptr<DecimalData> decimalData = std::dynamic_pointer_cast<DecimalData>(node);
if (decimalData != nullptr)
_input.push_back(decimalData);
}
if (_input.empty())
{
modelValidationState = NodeValidationState::Warning;
}
else
{
modelValidationState = NodeValidationState::Valid;
}
double result = 0.0f;
for (auto& node : _input)
{
std::shared_ptr<DecimalData> locked = node.lock();
result += locked->number();
}
_result = std::make_shared<DecimalData>(result);
dataUpdated(0);
}
QWidget *
embeddedWidget() override { return nullptr; }
NodeValidationState
validationState() const override
{
return modelValidationState;
}
QString
validationMessage() const override
{
return modelValidationError;
}
ConnectionPolicy portInConnectionPolicy(PortIndex) const override { return QtNodes::NodeDataModel::ConnectionPolicy::Many; }
private:
std::vector<std::weak_ptr<DecimalData>> _input;
std::shared_ptr<DecimalData> _result;
NodeValidationState modelValidationState = NodeValidationState::Warning;
QString modelValidationError = QString("Missing or incorrect inputs");
};