add posibility to use a unit schema other then the system schema to represent a quantity
This commit is contained in:
@@ -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
|
||||
{
|
||||
|
||||
@@ -35,6 +35,7 @@
|
||||
#endif
|
||||
|
||||
namespace Base {
|
||||
class UnitsSchema;
|
||||
|
||||
struct BaseExport QuantityFormat {
|
||||
enum NumberOption {
|
||||
@@ -143,12 +144,13 @@ public:
|
||||
_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);
|
||||
|
||||
|
||||
@@ -86,7 +86,6 @@ public:
|
||||
|
||||
static double defaultFactor;
|
||||
|
||||
protected:
|
||||
/// return an instance of the given enum value
|
||||
static UnitsSchemaPtr createSchema(UnitSystem s);
|
||||
|
||||
|
||||
@@ -231,6 +231,7 @@ end:
|
||||
double maximum;
|
||||
double minimum;
|
||||
double singleStep;
|
||||
std::unique_ptr<Base::UnitsSchema> 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());
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
#define GUI_QUANTITYSPINBOX_H
|
||||
|
||||
#include <QAbstractSpinBox>
|
||||
#include <Base/UnitsSchema.h>
|
||||
#include <Gui/MetaTypes.h>
|
||||
#include "ExpressionBinding.h"
|
||||
|
||||
@@ -96,6 +97,13 @@ public:
|
||||
/// 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:
|
||||
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user