PeriDyno 1.0.0
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 //initialize in ports
26 int input_num = getInputFields().size();
27 input_fields.resize(input_num);
28 auto inputs = getInputFields();
29 for (int i = 0; i < inputs.size(); i++)
30 {
31 input_fields[i] = std::make_shared<QtFieldData>(inputs[i]);;
32 // fprintf(stderr, (input_fields[i].expired()) ? "expired\n" : "nothing!\n");
33 }
34 }
35
37 }
38
39 unsigned int
40 QtModuleWidget::nPorts(PortType portType) const
41 {
42 unsigned int result;
43
44 if (portType == PortType::In)
45 {
46 result = input_fields.size();
47 }
48 else
49 {
50 result = output_fields.size();
51 }
52
53 return result;
54 }
55
56
57 NodeDataType QtModuleWidget::dataType(PortType portType, PortIndex portIndex) const
58 {
59 dyno::FBase* f = this->getField(portType, portIndex);
60
61 std::string name = f->getClassName();
62
63 return NodeDataType{ name.c_str(), name.c_str(), PortShape::Point };
64 }
65
66
67 std::shared_ptr<QtNodeData> QtModuleWidget::outData(PortIndex port)
68 {
69 return std::dynamic_pointer_cast<QtNodeData>(output_fields[port]);
70 }
71
72
73// std::shared_ptr<QtNodeData> QtModuleWidget::inData(PortIndex port)
74// {
75// // weak_ptr.lock() : if ptr is expired then return nullptr.
76// // fprintf(stderr, (input_fields[port].expired()) ? "expired\n" : "nothing!\n");
77// // return std::dynamic_pointer_cast<QtNodeData>(input_fields[port].lock());
78// return std::dynamic_pointer_cast<QtNodeData>(input_fields[port]);
79// }
80
82 {
83// // return m_name;
84 return dyno::FormatBlockCaptionName(mModule->caption());
85 }
86
88 {
89 return mModule->captionVisible();
90 }
91
92 QString QtModuleWidget::name() const
93 {
94 return QString::fromStdString(mModule->caption());
95 }
96
97 bool QtModuleWidget::portCaptionVisible(PortType portType, PortIndex portIndex) const
98 {
99 Q_UNUSED(portType); Q_UNUSED(portIndex);
100 return true;
101 }
102
103 QString QtModuleWidget::portCaption(PortType portType, PortIndex portIndex) const
104 {
105 dyno::FBase* f = this->getField(portType, portIndex);
106 std::string name = f->getObjectName();
107
109 }
110
112 {
113 return dyno::FormatDescription(mModule->description());
114 }
115
116 QString QtModuleWidget::portTips(PortType portType, PortIndex portIndex) const
117 {
118 dyno::FBase* f = this->getField(portType, portIndex);
119
120 auto fieldTip = [&](FBase* f) -> QString {
121 std::string tip;
122 tip += "Class: " + f->getClassName() + "\n";
123 tip += "Template: " + f->getTemplateName() + "\n";
124
125 return QString::fromStdString(tip);
126 };
127
128 return fieldTip(f);
129 }
130
131 void QtModuleWidget::setInData(std::shared_ptr<QtNodeData> data, PortIndex portIndex)
132 {
133 if (!mEditingEnabled)
134 return;
135
136 auto fieldData = std::dynamic_pointer_cast<QtFieldData>(data);
137
138 if (fieldData != nullptr)
139 {
140 auto field = fieldData->getField();
141
142 if (fieldData->connectionType() == CntType::Break)
143 {
144 field->disconnect(input_fields[portIndex]->getField());
145 fieldData->setConnectionType(CntType::Link);
146 }
147 else
148 {
149 field->connect(input_fields[portIndex]->getField());
150 }
151 }
152
153 updateModule();
154 }
155
156 bool QtModuleWidget::tryInData(PortIndex portIndex, std::shared_ptr<QtNodeData> nodeData)
157 {
158 if (!mEditingEnabled)
159 return false;
160
161 try
162 {
163 auto fieldExp = std::dynamic_pointer_cast<QtFieldData>(nodeData);
164 if (fieldExp == nullptr)
165 return false;
166
167 auto fieldInp = input_fields[portIndex];
168
169 if (fieldInp->getField()->getClassName() == fieldExp->getField()->getClassName())
170 {
171 std::string className = fieldInp->getField()->getClassName();
172 if (className == dyno::InstanceBase::className())
173 {
174 dyno::InstanceBase* instIn = dynamic_cast<dyno::InstanceBase*>(fieldInp->getField());
175 dyno::InstanceBase* instOut = dynamic_cast<dyno::InstanceBase*>(fieldExp->getField());
176
177 if (instIn != nullptr && instOut != nullptr)
178 return instIn->canBeConnectedBy(instOut);
179
180 return false;
181 }
182 else
183 return fieldInp->getField()->getTemplateName() == fieldExp->getField()->getTemplateName();
184 }
185 else
186 {
187 return false;
188 }
189 }
190 catch (std::bad_cast)
191 {
192 return false;
193 }
194
195 return true;
196 }
197
198 NodeValidationState QtModuleWidget::validationState() const
199 {
201 }
202
203 std::shared_ptr<Module> QtModuleWidget::getModule()
204 {
205 return mModule;
206 }
207
209 {
210 mEditingEnabled = true;
211 }
212
214 {
215 mEditingEnabled = false;
216 }
217
219 {
221 }
222
224 {
225 bool hasAllInputs = mModule->isInputComplete();
226
227 if (hasAllInputs)
228 {
229 modelValidationState = NodeValidationState::Valid;
230 modelValidationError = QString();
231 }
232 else
233 {
234 modelValidationState = NodeValidationState::Warning;
235 modelValidationError = QStringLiteral("Missing or incorrect inputs");
236 }
237
238 for (int i = 0; i < output_fields.size(); i++)
239 {
240 Q_EMIT dataUpdated(i);
241 }
242 }
243
244 FBase* QtModuleWidget::getField(PortType portType, PortIndex portIndex) const
245 {
246 return portType == PortType::In ? mModule->getInputFields()[portIndex] : mModule->getOutputFields()[portIndex];
247 }
248
249 std::vector<FBase*>& QtModuleWidget::getOutputFields()
250 {
251 return mModule->getOutputFields();
252 }
253
254 std::vector<FBase*>& QtModuleWidget::getInputFields()
255 {
256 return mModule->getInputFields();
257 }
258}
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()
InFieldPtr input_fields
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
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
QString validationMessage() const override
void setInData(std::shared_ptr< QtNodeData > data, PortIndex portIndex) override
QString caption() const override
bool tryInData(PortIndex portIndex, std::shared_ptr< QtNodeData > nodeData) override
std::string getObjectName()
Definition FBase.h:54
virtual const std::string getClassName()
Definition FBase.h:52
virtual const std::string getTemplateName()
Definition FBase.h:51
virtual bool canBeConnectedBy(InstanceBase *ins)=0
static const std::string className()
Definition FInstance.h:37
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