mirror of
https://bitbucket.org/smil3y/kdelibs.git
synced 2025-02-24 02:42:48 +00:00
kdecore: implement Kelvin unit support
Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
parent
1414a4f05f
commit
8b0241b034
2 changed files with 29 additions and 10 deletions
|
@ -40,6 +40,11 @@ KTemperaturePrivate::KTemperaturePrivate(const double number, const QString &uni
|
|||
} else if (unit == QString::fromUtf8("°F") || unit == QLatin1String("F") || unit == QLatin1String("Fahrenheit")) {
|
||||
m_tempunit = KTemperature::Fahrenheit;
|
||||
m_unit = QString::fromUtf8("°F");
|
||||
// kelvin and kelvins are for KUnitConversion compatibility
|
||||
} else if (unit == QLatin1String("K") || unit == QLatin1String("Kelvin")
|
||||
|| unit == QLatin1String("kelvin") || unit == QLatin1String("kelvins")) {
|
||||
m_tempunit = KTemperature::Kelvin;
|
||||
m_unit = QLatin1String("K");
|
||||
} else {
|
||||
kDebug() << "invalid unit" << unit;
|
||||
m_unit = QLatin1String("Unknown");
|
||||
|
@ -73,16 +78,29 @@ QString KTemperature::toString() const
|
|||
|
||||
double KTemperature::convertTo(const KTempUnit unit) const
|
||||
{
|
||||
static const double celsius_fahrenheit_ratio = 33.8;
|
||||
|
||||
if (unit == d->m_tempunit) {
|
||||
return d->m_number;
|
||||
}
|
||||
|
||||
if (d->m_tempunit == KTemperature::Fahrenheit && unit == KTemperature::Celsius) {
|
||||
return (d->m_number / celsius_fahrenheit_ratio);
|
||||
} else if (d->m_tempunit == KTemperature::Celsius && unit == KTemperature::Fahrenheit) {
|
||||
return (d->m_number * celsius_fahrenheit_ratio);
|
||||
// for reference:
|
||||
// https://www.rapidtables.com/convert/temperature/celsius-to-fahrenheit.html
|
||||
// https://www.rapidtables.com/convert/temperature/celsius-to-kelvin.html
|
||||
// https://www.rapidtables.com/convert/temperature/fahrenheit-to-celsius.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-fahrenheit.html
|
||||
if (d->m_tempunit == KTemperature::Celsius && unit == KTemperature::Fahrenheit) {
|
||||
return (d->m_number * 1.8 + 32);
|
||||
} else if (d->m_tempunit == KTemperature::Celsius && unit == KTemperature::Kelvin) {
|
||||
return (d->m_number + 273.15);
|
||||
} else if (d->m_tempunit == KTemperature::Fahrenheit && unit == KTemperature::Celsius) {
|
||||
return ((d->m_number - 32) / 1.8);
|
||||
} else if (d->m_tempunit == KTemperature::Fahrenheit && unit == KTemperature::Kelvin) {
|
||||
return ((d->m_number + 459.67) * 0.5);
|
||||
} else if (d->m_tempunit == KTemperature::Kelvin && unit == KTemperature::Celsius) {
|
||||
return (d->m_number - 273.15);
|
||||
} else if (d->m_tempunit == KTemperature::Kelvin && unit == KTemperature::Fahrenheit) {
|
||||
return (d->m_number * 1.8 - 459.67);
|
||||
}
|
||||
return 0.0;
|
||||
}
|
||||
|
|
|
@ -33,13 +33,14 @@ public:
|
|||
enum KTempUnit {
|
||||
Invalid,
|
||||
Celsius,
|
||||
Fahrenheit
|
||||
Fahrenheit,
|
||||
Kelvin
|
||||
};
|
||||
|
||||
/*!
|
||||
@brief Constructs convertor
|
||||
@param number value of the unit
|
||||
@param unit string representation of the unit one of: "°C", "C", "Celsius", "°F", "F" or "Fahrenheit"
|
||||
@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();
|
||||
|
@ -49,11 +50,11 @@ public:
|
|||
*/
|
||||
double number() const;
|
||||
/*!
|
||||
@return Short string representing the unit passed to the constructor, e.g. "°C" or "°F"
|
||||
@return Short string representing the unit passed to the constructor, e.g. "°C", "°F" or "K"
|
||||
*/
|
||||
QString unit() const;
|
||||
/*!
|
||||
@return Combination of the number and unit as string, e.g. "12 °C" or "123 °F"
|
||||
@return Combination of the number and unit as string, e.g. "12 °C", "123 °F" or "123 K"
|
||||
*/
|
||||
QString toString() const;
|
||||
/*!
|
||||
|
|
Loading…
Add table
Reference in a new issue