Base: simplify UnitsSchemas management
Fixes: Maintaining schemas is difficult and error-prone - Facilitate easy schemas add, remove, change, etc. - Remove 14 files containing approx 2,190 lines of if/else code and data - Place data in one file (UnitsSchemasData.h) using a normalized structure (including special functions) - Isolate and simplify data operations (code) - Remove schemas enum to keep data independent of code - Separate responsibilities: Specifications, data, schemas, schema - Add schema data 'isDefault' - Add schema data name - Prefer algorithms to raw loops - Add schemas unit tests - Tweak quantity unit tests
This commit is contained in:
@@ -20,124 +20,91 @@
|
||||
* *
|
||||
***************************************************************************/
|
||||
|
||||
|
||||
#include "PreCompiled.h"
|
||||
|
||||
#include <sstream>
|
||||
#ifndef _PreComp_
|
||||
#include <iomanip>
|
||||
#include <memory>
|
||||
#include <sstream>
|
||||
#endif
|
||||
|
||||
#include <CXX/WrapPython.h>
|
||||
#include <fmt/format.h>
|
||||
#include <QString>
|
||||
|
||||
#include "Exception.h"
|
||||
|
||||
#include "UnitsApi.h"
|
||||
#include "UnitsSchemaCentimeters.h"
|
||||
#include "UnitsSchemaInternal.h"
|
||||
#include "UnitsSchemaImperial1.h"
|
||||
#include "UnitsSchemaMKS.h"
|
||||
#include "UnitsSchemaMmMin.h"
|
||||
#include "UnitsSchemaFemMilliMeterNewton.h"
|
||||
#include "UnitsSchemaMeterDecimal.h"
|
||||
#include "UnitsSchema.h"
|
||||
#include "UnitsSchemas.h"
|
||||
#include "UnitsSchemasData.h"
|
||||
|
||||
using namespace Base;
|
||||
using Base::UnitsApi;
|
||||
using Base::UnitsSchema;
|
||||
using Base::UnitsSchemas;
|
||||
|
||||
// === static attributes ================================================
|
||||
|
||||
UnitsSchemaPtr UnitsApi::UserPrefSystem(new UnitsSchemaInternal());
|
||||
UnitSystem UnitsApi::currentSystem = UnitSystem::SI1;
|
||||
|
||||
int UnitsApi::UserPrefDecimals = 2;
|
||||
|
||||
QString UnitsApi::getDescription(UnitSystem system)
|
||||
void UnitsApi::init()
|
||||
{
|
||||
switch (system) {
|
||||
case UnitSystem::SI1:
|
||||
return tr("Standard (mm, kg, s, °)");
|
||||
case UnitSystem::SI2:
|
||||
return tr("MKS (m, kg, s, °)");
|
||||
case UnitSystem::Imperial1:
|
||||
return tr("US customary (in, lb)");
|
||||
case UnitSystem::ImperialDecimal:
|
||||
return tr("Imperial decimal (in, lb)");
|
||||
case UnitSystem::Centimeters:
|
||||
return tr("Building Euro (cm, m², m³)");
|
||||
case UnitSystem::ImperialBuilding:
|
||||
return tr("Building US (ft-in, sqft, cft)");
|
||||
case UnitSystem::MmMin:
|
||||
return tr("Metric small parts & CNC (mm, mm/min)");
|
||||
case UnitSystem::ImperialCivil:
|
||||
return tr("Imperial for Civil Eng (ft, ft/s)");
|
||||
case UnitSystem::FemMilliMeterNewton:
|
||||
return tr("FEM (mm, N, s)");
|
||||
case UnitSystem::MeterDecimal:
|
||||
return tr("Meter decimal (m, m², m³)");
|
||||
default:
|
||||
return tr("Unknown schema");
|
||||
}
|
||||
schemas = std::make_unique<UnitsSchemas>(UnitsSchemasData::unitSchemasDataPack);
|
||||
}
|
||||
|
||||
UnitsSchemaPtr UnitsApi::createSchema(UnitSystem system)
|
||||
std::vector<std::string> UnitsApi::getDescriptions()
|
||||
{
|
||||
switch (system) {
|
||||
case UnitSystem::SI1:
|
||||
return std::make_unique<UnitsSchemaInternal>();
|
||||
case UnitSystem::SI2:
|
||||
return std::make_unique<UnitsSchemaMKS>();
|
||||
case UnitSystem::Imperial1:
|
||||
return std::make_unique<UnitsSchemaImperial1>();
|
||||
case UnitSystem::ImperialDecimal:
|
||||
return std::make_unique<UnitsSchemaImperialDecimal>();
|
||||
case UnitSystem::Centimeters:
|
||||
return std::make_unique<UnitsSchemaCentimeters>();
|
||||
case UnitSystem::ImperialBuilding:
|
||||
return std::make_unique<UnitsSchemaImperialBuilding>();
|
||||
case UnitSystem::MmMin:
|
||||
return std::make_unique<UnitsSchemaMmMin>();
|
||||
case UnitSystem::ImperialCivil:
|
||||
return std::make_unique<UnitsSchemaImperialCivil>();
|
||||
case UnitSystem::FemMilliMeterNewton:
|
||||
return std::make_unique<UnitsSchemaFemMilliMeterNewton>();
|
||||
case UnitSystem::MeterDecimal:
|
||||
return std::make_unique<UnitsSchemaMeterDecimal>();
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
return schemas->descriptions();
|
||||
}
|
||||
|
||||
void UnitsApi::setSchema(UnitSystem system)
|
||||
std::vector<std::string> UnitsApi::getNames()
|
||||
{
|
||||
if (UserPrefSystem) {
|
||||
UserPrefSystem->resetSchemaUnits(); // for schemas changed the Quantity constants
|
||||
}
|
||||
|
||||
UserPrefSystem = createSchema(system);
|
||||
currentSystem = system;
|
||||
|
||||
// for wrong value fall back to standard schema
|
||||
if (!UserPrefSystem) {
|
||||
UserPrefSystem = std::make_unique<UnitsSchemaInternal>();
|
||||
currentSystem = UnitSystem::SI1;
|
||||
}
|
||||
|
||||
UserPrefSystem->setSchemaUnits(); // if necessary a unit schema can change the constants in
|
||||
// Quantity (e.g. mi=1.8km rather then 1.6km).
|
||||
return schemas->names();
|
||||
}
|
||||
|
||||
std::string UnitsApi::toString(const Base::Quantity& quantity, const QuantityFormat& format)
|
||||
std::size_t UnitsApi::count()
|
||||
{
|
||||
return static_cast<int>(schemas->count());
|
||||
}
|
||||
|
||||
bool UnitsApi::isMultiUnitAngle()
|
||||
{
|
||||
return schemas->currentSchema()->isMultiUnitAngle();
|
||||
}
|
||||
|
||||
bool UnitsApi::isMultiUnitLength()
|
||||
{
|
||||
return schemas->currentSchema()->isMultiUnitLength();
|
||||
}
|
||||
|
||||
std::string UnitsApi::getBasicLengthUnit()
|
||||
{
|
||||
return schemas->currentSchema()->getBasicLengthUnit();
|
||||
}
|
||||
|
||||
std::size_t UnitsApi::getFractDenominator()
|
||||
{
|
||||
return schemas->defFractDenominator();
|
||||
}
|
||||
|
||||
std::unique_ptr<UnitsSchema> UnitsApi::createSchema(const std::size_t num)
|
||||
{
|
||||
return std::make_unique<UnitsSchema>(schemas->spec(num));
|
||||
}
|
||||
|
||||
void UnitsApi::setSchema(const std::string& name)
|
||||
{
|
||||
schemas->select(name);
|
||||
}
|
||||
|
||||
void UnitsApi::setSchema(const size_t num)
|
||||
{
|
||||
schemas->select(num);
|
||||
}
|
||||
|
||||
std::string UnitsApi::toString(const Quantity& quantity, const QuantityFormat& format)
|
||||
{
|
||||
return fmt::format("'{} {}'", toNumber(quantity, format), quantity.getUnit().getString());
|
||||
}
|
||||
|
||||
std::string UnitsApi::toNumber(const Base::Quantity& quantity, const QuantityFormat& format)
|
||||
std::string UnitsApi::toNumber(const Quantity& quantity, const QuantityFormat& format)
|
||||
{
|
||||
return toNumber(quantity.getValue(), format);
|
||||
}
|
||||
|
||||
std::string UnitsApi::toNumber(double value, const QuantityFormat& format)
|
||||
std::string UnitsApi::toNumber(const double value, const QuantityFormat& format)
|
||||
{
|
||||
std::stringstream ss;
|
||||
|
||||
@@ -156,31 +123,6 @@ std::string UnitsApi::toNumber(double value, const QuantityFormat& format)
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
// return true if the current user schema uses multiple units for length (ex. Ft/In)
|
||||
bool UnitsApi::isMultiUnitLength()
|
||||
{
|
||||
return UserPrefSystem->isMultiUnitLength();
|
||||
}
|
||||
|
||||
// return true if the current user schema uses multiple units for angles (ex. DMS)
|
||||
bool UnitsApi::isMultiUnitAngle()
|
||||
{
|
||||
return UserPrefSystem->isMultiUnitAngle();
|
||||
}
|
||||
|
||||
std::string UnitsApi::getBasicLengthUnit()
|
||||
{
|
||||
return UserPrefSystem->getBasicLengthUnit();
|
||||
}
|
||||
|
||||
// === static translation methods ==========================================
|
||||
|
||||
std::string
|
||||
UnitsApi::schemaTranslate(const Base::Quantity& quant, double& factor, std::string& unitString)
|
||||
{
|
||||
return UserPrefSystem->schemaTranslate(quant, factor, unitString);
|
||||
}
|
||||
|
||||
double UnitsApi::toDouble(PyObject* args, const Base::Unit& u)
|
||||
{
|
||||
if (PyUnicode_Check(args)) {
|
||||
@@ -203,34 +145,30 @@ double UnitsApi::toDouble(PyObject* args, const Base::Unit& u)
|
||||
throw Base::UnitsMismatchError("Wrong parameter type!");
|
||||
}
|
||||
|
||||
Quantity UnitsApi::toQuantity(PyObject* args, const Base::Unit& u)
|
||||
std::string
|
||||
UnitsApi::schemaTranslate(const Quantity& quant, double& factor, std::string& unitString)
|
||||
{
|
||||
double d {};
|
||||
if (PyUnicode_Check(args)) {
|
||||
std::string str(PyUnicode_AsUTF8(args));
|
||||
// Parse the string
|
||||
Quantity q = Quantity::parse(str);
|
||||
d = q.getValue();
|
||||
}
|
||||
else if (PyFloat_Check(args)) {
|
||||
d = PyFloat_AsDouble(args);
|
||||
}
|
||||
else if (PyLong_Check(args)) {
|
||||
d = static_cast<double>(PyLong_AsLong(args));
|
||||
}
|
||||
else {
|
||||
throw Base::UnitsMismatchError("Wrong parameter type!");
|
||||
}
|
||||
|
||||
return Quantity(d, u);
|
||||
return schemas->currentSchema()->translate(quant, factor, unitString);
|
||||
}
|
||||
|
||||
void UnitsApi::setDecimals(int prec)
|
||||
std::string UnitsApi::schemaTranslate(const Quantity& quant)
|
||||
{
|
||||
UserPrefDecimals = prec;
|
||||
double dummy1 {}; // to satisfy GCC
|
||||
std::string dummy2;
|
||||
return schemas->currentSchema()->translate(quant, dummy1, dummy2);
|
||||
}
|
||||
|
||||
int UnitsApi::getDecimals()
|
||||
void UnitsApi::setDecimals(const std::size_t prec)
|
||||
{
|
||||
return UserPrefDecimals;
|
||||
decimals = prec;
|
||||
}
|
||||
|
||||
size_t UnitsApi::getDecimals()
|
||||
{
|
||||
return decimals;
|
||||
}
|
||||
|
||||
size_t UnitsApi::getDefDecimals()
|
||||
{
|
||||
return schemas->getDecimals();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user