add widget type to numeric PIVariantEdit
This commit is contained in:
@@ -112,6 +112,7 @@ void PIValueTreeEditParameters::createAttributes(QList<PIVariantEdit *> & list,
|
|||||||
auto dit = dal.makeIterator();
|
auto dit = dal.makeIterator();
|
||||||
while (dit.next()) {
|
while (dit.next()) {
|
||||||
auto * ve = new PIVariantEdit();
|
auto * ve = new PIVariantEdit();
|
||||||
|
ve->setAttributes(PIVariantEditorBase::editorDefaultAttributes(dit.value().typeID()));
|
||||||
ve->setValue(dit.value());
|
ve->setValue(dit.value());
|
||||||
ve->setProperty(property_name, PI2QString(dit.key()));
|
ve->setProperty(property_name, PI2QString(dit.key()));
|
||||||
list << ve;
|
list << ve;
|
||||||
|
|||||||
@@ -1,8 +1,11 @@
|
|||||||
#include "pivariant_edit_widgets.h"
|
#include "pivariant_edit_widgets.h"
|
||||||
|
|
||||||
|
#include "evalspinbox.h"
|
||||||
#include "pivaluetree.h"
|
#include "pivaluetree.h"
|
||||||
#include "pivariant_edit_enum.h"
|
#include "pivariant_edit_enum.h"
|
||||||
#include "pivarianttypes.h"
|
#include "pivarianttypes.h"
|
||||||
|
#include "scroll_spin_box.h"
|
||||||
|
#include "spinslider.h"
|
||||||
|
|
||||||
#include <QEvent>
|
#include <QEvent>
|
||||||
#include <QFileDialog>
|
#include <QFileDialog>
|
||||||
@@ -29,88 +32,231 @@ REGISTER_PIVARIANTEDITOR(PIVariantTypes::Dir, PIVariantEditors::Dir);
|
|||||||
|
|
||||||
using Attribute = PIValueTree::Attribute;
|
using Attribute = PIValueTree::Attribute;
|
||||||
|
|
||||||
// PIVariantEditors::Int
|
|
||||||
|
|
||||||
PIVariantMap PIVariantEditors::Int::attributes() const {
|
// PIVariantEditors::NumberBase
|
||||||
return {
|
|
||||||
{Attribute::minimum, widget->minimum() },
|
PIVariantEditors::NumberBase::NumberBase() {}
|
||||||
{Attribute::maximum, widget->maximum() },
|
|
||||||
{Attribute::singleStep, widget->singleStep()},
|
|
||||||
{Attribute::prefix, prefix },
|
void PIVariantEditors::NumberBase::setValueNumeric(double v) {
|
||||||
{Attribute::suffix, suffix },
|
switch (type) {
|
||||||
};
|
case tSpinBox: {
|
||||||
|
auto * w = qobject_cast<QDoubleSpinBox *>(widget);
|
||||||
|
if (w) w->setValue(v);
|
||||||
|
} break;
|
||||||
|
case tSlider: {
|
||||||
|
auto * w = qobject_cast<QSlider *>(widget);
|
||||||
|
if (w) w->setValue(v);
|
||||||
|
} break;
|
||||||
|
case tSpinSlider: {
|
||||||
|
auto * w = qobject_cast<SpinSlider *>(widget);
|
||||||
|
if (w) w->setValue(v);
|
||||||
|
} break;
|
||||||
|
case tEvalSpinBox: {
|
||||||
|
auto * w = qobject_cast<EvalSpinBox *>(widget);
|
||||||
|
if (w) {
|
||||||
|
PIString vs = PIString::fromNumber(v), es = PIString::fromNumber(w->value());
|
||||||
|
if (vs != es) w->setValue(v);
|
||||||
|
}
|
||||||
|
} break;
|
||||||
|
case tScrollSpinBox: {
|
||||||
|
auto * w = qobject_cast<ScrollSpinBox *>(widget);
|
||||||
|
if (w) w->setValue(v);
|
||||||
|
} break;
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
PIVariantMap PIVariantEditors::Int::defaultAttributes() {
|
double PIVariantEditors::NumberBase::valueNumeric() const {
|
||||||
|
switch (type) {
|
||||||
|
case tSpinBox: {
|
||||||
|
auto * w = qobject_cast<QDoubleSpinBox *>(widget);
|
||||||
|
if (w) return w->value();
|
||||||
|
} break;
|
||||||
|
case tSlider: {
|
||||||
|
auto * w = qobject_cast<QSlider *>(widget);
|
||||||
|
if (w) return w->value();
|
||||||
|
} break;
|
||||||
|
case tSpinSlider: {
|
||||||
|
auto * w = qobject_cast<SpinSlider *>(widget);
|
||||||
|
if (w) return w->value();
|
||||||
|
} break;
|
||||||
|
case tEvalSpinBox: {
|
||||||
|
auto * w = qobject_cast<EvalSpinBox *>(widget);
|
||||||
|
if (w) return w->value();
|
||||||
|
} break;
|
||||||
|
case tScrollSpinBox: {
|
||||||
|
auto * w = qobject_cast<ScrollSpinBox *>(widget);
|
||||||
|
if (w) return w->value();
|
||||||
|
} break;
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
|
return 0.;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
PIVariantMap PIVariantEditors::NumberBase::attributes() const {
|
||||||
|
auto etype = createTypes();
|
||||||
|
etype.selectValue(type);
|
||||||
|
PIVariantMap ret = {
|
||||||
|
{Attribute::widgetType, etype },
|
||||||
|
{Attribute::prefix, prefix},
|
||||||
|
{Attribute::suffix, suffix},
|
||||||
|
};
|
||||||
|
switch (type) {
|
||||||
|
case tSpinBox: {
|
||||||
|
auto * w = qobject_cast<QDoubleSpinBox *>(widget);
|
||||||
|
if (w) {
|
||||||
|
ret[Attribute::minimum] = w->minimum();
|
||||||
|
ret[Attribute::maximum] = w->maximum();
|
||||||
|
ret[Attribute::singleStep] = w->singleStep();
|
||||||
|
if (!is_int) ret[Attribute::decimals] = w->decimals();
|
||||||
|
}
|
||||||
|
} break;
|
||||||
|
case tSlider: {
|
||||||
|
auto * w = qobject_cast<QSlider *>(widget);
|
||||||
|
if (w) {
|
||||||
|
ret[Attribute::minimum] = w->minimum();
|
||||||
|
ret[Attribute::maximum] = w->maximum();
|
||||||
|
ret[Attribute::singleStep] = w->singleStep();
|
||||||
|
}
|
||||||
|
} break;
|
||||||
|
case tSpinSlider: {
|
||||||
|
auto * w = qobject_cast<SpinSlider *>(widget);
|
||||||
|
if (w) {
|
||||||
|
ret[Attribute::minimum] = w->minimum();
|
||||||
|
ret[Attribute::maximum] = w->maximum();
|
||||||
|
ret[Attribute::singleStep] = w->singleStep();
|
||||||
|
if (!is_int) ret[Attribute::decimals] = w->decimals();
|
||||||
|
}
|
||||||
|
} break;
|
||||||
|
case tEvalSpinBox: {
|
||||||
|
auto * w = qobject_cast<EvalSpinBox *>(widget);
|
||||||
|
if (w) {
|
||||||
|
ret[Attribute::expression] = Q2PIString(w->expression());
|
||||||
|
}
|
||||||
|
} break;
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
PIVariantMap PIVariantEditors::NumberBase::defaultAttributes() {
|
||||||
return {
|
return {
|
||||||
|
{Attribute::widgetType, createTypes() },
|
||||||
{Attribute::minimum, -std::numeric_limits<int>::max()},
|
{Attribute::minimum, -std::numeric_limits<int>::max()},
|
||||||
{Attribute::maximum, std::numeric_limits<int>::max() },
|
{Attribute::maximum, std::numeric_limits<int>::max() },
|
||||||
{Attribute::singleStep, 1. },
|
{Attribute::singleStep, 1. },
|
||||||
|
{Attribute::decimals, 3 },
|
||||||
{Attribute::prefix, "" },
|
{Attribute::prefix, "" },
|
||||||
{Attribute::suffix, "" },
|
{Attribute::suffix, "" },
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void PIVariantEditors::Int::retranslate() {
|
PIVariantTypes::Enum PIVariantEditors::NumberBase::createTypes() {
|
||||||
widget->setPrefix(PIVariantEditorBase::vtTr(prefix));
|
PIVariantTypes::Enum ret;
|
||||||
widget->setSuffix(PIVariantEditorBase::vtTr(suffix));
|
ret << PIVariantTypes::Enumerator(tSpinBox, "spin box") << PIVariantTypes::Enumerator(tSlider, "slider")
|
||||||
|
<< PIVariantTypes::Enumerator(tSpinSlider, "spin-slider") << PIVariantTypes::Enumerator(tEvalSpinBox, "eval spin box")
|
||||||
|
<< PIVariantTypes::Enumerator(tScrollSpinBox, "scroll spin box");
|
||||||
|
ret.selectValue(tSpinBox);
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void PIVariantEditors::Int::applyAttributes(const PIVariantMap & a) {
|
void PIVariantEditors::NumberBase::retranslate() {
|
||||||
prefix = a.value(Attribute::prefix).toString();
|
switch (type) {
|
||||||
suffix = a.value(Attribute::suffix).toString();
|
case tSpinBox: {
|
||||||
widget->setRange(a.value(Attribute::minimum, widget->minimum()).toInt(), a.value(Attribute::maximum, widget->maximum()).toInt());
|
auto * w = qobject_cast<QDoubleSpinBox *>(widget);
|
||||||
widget->setSingleStep(a.value(Attribute::singleStep, widget->singleStep()).toInt());
|
if (!w) return;
|
||||||
widget->setReadOnly(a.value(Attribute::readOnly, widget->isReadOnly()).toBool());
|
w->setPrefix(PIVariantEditorBase::vtTr(prefix));
|
||||||
retranslate();
|
w->setSuffix(PIVariantEditorBase::vtTr(suffix));
|
||||||
|
} break;
|
||||||
|
case tSpinSlider: {
|
||||||
|
auto * w = qobject_cast<SpinSlider *>(widget);
|
||||||
|
if (!w) return;
|
||||||
|
w->setPrefix(PIVariantEditorBase::vtTr(prefix));
|
||||||
|
w->setSuffix(PIVariantEditorBase::vtTr(suffix));
|
||||||
|
} break;
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void PIVariantEditors::NumberBase::applyAttributes(const PIVariantMap & a) {
|
||||||
|
Type new_type = static_cast<Type>(a.value(Attribute::widgetType).toEnum().selectedValue());
|
||||||
|
if (type != new_type) {
|
||||||
|
type = new_type;
|
||||||
|
if (widget) delete widget;
|
||||||
|
widget = nullptr;
|
||||||
|
// clang-format off
|
||||||
|
switch (type) {
|
||||||
|
case tSpinBox : widget = new QDoubleSpinBox(); break;
|
||||||
|
case tSlider : widget = new QSlider (Qt::Horizontal); break;
|
||||||
|
case tSpinSlider : widget = new SpinSlider (); break;
|
||||||
|
case tEvalSpinBox : widget = new EvalSpinBox (); break;
|
||||||
|
case tScrollSpinBox: widget = new ScrollSpinBox (); break;
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
|
// clang-format on
|
||||||
|
if (!widget) return;
|
||||||
|
widget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
|
||||||
|
layout()->addWidget(widget);
|
||||||
|
}
|
||||||
|
prefix = a.value(Attribute::prefix).toString();
|
||||||
|
suffix = a.value(Attribute::suffix).toString();
|
||||||
|
double min = a.value(Attribute::minimum).toDouble();
|
||||||
|
double max = a.value(Attribute::maximum).toDouble();
|
||||||
|
double step = a.value(Attribute::singleStep).toDouble();
|
||||||
|
int dec = is_int ? 0 : a.value(Attribute::decimals).toInt();
|
||||||
|
switch (type) {
|
||||||
|
case tSpinBox: {
|
||||||
|
auto * w = qobject_cast<QDoubleSpinBox *>(widget);
|
||||||
|
if (!w) return;
|
||||||
|
w->setPrefix(PIVariantEditorBase::vtTr(prefix));
|
||||||
|
w->setSuffix(PIVariantEditorBase::vtTr(suffix));
|
||||||
|
w->setRange(min, max);
|
||||||
|
w->setSingleStep(step);
|
||||||
|
w->setDecimals(dec);
|
||||||
|
} break;
|
||||||
|
case tSlider: {
|
||||||
|
auto * w = qobject_cast<QSlider *>(widget);
|
||||||
|
if (!w) return;
|
||||||
|
w->setTickInterval(piRoundd(piMaxd(1., (max - min) / 100.)));
|
||||||
|
w->setRange(min, max);
|
||||||
|
w->setSingleStep(step);
|
||||||
|
} break;
|
||||||
|
case tSpinSlider: {
|
||||||
|
auto * w = qobject_cast<SpinSlider *>(widget);
|
||||||
|
if (!w) return;
|
||||||
|
w->setPrefix(PIVariantEditorBase::vtTr(prefix));
|
||||||
|
w->setSuffix(PIVariantEditorBase::vtTr(suffix));
|
||||||
|
w->setMinimum(min);
|
||||||
|
w->setMaximum(max);
|
||||||
|
w->setSingleStep(step);
|
||||||
|
w->setDecimals(dec);
|
||||||
|
} break;
|
||||||
|
case tEvalSpinBox: {
|
||||||
|
auto * w = qobject_cast<EvalSpinBox *>(widget);
|
||||||
|
if (!w) return;
|
||||||
|
w->setExpression(PI2QString(a.value(Attribute::expression).toString()));
|
||||||
|
} break;
|
||||||
|
case tScrollSpinBox: {
|
||||||
|
auto * w = qobject_cast<ScrollSpinBox *>(widget);
|
||||||
|
if (!w) return;
|
||||||
|
} break;
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// PIVariantEditors::Int
|
||||||
|
|
||||||
|
|
||||||
// PIVariantEditors::Double
|
// PIVariantEditors::Double
|
||||||
|
|
||||||
PIVariantMap PIVariantEditors::Double::attributes() const {
|
|
||||||
return {
|
|
||||||
{Attribute::minimum, widget->minimum() },
|
|
||||||
{Attribute::maximum, widget->maximum() },
|
|
||||||
{Attribute::singleStep, widget->singleStep()},
|
|
||||||
{Attribute::decimals, widget->decimals() },
|
|
||||||
{Attribute::prefix, prefix },
|
|
||||||
{Attribute::suffix, suffix },
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
PIVariantMap PIVariantEditors::Double::defaultAttributes() {
|
|
||||||
return {
|
|
||||||
{Attribute::minimum, -std::numeric_limits<double>::max()},
|
|
||||||
{Attribute::maximum, std::numeric_limits<double>::max() },
|
|
||||||
{Attribute::singleStep, 1. },
|
|
||||||
{Attribute::decimals, 3 },
|
|
||||||
{Attribute::prefix, "" },
|
|
||||||
{Attribute::suffix, "" },
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void PIVariantEditors::Double::retranslate() {
|
|
||||||
widget->setPrefix(PIVariantEditorBase::vtTr(prefix));
|
|
||||||
widget->setSuffix(PIVariantEditorBase::vtTr(suffix));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void PIVariantEditors::Double::applyAttributes(const PIVariantMap & a) {
|
|
||||||
prefix = a.value(Attribute::prefix).toString();
|
|
||||||
suffix = a.value(Attribute::suffix).toString();
|
|
||||||
widget->setRange(a.value(Attribute::minimum, widget->minimum()).toDouble(), a.value(Attribute::maximum, widget->maximum()).toDouble());
|
|
||||||
widget->setSingleStep(a.value(Attribute::singleStep, widget->singleStep()).toDouble());
|
|
||||||
widget->setDecimals(a.value(Attribute::decimals, widget->decimals()).toInt());
|
|
||||||
widget->setReadOnly(a.value(Attribute::readOnly, widget->isReadOnly()).toBool());
|
|
||||||
retranslate();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// PIVariantEditors::String
|
// PIVariantEditors::String
|
||||||
|
|
||||||
|
|||||||
@@ -36,6 +36,35 @@
|
|||||||
namespace PIVariantEditors {
|
namespace PIVariantEditors {
|
||||||
|
|
||||||
|
|
||||||
|
class QAD_PIQT_UTILS_EXPORT NumberBase: public PIVariantEditorBase {
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
public:
|
||||||
|
NumberBase();
|
||||||
|
void setValueNumeric(double v);
|
||||||
|
double valueNumeric() const;
|
||||||
|
PIVariantMap attributes() const override;
|
||||||
|
static PIVariantMap defaultAttributes();
|
||||||
|
static PIVariantTypes::Enum createTypes();
|
||||||
|
void retranslate() override;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void applyAttributes(const PIVariantMap & a) override;
|
||||||
|
enum Type {
|
||||||
|
tInvalid,
|
||||||
|
tSpinBox,
|
||||||
|
tSlider,
|
||||||
|
tSpinSlider,
|
||||||
|
tEvalSpinBox,
|
||||||
|
tScrollSpinBox,
|
||||||
|
};
|
||||||
|
Type type = tInvalid;
|
||||||
|
PIString prefix, suffix;
|
||||||
|
QWidget * widget = nullptr;
|
||||||
|
bool is_int = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
class QAD_PIQT_UTILS_EXPORT Bool: public PIVariantEditorBase {
|
class QAD_PIQT_UTILS_EXPORT Bool: public PIVariantEditorBase {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
@@ -52,47 +81,29 @@ private:
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
class QAD_PIQT_UTILS_EXPORT Int: public PIVariantEditorBase {
|
class QAD_PIQT_UTILS_EXPORT Int: public NumberBase {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Int() {
|
Int(): NumberBase() {
|
||||||
widget = new QSpinBox();
|
is_int = true;
|
||||||
widget->setRange(-std::numeric_limits<int>::max(), std::numeric_limits<int>::max());
|
applyAttributes(NumberBase::defaultAttributes());
|
||||||
layout()->addWidget(widget);
|
|
||||||
}
|
}
|
||||||
void setValue(const PIVariant & v) override { widget->setValue(v.toInt()); }
|
void setValue(const PIVariant & v) override { setValueNumeric(v.toInt()); }
|
||||||
PIVariant value() const override { return widget->value(); }
|
PIVariant value() const override { return piRoundd(valueNumeric()); }
|
||||||
PIVariantMap attributes() const override;
|
|
||||||
static PIVariantMap defaultAttributes();
|
|
||||||
void retranslate();
|
|
||||||
|
|
||||||
private:
|
|
||||||
void applyAttributes(const PIVariantMap & a) override;
|
|
||||||
PIString prefix, suffix;
|
|
||||||
QSpinBox * widget;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
class QAD_PIQT_UTILS_EXPORT Double: public PIVariantEditorBase {
|
class QAD_PIQT_UTILS_EXPORT Double: public NumberBase {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Double() {
|
Double(): NumberBase() {
|
||||||
widget = new QDoubleSpinBox();
|
is_int = false;
|
||||||
widget->setRange(-std::numeric_limits<float>::max(), std::numeric_limits<float>::max());
|
applyAttributes(NumberBase::defaultAttributes());
|
||||||
layout()->addWidget(widget);
|
|
||||||
}
|
}
|
||||||
void setValue(const PIVariant & v) override { widget->setValue(v.toDouble()); }
|
void setValue(const PIVariant & v) override { setValueNumeric(v.toDouble()); }
|
||||||
PIVariant value() const override { return widget->value(); }
|
PIVariant value() const override { return valueNumeric(); }
|
||||||
PIVariantMap attributes() const override;
|
|
||||||
static PIVariantMap defaultAttributes();
|
|
||||||
void retranslate();
|
|
||||||
|
|
||||||
private:
|
|
||||||
void applyAttributes(const PIVariantMap & a) override;
|
|
||||||
PIString prefix, suffix;
|
|
||||||
QDoubleSpinBox * widget;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user