add class QuantityFormat to control user string of Quantity
do some code refactoration to reduce duplicate code
This commit is contained in:
parent
e0c69c8afe
commit
f39b89a54d
|
@ -41,6 +41,14 @@
|
||||||
|
|
||||||
using namespace Base;
|
using namespace Base;
|
||||||
|
|
||||||
|
QuantityFormat::QuantityFormat()
|
||||||
|
: option(static_cast<NumberOption>(OmitGroupSeparator | RejectGroupSeparator))
|
||||||
|
, format(Fixed)
|
||||||
|
, precision(UnitsApi::getDecimals())
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
Quantity::Quantity()
|
Quantity::Quantity()
|
||||||
{
|
{
|
||||||
|
@ -58,14 +66,11 @@ Quantity::Quantity(double Value, const Unit& unit)
|
||||||
this->_Value = Value;
|
this->_Value = Value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
double Quantity::getValueAs(const Quantity &q)const
|
double Quantity::getValueAs(const Quantity &q)const
|
||||||
{
|
{
|
||||||
return _Value/q.getValue();
|
return _Value/q.getValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
bool Quantity::operator ==(const Quantity& that) const
|
bool Quantity::operator ==(const Quantity& that) const
|
||||||
{
|
{
|
||||||
return (this->_Value == that._Value) && (this->_Unit == that._Unit) ;
|
return (this->_Value == that._Value) && (this->_Unit == that._Unit) ;
|
||||||
|
@ -73,7 +78,7 @@ bool Quantity::operator ==(const Quantity& that) const
|
||||||
|
|
||||||
bool Quantity::operator <(const Quantity& that) const
|
bool Quantity::operator <(const Quantity& that) const
|
||||||
{
|
{
|
||||||
if(this->_Unit != that._Unit)
|
if (this->_Unit != that._Unit)
|
||||||
throw Base::Exception("Quantity::operator <(): quantities need to have same unit to compare");
|
throw Base::Exception("Quantity::operator <(): quantities need to have same unit to compare");
|
||||||
|
|
||||||
return (this->_Value < that._Value) ;
|
return (this->_Value < that._Value) ;
|
||||||
|
@ -81,7 +86,7 @@ bool Quantity::operator <(const Quantity& that) const
|
||||||
|
|
||||||
bool Quantity::operator >(const Quantity& that) const
|
bool Quantity::operator >(const Quantity& that) const
|
||||||
{
|
{
|
||||||
if(this->_Unit != that._Unit)
|
if (this->_Unit != that._Unit)
|
||||||
throw Base::Exception("Quantity::operator >(): quantities need to have same unit to compare");
|
throw Base::Exception("Quantity::operator >(): quantities need to have same unit to compare");
|
||||||
|
|
||||||
return (this->_Value > that._Value) ;
|
return (this->_Value > that._Value) ;
|
||||||
|
@ -99,7 +104,7 @@ Quantity Quantity::operator /(const Quantity &p) const
|
||||||
|
|
||||||
Quantity Quantity::pow(const Quantity &p) const
|
Quantity Quantity::pow(const Quantity &p) const
|
||||||
{
|
{
|
||||||
if(!p._Unit.isEmpty())
|
if (!p._Unit.isEmpty())
|
||||||
throw Base::Exception("Quantity::pow(): exponent must not have a unit");
|
throw Base::Exception("Quantity::pow(): exponent must not have a unit");
|
||||||
return Quantity(
|
return Quantity(
|
||||||
std::pow(this->_Value, p._Value),
|
std::pow(this->_Value, p._Value),
|
||||||
|
@ -109,14 +114,14 @@ Quantity Quantity::pow(const Quantity &p) const
|
||||||
|
|
||||||
Quantity Quantity::operator +(const Quantity &p) const
|
Quantity Quantity::operator +(const Quantity &p) const
|
||||||
{
|
{
|
||||||
if(this->_Unit != p._Unit)
|
if (this->_Unit != p._Unit)
|
||||||
throw Base::Exception("Quantity::operator +(): Unit mismatch in plus operation");
|
throw Base::Exception("Quantity::operator +(): Unit mismatch in plus operation");
|
||||||
return Quantity(this->_Value + p._Value,this->_Unit);
|
return Quantity(this->_Value + p._Value,this->_Unit);
|
||||||
}
|
}
|
||||||
|
|
||||||
Quantity& Quantity::operator +=(const Quantity &p)
|
Quantity& Quantity::operator +=(const Quantity &p)
|
||||||
{
|
{
|
||||||
if(this->_Unit != p._Unit)
|
if (this->_Unit != p._Unit)
|
||||||
throw Base::Exception("Quantity::operator +=(): Unit mismatch in plus operation");
|
throw Base::Exception("Quantity::operator +=(): Unit mismatch in plus operation");
|
||||||
|
|
||||||
_Value += p._Value;
|
_Value += p._Value;
|
||||||
|
@ -126,14 +131,14 @@ Quantity& Quantity::operator +=(const Quantity &p)
|
||||||
|
|
||||||
Quantity Quantity::operator -(const Quantity &p) const
|
Quantity Quantity::operator -(const Quantity &p) const
|
||||||
{
|
{
|
||||||
if(this->_Unit != p._Unit)
|
if (this->_Unit != p._Unit)
|
||||||
throw Base::Exception("Quantity::operator +(): Unit mismatch in minus operation");
|
throw Base::Exception("Quantity::operator +(): Unit mismatch in minus operation");
|
||||||
return Quantity(this->_Value - p._Value,this->_Unit);
|
return Quantity(this->_Value - p._Value,this->_Unit);
|
||||||
}
|
}
|
||||||
|
|
||||||
Quantity& Quantity::operator -=(const Quantity &p)
|
Quantity& Quantity::operator -=(const Quantity &p)
|
||||||
{
|
{
|
||||||
if(this->_Unit != p._Unit)
|
if (this->_Unit != p._Unit)
|
||||||
throw Base::Exception("Quantity::operator -=(): Unit mismatch in minus operation");
|
throw Base::Exception("Quantity::operator -=(): Unit mismatch in minus operation");
|
||||||
|
|
||||||
_Value -= p._Value;
|
_Value -= p._Value;
|
||||||
|
@ -150,12 +155,13 @@ Quantity& Quantity::operator = (const Quantity &New)
|
||||||
{
|
{
|
||||||
this->_Value = New._Value;
|
this->_Value = New._Value;
|
||||||
this->_Unit = New._Unit;
|
this->_Unit = New._Unit;
|
||||||
|
this->_Format = New._Format;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
QString Quantity::getUserString(double &factor,QString &unitString)const
|
QString Quantity::getUserString(double& factor, QString& unitString) const
|
||||||
{
|
{
|
||||||
return Base::UnitsApi::schemaTranslate(*this,factor,unitString);
|
return Base::UnitsApi::schemaTranslate(*this, factor, unitString);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// true if it has a number without a unit
|
/// true if it has a number without a unit
|
||||||
|
@ -163,6 +169,7 @@ bool Quantity::isDimensionless(void)const
|
||||||
{
|
{
|
||||||
return _Value != DOUBLE_MIN && _Unit.isEmpty();
|
return _Value != DOUBLE_MIN && _Unit.isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
// true if it has a number and a valid unit
|
// true if it has a number and a valid unit
|
||||||
bool Quantity::isQuantity(void)const
|
bool Quantity::isQuantity(void)const
|
||||||
{
|
{
|
||||||
|
|
|
@ -36,6 +36,35 @@
|
||||||
|
|
||||||
namespace Base {
|
namespace Base {
|
||||||
|
|
||||||
|
struct QuantityFormat {
|
||||||
|
enum NumberOption {
|
||||||
|
None = 0x00,
|
||||||
|
OmitGroupSeparator = 0x01,
|
||||||
|
RejectGroupSeparator = 0x02
|
||||||
|
};
|
||||||
|
enum NumberFormat {
|
||||||
|
Default = 0,
|
||||||
|
Fixed = 1,
|
||||||
|
Scientific = 2
|
||||||
|
};
|
||||||
|
|
||||||
|
NumberOption option;
|
||||||
|
NumberFormat format;
|
||||||
|
int precision;
|
||||||
|
|
||||||
|
QuantityFormat();
|
||||||
|
inline char toFormat() const {
|
||||||
|
switch (format) {
|
||||||
|
case Fixed:
|
||||||
|
return 'f';
|
||||||
|
case Scientific:
|
||||||
|
return 'e';
|
||||||
|
default:
|
||||||
|
return 'g';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The Quantity class.
|
* The Quantity class.
|
||||||
*/
|
*/
|
||||||
|
@ -65,9 +94,15 @@ public:
|
||||||
Quantity pow(const Quantity&)const;
|
Quantity pow(const Quantity&)const;
|
||||||
//@}
|
//@}
|
||||||
|
|
||||||
|
const QuantityFormat& getFormat() const {
|
||||||
|
return _Format;
|
||||||
|
}
|
||||||
|
void setFormat(const QuantityFormat& f) {
|
||||||
|
_Format = f;
|
||||||
|
}
|
||||||
/// transfer to user prefered unit/potence
|
/// transfer to user prefered unit/potence
|
||||||
QString getUserString(double &factor,QString &unitString)const;
|
QString getUserString(double &factor, QString &unitString)const;
|
||||||
QString getUserString(void)const{ // to satisfy GCC
|
QString getUserString(void) const { // to satisfy GCC
|
||||||
double dummy1;
|
double dummy1;
|
||||||
QString dummy2;
|
QString dummy2;
|
||||||
return getUserString(dummy1,dummy2);
|
return getUserString(dummy1,dummy2);
|
||||||
|
@ -178,15 +213,13 @@ public:
|
||||||
static Quantity Degree;
|
static Quantity Degree;
|
||||||
static Quantity Radian;
|
static Quantity Radian;
|
||||||
static Quantity Gon;
|
static Quantity Gon;
|
||||||
|
|
||||||
|
|
||||||
//@}
|
//@}
|
||||||
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
double _Value;
|
double _Value;
|
||||||
Unit _Unit;
|
Unit _Unit;
|
||||||
|
QuantityFormat _Format;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace Base
|
} // namespace Base
|
||||||
|
|
|
@ -116,7 +116,7 @@ void UnitsApi::setSchema(UnitSystem s)
|
||||||
|
|
||||||
// === static translation methodes ==========================================
|
// === static translation methodes ==========================================
|
||||||
|
|
||||||
QString UnitsApi::schemaTranslate(Base::Quantity quant,double &factor,QString &unitString)
|
QString UnitsApi::schemaTranslate(const Base::Quantity& quant, double &factor, QString &unitString)
|
||||||
{
|
{
|
||||||
return UserPrefSystem->schemaTranslate(quant,factor,unitString);
|
return UserPrefSystem->schemaTranslate(quant,factor,unitString);
|
||||||
}
|
}
|
||||||
|
|
|
@ -56,11 +56,11 @@ public:
|
||||||
/// return the active schema
|
/// return the active schema
|
||||||
static UnitSystem getSchema(void){return actSystem;}
|
static UnitSystem getSchema(void){return actSystem;}
|
||||||
|
|
||||||
static QString schemaTranslate(Base::Quantity quant,double &factor,QString &unitString);
|
static QString schemaTranslate(const Base::Quantity& quant, double &factor, QString &unitString);
|
||||||
static QString schemaTranslate(Base::Quantity quant){ // to satisfy GCC
|
static QString schemaTranslate(const Base::Quantity& quant) { // to satisfy GCC
|
||||||
double dummy1;
|
double dummy1;
|
||||||
QString dummy2;
|
QString dummy2;
|
||||||
return UnitsApi::schemaTranslate(quant,dummy1,dummy2);
|
return UnitsApi::schemaTranslate(quant, dummy1, dummy2);
|
||||||
}
|
}
|
||||||
/// generate a value for a quantity with default user prefered system
|
/// generate a value for a quantity with default user prefered system
|
||||||
static double toDbl(PyObject *ArgObj,const Base::Unit &u=Base::Unit());
|
static double toDbl(PyObject *ArgObj,const Base::Unit &u=Base::Unit());
|
||||||
|
|
|
@ -26,7 +26,25 @@
|
||||||
# include <unistd.h>
|
# include <unistd.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include <QString>
|
||||||
|
#include <QLocale>
|
||||||
|
|
||||||
#include "Exception.h"
|
#include "Exception.h"
|
||||||
#include "UnitsApi.h"
|
#include "UnitsApi.h"
|
||||||
#include "UnitsSchema.h"
|
#include "UnitsSchema.h"
|
||||||
|
|
||||||
|
using namespace Base;
|
||||||
|
|
||||||
|
QString UnitsSchema::toLocale(const Base::Quantity& quant, double factor, const QString& unitString) const
|
||||||
|
{
|
||||||
|
//return QString::fromUtf8("%L1 %2").arg(quant.getValue() / factor).arg(unitString);
|
||||||
|
QLocale Lc = QLocale::system();
|
||||||
|
const QuantityFormat& format = quant.getFormat();
|
||||||
|
if (format.option != QuantityFormat::None) {
|
||||||
|
uint opt = static_cast<uint>(format.option);
|
||||||
|
Lc.setNumberOptions(static_cast<QLocale::NumberOptions>(opt));
|
||||||
|
}
|
||||||
|
|
||||||
|
QString Ln = Lc.toString((quant.getValue() / factor), format.toFormat(), format.precision);
|
||||||
|
return QString::fromUtf8("%1 %2").arg(Ln).arg(unitString);
|
||||||
|
}
|
||||||
|
|
|
@ -61,6 +61,8 @@ public:
|
||||||
|
|
||||||
/// This method translates the quantity in a string as the user may expect it.
|
/// This method translates the quantity in a string as the user may expect it.
|
||||||
virtual QString schemaTranslate(const Base::Quantity& quant, double &factor, QString &unitString)=0;
|
virtual QString schemaTranslate(const Base::Quantity& quant, double &factor, QString &unitString)=0;
|
||||||
|
|
||||||
|
QString toLocale(const Base::Quantity& quant, double factor, const QString& unitString) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -60,9 +60,5 @@ QString UnitsSchemaCentimeters::schemaTranslate(const Base::Quantity& quant, dou
|
||||||
factor = 1.0;
|
factor = 1.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
//return QString::fromUtf8("%L1 %2").arg(quant.getValue() / factor).arg(unitString);
|
return toLocale(quant, factor, unitString);
|
||||||
QLocale Lc = QLocale::system();
|
|
||||||
Lc.setNumberOptions(Lc.OmitGroupSeparator | Lc.RejectGroupSeparator);
|
|
||||||
QString Ln = Lc.toString((quant.getValue() / factor), 'f', Base::UnitsApi::getDecimals());
|
|
||||||
return QString::fromUtf8("%1 %2").arg(Ln).arg(unitString);
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -133,11 +133,7 @@ QString UnitsSchemaImperial1::schemaTranslate(const Quantity &quant, double &fac
|
||||||
factor = 1.0;
|
factor = 1.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
//return QString::fromLatin1("%L1 %2").arg(quant.getValue() / factor).arg(unitString);
|
return toLocale(quant, factor, unitString);
|
||||||
QLocale Lc = QLocale::system();
|
|
||||||
Lc.setNumberOptions(Lc.OmitGroupSeparator | Lc.RejectGroupSeparator);
|
|
||||||
QString Ln = Lc.toString((quant.getValue() / factor), 'f', Base::UnitsApi::getDecimals());
|
|
||||||
return QString::fromUtf8("%1 %2").arg(Ln).arg(unitString);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QString UnitsSchemaImperialDecimal::schemaTranslate(const Base::Quantity& quant, double &factor, QString &unitString)
|
QString UnitsSchemaImperialDecimal::schemaTranslate(const Base::Quantity& quant, double &factor, QString &unitString)
|
||||||
|
@ -198,11 +194,7 @@ QString UnitsSchemaImperialDecimal::schemaTranslate(const Base::Quantity& quant,
|
||||||
factor = 1.0;
|
factor = 1.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
//return QString::fromLatin1("%L1 %2").arg(quant.getValue() / factor).arg(unitString);
|
return toLocale(quant, factor, unitString);
|
||||||
QLocale Lc = QLocale::system();
|
|
||||||
Lc.setNumberOptions(Lc.OmitGroupSeparator | Lc.RejectGroupSeparator);
|
|
||||||
QString Ln = Lc.toString((quant.getValue() / factor), 'f', Base::UnitsApi::getDecimals());
|
|
||||||
return QString::fromUtf8("%1 %2").arg(Ln).arg(unitString);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QString UnitsSchemaImperialBuilding::schemaTranslate(const Quantity &quant, double &factor, QString &unitString)
|
QString UnitsSchemaImperialBuilding::schemaTranslate(const Quantity &quant, double &factor, QString &unitString)
|
||||||
|
@ -279,8 +271,5 @@ QString UnitsSchemaImperialBuilding::schemaTranslate(const Quantity &quant, doub
|
||||||
factor = 1.0;
|
factor = 1.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
QLocale Lc = QLocale::system();
|
return toLocale(quant, factor, unitString);
|
||||||
Lc.setNumberOptions(Lc.OmitGroupSeparator | Lc.RejectGroupSeparator);
|
|
||||||
QString Ln = Lc.toString((quant.getValue() / factor), 'f', Base::UnitsApi::getDecimals());
|
|
||||||
return QString::fromUtf8("%1 %2").arg(Ln).arg(unitString);
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -161,9 +161,5 @@ QString UnitsSchemaInternal::schemaTranslate(const Quantity &quant, double &fact
|
||||||
factor = 1.0;
|
factor = 1.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
//return QString::fromUtf8("%L1 %2").arg(quant.getValue() / factor).arg(unitString);
|
return toLocale(quant, factor, unitString);
|
||||||
QLocale Lc = QLocale::system();
|
|
||||||
Lc.setNumberOptions(Lc.OmitGroupSeparator | Lc.RejectGroupSeparator);
|
|
||||||
QString Ln = Lc.toString((quant.getValue() / factor), 'f', Base::UnitsApi::getDecimals());
|
|
||||||
return QString::fromUtf8("%1 %2").arg(Ln).arg(unitString);
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -162,9 +162,5 @@ QString UnitsSchemaMKS::schemaTranslate(const Quantity &quant, double &factor, Q
|
||||||
factor = 1.0;
|
factor = 1.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
//return QString::fromUtf8("%L1 %2").arg(quant.getValue() / factor).arg(unitString);
|
return toLocale(quant, factor, unitString);
|
||||||
QLocale Lc = QLocale::system();
|
|
||||||
Lc.setNumberOptions(Lc.OmitGroupSeparator | Lc.RejectGroupSeparator);
|
|
||||||
QString Ln = Lc.toString((quant.getValue() / factor), 'f', Base::UnitsApi::getDecimals());
|
|
||||||
return QString::fromUtf8("%1 %2").arg(Ln).arg(unitString);
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user