Physics Simulation & Visualization Tool 0.1
A C++ physics simulation engine with real-time 3D visualization
Loading...
Searching...
No Matches
InspectorRow.cpp
Go to the documentation of this file.
1#include "InspectorRow.h"
2#include <QCheckBox>
3#include "../Vector3Widget.h"
4#include "../ScalarWidget.h"
5
6InspectorRow::InspectorRow(const QString &lbl, QWidget* parent) : label(lbl) {
7 container = new QWidget(parent);
8 layout = new QHBoxLayout(container);
9 layout->setContentsMargins(0,0,0,0);
10 layout->setSpacing(5);
11}
12
13InspectorRow::InspectorRow(const QString &lbl, QWidget* customEditor, std::function<void()> updateLogic) : label(lbl), container(customEditor) {
14 if (updateLogic) {
15 updaters.push_back(updateLogic);
16 }
17}
18
19InspectorRow& InspectorRow::addVec3(const std::function<glm::vec3()> &get, const std::function<void(glm::vec3)> &set, const QString& unit, const std::function<void(Vector3Widget*)> &onInit) {
20 auto* vec = new Vector3Widget(" " + unit);
21 vec->setValue(get());
22 layout->addWidget(vec);
23
24 if (onInit) {
25 onInit(vec);
26 }
27
28 QObject::connect(vec, &Vector3Widget::valueChanged, [set](glm::vec3 v){
29 if (set) set(v);
30 });
31
32 updaters.emplace_back([vec, get]() {
33 if (!get) return;
34 vec->setValue(get());
35 });
36
37 return *this;
38}
39
40InspectorRow &InspectorRow::addScalar(const std::function<double()> &get, const std::function<void(double)> &set, const QString& unit, const std::function<void(ScalarWidget*)> &onInit) {
41 auto* scalar = new ScalarWidget(" " + unit);
42 scalar->setValue(get());
43 layout->addWidget(scalar);
44
45 if (onInit) {
46 onInit(scalar);
47 }
48
49 QObject::connect(scalar, &ScalarWidget::valueChanged, [set](double value){
50 if (set) set(value);
51 });
52
53 updaters.emplace_back([scalar, get]() {
54 if (!get) return;
55 scalar->setValue(get());
56 });
57
58 return *this;
59}
60
61
62InspectorRow& InspectorRow::addCheckbox(const std::function<bool()> &get, const std::function<void(bool)> &set, const std::function<void(QCheckBox*)> &onInit) {
63 QCheckBox* cb = new QCheckBox();
64 cb->setChecked(get());
65 layout->addWidget(cb);
66
67 if (onInit) {
68 onInit(cb);
69 }
70
71 QObject::connect(cb, &QCheckBox::toggled, [set](bool checked){
72 if (set) set(checked);
73 });
74
75 updaters.emplace_back([cb, get]() {
76 if (!get) return;
77 bool val = get();
78 if (cb->isChecked() != val) {
79 const QSignalBlocker blocker(cb);
80 cb->setChecked(val);
81 }
82 });
83
84 return *this;
85}
86InspectorRow& InspectorRow::addButton(const QString& text, const std::function<void()> &onClick, const std::function<void(QPushButton*)> &onInit) {
87 QPushButton* btn = new QPushButton(text);
88 layout->addWidget(btn);
89
90 if (onInit) {
91 onInit(btn);
92 }
93
94 QObject::connect(btn, &QPushButton::clicked, [onClick](){
95 if (onClick) onClick();
96 });
97
98 return *this;
99}
InspectorRow & addButton(const QString &text, const std::function< void()> &onClick, const std::function< void(QPushButton *)> &onInit=nullptr)
InspectorRow & addCheckbox(const std::function< bool()> &get, const std::function< void(bool)> &set, const std::function< void(QCheckBox *)> &onInit=nullptr)
InspectorRow & addVec3(const std::function< glm::vec3()> &get, const std::function< void(glm::vec3)> &set, const QString &unit="", const std::function< void(Vector3Widget *)> &onInit=nullptr)
InspectorRow(const QString &lbl, QWidget *parent=nullptr)
InspectorRow & addScalar(const std::function< double()> &get, const std::function< void(double)> &set, const QString &unit="", const std::function< void(ScalarWidget *)> &onInit=nullptr)
void valueChanged(double newValue)
void valueChanged(const glm::vec3 &newValue)