Base: Units: return std::string
This commit is contained in:
committed by
Yorik van Havre
parent
6b6e1fbd26
commit
0907c7bfda
@@ -253,7 +253,7 @@ QString Quantity::getSafeUserString() const
|
||||
auto feedbackQty = parse(retString);
|
||||
auto feedbackVal = feedbackQty.getValue();
|
||||
if (feedbackVal == 0) {
|
||||
retString = QStringLiteral("%1 %2").arg(this->myValue).arg(this->getUnit().getString());
|
||||
retString = QStringLiteral("%1 %2").arg(this->myValue).arg(QString::fromStdString(this->getUnit().getString()));
|
||||
}
|
||||
}
|
||||
retString =
|
||||
|
||||
@@ -42,7 +42,7 @@ std::string QuantityPy::representation() const
|
||||
Py::Float flt(val);
|
||||
ret << static_cast<std::string>(flt.repr());
|
||||
if (!unit.isEmpty()) {
|
||||
ret << " " << unit.getString().toUtf8().constData();
|
||||
ret << " " << unit.getString();
|
||||
}
|
||||
|
||||
return ret.str();
|
||||
@@ -63,7 +63,7 @@ PyObject* QuantityPy::toStr(PyObject* args)
|
||||
ret.setf(std::ios::fixed, std::ios::floatfield);
|
||||
ret << val;
|
||||
if (!unit.isEmpty()) {
|
||||
ret << " " << unit.getString().toUtf8().constData();
|
||||
ret << " " << unit.getString();
|
||||
}
|
||||
|
||||
return Py_BuildValue("s", ret.str().c_str());
|
||||
|
||||
@@ -125,10 +125,10 @@ Unit::Unit() //NOLINT
|
||||
Val = 0;
|
||||
}
|
||||
|
||||
Unit::Unit(const QString& expr) // NOLINT
|
||||
Unit::Unit(const std::string& expr) // NOLINT
|
||||
{
|
||||
try {
|
||||
*this = Quantity::parse(expr).getUnit();
|
||||
*this = Quantity::parse(QString::fromStdString(expr)).getUnit();
|
||||
}
|
||||
catch (const Base::ParserError&) {
|
||||
Val = 0;
|
||||
@@ -358,7 +358,7 @@ Unit Unit::operator /(const Unit &right) const
|
||||
return result;
|
||||
}
|
||||
|
||||
QString Unit::getString() const
|
||||
std::string Unit::getString() const
|
||||
{
|
||||
if (isEmpty()) {
|
||||
return {};
|
||||
@@ -582,10 +582,10 @@ QString Unit::getString() const
|
||||
}
|
||||
}
|
||||
|
||||
return QString::fromUtf8(ret.str().c_str());
|
||||
return ret.str();
|
||||
}
|
||||
|
||||
QString Unit::getTypeString() const
|
||||
std::string Unit::getTypeString() const
|
||||
{
|
||||
static std::array<std::pair<Unit, std::string>, 55> unitSpecs {{
|
||||
{ Unit::Acceleration, "Acceleration" },
|
||||
@@ -651,9 +651,9 @@ QString Unit::getTypeString() const
|
||||
});
|
||||
|
||||
if (spec == std::end(unitSpecs))
|
||||
return QString();
|
||||
return "";
|
||||
|
||||
return QString::fromStdString(spec->second);
|
||||
return spec->second;
|
||||
}
|
||||
|
||||
// SI base units
|
||||
|
||||
@@ -25,7 +25,7 @@
|
||||
#define BASE_Unit_H
|
||||
|
||||
#include <cstdint>
|
||||
#include <QString>
|
||||
#include <string>
|
||||
#include <FCGlobal.h>
|
||||
|
||||
namespace Base
|
||||
@@ -49,7 +49,7 @@ public:
|
||||
Unit();
|
||||
Unit(const Unit&) = default;
|
||||
Unit(Unit&&) = default;
|
||||
explicit Unit(const QString& expr);
|
||||
explicit Unit(const std::string& expr);
|
||||
/// Destruction
|
||||
~Unit() = default;
|
||||
|
||||
@@ -81,9 +81,9 @@ public:
|
||||
int angle() const;
|
||||
bool isEmpty() const;
|
||||
|
||||
QString getString() const;
|
||||
std::string getString() const;
|
||||
/// get the type as an string such as "Area", "Length" or "Pressure".
|
||||
QString getTypeString() const;
|
||||
std::string getTypeString() const;
|
||||
|
||||
/** Predefined Unit types. */
|
||||
//@{
|
||||
|
||||
@@ -38,7 +38,7 @@ std::string UnitPy::representation() const
|
||||
Unit* self = getUnitPtr();
|
||||
|
||||
ret << "Unit: ";
|
||||
ret << self->getString().toUtf8().constData() << " (";
|
||||
ret << self->getString() << " (";
|
||||
ret << (*self).length() << ",";
|
||||
ret << (*self).mass() << ",";
|
||||
ret << (*self).time() << ",";
|
||||
@@ -48,7 +48,7 @@ std::string UnitPy::representation() const
|
||||
ret << (*self).luminousIntensity() << ",";
|
||||
ret << (*self).angle() << ")";
|
||||
|
||||
std::string type = self->getTypeString().toUtf8().constData();
|
||||
std::string type = self->getTypeString();
|
||||
if (!type.empty()) {
|
||||
ret << " [" << type << "]";
|
||||
}
|
||||
@@ -207,7 +207,7 @@ PyObject* UnitPy::richCompare(PyObject* v, PyObject* w, int op)
|
||||
|
||||
Py::String UnitPy::getType() const
|
||||
{
|
||||
return {getUnitPtr()->getTypeString().toUtf8(), "utf-8"};
|
||||
return {getUnitPtr()->getTypeString(), "utf-8"};
|
||||
}
|
||||
|
||||
Py::Tuple UnitPy::getSignature() const
|
||||
|
||||
@@ -22,12 +22,14 @@
|
||||
|
||||
|
||||
#include "PreCompiled.h"
|
||||
#ifdef __GNUC__
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <locale>
|
||||
#include <memory>
|
||||
|
||||
#include <CXX/WrapPython.h>
|
||||
#include <memory>
|
||||
#include <fmt/format.h>
|
||||
#include <QString>
|
||||
#include "Exception.h"
|
||||
|
||||
@@ -139,23 +141,33 @@ void UnitsApi::setSchema(UnitSystem system)
|
||||
// Quantity (e.g. mi=1.8km rather then 1.6km).
|
||||
}
|
||||
|
||||
QString UnitsApi::toString(const Base::Quantity& quantity, const QuantityFormat& format)
|
||||
std::string UnitsApi::toString(const Base::Quantity& quantity, const QuantityFormat& format)
|
||||
{
|
||||
QString value = QString::fromLatin1("'%1 %2'")
|
||||
.arg(quantity.getValue(), 0, format.toFormat(), format.precision)
|
||||
.arg(quantity.getUnit().getString());
|
||||
return value;
|
||||
return fmt::format("'{} {}'", toNumber(quantity, format), quantity.getUnit().getString());
|
||||
}
|
||||
|
||||
QString UnitsApi::toNumber(const Base::Quantity& quantity, const QuantityFormat& format)
|
||||
std::string UnitsApi::toNumber(const Base::Quantity& quantity, const QuantityFormat& format)
|
||||
{
|
||||
return toNumber(quantity.getValue(), format);
|
||||
}
|
||||
|
||||
QString UnitsApi::toNumber(double value, const QuantityFormat& format)
|
||||
std::string UnitsApi::toNumber(double value, const QuantityFormat& format)
|
||||
{
|
||||
QString number = QString::fromLatin1("%1").arg(value, 0, format.toFormat(), format.precision);
|
||||
return number;
|
||||
std::stringstream ss;
|
||||
|
||||
switch (format.format) {
|
||||
case QuantityFormat::Fixed:
|
||||
ss << std::fixed;
|
||||
break;
|
||||
case QuantityFormat::Scientific:
|
||||
ss << std::scientific;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
ss << std::setprecision(format.precision) << value;
|
||||
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
// return true if the current user schema uses multiple units for length (ex. Ft/In)
|
||||
|
||||
@@ -73,22 +73,22 @@ public:
|
||||
* The string is a number in C locale (i.e. the decimal separator is always a dot) and if
|
||||
* needed represented in scientific notation. The string also includes the unit of the quantity.
|
||||
*/
|
||||
static QString toString(const Base::Quantity& q,
|
||||
const QuantityFormat& f = QuantityFormat(QuantityFormat::Default));
|
||||
static std::string toString(const Base::Quantity& q,
|
||||
const QuantityFormat& f = QuantityFormat(QuantityFormat::Default));
|
||||
/** Get a number as string for a quantity of a given format.
|
||||
* The string is a number in C locale (i.e. the decimal separator is always a dot) and if
|
||||
* needed represented in scientific notation. The string doesn't include the unit of the
|
||||
* quantity.
|
||||
*/
|
||||
static QString toNumber(const Base::Quantity& q,
|
||||
const QuantityFormat& f = QuantityFormat(QuantityFormat::Default));
|
||||
static std::string toNumber(const Base::Quantity& q,
|
||||
const QuantityFormat& f = QuantityFormat(QuantityFormat::Default));
|
||||
/** Get a number as string for a double of a given format.
|
||||
* The string is a number in C locale (i.e. the decimal separator is always a dot) and if
|
||||
* needed represented in scientific notation. The string doesn't include the unit of the
|
||||
* quantity.
|
||||
*/
|
||||
static QString toNumber(double value,
|
||||
const QuantityFormat& f = QuantityFormat(QuantityFormat::Default));
|
||||
static std::string toNumber(double value,
|
||||
const QuantityFormat& f = QuantityFormat(QuantityFormat::Default));
|
||||
|
||||
/// generate a value for a quantity with default user preferred system
|
||||
static double toDouble(PyObject* args, const Base::Unit& u = Base::Unit());
|
||||
|
||||
@@ -221,6 +221,5 @@ PyObject* UnitsApi::sToNumber(PyObject* /*self*/, PyObject* args)
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
QString string = toNumber(value, qf);
|
||||
return Py::new_reference_to(Py::String(string.toStdString()));
|
||||
return Py::new_reference_to(Py::String(toNumber(value, qf)));
|
||||
}
|
||||
|
||||
@@ -72,7 +72,7 @@ QString UnitsSchemaCentimeters::schemaTranslate(const Base::Quantity& quant,
|
||||
}
|
||||
else {
|
||||
// default action for all cases without special treatment:
|
||||
unitString = quant.getUnit().getString();
|
||||
unitString = QString::fromStdString(quant.getUnit().getString());
|
||||
factor = 1.0;
|
||||
}
|
||||
|
||||
|
||||
@@ -52,7 +52,7 @@ QString UnitsSchemaFemMilliMeterNewton::schemaTranslate(const Quantity& quant,
|
||||
}
|
||||
else {
|
||||
// default action for all cases without special treatment:
|
||||
unitString = quant.getUnit().getString();
|
||||
unitString = QString::fromStdString(quant.getUnit().getString());
|
||||
factor = 1.0;
|
||||
}
|
||||
return toLocale(quant, factor, unitString);
|
||||
|
||||
@@ -123,7 +123,7 @@ UnitsSchemaImperial1::schemaTranslate(const Quantity& quant, double& factor, QSt
|
||||
}
|
||||
else {
|
||||
// default action for all cases without special treatment:
|
||||
unitString = quant.getUnit().getString();
|
||||
unitString = QString::fromStdString(quant.getUnit().getString());
|
||||
factor = 1.0;
|
||||
}
|
||||
|
||||
@@ -184,7 +184,7 @@ QString UnitsSchemaImperialDecimal::schemaTranslate(const Base::Quantity& quant,
|
||||
}
|
||||
else {
|
||||
// default action for all cases without special treatment:
|
||||
unitString = quant.getUnit().getString();
|
||||
unitString = QString::fromStdString(quant.getUnit().getString());
|
||||
factor = 1.0;
|
||||
}
|
||||
|
||||
@@ -312,7 +312,7 @@ QString UnitsSchemaImperialBuilding::schemaTranslate(const Quantity& quant,
|
||||
factor = 25.4 / 60;
|
||||
}
|
||||
else {
|
||||
unitString = quant.getUnit().getString();
|
||||
unitString = QString::fromStdString(quant.getUnit().getString());
|
||||
factor = 1.0;
|
||||
}
|
||||
|
||||
@@ -392,11 +392,11 @@ QString UnitsSchemaImperialCivil::schemaTranslate(const Base::Quantity& quant,
|
||||
// output << std::setprecision(Base::UnitsApi::getDecimals()) << std::fixed <<
|
||||
// rawSeconds << secondString.toStdString();
|
||||
// }
|
||||
return QString::fromUtf8(output.str().c_str());
|
||||
return QString::fromStdString(output.str());
|
||||
}
|
||||
else {
|
||||
// default action for all cases without special treatment:
|
||||
unitString = quant.getUnit().getString();
|
||||
unitString = QString::fromStdString(quant.getUnit().getString());
|
||||
factor = 1.0;
|
||||
}
|
||||
|
||||
|
||||
@@ -630,7 +630,7 @@ UnitsSchemaInternal::schemaTranslate(const Quantity& quant, double& factor, QStr
|
||||
}
|
||||
else {
|
||||
// default action for all cases without special treatment:
|
||||
unitString = quant.getUnit().getString();
|
||||
unitString = QString::fromStdString(quant.getUnit().getString());
|
||||
factor = 1.0;
|
||||
}
|
||||
|
||||
|
||||
@@ -617,7 +617,7 @@ QString UnitsSchemaMKS::schemaTranslate(const Quantity& quant, double& factor, Q
|
||||
}
|
||||
else {
|
||||
// default action for all cases without special treatment:
|
||||
unitString = quant.getUnit().getString();
|
||||
unitString = QString::fromStdString(quant.getUnit().getString());
|
||||
factor = 1.0;
|
||||
}
|
||||
|
||||
|
||||
@@ -83,7 +83,7 @@ QString UnitsSchemaMeterDecimal::schemaTranslate(const Base::Quantity& quant,
|
||||
}
|
||||
else {
|
||||
// default action for all cases without special treatment:
|
||||
unitString = quant.getUnit().getString();
|
||||
unitString = QString::fromStdString(quant.getUnit().getString());
|
||||
factor = 1.0;
|
||||
}
|
||||
|
||||
|
||||
@@ -52,7 +52,7 @@ UnitsSchemaMmMin::schemaTranslate(const Quantity& quant, double& factor, QString
|
||||
}
|
||||
else {
|
||||
// default action for all cases without special treatment:
|
||||
unitString = quant.getUnit().getString();
|
||||
unitString = QString::fromStdString(quant.getUnit().getString());
|
||||
factor = 1.0;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user