vtkProperties

This commit is contained in:
AndreaRigoni
2026-03-23 17:46:42 +00:00
parent 5d0efb3078
commit f13342ff30
19 changed files with 745 additions and 186 deletions

View File

@@ -0,0 +1,173 @@
#include "PropertyWidgets.h"
#include <QSignalBlocker>
#include "Vtk/uLibVtkInterface.h"
namespace uLib {
namespace Qt {
PropertyWidgetBase::PropertyWidgetBase(PropertyBase* prop, QWidget* parent)
: QWidget(parent), m_BaseProperty(prop) {
m_Layout = new QHBoxLayout(this);
m_Layout->setContentsMargins(4, 2, 4, 2);
m_Label = new QLabel(QString::fromStdString(prop->GetName()), this);
m_Label->setMinimumWidth(100);
m_Layout->addWidget(m_Label);
}
PropertyWidgetBase::~PropertyWidgetBase() {}
DoublePropertyWidget::DoublePropertyWidget(Property<double>* prop, QWidget* parent)
: PropertyWidgetBase(prop, parent), m_Prop(prop) {
m_SpinBox = new QDoubleSpinBox(this);
m_SpinBox->setRange(-1000000.0, 1000000.0);
m_SpinBox->setValue(prop->Get());
m_Layout->addWidget(m_SpinBox, 1);
connect(m_SpinBox, static_cast<void(QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged),
[this](double val){ if (m_Prop->Get() != val) m_Prop->Set(val); });
uLib::Object::connect(m_Prop, &Property<double>::PropertyChanged, [this](){
if (m_SpinBox->value() != m_Prop->Get()) {
QSignalBlocker blocker(m_SpinBox);
m_SpinBox->setValue(m_Prop->Get());
}
});
}
DoublePropertyWidget::~DoublePropertyWidget() {}
FloatPropertyWidget::FloatPropertyWidget(Property<float>* prop, QWidget* parent)
: PropertyWidgetBase(prop, parent), m_Prop(prop) {
m_SpinBox = new QDoubleSpinBox(this);
m_SpinBox->setRange(-1000000.0, 1000000.0);
m_SpinBox->setDecimals(3);
m_SpinBox->setValue(prop->Get());
m_Layout->addWidget(m_SpinBox, 1);
connect(m_SpinBox, static_cast<void(QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged),
[this](double val){ if (m_Prop->Get() != (float)val) m_Prop->Set((float)val); });
uLib::Object::connect(m_Prop, &Property<float>::PropertyChanged, [this](){
if (m_SpinBox->value() != (double)m_Prop->Get()) {
QSignalBlocker blocker(m_SpinBox);
m_SpinBox->setValue((double)m_Prop->Get());
}
});
}
FloatPropertyWidget::~FloatPropertyWidget() {}
IntPropertyWidget::IntPropertyWidget(Property<int>* prop, QWidget* parent)
: PropertyWidgetBase(prop, parent), m_Prop(prop) {
m_SpinBox = new QSpinBox(this);
m_SpinBox->setRange(-1000000, 1000000);
m_SpinBox->setValue(prop->Get());
m_Layout->addWidget(m_SpinBox, 1);
connect(m_SpinBox, static_cast<void(QSpinBox::*)(int)>(&QSpinBox::valueChanged),
[this](int val){ if (m_Prop->Get() != val) m_Prop->Set(val); });
uLib::Object::connect(m_Prop, &Property<int>::PropertyChanged, [this](){
if (m_SpinBox->value() != m_Prop->Get()) {
QSignalBlocker blocker(m_SpinBox);
m_SpinBox->setValue(m_Prop->Get());
}
});
}
IntPropertyWidget::~IntPropertyWidget() {}
BoolPropertyWidget::BoolPropertyWidget(Property<bool>* prop, QWidget* parent)
: PropertyWidgetBase(prop, parent), m_Prop(prop) {
m_CheckBox = new QCheckBox(this);
m_CheckBox->setChecked(prop->Get());
m_Layout->addWidget(m_CheckBox, 1);
connect(m_CheckBox, &QCheckBox::toggled, [this](bool val){ if (m_Prop->Get() != val) m_Prop->Set(val); });
uLib::Object::connect(m_Prop, &Property<bool>::PropertyChanged, [this](){
if (m_CheckBox->isChecked() != m_Prop->Get()) {
QSignalBlocker blocker(m_CheckBox);
m_CheckBox->setChecked(m_Prop->Get());
}
});
}
BoolPropertyWidget::~BoolPropertyWidget() {}
StringPropertyWidget::StringPropertyWidget(Property<std::string>* prop, QWidget* parent)
: PropertyWidgetBase(prop, parent), m_Prop(prop) {
m_LineEdit = new QLineEdit(this);
m_LineEdit->setText(QString::fromStdString(prop->Get()));
m_Layout->addWidget(m_LineEdit, 1);
connect(m_LineEdit, &QLineEdit::editingFinished, [this](){
std::string val = m_LineEdit->text().toStdString();
if (m_Prop->Get() != val) m_Prop->Set(val);
});
uLib::Object::connect(m_Prop, &Property<std::string>::PropertyChanged, [this](){
if (m_LineEdit->text().toStdString() != m_Prop->Get()) {
QSignalBlocker blocker(m_LineEdit);
m_LineEdit->setText(QString::fromStdString(m_Prop->Get()));
}
});
}
StringPropertyWidget::~StringPropertyWidget() {}
PropertyEditor::PropertyEditor(QWidget* parent) : QWidget(parent), m_Object(nullptr) {
m_MainLayout = new QVBoxLayout(this);
m_MainLayout->setContentsMargins(0, 0, 0, 0);
m_ScrollArea = new QScrollArea(this);
m_ScrollArea->setWidgetResizable(true);
m_MainLayout->addWidget(m_ScrollArea);
m_Container = new QWidget();
m_ContainerLayout = new QVBoxLayout(m_Container);
m_ContainerLayout->setAlignment(::Qt::AlignTop);
m_ScrollArea->setWidget(m_Container);
registerFactory<double>([](PropertyBase* p, QWidget* parent){
return new DoublePropertyWidget(static_cast<Property<double>*>(p), parent);
});
registerFactory<float>([](PropertyBase* p, QWidget* parent){
return new FloatPropertyWidget(static_cast<Property<float>*>(p), parent);
});
registerFactory<int>([](PropertyBase* p, QWidget* parent){
return new IntPropertyWidget(static_cast<Property<int>*>(p), parent);
});
registerFactory<bool>([](PropertyBase* p, QWidget* parent){
return new BoolPropertyWidget(static_cast<Property<bool>*>(p), parent);
});
registerFactory<std::string>([](PropertyBase* p, QWidget* parent){
return new StringPropertyWidget(static_cast<Property<std::string>*>(p), parent);
});
}
PropertyEditor::~PropertyEditor() {}
void PropertyEditor::setObject(::uLib::Object* obj, bool displayOnly) {
m_Object = obj;
clear();
if (!obj) return;
// Choose which properties to show
const std::vector<::uLib::PropertyBase*>* props = &obj->GetProperties();
if (displayOnly) {
if (auto* puppet = dynamic_cast<::uLib::Vtk::Puppet*>(obj)) {
props = &puppet->GetDisplayProperties();
} else {
// If it's not a puppet but displayOnly is requested, showing nothing or fallback?
// Fallback: core properties.
}
}
for (auto* prop : *props) {
auto it = m_Factories.find(prop->GetTypeIndex());
if (it != m_Factories.end()) {
QWidget* widget = it->second(prop, m_Container);
m_ContainerLayout->addWidget(widget);
} else {
QWidget* fallback = new PropertyWidgetBase(prop, m_Container);
fallback->layout()->addWidget(new QLabel("(Read-only: " + QString::fromStdString(prop->GetValueAsString()) + ")"));
m_ContainerLayout->addWidget(fallback);
}
}
m_ContainerLayout->addStretch(1);
}
void PropertyEditor::clear() {
QLayoutItem* item;
while ((item = m_ContainerLayout->takeAt(0)) != nullptr) {
delete item->widget();
delete item;
}
}
} // namespace Qt
} // namespace uLib