Skip to content

Commit

Permalink
add posibility to use a unit schema other then the system schema to r…
Browse files Browse the repository at this point in the history
…epresent a quantity
  • Loading branch information
wwmayer committed Dec 21, 2019
1 parent 04b54cd commit 98dfe7c
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 6 deletions.
5 changes: 5 additions & 0 deletions src/Base/Quantity.cpp
Expand Up @@ -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
{
Expand Down
4 changes: 3 additions & 1 deletion src/Base/Quantity.h
Expand Up @@ -35,6 +35,7 @@
#endif

namespace Base {
class UnitsSchema;

struct BaseExport QuantityFormat {
enum NumberOption {
Expand Down Expand Up @@ -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);

Expand Down
1 change: 0 additions & 1 deletion src/Base/UnitsApi.h
Expand Up @@ -86,7 +86,6 @@ class BaseExport UnitsApi

static double defaultFactor;

protected:
/// return an instance of the given enum value
static UnitsSchemaPtr createSchema(UnitSystem s);

Expand Down
47 changes: 43 additions & 4 deletions src/Gui/QuantitySpinBox.cpp
Expand Up @@ -231,6 +231,7 @@ class QuantitySpinBoxPrivate
double maximum;
double minimum;
double singleStep;
std::unique_ptr<Base::UnitsSchema> scheme;
};
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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());
}
Expand Down
10 changes: 10 additions & 0 deletions src/Gui/QuantitySpinBox.h
Expand Up @@ -25,6 +25,7 @@
#define GUI_QUANTITYSPINBOX_H

#include <QAbstractSpinBox>
#include <Base/UnitsSchema.h>
#include <Gui/MetaTypes.h>
#include "ExpressionBinding.h"

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 98dfe7c

Please sign in to comment.