add cylinder
This commit is contained in:
@@ -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() {}
|
||||
|
||||
Reference in New Issue
Block a user