PeriDyno 1.0.0
Loading...
Searching...
No Matches
PPropertyWidget.cpp
Go to the documentation of this file.
1#include "PPropertyWidget.h"
2
3//Framework
4#include "Module.h"
5#include "Node.h"
6#include "SceneGraph.h"
7
8//Node editor
11#include "nodes/QNode"
12
13#include "LockerButton.h"
14#include <QVBoxLayout>
15#include <QScrollArea>
16#include <QGridLayout>
17
19
20namespace dyno
21{
22 std::map<std::string, PPropertyWidget::FieldWidgetMeta> PPropertyWidget::sFieldWidgetMeta {};
23
24 //QWidget-->QVBoxLayout-->QScrollArea-->QWidget-->QGridLayout
26 : QWidget(parent)
27 , mMainLayout()
28 {
29 mMainLayout = new QVBoxLayout;
30 mScrollArea = new QScrollArea;
31
32 mMainLayout->setContentsMargins(0, 0, 0, 0);
33 mMainLayout->setSpacing(0);
34 mMainLayout->addWidget(mScrollArea);
35
36 mScrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
37 mScrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
38 mScrollArea->setWidgetResizable(true);
39
40 mScrollLayout = new QGridLayout;
41 mScrollLayout->setAlignment(Qt::AlignTop);
42 mScrollLayout->setContentsMargins(0, 0, 0, 0);
43
44 QWidget * m_scroll_widget = new QWidget;
45 m_scroll_widget->setLayout(mScrollLayout);
46 mScrollArea->setWidget(m_scroll_widget);
47 mScrollArea->setContentsMargins(0, 0, 0, 0);
48 //setMinimumWidth(250);
49 setLayout(mMainLayout);
50 }
51
56
58 sFieldWidgetMeta[meta.type->name()] = meta;
59 return 0;
60 }
62 if(sFieldWidgetMeta.count(name))
63 return &sFieldWidgetMeta.at(name);
64 return nullptr;
65 }
66
68 {
69 return QSize(512, 20);
70 }
71
72 QWidget* PPropertyWidget::addWidget(QWidget* widget)
73 {
74 mScrollLayout->addWidget(widget);
75 mPropertyItems.push_back(widget);
76
77 return widget;
78 }
79
81 {
82 //TODO: check whether m_widgets[i] should be explicitly deleted
83 for (int i = 0; i < mPropertyItems.size(); i++)
84 {
85 mScrollLayout->removeWidget(mPropertyItems[i]);
86 delete mPropertyItems[i];
87 }
88 mPropertyItems.clear();
89 }
90
91 void PPropertyWidget::showModuleProperty(std::shared_ptr<Module> module)
92 {
93 if (module == nullptr)
94 return;
95
96 this->removeAllWidgets();
97
98 QWidget* mWidget = new QWidget;
99
100 std::string mLabel[2] = { {" Control Variables" }, {" State Variables" } };
101
102 int propertyNum[2];
103
104 int n = 2;//label number
105 for (int i = 0; i < n; i++) {
107 mPropertyLabel[i]->setContentsMargins(8, 0, 0, 0);
108 mPropertyLabel[i]->SetTextLabel(QString::fromStdString(mLabel[i]));
109 mPropertyLabel[i]->SetImageLabel(QPixmap((getAssetPath() + "/icon/arrow_down_pressed.png").c_str()));
110 mPropertyLabel[i]->GetTextHandle()->setAttribute(Qt::WA_TransparentForMouseEvents, true);
111 mPropertyLabel[i]->GetImageHandle()->setAttribute(Qt::WA_TransparentForMouseEvents, true);
112
113 mPropertyWidget[i] = new QWidget(this);
114 mPropertyWidget[i]->setVisible(true);
115 mPropertyWidget[i]->setStyleSheet("background-color: transparent;");//"color:red;background-color:green;"
116
117 propertyNum[i] = 0;
118
119 mPropertyLayout[i] = new QGridLayout;
120 }
121
122 std::vector<FBase*>& fields = module->getAllFields();
123 for (FBase * var : fields)
124 {
125 if (var != nullptr) {
126 if (var->getFieldType() == FieldTypeEnum::Param)
127 {
128 if (var->getClassName() == std::string("FVar"))
129 {
130 this->addScalarFieldWidget(var, mPropertyLayout[0], propertyNum[0]);
131 propertyNum[0]++;
132 }
133 }
134 }
135 }
136
137 QVBoxLayout* vlayout = new QVBoxLayout;
138
139 for (int i = 0; i < n; i++) {
140 mFlag[i] = false;
141
142 if (propertyNum[i] != 0) {
143 vlayout->addWidget(mPropertyLabel[i]);
144 mPropertyWidget[i]->setLayout(mPropertyLayout[i]);
145 vlayout->addWidget(mPropertyWidget[i]);
146 }
147 else
148 {
149 vlayout->addWidget(mPropertyLabel[i]);
150 mPropertyWidget[i]->setLayout(mPropertyLayout[i]);
151 vlayout->addWidget(mPropertyWidget[i]);
152
153 mPropertyWidget[i]->setVisible(false);
154 mPropertyLabel[i]->setVisible(false);
155 }
156
157 connect(mPropertyLabel[i], &LockerButton::clicked, [this, i, vlayout]() {
158 if (!mFlag[i])
159 {
160 mPropertyLabel[i]->SetImageLabel(QPixmap((getAssetPath() + "/icon/arrow_right_pressed.png").c_str()));
161 mPropertyWidget[i]->setVisible(false);
162 }
163 else
164 {
165 mPropertyLabel[i]->SetImageLabel(QPixmap((getAssetPath() + "/icon/arrow_down_pressed.png").c_str()));
166 mPropertyWidget[i]->setVisible(true);
167 }
168 mFlag[i] = !mFlag[i];
169 });
170 }
171 vlayout->setContentsMargins(0, 0, 0, 0);
172 vlayout->setSpacing(0);
173 mWidget->setLayout(vlayout);
174
175 addWidget(mWidget);
176
177 mSeleted = module;
178 }
179
180 void PPropertyWidget::showNodeProperty(std::shared_ptr<Node> node)
181 {
182 if (node == nullptr)
183 return;
184
185 this->removeAllWidgets();
186
187 QWidget* mWidget = new QWidget;
188
189 std::string mLabel[2] = { {" Control Variables" }, {" State Variables" } };
190
191 int propertyNum[2];
192
193 int n = 2;//label number
194 for (int i = 0; i < n; i++) {
196 mPropertyLabel[i]->setContentsMargins(8, 0, 0, 0);
197 mPropertyLabel[i]->SetTextLabel(QString::fromStdString(mLabel[i]));
198 mPropertyLabel[i]->SetImageLabel(QPixmap((getAssetPath() + "/icon/arrow_down_pressed.png").c_str()));
199 mPropertyLabel[i]->GetTextHandle()->setAttribute(Qt::WA_TransparentForMouseEvents, true);
200 mPropertyLabel[i]->GetImageHandle()->setAttribute(Qt::WA_TransparentForMouseEvents, true);
201
202 mPropertyWidget[i] = new QWidget(this);
203 mPropertyWidget[i]->setVisible(true);
204 mPropertyWidget[i]->setStyleSheet("background-color: transparent;");
205
206
207 propertyNum[i] = 0;
208
209 mPropertyLayout[i] = new QGridLayout;
210 }
211
212
213 {
214 QGroupBox* title = new QGroupBox;
215 //title->setStyleSheet("border:none");
216 QGridLayout* layout = new QGridLayout;
217 layout->setContentsMargins(0, 0, 0, 0);
218 layout->setSpacing(0);
219
220 title->setLayout(layout);
221
222 QLabel* name = new QLabel();
223 //name->setStyleSheet("font: bold; background-color: rgb(230, 230, 230);");
224 name->setFixedHeight(25);
225 name->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
226 name->setText("Name");
227 layout->addWidget(name, 0, 0);
228
229 QLabel* output = new QLabel();
230 //output->setStyleSheet("font: bold; background-color: rgb(230, 230, 230);");
231 output->setFixedSize(64, 25);
232 output->setText("Output");
233 layout->addWidget(output, 0, 1, Qt::AlignRight);
234
235 mPropertyLayout[1]->addWidget(title);
236 }
237
238 std::vector<FBase*>& fields = node->getAllFields();
239 for (FBase * var : fields)
240 {
241 if (var != nullptr) {
242 if (var->getFieldType() == FieldTypeEnum::Param)
243 {
244 if (var->getClassName() == std::string("FVar"))
245 {
246 this->addScalarFieldWidget(var, mPropertyLayout[0], propertyNum[0]);
247 propertyNum[0]++;
248 }
249 }
250 else if (var->getFieldType() == FieldTypeEnum::State) {
251 this->addStateFieldWidget(var);
252 propertyNum[1]++;
253 }
254 }
255 }
256
257 QVBoxLayout* vlayout = new QVBoxLayout;
258
259 for (int i = 0; i < n; i++) {
260 mFlag[i] = false;
261
262 if (propertyNum[i] != 0) {
263 vlayout->addWidget(mPropertyLabel[i]);
264 mPropertyWidget[i]->setLayout(mPropertyLayout[i]);
265 vlayout->addWidget(mPropertyWidget[i]);
266 }
267
268 connect(mPropertyLabel[i], &LockerButton::clicked, [this, i, vlayout]() {
269 if (!mFlag[i])
270 {
271 mPropertyLabel[i]->SetImageLabel(QPixmap((getAssetPath() + "/icon/arrow_right_pressed.png").c_str()));
272 mPropertyWidget[i]->setVisible(false);
273 }
274 else
275 {
276 mPropertyLabel[i]->SetImageLabel(QPixmap((getAssetPath() + "/icon/arrow_down_pressed.png").c_str()));
277 mPropertyWidget[i]->setVisible(true);
278 }
279 mFlag[i] = !mFlag[i];
280 });
281 }
282 vlayout->setContentsMargins(0, 0, 0, 0);
283 vlayout->setSpacing(0);
284 mWidget->setLayout(vlayout);
285
286 addWidget(mWidget);
287
288 mSeleted = node;
289 }
290
291 void PPropertyWidget::showProperty(Qt::QtNode& block)
292 {
293 auto dataModel = block.nodeDataModel();
294
295 auto node = dynamic_cast<Qt::QtNodeWidget*>(dataModel);
296 if (node != nullptr)
297 {
298 this->showNodeProperty(node->getNode());
299 }
300 else
301 {
302 auto module = dynamic_cast<Qt::QtModuleWidget*>(dataModel);
303 if (module != nullptr)
304 {
305 this->showModuleProperty(module->getModule());
306 }
307 }
308 }
309
311 {
312 this->removeAllWidgets();
313 }
314
316 {
317 auto node = std::dynamic_pointer_cast<Node>(mSeleted);
318
319 if (node != nullptr)
320 {
321 emit nodeUpdated(node);
322 }
323
324 auto module = std::dynamic_pointer_cast<Module>(mSeleted);
325
326 if (module != nullptr)
327 emit moduleUpdated(module);
328 }
329
331 QWidget* fw {nullptr};
332 std::string template_name = field->getTemplateName();
333
334 auto reg = getRegistedWidget(template_name);
335 if(reg) {
336 fw = reg->constructor(field);
337 }
338
339 return fw;
340 }
341
342 void PPropertyWidget::addScalarFieldWidget(FBase* field, QGridLayout* layout,int j)
343 {
344 QWidget* fw = createFieldWidget(field);
345 if(fw != nullptr) {
346 this->connect(fw, SIGNAL(fieldChanged()), this, SLOT(contentUpdated()));
347 layout->addWidget(fw, j, 0);
348 }
349 else
350 {
351 std::cout <<(field->getObjectName());
352 }
353 }
354
356 {
357 auto fw = new QStateFieldWidget(field);
358 this->addWidget(fw);
359 }
360
362 {
363 auto widget = new QStateFieldWidget(field);
365 mPropertyLayout[1]->addWidget(widget);
366 }
367}
The model dictates the number of inputs and outputs for the Node.
std::string getObjectName()
Definition FBase.h:54
virtual const std::string getTemplateName()
Definition FBase.h:51
void showProperty(Qt::QtNode &block)
void addStateFieldWidget(FBase *field)
static FieldWidgetMeta * getRegistedWidget(const std::string &)
static int registerWidget(const FieldWidgetMeta &)
void nodeUpdated(std::shared_ptr< Node > node)
void addArrayFieldWidget(FBase *field)
void showModuleProperty(std::shared_ptr< Module > module)
QVBoxLayout * mMainLayout
QScrollArea * mScrollArea
QWidget * addWidget(QWidget *widget)
LockerButton * mPropertyLabel[3]
QGridLayout * mPropertyLayout[3]
static QWidget * createFieldWidget(FBase *field)
void showNodeProperty(std::shared_ptr< Node > node)
std::vector< QWidget * > mPropertyItems
static std::map< std::string, FieldWidgetMeta > sFieldWidgetMeta
void addScalarFieldWidget(FBase *field, QGridLayout *layout, int j)
QGridLayout * mScrollLayout
PPropertyWidget(QWidget *parent=nullptr)
virtual QSize sizeHint() const
void moduleUpdated(std::shared_ptr< Module > node)
void stateFieldUpdated(FBase *field, int status)
std::shared_ptr< OBase > mSeleted
void stateUpdated(FBase *field, int status)
This is an implementation of AdditiveCCD based on peridyno.
Definition Array.h:25
@ Param
Definition FBase.h:34
@ State
Definition FBase.h:35