[Base]retrieve unit text
Co-authored-by: Chris Hennes <chennes@pioneerlibrarysystem.org>
This commit is contained in:
committed by
Chris Hennes
parent
a431ccc156
commit
c9fffa6789
@@ -66,6 +66,11 @@ std::string UnitsApi::getBasicLengthUnit()
|
|||||||
return schemas->currentSchema()->getBasicLengthUnit();
|
return schemas->currentSchema()->getBasicLengthUnit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string UnitsApi::getUnitText(const Quantity& quant)
|
||||||
|
{
|
||||||
|
return schemas->currentSchema()->getUnitText(quant);
|
||||||
|
}
|
||||||
|
|
||||||
void UnitsApi::setDecimals(const int prec)
|
void UnitsApi::setDecimals(const int prec)
|
||||||
{
|
{
|
||||||
decimals = prec;
|
decimals = prec;
|
||||||
|
|||||||
@@ -68,6 +68,7 @@ public:
|
|||||||
static bool isMultiUnitAngle();
|
static bool isMultiUnitAngle();
|
||||||
static bool isMultiUnitLength();
|
static bool isMultiUnitLength();
|
||||||
static std::string getBasicLengthUnit();
|
static std::string getBasicLengthUnit();
|
||||||
|
static std::string getUnitText(const Quantity& quant);
|
||||||
|
|
||||||
static std::size_t getDefSchemaNum()
|
static std::size_t getDefSchemaNum()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -35,6 +35,8 @@
|
|||||||
#include "Exception.h"
|
#include "Exception.h"
|
||||||
#include "Quantity.h"
|
#include "Quantity.h"
|
||||||
|
|
||||||
|
#include "Console.h"
|
||||||
|
|
||||||
using Base::UnitsSchema;
|
using Base::UnitsSchema;
|
||||||
using Base::UnitsSchemaSpec;
|
using Base::UnitsSchemaSpec;
|
||||||
|
|
||||||
@@ -143,3 +145,34 @@ int UnitsSchema::getNum() const
|
|||||||
{
|
{
|
||||||
return static_cast<int>(spec.num);
|
return static_cast<int>(spec.num);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//! return the unit text for this quantity in this schema. ex 10 mm => "mm"
|
||||||
|
//! a more general approach than getBasicLengthUnit.
|
||||||
|
//! TODO: some common code here with translate()
|
||||||
|
std::string UnitsSchema::getUnitText(const Base::Quantity& quant) const
|
||||||
|
{
|
||||||
|
std::string typeString = quant.getUnit().getTypeString(); // "Area", "Mass", ...
|
||||||
|
const auto value = quant.getValue();
|
||||||
|
|
||||||
|
// TODO: some common code here with translate()
|
||||||
|
if (!spec.translationSpecs.contains(typeString)) {
|
||||||
|
Base::Console().log("Schema %s has no entry for %s\n",
|
||||||
|
getName().c_str(),
|
||||||
|
typeString.c_str());
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
auto unitSpecs = spec.translationSpecs.at(typeString);
|
||||||
|
|
||||||
|
auto isSuitable = [&](const UnitTranslationSpec& row) {
|
||||||
|
return row.threshold > value || row.threshold == 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
const auto unitSpec = std::ranges::find_if(unitSpecs, isSuitable);
|
||||||
|
if (unitSpec == unitSpecs.end()) {
|
||||||
|
throw RuntimeError("Suitable threshold not found (2). Schema: " + spec.name
|
||||||
|
+ " value: " + std::to_string(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
return unitSpec->unitString;
|
||||||
|
}
|
||||||
|
|||||||
@@ -50,6 +50,7 @@ public:
|
|||||||
[[nodiscard]] std::string getName() const;
|
[[nodiscard]] std::string getName() const;
|
||||||
[[nodiscard]] std::string getDescription() const;
|
[[nodiscard]] std::string getDescription() const;
|
||||||
[[nodiscard]] int getNum() const;
|
[[nodiscard]] int getNum() const;
|
||||||
|
[[nodiscard]] std::string getUnitText(const Quantity& quant) const;
|
||||||
|
|
||||||
std::string translate(const Quantity& quant) const;
|
std::string translate(const Quantity& quant) const;
|
||||||
std::string translate(const Quantity& quant, double& factor, std::string& unitString) const;
|
std::string translate(const Quantity& quant, double& factor, std::string& unitString) const;
|
||||||
|
|||||||
Reference in New Issue
Block a user