From 98dfe7c3870a6ec15bfef98f53ed31a79f452d02 Mon Sep 17 00:00:00 2001 From: wmayer Date: Sat, 21 Dec 2019 13:35:14 +0100 Subject: [PATCH] add posibility to use a unit schema other then the system schema to represent a quantity --- src/Base/Quantity.cpp | 5 ++++ src/Base/Quantity.h | 4 +++- src/Base/UnitsApi.h | 1 - src/Gui/QuantitySpinBox.cpp | 47 +++++++++++++++++++++++++++++++++---- src/Gui/QuantitySpinBox.h | 10 ++++++++ 5 files changed, 61 insertions(+), 6 deletions(-) diff --git a/src/Base/Quantity.cpp b/src/Base/Quantity.cpp index 1c2edaf97108..f647151b5ce9 100644 --- a/src/Base/Quantity.cpp +++ b/src/Base/Quantity.cpp @@ -213,6 +213,11 @@ QString Quantity::getUserString(double& factor, QString& unitString) const return Base::UnitsApi::schemaTranslate(*this, factor, unitString); } +QString Quantity::getUserString(UnitsSchema* schema, double &factor, QString &unitString) const +{ + return schema->schemaTranslate(*this, factor, unitString); +} + /// true if it has a number without a unit bool Quantity::isDimensionless(void)const { diff --git a/src/Base/Quantity.h b/src/Base/Quantity.h index 851142c8025a..2ae0397d9dfd 100644 --- a/src/Base/Quantity.h +++ b/src/Base/Quantity.h @@ -35,6 +35,7 @@ #endif namespace Base { +class UnitsSchema; struct BaseExport QuantityFormat { enum NumberOption { @@ -143,12 +144,13 @@ class BaseExport Quantity _Format = f; } /// transfer to user preferred unit/potence - QString getUserString(double &factor, QString &unitString)const; + QString getUserString(double &factor, QString &unitString) const; QString getUserString(void) const { // to satisfy GCC double dummy1; QString dummy2; return getUserString(dummy1,dummy2); } + QString getUserString(UnitsSchema* schema, double &factor, QString &unitString) const; static Quantity parse(const QString &string); diff --git a/src/Base/UnitsApi.h b/src/Base/UnitsApi.h index e3fb95869354..24f5dd5cc625 100644 --- a/src/Base/UnitsApi.h +++ b/src/Base/UnitsApi.h @@ -86,7 +86,6 @@ class BaseExport UnitsApi static double defaultFactor; -protected: /// return an instance of the given enum value static UnitsSchemaPtr createSchema(UnitSystem s); diff --git a/src/Gui/QuantitySpinBox.cpp b/src/Gui/QuantitySpinBox.cpp index 4df87365cfbd..cfbdc0b19665 100644 --- a/src/Gui/QuantitySpinBox.cpp +++ b/src/Gui/QuantitySpinBox.cpp @@ -231,6 +231,7 @@ class QuantitySpinBoxPrivate double maximum; double minimum; double singleStep; + std::unique_ptr scheme; }; } @@ -373,7 +374,7 @@ void Gui::QuantitySpinBox::onChange() std::stringstream s; s << value->getValue(); - lineEdit()->setText(value->getQuantity().getUserString()); + lineEdit()->setText(getUserString(value->getQuantity())); setReadOnly(true); QPixmap pixmap = getIcon(":/icons/bound-expression.svg", QSize(iconHeight, iconHeight)); iconLabel->setPixmap(pixmap); @@ -488,7 +489,7 @@ void QuantitySpinBox::updateText(const Quantity &quant) Q_D(QuantitySpinBox); double dFactor; - QString txt = quant.getUserString(dFactor,d->unitStr); + QString txt = getUserString(quant, dFactor, d->unitStr); d->unitValue = quant.getValue()/dFactor; lineEdit()->setText(txt); } @@ -566,7 +567,7 @@ void QuantitySpinBox::userInput(const QString & text) } double factor; - res.getUserString(factor,d->unitStr); + getUserString(res, factor, d->unitStr); d->unitValue = res.getValue()/factor; d->quantity = res; @@ -697,6 +698,44 @@ void QuantitySpinBox::setDecimals(int v) updateText(d->quantity); } +void QuantitySpinBox::setSchema(const Base::UnitSystem& s) +{ + Q_D(QuantitySpinBox); + d->scheme = Base::UnitsApi::createSchema(s); + updateText(d->quantity); +} + +void QuantitySpinBox::clearSchema() +{ + Q_D(QuantitySpinBox); + d->scheme = nullptr; + updateText(d->quantity); +} + +QString QuantitySpinBox::getUserString(const Base::Quantity& val, double& factor, QString& unitString) const +{ + Q_D(const QuantitySpinBox); + if (d->scheme) { + return val.getUserString(d->scheme.get(), factor, unitString); + } + else { + return val.getUserString(factor, unitString); + } +} + +QString QuantitySpinBox::getUserString(const Base::Quantity& val) const +{ + Q_D(const QuantitySpinBox); + if (d->scheme) { + double factor; + QString unitString; + return val.getUserString(d->scheme.get(), factor, unitString); + } + else { + return val.getUserString(); + } +} + QAbstractSpinBox::StepEnabled QuantitySpinBox::stepEnabled() const { Q_D(const QuantitySpinBox); @@ -827,7 +866,7 @@ QString QuantitySpinBox::textFromValue(const Base::Quantity& value) const { double factor; QString unitStr; - QString str = value.getUserString(factor, unitStr); + QString str = getUserString(value, factor, unitStr); if (qAbs(value.getValue()) >= 1000.0) { str.remove(locale().groupSeparator()); } diff --git a/src/Gui/QuantitySpinBox.h b/src/Gui/QuantitySpinBox.h index 23714bfb99cb..f5ba9e8d62eb 100644 --- a/src/Gui/QuantitySpinBox.h +++ b/src/Gui/QuantitySpinBox.h @@ -25,6 +25,7 @@ #define GUI_QUANTITYSPINBOX_H #include +#include #include #include "ExpressionBinding.h" @@ -96,6 +97,13 @@ class GuiExport QuantitySpinBox : public QAbstractSpinBox, public ExpressionBind /// Sets the number of decimals void setDecimals(int v); + /// Sets a specific unit schema to handle quantities. + /// The system-wide schema won't be used any more. + void setSchema(const Base::UnitSystem& s); + + /// Clears the schemaand again use the system-wide schema. + void clearSchema(); + /// Gets the path of the bound property QString boundToName() const; /// Sets the path of the bound property @@ -146,6 +154,8 @@ protected Q_SLOTS: private: void updateText(const Base::Quantity&); + QString getUserString(const Base::Quantity& val, double& factor, QString& unitString) const; + QString getUserString(const Base::Quantity& val) const; Q_SIGNALS: /** Gets emitted if the user has entered a VALID input