mirror of
https://bitbucket.org/smil3y/kdelibs.git
synced 2025-02-25 03:12:49 +00:00
Merge branch 'master' of https://github.com/fluxer/kdelibs into devinfo
This commit is contained in:
commit
03dabe1cb4
2 changed files with 647 additions and 34 deletions
|
@ -18,39 +18,70 @@
|
||||||
|
|
||||||
#include "kunitconversion.h"
|
#include "kunitconversion.h"
|
||||||
#include "kdebug.h"
|
#include "kdebug.h"
|
||||||
|
#include "klocale.h"
|
||||||
|
|
||||||
#include <qmath.h>
|
#include <qmath.h>
|
||||||
|
|
||||||
|
double KUnitConversion::round(const double number, const uint digits)
|
||||||
|
{
|
||||||
|
const double poweroften = qPow(10, digits);
|
||||||
|
return qRound(number * poweroften) / poweroften;
|
||||||
|
}
|
||||||
|
|
||||||
class KTemperaturePrivate {
|
class KTemperaturePrivate {
|
||||||
public:
|
public:
|
||||||
|
KTemperaturePrivate(const double number, const KTemperature::KTempUnit unit);
|
||||||
KTemperaturePrivate(const double number, const QString &unit);
|
KTemperaturePrivate(const double number, const QString &unit);
|
||||||
|
|
||||||
double m_number;
|
double m_number;
|
||||||
QString m_unit;
|
QString m_unit;
|
||||||
KTemperature::KTempUnit m_tempunit;
|
KTemperature::KTempUnit m_unitenum;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
KTemperaturePrivate::KTemperaturePrivate(const double number, const KTemperature::KTempUnit unit)
|
||||||
|
: m_number(number),
|
||||||
|
m_unitenum(unit)
|
||||||
|
{
|
||||||
|
if (unit == KTemperature::Celsius) {
|
||||||
|
m_unit = QString::fromUtf8("°C");
|
||||||
|
} else if (unit == KTemperature::Fahrenheit) {
|
||||||
|
m_unit = QString::fromUtf8("°F");
|
||||||
|
} else if (unit == KTemperature::Kelvin) {
|
||||||
|
m_unit = QString::fromUtf8("K");
|
||||||
|
} else {
|
||||||
|
m_unit = QLatin1String("Unknown");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
KTemperaturePrivate::KTemperaturePrivate(const double number, const QString &unit)
|
KTemperaturePrivate::KTemperaturePrivate(const double number, const QString &unit)
|
||||||
: m_number(number),
|
: m_number(number),
|
||||||
m_tempunit(KTemperature::Invalid)
|
m_unitenum(KTemperature::Invalid)
|
||||||
{
|
{
|
||||||
if (unit == QString::fromUtf8("°C") || unit == QLatin1String("C") || unit == QLatin1String("Celsius")) {
|
// any unit other than the string representation of the enum is for KUnitConversion compatibility
|
||||||
m_tempunit = KTemperature::Celsius;
|
if (unit == QLatin1String("Celsius")
|
||||||
|
|| unit == QString::fromUtf8("°C") || unit == QLatin1String("C")) {
|
||||||
|
m_unitenum = KTemperature::Celsius;
|
||||||
m_unit = QString::fromUtf8("°C");
|
m_unit = QString::fromUtf8("°C");
|
||||||
} else if (unit == QString::fromUtf8("°F") || unit == QLatin1String("F") || unit == QLatin1String("Fahrenheit")) {
|
} else if (unit == QLatin1String("Fahrenheit")
|
||||||
m_tempunit = KTemperature::Fahrenheit;
|
|| unit == QString::fromUtf8("°F") || unit == QLatin1String("F")) {
|
||||||
|
m_unitenum = KTemperature::Fahrenheit;
|
||||||
m_unit = QString::fromUtf8("°F");
|
m_unit = QString::fromUtf8("°F");
|
||||||
// kelvin and kelvins are for KUnitConversion compatibility
|
} else if (unit == QLatin1String("Kelvin")
|
||||||
} else if (unit == QLatin1String("K") || unit == QLatin1String("Kelvin")
|
|| unit == QLatin1String("K") || unit == QLatin1String("kelvin")
|
||||||
|| unit == QLatin1String("kelvin") || unit == QLatin1String("kelvins")) {
|
|| unit == QLatin1String("kelvins")) {
|
||||||
m_tempunit = KTemperature::Kelvin;
|
m_unitenum = KTemperature::Kelvin;
|
||||||
m_unit = QLatin1String("K");
|
m_unit = QLatin1String("K");
|
||||||
} else {
|
} else {
|
||||||
kDebug() << "invalid unit" << unit;
|
kDebug() << "invalid temperature unit" << unit;
|
||||||
m_unit = QLatin1String("Unknown");
|
m_unit = QLatin1String("Unknown");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
KTemperature::KTemperature(const double number, const KTempUnit unit)
|
||||||
|
: d(new KTemperaturePrivate(number, unit))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
KTemperature::KTemperature(const double number, const QString &unit)
|
KTemperature::KTemperature(const double number, const QString &unit)
|
||||||
: d(new KTemperaturePrivate(number, unit))
|
: d(new KTemperaturePrivate(number, unit))
|
||||||
{
|
{
|
||||||
|
@ -71,6 +102,11 @@ QString KTemperature::unit() const
|
||||||
return d->m_unit;
|
return d->m_unit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
KTemperature::KTempUnit KTemperature::unitEnum() const
|
||||||
|
{
|
||||||
|
return d->m_unitenum;
|
||||||
|
}
|
||||||
|
|
||||||
QString KTemperature::toString() const
|
QString KTemperature::toString() const
|
||||||
{
|
{
|
||||||
return QString::fromLatin1("%1 %2").arg(QString::number(d->m_number), d->m_unit);
|
return QString::fromLatin1("%1 %2").arg(QString::number(d->m_number), d->m_unit);
|
||||||
|
@ -78,7 +114,7 @@ QString KTemperature::toString() const
|
||||||
|
|
||||||
double KTemperature::convertTo(const KTempUnit unit) const
|
double KTemperature::convertTo(const KTempUnit unit) const
|
||||||
{
|
{
|
||||||
if (unit == d->m_tempunit) {
|
if (unit == d->m_unitenum) {
|
||||||
return d->m_number;
|
return d->m_number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -89,24 +125,468 @@ double KTemperature::convertTo(const KTempUnit unit) const
|
||||||
// https://www.rapidtables.com/convert/temperature/fahrenheit-to-kelvin.html
|
// https://www.rapidtables.com/convert/temperature/fahrenheit-to-kelvin.html
|
||||||
// https://www.rapidtables.com/convert/temperature/kelvin-to-celsius.html
|
// https://www.rapidtables.com/convert/temperature/kelvin-to-celsius.html
|
||||||
// https://www.rapidtables.com/convert/temperature/kelvin-to-fahrenheit.html
|
// https://www.rapidtables.com/convert/temperature/kelvin-to-fahrenheit.html
|
||||||
if (d->m_tempunit == KTemperature::Celsius && unit == KTemperature::Fahrenheit) {
|
if (d->m_unitenum == KTemperature::Celsius && unit == KTemperature::Fahrenheit) {
|
||||||
return (d->m_number * 1.8 + 32);
|
return (d->m_number * 1.8 + 32);
|
||||||
} else if (d->m_tempunit == KTemperature::Celsius && unit == KTemperature::Kelvin) {
|
} else if (d->m_unitenum == KTemperature::Celsius && unit == KTemperature::Kelvin) {
|
||||||
return (d->m_number + 273.15);
|
return (d->m_number + 273.15);
|
||||||
} else if (d->m_tempunit == KTemperature::Fahrenheit && unit == KTemperature::Celsius) {
|
} else if (d->m_unitenum == KTemperature::Fahrenheit && unit == KTemperature::Celsius) {
|
||||||
return ((d->m_number - 32) / 1.8);
|
return ((d->m_number - 32) / 1.8);
|
||||||
} else if (d->m_tempunit == KTemperature::Fahrenheit && unit == KTemperature::Kelvin) {
|
} else if (d->m_unitenum == KTemperature::Fahrenheit && unit == KTemperature::Kelvin) {
|
||||||
return ((d->m_number + 459.67) * 0.5);
|
return ((d->m_number + 459.67) * 0.5);
|
||||||
} else if (d->m_tempunit == KTemperature::Kelvin && unit == KTemperature::Celsius) {
|
} else if (d->m_unitenum == KTemperature::Kelvin && unit == KTemperature::Celsius) {
|
||||||
return (d->m_number - 273.15);
|
return (d->m_number - 273.15);
|
||||||
} else if (d->m_tempunit == KTemperature::Kelvin && unit == KTemperature::Fahrenheit) {
|
} else if (d->m_unitenum == KTemperature::Kelvin && unit == KTemperature::Fahrenheit) {
|
||||||
return (d->m_number * 1.8 - 459.67);
|
return (d->m_number * 1.8 - 459.67);
|
||||||
}
|
}
|
||||||
return 0.0;
|
return 0.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
double KTemperature::round(const double number, const uint digits)
|
QString KTemperature::description()
|
||||||
{
|
{
|
||||||
const double poweroften = qPow(10, digits);
|
return i18n("Temperature");
|
||||||
return qRound(number * poweroften) / poweroften;
|
}
|
||||||
|
|
||||||
|
QString KTemperature::unitDescription(const KTempUnit unit)
|
||||||
|
{
|
||||||
|
switch (unit) {
|
||||||
|
case KTemperature::Celsius:
|
||||||
|
return i18n("Celsius (°C)");
|
||||||
|
case KTemperature::Fahrenheit:
|
||||||
|
return i18n("Fahrenheit (°F)");
|
||||||
|
case KTemperature::Kelvin:
|
||||||
|
return i18n("Kelvin (K)");
|
||||||
|
case KTemperature::Invalid:
|
||||||
|
case KTemperature::UnitCount:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return i18n("Unknown");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class KVelocityPrivate {
|
||||||
|
public:
|
||||||
|
KVelocityPrivate(const double number, const KVelocity::KVeloUnit unit);
|
||||||
|
KVelocityPrivate(const double number, const QString &unit);
|
||||||
|
|
||||||
|
double m_number;
|
||||||
|
QString m_unit;
|
||||||
|
KVelocity::KVeloUnit m_unitenum;
|
||||||
|
};
|
||||||
|
|
||||||
|
KVelocityPrivate::KVelocityPrivate(const double number, const KVelocity::KVeloUnit unit)
|
||||||
|
: m_number(number),
|
||||||
|
m_unitenum(unit)
|
||||||
|
{
|
||||||
|
if (unit == KVelocity::MeterPerSecond) {
|
||||||
|
m_unit = QLatin1String("m/s");
|
||||||
|
} else if (unit == KVelocity::KilometerPerHour) {
|
||||||
|
m_unit = QLatin1String("km/h");
|
||||||
|
} else if (unit == KVelocity::MilePerHour) {
|
||||||
|
m_unit = QLatin1String("mph");
|
||||||
|
} else if (unit == KVelocity::Knot) {
|
||||||
|
m_unit = QLatin1String("kt");
|
||||||
|
} else {
|
||||||
|
m_unit = QLatin1String("Unknown");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
KVelocityPrivate::KVelocityPrivate(const double number, const QString &unit)
|
||||||
|
: m_number(number),
|
||||||
|
m_unitenum(KVelocity::Invalid)
|
||||||
|
{
|
||||||
|
if (unit == QLatin1String("MeterPerSecond")
|
||||||
|
|| unit == QLatin1String("meter per second") || unit == QLatin1String("meters per second")
|
||||||
|
|| unit == QLatin1String("m/s") || unit == QLatin1String("ms")) {
|
||||||
|
m_unitenum = KVelocity::MeterPerSecond;
|
||||||
|
m_unit = QLatin1String("m/s");
|
||||||
|
} else if (unit == QLatin1String("KilometerPerHour")
|
||||||
|
|| unit == QLatin1String("kilometer per hour") || unit == QLatin1String("kilometers per hour")
|
||||||
|
|| unit == QLatin1String("km/h") || unit == QLatin1String("kmh")) {
|
||||||
|
m_unitenum = KVelocity::KilometerPerHour;
|
||||||
|
m_unit = QLatin1String("km/h");
|
||||||
|
} else if (unit == QLatin1String("MilePerHour")
|
||||||
|
|| unit == QLatin1String("mile per hour") || unit == QLatin1String("miles per hour")
|
||||||
|
|| unit == QLatin1String("mph")) {
|
||||||
|
m_unitenum = KVelocity::MilePerHour;
|
||||||
|
m_unit = QLatin1String("mph");
|
||||||
|
} else if (unit == QLatin1String("Knot")
|
||||||
|
|| unit == QLatin1String("knot") || unit == QLatin1String("knots")
|
||||||
|
|| unit == QLatin1String("kt") || unit == QLatin1String("nautical miles per hour")) {
|
||||||
|
m_unitenum = KVelocity::Knot;
|
||||||
|
m_unit = QLatin1String("kt");
|
||||||
|
} else {
|
||||||
|
kDebug() << "invalid velocity unit" << unit;
|
||||||
|
m_unit = QLatin1String("Unknown");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
KVelocity::KVelocity(const double number, const KVeloUnit unit)
|
||||||
|
: d(new KVelocityPrivate(number, unit))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
KVelocity::KVelocity(const double number, const QString &unit)
|
||||||
|
: d(new KVelocityPrivate(number, unit))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
KVelocity::~KVelocity()
|
||||||
|
{
|
||||||
|
delete d;
|
||||||
|
}
|
||||||
|
|
||||||
|
double KVelocity::number() const
|
||||||
|
{
|
||||||
|
return d->m_number;
|
||||||
|
}
|
||||||
|
|
||||||
|
QString KVelocity::unit() const
|
||||||
|
{
|
||||||
|
return d->m_unit;
|
||||||
|
}
|
||||||
|
|
||||||
|
KVelocity::KVeloUnit KVelocity::unitEnum() const
|
||||||
|
{
|
||||||
|
return d->m_unitenum;
|
||||||
|
}
|
||||||
|
|
||||||
|
QString KVelocity::toString() const
|
||||||
|
{
|
||||||
|
return QString::fromLatin1("%1 %2").arg(QString::number(d->m_number), d->m_unit);
|
||||||
|
}
|
||||||
|
|
||||||
|
double KVelocity::convertTo(const KVeloUnit unit) const
|
||||||
|
{
|
||||||
|
if (unit == d->m_unitenum) {
|
||||||
|
return d->m_number;
|
||||||
|
}
|
||||||
|
|
||||||
|
// for reference:
|
||||||
|
// https://www.metric-conversions.org/speed/meters-per-second-to-kilometers-per-hour.htm
|
||||||
|
// https://www.metric-conversions.org/speed/meters-per-second-to-miles-per-hour.htm
|
||||||
|
// https://www.metric-conversions.org/speed/meters-per-second-to-knots.htm
|
||||||
|
// https://www.metric-conversions.org/speed/kilometers-per-hour-to-meters-per-second.htm
|
||||||
|
// https://www.metric-conversions.org/speed/kilometers-per-hour-to-miles-per-hour.htm
|
||||||
|
// https://www.metric-conversions.org/speed/kilometers-per-hour-to-knots.htm
|
||||||
|
// https://www.metric-conversions.org/speed/miles-per-hour-to-meters-per-second.htm
|
||||||
|
// https://www.metric-conversions.org/speed/miles-per-hour-to-kilometers-per-hour.htm
|
||||||
|
// https://www.metric-conversions.org/speed/miles-per-hour-to-knots.htm
|
||||||
|
// https://www.metric-conversions.org/speed/knots-to-meters-per-second.htm
|
||||||
|
// https://www.metric-conversions.org/speed/knots-to-kilometers-per-hour.htm
|
||||||
|
// https://www.metric-conversions.org/speed/knots-to-miles-per-hour.htm
|
||||||
|
if (d->m_unitenum == KVelocity::MeterPerSecond && unit == KVelocity::KilometerPerHour) {
|
||||||
|
return (d->m_number * 3.6);
|
||||||
|
} else if (d->m_unitenum == KVelocity::MeterPerSecond && unit == KVelocity::MilePerHour) {
|
||||||
|
return (d->m_number * 2.236936);
|
||||||
|
} else if (d->m_unitenum == KVelocity::MeterPerSecond && unit == KVelocity::Knot) {
|
||||||
|
return (d->m_number * 1.943844);
|
||||||
|
} else if (d->m_unitenum == KVelocity::KilometerPerHour && unit == KVelocity::MeterPerSecond) {
|
||||||
|
return (d->m_number / 3.6);
|
||||||
|
} else if (d->m_unitenum == KVelocity::KilometerPerHour && unit == KVelocity::MilePerHour) {
|
||||||
|
return (d->m_number / 1.609344);
|
||||||
|
} else if (d->m_unitenum == KVelocity::KilometerPerHour && unit == KVelocity::Knot) {
|
||||||
|
return d->m_number / 1.852;
|
||||||
|
} else if (d->m_unitenum == KVelocity::MilePerHour && unit == KVelocity::MeterPerSecond) {
|
||||||
|
return (d->m_number / 2.236936);
|
||||||
|
} else if (d->m_unitenum == KVelocity::MilePerHour && unit == KVelocity::KilometerPerHour) {
|
||||||
|
return (d->m_number * 1.609344);
|
||||||
|
} else if (d->m_unitenum == KVelocity::MilePerHour && unit == KVelocity::Knot) {
|
||||||
|
return (d->m_number / 1.150779);
|
||||||
|
} else if (d->m_unitenum == KVelocity::Knot && unit == KVelocity::MeterPerSecond) {
|
||||||
|
return (d->m_number / 1.943844);
|
||||||
|
} else if (d->m_unitenum == KVelocity::Knot && unit == KVelocity::KilometerPerHour) {
|
||||||
|
return (d->m_number * 1.852);
|
||||||
|
} else if (d->m_unitenum == KVelocity::Knot && unit == KVelocity::MilePerHour) {
|
||||||
|
return (d->m_number * 1.150779);
|
||||||
|
}
|
||||||
|
return 0.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
QString KVelocity::description()
|
||||||
|
{
|
||||||
|
return i18n("Velocity");
|
||||||
|
}
|
||||||
|
|
||||||
|
QString KVelocity::unitDescription(const KVeloUnit unit)
|
||||||
|
{
|
||||||
|
switch (unit) {
|
||||||
|
case KVelocity::MeterPerSecond:
|
||||||
|
return i18n("Meter per second (m/s)");
|
||||||
|
case KVelocity::KilometerPerHour:
|
||||||
|
return i18n("Kilometer per hour (km/h)");
|
||||||
|
case KVelocity::MilePerHour:
|
||||||
|
return i18n("Mile per hour (mph)");
|
||||||
|
case KVelocity::Knot:
|
||||||
|
return i18n("Knot (kt)");
|
||||||
|
case KVelocity::Invalid:
|
||||||
|
case KVelocity::UnitCount:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return i18n("Unknown");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class KPressurePrivate {
|
||||||
|
public:
|
||||||
|
KPressurePrivate(const double number, const KPressure::KPresUnit unit);
|
||||||
|
KPressurePrivate(const double number, const QString &unit);
|
||||||
|
|
||||||
|
double m_number;
|
||||||
|
QString m_unit;
|
||||||
|
KPressure::KPresUnit m_unitenum;
|
||||||
|
};
|
||||||
|
|
||||||
|
KPressurePrivate::KPressurePrivate(const double number, const KPressure::KPresUnit unit)
|
||||||
|
: m_number(number),
|
||||||
|
m_unitenum(unit)
|
||||||
|
{
|
||||||
|
if (unit == KPressure::Kilopascal) {
|
||||||
|
m_unit = QLatin1String("kPa");
|
||||||
|
} else if (unit == KPressure::Hectopascal) {
|
||||||
|
m_unit = QLatin1String("hPa");
|
||||||
|
} else if (unit == KPressure::Millibar) {
|
||||||
|
m_unit = QLatin1String("mbar");
|
||||||
|
} else if (unit == KPressure::InchesOfMercury) {
|
||||||
|
m_unit = QLatin1String("inHg");
|
||||||
|
} else {
|
||||||
|
m_unit = QLatin1String("Unknown");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
KPressurePrivate::KPressurePrivate(const double number, const QString &unit)
|
||||||
|
: m_number(number),
|
||||||
|
m_unitenum(KPressure::Invalid)
|
||||||
|
{
|
||||||
|
if (unit == QLatin1String("Kilopascal")
|
||||||
|
|| unit == QLatin1String("kilopascal") || unit == QLatin1String("kilopascals")
|
||||||
|
|| unit == QLatin1String("kPa")) {
|
||||||
|
m_unitenum = KPressure::Kilopascal;
|
||||||
|
m_unit = QLatin1String("kPa");
|
||||||
|
} else if (unit == QLatin1String("Hectopascal")
|
||||||
|
|| unit == QLatin1String("hectopascal") || unit == QLatin1String("hectopascals")
|
||||||
|
|| unit == QLatin1String("hPa")) {
|
||||||
|
m_unitenum = KPressure::Hectopascal;
|
||||||
|
m_unit = QLatin1String("hPa");
|
||||||
|
} else if (unit == QLatin1String("Millibar")
|
||||||
|
|| unit == QLatin1String("millibar") || unit == QLatin1String("millibars")
|
||||||
|
|| unit == QLatin1String("mbar") || unit == QLatin1String("mb")) {
|
||||||
|
m_unitenum = KPressure::Millibar;
|
||||||
|
m_unit = QLatin1String("mbar");
|
||||||
|
} else if (unit == QLatin1String("InchesOfMercury")
|
||||||
|
|| unit == QLatin1String("inch of mercury") || unit == QLatin1String("inches of mercury")
|
||||||
|
|| unit == QLatin1String("inHg")) {
|
||||||
|
m_unitenum = KPressure::InchesOfMercury;
|
||||||
|
m_unit = QLatin1String("inHg");
|
||||||
|
} else {
|
||||||
|
kDebug() << "invalid pressure unit" << unit;
|
||||||
|
m_unit = QLatin1String("Unknown");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
KPressure::KPressure(const double number, const KPresUnit unit)
|
||||||
|
: d(new KPressurePrivate(number, unit))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
KPressure::KPressure(const double number, const QString &unit)
|
||||||
|
: d(new KPressurePrivate(number, unit))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
KPressure::~KPressure()
|
||||||
|
{
|
||||||
|
delete d;
|
||||||
|
}
|
||||||
|
|
||||||
|
double KPressure::number() const
|
||||||
|
{
|
||||||
|
return d->m_number;
|
||||||
|
}
|
||||||
|
|
||||||
|
QString KPressure::unit() const
|
||||||
|
{
|
||||||
|
return d->m_unit;
|
||||||
|
}
|
||||||
|
|
||||||
|
KPressure::KPresUnit KPressure::unitEnum() const
|
||||||
|
{
|
||||||
|
return d->m_unitenum;
|
||||||
|
}
|
||||||
|
|
||||||
|
QString KPressure::toString() const
|
||||||
|
{
|
||||||
|
return QString::fromLatin1("%1 %2").arg(QString::number(d->m_number), d->m_unit);
|
||||||
|
}
|
||||||
|
|
||||||
|
double KPressure::convertTo(const KPresUnit unit) const
|
||||||
|
{
|
||||||
|
if (unit == d->m_unitenum) {
|
||||||
|
return d->m_number;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (d->m_unitenum == KPressure::Kilopascal && unit == KPressure::Hectopascal) {
|
||||||
|
return (d->m_number * 10.0);
|
||||||
|
} else if (d->m_unitenum == KPressure::Kilopascal && unit == KPressure::Millibar) {
|
||||||
|
return (d->m_number * 10.0);
|
||||||
|
} else if (d->m_unitenum == KPressure::Kilopascal && unit == KPressure::InchesOfMercury) {
|
||||||
|
return (d->m_number * 3.386398);
|
||||||
|
} else if (d->m_unitenum == KPressure::Hectopascal && unit == KPressure::Kilopascal) {
|
||||||
|
return (d->m_number / 10.0);
|
||||||
|
} else if (d->m_unitenum == KPressure::Hectopascal && unit == KPressure::Millibar) {
|
||||||
|
return (d->m_number * 1.0);
|
||||||
|
} else if (d->m_unitenum == KPressure::Hectopascal && unit == KPressure::InchesOfMercury) {
|
||||||
|
return (d->m_number / 3.386398);
|
||||||
|
} else if (d->m_unitenum == KPressure::Millibar && unit == KPressure::Kilopascal) {
|
||||||
|
return (d->m_number / 0.1);
|
||||||
|
} else if (d->m_unitenum == KPressure::Millibar && unit == KPressure::Hectopascal) {
|
||||||
|
return (d->m_number * 1.0);
|
||||||
|
} else if (d->m_unitenum == KPressure::Millibar && unit == KPressure::InchesOfMercury) {
|
||||||
|
return (d->m_number / 33.8639);
|
||||||
|
} else if (d->m_unitenum == KPressure::InchesOfMercury && unit == KPressure::Kilopascal) {
|
||||||
|
return (d->m_number * 3.386398);
|
||||||
|
} else if (d->m_unitenum == KPressure::InchesOfMercury && unit == KPressure::Hectopascal) {
|
||||||
|
return (d->m_number * 33.8639);
|
||||||
|
} else if (d->m_unitenum == KPressure::InchesOfMercury && unit == KPressure::Millibar) {
|
||||||
|
return (d->m_number * 33.8639);
|
||||||
|
}
|
||||||
|
|
||||||
|
return d->m_number;
|
||||||
|
}
|
||||||
|
|
||||||
|
QString KPressure::description()
|
||||||
|
{
|
||||||
|
return i18n("Pressure");
|
||||||
|
}
|
||||||
|
|
||||||
|
QString KPressure::unitDescription(const KPresUnit unit)
|
||||||
|
{
|
||||||
|
switch (unit) {
|
||||||
|
case KPressure::Kilopascal:
|
||||||
|
return i18n("Kilopascal (kPa)");
|
||||||
|
case KPressure::Hectopascal:
|
||||||
|
return i18n("Hectopascal (hPa)");
|
||||||
|
case KPressure::Millibar:
|
||||||
|
return i18n("Millibar (mbar)");
|
||||||
|
case KPressure::InchesOfMercury:
|
||||||
|
return i18n("Inch of mercury (inHg)");
|
||||||
|
case KPressure::Invalid:
|
||||||
|
case KPressure::UnitCount:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return i18n("Unknown");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class KLengthPrivate {
|
||||||
|
public:
|
||||||
|
KLengthPrivate(const double number, const KLength::KLengUnit unit);
|
||||||
|
KLengthPrivate(const double number, const QString &unit);
|
||||||
|
|
||||||
|
double m_number;
|
||||||
|
QString m_unit;
|
||||||
|
KLength::KLengUnit m_unitenum;
|
||||||
|
};
|
||||||
|
|
||||||
|
KLengthPrivate::KLengthPrivate(const double number, const KLength::KLengUnit unit)
|
||||||
|
: m_number(number),
|
||||||
|
m_unitenum(unit)
|
||||||
|
{
|
||||||
|
if (unit == KLength::Mile) {
|
||||||
|
m_unit = QLatin1String("mi");
|
||||||
|
} else if (unit == KLength::Kilometer) {
|
||||||
|
m_unit = QLatin1String("km");
|
||||||
|
} else {
|
||||||
|
m_unit = QLatin1String("Unknown");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
KLengthPrivate::KLengthPrivate(const double number, const QString &unit)
|
||||||
|
: m_number(number),
|
||||||
|
m_unitenum(KLength::Invalid)
|
||||||
|
{
|
||||||
|
if (unit == QLatin1String("Mile")
|
||||||
|
|| unit == QLatin1String("mile") || unit == QLatin1String("miles")
|
||||||
|
|| unit == QLatin1String("mi")) {
|
||||||
|
m_unitenum = KLength::Mile;
|
||||||
|
m_unit = QLatin1String("mi");
|
||||||
|
} else if (unit == QLatin1String("Kilometer")
|
||||||
|
|| unit == QLatin1String("kilometer") || unit == QLatin1String("kilometers")
|
||||||
|
|| unit == QLatin1String("km")) {
|
||||||
|
m_unitenum = KLength::Kilometer;
|
||||||
|
m_unit = QLatin1String("km");
|
||||||
|
} else {
|
||||||
|
kDebug() << "invalid length unit" << unit;
|
||||||
|
m_unit = QLatin1String("Unknown");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
KLength::KLength(const double number, const QString &unit)
|
||||||
|
: d(new KLengthPrivate(number, unit))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
KLength::KLength(const double number, const KLengUnit unit)
|
||||||
|
: d(new KLengthPrivate(number, unit))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
KLength::~KLength()
|
||||||
|
{
|
||||||
|
delete d;
|
||||||
|
}
|
||||||
|
|
||||||
|
double KLength::number() const
|
||||||
|
{
|
||||||
|
return d->m_number;
|
||||||
|
}
|
||||||
|
|
||||||
|
QString KLength::unit() const
|
||||||
|
{
|
||||||
|
return d->m_unit;
|
||||||
|
}
|
||||||
|
|
||||||
|
KLength::KLengUnit KLength::unitEnum() const
|
||||||
|
{
|
||||||
|
return d->m_unitenum;
|
||||||
|
}
|
||||||
|
|
||||||
|
QString KLength::toString() const
|
||||||
|
{
|
||||||
|
return QString::fromLatin1("%1 %2").arg(QString::number(d->m_number), d->m_unit);
|
||||||
|
}
|
||||||
|
|
||||||
|
double KLength::convertTo(const KLengUnit unit) const
|
||||||
|
{
|
||||||
|
if (unit == d->m_unitenum) {
|
||||||
|
return d->m_number;
|
||||||
|
}
|
||||||
|
|
||||||
|
// for reference:
|
||||||
|
// https://www.rapidtables.com/convert/length/mile-to-km.html
|
||||||
|
// https://www.rapidtables.com/convert/length/km-to-mile.html
|
||||||
|
if (d->m_unitenum == KLength::Mile && unit == KLength::Kilometer) {
|
||||||
|
return (d->m_number * 1.609344);
|
||||||
|
} else if (d->m_unitenum == KLength::Kilometer && unit == KLength::Mile) {
|
||||||
|
return (d->m_number / 1.609344);
|
||||||
|
}
|
||||||
|
return 0.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
QString KLength::description()
|
||||||
|
{
|
||||||
|
return i18n("Length");
|
||||||
|
}
|
||||||
|
|
||||||
|
QString KLength::unitDescription(const KLengUnit unit)
|
||||||
|
{
|
||||||
|
switch (unit) {
|
||||||
|
case KLength::Mile:
|
||||||
|
return i18n("Mile (mi)");
|
||||||
|
case KLength::Kilometer:
|
||||||
|
return i18n("Kilometer (km)");
|
||||||
|
case KLength::Invalid:
|
||||||
|
case KLength::UnitCount:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return i18n("Unknown");
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,25 +22,50 @@
|
||||||
#include <QString>
|
#include <QString>
|
||||||
|
|
||||||
class KTemperaturePrivate;
|
class KTemperaturePrivate;
|
||||||
|
class KVelocityPrivate;
|
||||||
|
class KPressurePrivate;
|
||||||
|
class KLengthPrivate;
|
||||||
|
|
||||||
|
/*!
|
||||||
|
Base class for all other conversion classes
|
||||||
|
|
||||||
|
@since 4.20
|
||||||
|
@see KTemperature, KVelocity, KPressure, KLength
|
||||||
|
*/
|
||||||
|
class KUnitConversion {
|
||||||
|
public:
|
||||||
|
/*!
|
||||||
|
@return Rounded number up to number of decimal digits specified by @p digits
|
||||||
|
*/
|
||||||
|
static double round(const double number, const uint digits);
|
||||||
|
};
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
Temperature conversion class
|
Temperature conversion class
|
||||||
|
|
||||||
@since 4.20
|
@since 4.20
|
||||||
@todo implement other classes to substitute KUnitConversion
|
|
||||||
*/
|
*/
|
||||||
class KTemperature {
|
class KTemperature : public KUnitConversion {
|
||||||
public:
|
public:
|
||||||
enum KTempUnit {
|
enum KTempUnit {
|
||||||
Invalid,
|
Invalid = -1,
|
||||||
Celsius,
|
Celsius = 0,
|
||||||
Fahrenheit,
|
Fahrenheit = 1,
|
||||||
Kelvin
|
Kelvin = 2,
|
||||||
|
UnitCount = 3
|
||||||
};
|
};
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@brief Constructs convertor
|
@brief Constructs convertor
|
||||||
@param number value of the unit
|
@param number value of the unit
|
||||||
@param unit string representation of the unit one of: "°C", "C", "Celsius", "°F", "F", "Fahrenheit", "Kelvin" or "K"
|
@param unit unit enum, one of: Invalid, Celsius, Fahrenheit or Kelvin
|
||||||
|
*/
|
||||||
|
KTemperature(const double number, const KTempUnit unit);
|
||||||
|
/*!
|
||||||
|
@brief Constructs convertor
|
||||||
|
@param number value of the unit
|
||||||
|
@param unit string representation of the unit, one of: "°C", "C", "Celsius", "°F", "F",
|
||||||
|
"Fahrenheit", "Kelvin" or "K"
|
||||||
*/
|
*/
|
||||||
KTemperature(const double number, const QString &unit);
|
KTemperature(const double number, const QString &unit);
|
||||||
~KTemperature();
|
~KTemperature();
|
||||||
|
@ -50,26 +75,134 @@ public:
|
||||||
*/
|
*/
|
||||||
double number() const;
|
double number() const;
|
||||||
/*!
|
/*!
|
||||||
@return Short string representing the unit passed to the constructor, e.g. "°C", "°F" or "K"
|
@return Short string representing the unit passed to the constructor, e.g. "Unknown" (for
|
||||||
|
@p KTempUnit::Invalid), "°F" or "K"
|
||||||
*/
|
*/
|
||||||
QString unit() const;
|
QString unit() const;
|
||||||
/*!
|
/*!
|
||||||
@return Combination of the number and unit as string, e.g. "12 °C", "123 °F" or "123 K"
|
@return Enum representing the unit passed to the constructor, e.g. @p KTempUnit::Invalid,
|
||||||
|
@p KTempUnit::Fahrenheit or @p KTempUnit::Kelvin
|
||||||
|
*/
|
||||||
|
KTempUnit unitEnum() const;
|
||||||
|
/*!
|
||||||
|
@return Combination of the number and short unit as string, e.g. "12 °C", "123 °F" or
|
||||||
|
"123 K"
|
||||||
*/
|
*/
|
||||||
QString toString() const;
|
QString toString() const;
|
||||||
/*!
|
/*!
|
||||||
@return Number converted to different unit
|
@return Number converted to different unit
|
||||||
*/
|
*/
|
||||||
double convertTo(const KTempUnit unit) const;
|
double convertTo(const KTempUnit unit) const;
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@return Rounded number up to number of decimal digits specified by @p digits
|
@return Translated convertor description, e.g. "Температура"
|
||||||
*/
|
*/
|
||||||
static double round(const double number, const uint digits);
|
static QString description();
|
||||||
|
/*!
|
||||||
|
@return Translated unit description, e.g. "Unknown" (for @p KTempUnit::Invalid),
|
||||||
|
"Fahrenheitia (°F)" or "Келвин (K)"
|
||||||
|
*/
|
||||||
|
static QString unitDescription(const KTempUnit unit);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Q_DISABLE_COPY(KTemperature);
|
Q_DISABLE_COPY(KTemperature);
|
||||||
KTemperaturePrivate * const d;
|
KTemperaturePrivate * const d;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
/*!
|
||||||
|
Velocity conversion class
|
||||||
|
|
||||||
|
@since 4.20
|
||||||
|
*/
|
||||||
|
class KVelocity : public KUnitConversion {
|
||||||
|
public:
|
||||||
|
enum KVeloUnit {
|
||||||
|
Invalid = -1,
|
||||||
|
MeterPerSecond = 0,
|
||||||
|
KilometerPerHour = 1,
|
||||||
|
MilePerHour = 2,
|
||||||
|
Knot = 3,
|
||||||
|
UnitCount = 4
|
||||||
|
};
|
||||||
|
|
||||||
|
KVelocity(const double number, const KVeloUnit unit);
|
||||||
|
KVelocity(const double number, const QString &unit);
|
||||||
|
~KVelocity();
|
||||||
|
|
||||||
|
double number() const;
|
||||||
|
QString unit() const;
|
||||||
|
KVeloUnit unitEnum() const;
|
||||||
|
QString toString() const;
|
||||||
|
double convertTo(const KVeloUnit unit) const;
|
||||||
|
static QString description();
|
||||||
|
static QString unitDescription(const KVeloUnit unit);
|
||||||
|
|
||||||
|
private:
|
||||||
|
Q_DISABLE_COPY(KVelocity);
|
||||||
|
KVelocityPrivate * const d;
|
||||||
|
};
|
||||||
|
|
||||||
|
/*!
|
||||||
|
Pressure conversion class
|
||||||
|
|
||||||
|
@since 4.20
|
||||||
|
*/
|
||||||
|
class KPressure : public KUnitConversion {
|
||||||
|
public:
|
||||||
|
enum KPresUnit {
|
||||||
|
Invalid = -1,
|
||||||
|
Kilopascal = 0,
|
||||||
|
Hectopascal = 1,
|
||||||
|
Millibar = 2,
|
||||||
|
InchesOfMercury = 3,
|
||||||
|
UnitCount = 4
|
||||||
|
};
|
||||||
|
|
||||||
|
KPressure(const double number, const KPresUnit unit);
|
||||||
|
KPressure(const double number, const QString &unit);
|
||||||
|
~KPressure();
|
||||||
|
|
||||||
|
double number() const;
|
||||||
|
QString unit() const;
|
||||||
|
KPresUnit unitEnum() const;
|
||||||
|
QString toString() const;
|
||||||
|
double convertTo(const KPresUnit unit) const;
|
||||||
|
static QString description();
|
||||||
|
static QString unitDescription(const KPresUnit unit);
|
||||||
|
|
||||||
|
private:
|
||||||
|
Q_DISABLE_COPY(KPressure);
|
||||||
|
KPressurePrivate * const d;
|
||||||
|
};
|
||||||
|
|
||||||
|
/*!
|
||||||
|
Length conversion class
|
||||||
|
|
||||||
|
@since 4.20
|
||||||
|
*/
|
||||||
|
class KLength : public KUnitConversion {
|
||||||
|
public:
|
||||||
|
enum KLengUnit {
|
||||||
|
Invalid = -1,
|
||||||
|
Mile = 0,
|
||||||
|
Kilometer = 1,
|
||||||
|
UnitCount = 2
|
||||||
|
};
|
||||||
|
|
||||||
|
KLength(const double number, const KLengUnit unit);
|
||||||
|
KLength(const double number, const QString &unit);
|
||||||
|
~KLength();
|
||||||
|
|
||||||
|
double number() const;
|
||||||
|
QString unit() const;
|
||||||
|
KLengUnit unitEnum() const;
|
||||||
|
QString toString() const;
|
||||||
|
double convertTo(const KLengUnit unit) const;
|
||||||
|
static QString description();
|
||||||
|
static QString unitDescription(const KLengUnit unit);
|
||||||
|
|
||||||
|
private:
|
||||||
|
Q_DISABLE_COPY(KLength);
|
||||||
|
KLengthPrivate * const d;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // KUNITCONVERSION_H
|
||||||
|
|
Loading…
Add table
Reference in a new issue