PeriDyno 1.2.1
Loading...
Searching...
No Matches
QtModuleWidget.cpp
Go to the documentation of this file.
1#include "QtModuleWidget.h"
2
3#include "Module.h"
4
5#include "Format.h"
6
7namespace Qt
8{
9
10 QtModuleWidget::QtModuleWidget(std::shared_ptr<Module> base)
11 {
12 mModule = base;
13
14 if (mModule != nullptr)
15 {
16 //initialize out ports
17 int output_num = getOutputFields().size();
18 output_fields.resize(output_num);
19 auto outputs = getOutputFields();
20 for (int i = 0; i < outputs.size(); i++)
21 {
22 output_fields[i] = std::make_shared<QtFieldData>(outputs[i]);
23 }
24
25 mModuleExport = std::make_shared<QtExportModule>(base);
26
27 //initialize in ports
28 int input_num = getInputFields().size();
29 input_fields.resize(input_num);
30 auto inputs = getInputFields();
31 for (int i = 0; i < inputs.size(); i++)
32 {
33 input_fields[i] = std::make_shared<QtFieldData>(inputs[i]);;
34 // fprintf(stderr, (input_fields[i].expired()) ? "expired\n" : "nothing!\n");
35 }
36
37 auto imports = mModule->getImportModules();
38 auto imports_num = imports.size();
39
40 mModuleImports.resize(imports_num);
41 for (int i = 0; i < imports.size(); i++)
42 {
43 mModuleImports[i] = std::make_shared<QtImportModule>(imports[i]);
44 }
45 }
46
48 }
49
50 unsigned int
52 {
53 unsigned int result;
54
55 if (portType == PortType::In)
56 {
57 result = mModule->allowImported() ? mModuleImports.size() + input_fields.size() : input_fields.size();
58 }
59 else
60 {
61 result = mModule->allowExported() ? 1 + output_fields.size() : output_fields.size();
62 }
63
64 return result;
65 }
66
67
69 {
70 switch (portType)
71 {
72 case PortType::In:
73 if (mModule->allowImported() && portIndex < mModuleImports.size())
74 {
75 return NodeDataType{ "port", "port", PortShape::Arrow };
76 }
77 else
78 {
79 PortIndex recalculatedIndex = mModule->allowImported() ? portIndex - mModuleImports.size() : portIndex;
80
81 dyno::FBase* f = this->getField(portType, recalculatedIndex);
82
83 std::string name = f->getClassName();
84
85 return NodeDataType{ name.c_str(), name.c_str(), PortShape::Point };
86 }
87 break;
88 case PortType::Out:
89 if (mModule->allowExported() && portIndex == 0)
90 {
91 return NodeDataType{ "port", "port", PortShape::Arrow };
92 }
93 else
94 {
95 PortIndex recalculatedIndex = mModule->allowExported() ? portIndex - 1 : portIndex;
96
97 dyno::FBase* f = this->getField(portType, recalculatedIndex);
98
99 std::string name = f->getClassName();
100
101 return NodeDataType{ name.c_str(), name.c_str(), PortShape::Point };
102 }
103 break;
104 case PortType::None:
105 break;
106 }
107
108 return NodeDataType{ "port", "port", PortShape::Point };
109 }
110
111
112 std::shared_ptr<QtNodeData> QtModuleWidget::outData(PortIndex port)
113 {
114 if (mModule->allowExported())
115 {
116 return port == 0 ? std::static_pointer_cast<QtNodeData>(mModuleExport) : std::static_pointer_cast<QtNodeData>(output_fields[port - 1]);
117 }
118 else
119 return std::static_pointer_cast<QtNodeData>(output_fields[port]);
120 }
121
122
123// std::shared_ptr<QtNodeData> QtModuleWidget::inData(PortIndex port)
124// {
125// // weak_ptr.lock() : if ptr is expired then return nullptr.
126// // fprintf(stderr, (input_fields[port].expired()) ? "expired\n" : "nothing!\n");
127// // return std::dynamic_pointer_cast<QtNodeData>(input_fields[port].lock());
128// return std::dynamic_pointer_cast<QtNodeData>(input_fields[port]);
129// }
130
132 {
133// // return m_name;
134 return dyno::FormatBlockCaptionName(mModule->caption());
135 }
136
138 {
139 return mModule->captionVisible();
140 }
141
142 QString QtModuleWidget::name() const
143 {
144 return QString::fromStdString(mModule->caption());
145 }
146
148 {
149 Q_UNUSED(portType); Q_UNUSED(portIndex);
150 return true;
151 }
152
153 QString QtModuleWidget::portCaption(PortType portType, PortIndex portIndex) const
154 {
155 switch (portType)
156 {
157 case PortType::In:
158 if (mModule->allowImported() && portIndex < mModuleImports.size())
159 {
160 return QString("");
161 }
162 else
163 {
164 PortIndex recalculatedIndex = mModule->allowImported() ? portIndex - mModuleImports.size() : portIndex;
165 dyno::FBase* f = this->getField(portType, recalculatedIndex);
166 std::string name = f->getObjectName();
167
169 }
170 break;
171 case PortType::Out:
172 if (mModule->allowExported() && portIndex == 0)
173 {
174 return QString("");
175 }
176 else
177 {
178 PortIndex recalculatedIndex = mModule->allowExported() ? portIndex - 1 : portIndex;
179 dyno::FBase* f = this->getField(portType, recalculatedIndex);
180 std::string name = f->getObjectName();
181
183 }
184 break;
185 case PortType::None:
186 break;
187 }
188
189 return QString("");
190 }
191
193 {
194 return dyno::FormatDescription(mModule->description());
195 }
196
197 QString QtModuleWidget::portTips(PortType portType, PortIndex portIndex) const
198 {
199 auto fieldTip = [&](FBase* f) -> QString {
200 std::string tip;
201 tip += "Class: " + f->getClassName() + "\n";
202 tip += "Template: " + f->getTemplateName() + "\n";
203
204 return QString::fromStdString(tip);
205 };
206
207 switch (portType)
208 {
209 case PortType::In:
210 if (mModule->allowImported() && portIndex < mModuleImports.size())
211 {
212 return QString("");
213 }
214 else
215 {
216 PortIndex recalculatedIndex = mModule->allowImported() ? portIndex - mModuleImports.size() : portIndex;
217
218 dyno::FBase* f = this->getField(portType, recalculatedIndex);
219
220 return fieldTip(f);
221 }
222 break;
223 case PortType::Out:
224 if (mModule->allowExported() && portIndex == 0)
225 {
226 return QString("");
227 }
228 else
229 {
230 PortIndex recalculatedIndex = mModule->allowExported() ? portIndex - 1 : portIndex;
231 dyno::FBase* f = this->getField(portType, recalculatedIndex);
232
233 return fieldTip(f);
234 }
235 break;
236 case PortType::None:
237 break;
238 }
239
240 return QString("");
241 }
242
243 void QtModuleWidget::setInData(std::shared_ptr<QtNodeData> data, PortIndex portIndex)
244 {
245 if (!mEditingEnabled)
246 return;
247
248 if (this->allowImported() && portIndex < mModuleImports.size())
249 {
250 auto module_port = std::dynamic_pointer_cast<QtExportModule>(data);
251
252 if (module_port != nullptr)
253 {
254 auto m = module_port->getModule();
255
256 if (module_port->connectionType() == CntType::Break)
257 {
258 //mNodeInport[portIndex]->getNodePort()->removeNode(nd.get());
259 m->disconnect(mModuleImports[portIndex]->getModulePort());
260
261 //TODO: recover the connection state, use a more elegant way in the future
262 data->setConnectionType(CntType::Link);
263 }
264 else
265 {
266 m->connect(mModuleImports[portIndex]->getModulePort());
267 }
268 }
269 }
270 else
271 {
272 PortIndex recalcualtedIndex = this->allowImported() ? portIndex - mModuleImports.size() : portIndex;
273 auto fieldData = std::dynamic_pointer_cast<QtFieldData>(data);
274
275 if (fieldData != nullptr)
276 {
277 auto field = fieldData->getField();
278
279 if (fieldData->connectionType() == CntType::Break)
280 {
281 field->disconnect(input_fields[recalcualtedIndex]->getField());
282 fieldData->setConnectionType(CntType::Link);
283 }
284 else
285 {
286 field->connect(input_fields[recalcualtedIndex]->getField());
287 }
288 }
289 }
290
291 updateModule();
292 }
293
294 bool QtModuleWidget::tryInData(PortIndex portIndex, std::shared_ptr<QtNodeData> nodeData)
295 {
296 if (!mEditingEnabled)
297 return false;
298
299 try
300 {
301 if (this->allowImported() && portIndex < mModuleImports.size())
302 {
303 try
304 {
305 auto moduleExp = std::dynamic_pointer_cast<QtExportModule>(nodeData);
306
307 if (moduleExp == nullptr)
308 return false;
309
310 auto moduleImp = mModuleImports[portIndex];
311
312 return moduleImp->getModulePort()->isKindOf(moduleExp->getModule().get());;
313 }
314 catch (std::bad_cast)
315 {
316 return false;
317 }
318 }
319 else
320 {
321 PortIndex recalcualtedIndex = this->allowImported() ? portIndex - mModuleImports.size() : portIndex;
322
323 auto fieldExp = std::dynamic_pointer_cast<QtFieldData>(nodeData);
324 if (fieldExp == nullptr)
325 return false;
326
327 auto fieldInp = input_fields[recalcualtedIndex];
328
329 if (fieldInp->getField()->getClassName() == fieldExp->getField()->getClassName())
330 {
331 std::string className = fieldInp->getField()->getClassName();
332 if (className == dyno::InstanceBase::className())
333 {
334 dyno::InstanceBase* instIn = dynamic_cast<dyno::InstanceBase*>(fieldInp->getField());
335 dyno::InstanceBase* instOut = dynamic_cast<dyno::InstanceBase*>(fieldExp->getField());
336
337 if (instIn != nullptr && instOut != nullptr)
338 return instIn->canBeConnectedBy(instOut);
339
340 return false;
341 }
342 else
343 return fieldInp->getField()->getTemplateName() == fieldExp->getField()->getTemplateName();
344 }
345 else
346 {
347 return false;
348 }
349 }
350 }
351 catch (std::bad_cast)
352 {
353 return false;
354 }
355
356 return true;
357 }
358
363
364 QtNodeDataModel::ConnectionPolicy QtModuleWidget::portInConnectionPolicy(PortIndex portIndex) const
365 {
366 if (portIndex < mModuleImports.size())
367 {
368 auto portType = mModuleImports[portIndex]->getModulePort()->getPortType();
369
370 return portType == dyno::ModulePortType::M_Single ? ConnectionPolicy::One : ConnectionPolicy::Many;
371 }
372 else
373 {
374 auto fieldInp = input_fields[portIndex - mModuleImports.size()];
375
376 return fieldInp->getField()->inputPolicy() == FBase::One ? ConnectionPolicy::One : ConnectionPolicy::Many;
377 }
378 }
379
380 std::shared_ptr<Module> QtModuleWidget::getModule()
381 {
382 return mModule;
383 }
384
386 {
387 mEditingEnabled = true;
388 }
389
391 {
392 mEditingEnabled = false;
393 }
394
396 {
398 }
399
401 {
402 bool hasAllInputs = mModule->isInputComplete();
403
404 if (hasAllInputs)
405 {
407 modelValidationError = QString();
408 }
409 else
410 {
412 modelValidationError = QStringLiteral("Missing or incorrect inputs");
413 }
414
415 for (int i = 0; i < output_fields.size(); i++)
416 {
417 Q_EMIT dataUpdated(i);
418 }
419 }
420
422 {
423 return portType == PortType::In ? mModule->getInputFields()[portIndex] : mModule->getOutputFields()[portIndex];
424 }
425
426 std::vector<FBase*>& QtModuleWidget::getOutputFields()
427 {
428 return mModule->getOutputFields();
429 }
430
431 std::vector<FBase*>& QtModuleWidget::getInputFields()
432 {
433 return mModule->getInputFields();
434 }
435}
int PortIndex
PortType
NodeValidationState
@ One
Definition FBase.h:56
std::shared_ptr< Module > getModule()
FBase * getField(PortType portType, PortIndex portIndex) const
void enableEditing()
When enabled, the scenegraph can be updated as long as the corresponding GUI is updated.
QString portCaption(PortType portType, PortIndex portIndex) const override
std::vector< FBase * > & getOutputFields()
QString portTips(PortType portType, PortIndex portIndex) const override
std::shared_ptr< QtNodeData > outData(PortIndex port) override
QString name() const override
bool captionVisible() const override
whether to hide caption in GUI
NodeValidationState modelValidationState
ExportModulePtr mModuleExport
QtModuleWidget(std::shared_ptr< Module > base=nullptr)
void disableEditing()
When disabled, the scenegraph can not be affected by the corresponding GUI.
NodeDataType dataType(PortType portType, PortIndex portIndex) const override
virtual void updateModule()
std::vector< FBase * > & getInputFields()
bool portCaptionVisible(PortType portType, PortIndex portIndex) const override
OutFieldPtr output_fields
QString nodeTips() const override
std::shared_ptr< Module > mModule
NodeValidationState validationState() const override
unsigned int nPorts(PortType portType) const override
QtNodeDataModel::ConnectionPolicy portInConnectionPolicy(PortIndex portIndex) const override
QString validationMessage() const override
void setInData(std::shared_ptr< QtNodeData > data, PortIndex portIndex) override
bool allowImported() const override
ImportModulePtr mModuleImports
QString caption() const override
bool tryInData(PortIndex portIndex, std::shared_ptr< QtNodeData > nodeData) override
std::string getObjectName()
Definition FBase.h:64
virtual const std::string getClassName()
Definition FBase.h:62
virtual bool canBeConnectedBy(InstanceBase *ins)=0
static const std::string className()
Definition FInstance.h:40
QString FormatBlockCaptionName(std::string name)
Definition Format.cpp:64
QString FormatDescription(std::string name)
Definition Format.cpp:93
QString FormatBlockPortName(std::string name)
Definition Format.cpp:35
@ M_Single
Definition ModulePort.h:28