PeriDyno 1.2.1
Loading...
Searching...
No Matches
WtModuleWidget.cpp
Go to the documentation of this file.
1#include "WtModuleWidget.h"
2
3WtModuleWidget::WtModuleWidget(std::shared_ptr<Module> base)
4{
5 m_Module = base;
6
7 if (m_Module != nullptr)
8 {
9 //initialize out ports
10 int output_num = getOutputFields().size();
11
12 output_fields.resize(output_num);
13
14 auto outputs = getOutputFields();
15
16 for (int i = 0; i < outputs.size(); i++)
17 {
18 output_fields[i] = std::make_shared<WtFieldData>(outputs[i]);
19 }
20
21 //initialize in ports
22 int input_num = getInputFields().size();
23
24 input_fields.resize(input_num);
25
26 auto inputs = getInputFields();
27
28 for (int i = 0; i < inputs.size(); i++)
29 {
30 input_fields[i] = std::make_shared<WtFieldData>(inputs[i]);;
31 }
32 }
33
35}
36
37std::string WtModuleWidget::caption() const
38{
39 return m_Module->caption();
40}
41
43{
44 return m_Module->captionVisible();
45}
46
47std::string WtModuleWidget::name() const
48{
49 return m_Module->caption();
50}
51
52std::string WtModuleWidget::portCaption(PortType portType, PortIndex portIndex) const
53{
54 dyno::FBase* f = this->getField(portType, portIndex);
55
56 std::string name = f->getObjectName();
57
58 return name;
59}
60
61std::string WtModuleWidget::nodeTips() const
62{
63 return m_Module->description();
64}
65
66std::string WtModuleWidget::portTips(PortType portType, PortIndex portIndex) const
67{
68 dyno::FBase* f = this->getField(portType, portIndex);
69
70 auto fieldTip = [&](FBase* f) -> std::string {
71 std::string tip;
72 tip += "Class: " + f->getClassName() + "\n";
73 tip += "Template: " + f->getTemplateName() + "\n";
74
75 return tip;
76 };
77
78 return fieldTip(f);
79}
80
82{
84}
85
86unsigned int WtModuleWidget::nPorts(PortType portType) const
87{
88 return (portType == PortType::In) ? input_fields.size() : output_fields.size();
89}
90
92{
93 return true;
94}
95
96std::shared_ptr<WtNodeData> WtModuleWidget::outData(PortIndex port)
97{
98 return std::dynamic_pointer_cast<WtNodeData>(output_fields[port]);
99}
100
101void WtModuleWidget::setInData(std::shared_ptr<WtNodeData> data, PortIndex portIndex)
102{
103 if (!mEditingEnabled)
104 return;
105
106 auto fieldData = std::dynamic_pointer_cast<WtFieldData>(data);
107
108 if (fieldData != nullptr)
109 {
110 auto field = fieldData->getField();
111
112 if (fieldData->connectionType() == CntType::Break)
113 {
114 field->disconnect(input_fields[portIndex]->getField());
115 fieldData->setConnectionType(CntType::Link);
116 }
117 else
118 {
119 field->connect(input_fields[portIndex]->getField());
120 }
121 }
122
123 updateModule();
124}
125
126bool WtModuleWidget::tryInData(PortIndex portIndex, std::shared_ptr<WtNodeData> nodeData)
127{
128 if (!mEditingEnabled)
129 return false;
130
131 try
132 {
133 auto fieldExp = std::dynamic_pointer_cast<WtFieldData>(nodeData);
134
135 if (fieldExp == nullptr)
136 return false;
137
138 auto fieldInp = input_fields[portIndex];
139
140 if (fieldInp->getField()->getClassName() == fieldExp->getField()->getClassName())
141 {
142 std::string className = fieldExp->getField()->getClassName();
143
144 if (className == dyno::InstanceBase::className())
145 {
146 dyno::InstanceBase* instIn = dynamic_cast<dyno::InstanceBase*>(fieldInp->getField());
147
148 dyno::InstanceBase* instOut = dynamic_cast<dyno::InstanceBase*>(fieldExp->getField());
149
150 if (instIn != nullptr && instOut != nullptr)
151 return instIn->canBeConnectedBy(instOut);
152
153 return false;
154 }
155 else
156 {
157 return fieldInp->getField()->getTemplateName() == fieldExp->getField()->getTemplateName();
158 }
159 }
160 else
161 {
162 return false;
163 }
164 }
165 catch (std::bad_cast)
166 {
167 return false;
168 }
169 return true;
170}
171
173{
174 dyno::FBase* f = this->getField(portType, portIndex);
175
176 std::string name = f->getClassName();
177
178 return NodeDataType{ name.c_str(), name.c_str(), PortShape::Point };
179}
180
185
186std::shared_ptr<Module> WtModuleWidget::getModule()
187{
188 return m_Module;
189}
190
195
200
202{
203 //bool hasAllInputs = m_Module->isInputComplete();
204
205 //if (hasAllInputs)
206 //{
207 // modelValidationState = NodeValidationState::Valid;
208 // modelValidationError = std::string();
209 //}
210 //else
211 //{
212 // modelValidationState = NodeValidationState::Warning;
213 // //modelValidationError = QStringLiteral("Missing or incorrect inputs");
214 //}
215
216 //for (int i = 0; i < output_fields.size(); i++)
217 //{
218 // dataUpdated(i);
219 //}
220}
221
223{
224 return portType == PortType::In ? m_Module->getInputFields()[portIndex] : m_Module->getOutputFields()[portIndex];
225}
226
228{
229 return m_Module->getOutputFields();
230}
231
232std::vector<FBase*>& WtModuleWidget::getInputFields()
233{
234 return m_Module->getInputFields();
235}
int PortIndex
PortType
NodeValidationState
std::string validationMessage() const override
bool portCaptionVisible(PortType portType, PortIndex portIndex) const override
It is possible to hide port caption in GUI.
std::vector< FBase * > & getOutputFields()
virtual void updateModule()
FBase * getField(PortType portType, PortIndex portIndex) const
std::shared_ptr< Module > m_Module
NodeValidationState validationState() const override
std::string portCaption(PortType portType, PortIndex portIndex) const override
Port caption is used in GUI to label individual ports.
std::shared_ptr< WtNodeData > outData(PortIndex port) override
std::vector< FBase * > & getInputFields()
NodeValidationState modelValidationState
WtModuleWidget(std::shared_ptr< Module > base=nullptr)
OutFieldPtr output_fields
std::string nodeTips() const override
std::string modelValidationError
bool tryInData(PortIndex portIndex, std::shared_ptr< WtNodeData > nodeData) override
std::string caption() const override
bool captionVisible() const override
It is possible to hide caption in GUI.
std::string name() const override
Name makes this model unique.
InFieldPtr input_fields
unsigned int nPorts(PortType portType) const override
std::shared_ptr< Module > getModule()
void setInData(std::shared_ptr< WtNodeData > data, PortIndex portIndex) override
Triggers the algorithm.
NodeDataType dataType(PortType portType, PortIndex portIndex) const override
std::string portTips(PortType portType, PortIndex portIndex) const override
std::string getObjectName()
Definition FBase.h:64
virtual const std::string getClassName()
Definition FBase.h:62
virtual const std::string getTemplateName()
Definition FBase.h:61
virtual bool canBeConnectedBy(InstanceBase *ins)=0
static const std::string className()
Definition FInstance.h:40