add cylinder

This commit is contained in:
AndreaRigoni
2026-03-24 15:22:50 +00:00
parent b45cde0bad
commit 51e6dbb4f5
11 changed files with 523 additions and 121 deletions

View File

@@ -1,6 +1,10 @@
#include "PropertyWidgets.h"
#include <QSignalBlocker>
#include <QRegularExpression>
#include <QRegularExpressionMatch>
#include "Vtk/uLibVtkInterface.h"
#include "Math/Units.h"
#include "Math/Dense.h"
namespace uLib {
namespace Qt {
@@ -15,57 +19,124 @@ PropertyWidgetBase::PropertyWidgetBase(PropertyBase* prop, QWidget* parent)
}
PropertyWidgetBase::~PropertyWidgetBase() {}
// Helper for unit parsing
static double parseWithUnits(const QString& text, double* factorOut = nullptr, QString* suffixOut = nullptr) {
static QRegularExpression re("^\\s*([-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?)\\s*(_?[a-zA-Z]+)?\\s*$");
QRegularExpressionMatch match = re.match(text);
if (!match.hasMatch()) return 0.0;
double num = match.captured(1).toDouble();
QString unit = match.captured(3);
double factor = 1.0;
if (!unit.isEmpty()) {
QString u = unit.startsWith('_') ? unit.mid(1) : unit;
if (u == "m") factor = CLHEP::meter;
else if (u == "cm") factor = CLHEP::centimeter;
else if (u == "mm") factor = CLHEP::millimeter;
else if (u == "um") factor = CLHEP::micrometer;
else if (u == "nm") factor = CLHEP::nanometer;
else if (u == "km") factor = CLHEP::kilometer;
else if (u == "deg") factor = CLHEP::degree;
else if (u == "rad") factor = CLHEP::radian;
else if (u == "ns") factor = CLHEP::nanosecond;
else if (u == "s") factor = CLHEP::second;
else if (u == "ms") factor = CLHEP::millisecond;
else if (u == "MeV") factor = CLHEP::megaelectronvolt;
else if (u == "eV") factor = CLHEP::electronvolt;
else if (u == "keV") factor = CLHEP::kiloelectronvolt;
else if (u == "GeV") factor = CLHEP::gigaelectronvolt;
else if (u == "TeV") factor = CLHEP::teraelectronvolt;
if (suffixOut) *suffixOut = u;
} else if (suffixOut) {
// Reuse previous suffix if none provided, or empty
}
if (factorOut) *factorOut = factor;
return num * factor;
}
// UnitLineEdit implementation
UnitLineEdit::UnitLineEdit(QWidget* parent) : QLineEdit(parent), m_Value(0), m_Factor(1.0), m_Suffix("mm"), m_IsInteger(false) {
connect(this, &QLineEdit::editingFinished, this, &UnitLineEdit::onEditingFinished);
}
void UnitLineEdit::setValue(double val) {
if (m_Value != val) {
m_Value = val;
// Initial heuristic for unit if it was mm and value becomes large
if (!m_IsInteger && m_Suffix == "mm" && std::abs(val) >= 1000.0) { m_Suffix = "m"; m_Factor = CLHEP::meter; }
updateText();
}
}
void UnitLineEdit::onEditingFinished() {
double factor = m_Factor;
QString suffix = m_Suffix;
double parsedVal = parseWithUnits(text(), &factor, &suffix);
if (!suffix.isEmpty()) {
m_Suffix = suffix;
m_Factor = factor;
}
if (m_IsInteger) {
parsedVal = std::round(parsedVal);
}
if (m_Value != parsedVal) {
m_Value = parsedVal;
emit valueManualChanged(m_Value);
}
updateText();
}
void UnitLineEdit::updateText() {
QSignalBlocker blocker(this);
if (m_IsInteger) {
setText(QString::number((int)m_Value));
} else {
double displayVal = m_Value / m_Factor;
setText(QString::number(displayVal, 'g', 6) + " " + m_Suffix);
}
}
void UnitLineEdit::setIntegerOnly(bool integerOnly) {
m_IsInteger = integerOnly;
updateText();
}
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); });
m_Edit = new UnitLineEdit(this);
m_Layout->addWidget(m_Edit, 1);
m_Edit->setValue(prop->Get());
connect(m_Edit, &UnitLineEdit::valueManualChanged, [this](double 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());
}
m_Edit->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); });
m_Edit = new UnitLineEdit(this);
m_Layout->addWidget(m_Edit, 1);
m_Edit->setValue(prop->Get());
connect(m_Edit, &UnitLineEdit::valueManualChanged, [this](double 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());
}
m_Edit->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); });
m_Edit = new UnitLineEdit(this);
m_Edit->setIntegerOnly(true);
m_Layout->addWidget(m_Edit, 1);
m_Edit->setValue(prop->Get());
connect(m_Edit, &UnitLineEdit::valueManualChanged, [this](double val){ m_Prop->Set((int)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());
}
m_Edit->setValue((double)m_Prop->Get());
});
}
IntPropertyWidget::~IntPropertyWidget() {}
BoolPropertyWidget::BoolPropertyWidget(Property<bool>* prop, QWidget* parent)
: PropertyWidgetBase(prop, parent), m_Prop(prop) {
@@ -126,6 +197,17 @@ PropertyEditor::PropertyEditor(QWidget* parent) : QWidget(parent), m_Object(null
registerFactory<std::string>([](PropertyBase* p, QWidget* parent){
return new StringPropertyWidget(static_cast<Property<std::string>*>(p), parent);
});
// Vector Registration
registerFactory<Vector2i>([](PropertyBase* p, QWidget* parent){ return new VectorPropertyWidget<Vector2i, 2>(static_cast<Property<Vector2i>*>(p), parent); });
registerFactory<Vector2f>([](PropertyBase* p, QWidget* parent){ return new VectorPropertyWidget<Vector2f, 2>(static_cast<Property<Vector2f>*>(p), parent); });
registerFactory<Vector2d>([](PropertyBase* p, QWidget* parent){ return new VectorPropertyWidget<Vector2d, 2>(static_cast<Property<Vector2d>*>(p), parent); });
registerFactory<Vector3i>([](PropertyBase* p, QWidget* parent){ return new VectorPropertyWidget<Vector3i, 3>(static_cast<Property<Vector3i>*>(p), parent); });
registerFactory<Vector3f>([](PropertyBase* p, QWidget* parent){ return new VectorPropertyWidget<Vector3f, 3>(static_cast<Property<Vector3f>*>(p), parent); });
registerFactory<Vector3d>([](PropertyBase* p, QWidget* parent){ return new VectorPropertyWidget<Vector3d, 3>(static_cast<Property<Vector3d>*>(p), parent); });
registerFactory<Vector4i>([](PropertyBase* p, QWidget* parent){ return new VectorPropertyWidget<Vector4i, 4>(static_cast<Property<Vector4i>*>(p), parent); });
registerFactory<Vector4f>([](PropertyBase* p, QWidget* parent){ return new VectorPropertyWidget<Vector4f, 4>(static_cast<Property<Vector4f>*>(p), parent); });
registerFactory<Vector4d>([](PropertyBase* p, QWidget* parent){ return new VectorPropertyWidget<Vector4d, 4>(static_cast<Property<Vector4d>*>(p), parent); });
}
PropertyEditor::~PropertyEditor() {}

View File

@@ -5,8 +5,6 @@
#include <QLabel>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QDoubleSpinBox>
#include <QSpinBox>
#include <QLineEdit>
#include <QCheckBox>
#include <QScrollArea>
@@ -16,6 +14,7 @@
#include "Core/Property.h"
#include "Core/Object.h"
#include "Math/Dense.h"
namespace uLib {
namespace Qt {
@@ -33,34 +32,90 @@ protected:
QLabel* m_Label;
};
class UnitLineEdit : public QLineEdit {
Q_OBJECT
public:
UnitLineEdit(QWidget* parent = nullptr);
void setValue(double val);
double getValue() const { return m_Value; }
void setIntegerOnly(bool b);
signals:
void valueManualChanged(double val);
private slots:
void onEditingFinished();
private:
void updateText();
double m_Value;
double m_Factor;
QString m_Suffix;
bool m_IsInteger;
};
class DoublePropertyWidget : public PropertyWidgetBase {
Q_OBJECT
public:
DoublePropertyWidget(Property<double>* prop, QWidget* parent = nullptr);
virtual ~DoublePropertyWidget();
private:
Property<double>* m_Prop;
QDoubleSpinBox* m_SpinBox;
UnitLineEdit* m_Edit;
};
class FloatPropertyWidget : public PropertyWidgetBase {
Q_OBJECT
public:
FloatPropertyWidget(Property<float>* prop, QWidget* parent = nullptr);
virtual ~FloatPropertyWidget();
private:
Property<float>* m_Prop;
QDoubleSpinBox* m_SpinBox;
UnitLineEdit* m_Edit;
};
class IntPropertyWidget : public PropertyWidgetBase {
Q_OBJECT
public:
IntPropertyWidget(Property<int>* prop, QWidget* parent = nullptr);
virtual ~IntPropertyWidget();
private:
Property<int>* m_Prop;
QSpinBox* m_SpinBox;
UnitLineEdit* m_Edit;
};
template <typename VecT, int Size>
class VectorPropertyWidget : public PropertyWidgetBase {
public:
VectorPropertyWidget(Property<VecT>* prop, QWidget* parent = nullptr)
: PropertyWidgetBase(prop, parent), m_Prop(prop) {
for (int i = 0; i < Size; ++i) {
m_Edits[i] = new UnitLineEdit(this);
if (std::is_integral<typename VecT::Scalar>::value) {
m_Edits[i]->setIntegerOnly(true);
}
m_Layout->addWidget(m_Edits[i], 1);
connect(m_Edits[i], &UnitLineEdit::valueManualChanged, [this, i](double val){
VecT v = m_Prop->Get();
v(i) = (typename VecT::Scalar)val;
if (m_Prop->Get() != v) m_Prop->Set(v);
});
}
updateEdits();
uLib::Object::connect(m_Prop, &Property<VecT>::PropertyChanged, [this](){
updateEdits();
});
}
private:
void updateEdits() {
VecT v = m_Prop->Get();
for (int i = 0; i < Size; ++i) {
if (!m_Edits[i]->hasFocus()) {
m_Edits[i]->setValue((double)v(i));
}
}
}
Property<VecT>* m_Prop;
UnitLineEdit* m_Edits[Size];
};
class BoolPropertyWidget : public PropertyWidgetBase {