From 1414a4f05f387dea143135bcdfbb24582be1809b Mon Sep 17 00:00:00 2001 From: Ivailo Monev Date: Thu, 15 Jul 2021 22:39:03 +0300 Subject: [PATCH] kdecore: new class for temperature conversion Signed-off-by: Ivailo Monev --- kdecore/CMakeLists.txt | 2 + kdecore/util/kunitconversion.cpp | 94 ++++++++++++++++++++++++++++++++ kdecore/util/kunitconversion.h | 74 +++++++++++++++++++++++++ 3 files changed, 170 insertions(+) create mode 100644 kdecore/util/kunitconversion.cpp create mode 100644 kdecore/util/kunitconversion.h diff --git a/kdecore/CMakeLists.txt b/kdecore/CMakeLists.txt index 042127e9..fdbc842d 100644 --- a/kdecore/CMakeLists.txt +++ b/kdecore/CMakeLists.txt @@ -235,6 +235,7 @@ set(kdecore_LIB_SRCS util/kshell.cpp util/krandom.cpp util/krandomsequence.cpp + util/kunitconversion.cpp util/qtest_kde.cpp kernel/ktoolinvocation_x11.cpp @@ -420,6 +421,7 @@ install( util/kshell.h util/krandom.h util/krandomsequence.h + util/kunitconversion.h util/ksharedptr.h util/ksortablelist.h util/kuser.h diff --git a/kdecore/util/kunitconversion.cpp b/kdecore/util/kunitconversion.cpp new file mode 100644 index 00000000..d6b842c1 --- /dev/null +++ b/kdecore/util/kunitconversion.cpp @@ -0,0 +1,94 @@ +/* This file is part of the KDE libraries + Copyright (C) 2021 Ivailo Monev + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License version 2, as published by the Free Software Foundation. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#include "kunitconversion.h" +#include "kdebug.h" + +#include + +class KTemperaturePrivate { +public: + KTemperaturePrivate(const double number, const QString &unit); + + double m_number; + QString m_unit; + KTemperature::KTempUnit m_tempunit; +}; + +KTemperaturePrivate::KTemperaturePrivate(const double number, const QString &unit) + : m_number(number), + m_tempunit(KTemperature::Invalid) +{ + if (unit == QString::fromUtf8("°C") || unit == QLatin1String("C") || unit == QLatin1String("Celsius")) { + m_tempunit = KTemperature::Celsius; + m_unit = QString::fromUtf8("°C"); + } else if (unit == QString::fromUtf8("°F") || unit == QLatin1String("F") || unit == QLatin1String("Fahrenheit")) { + m_tempunit = KTemperature::Fahrenheit; + m_unit = QString::fromUtf8("°F"); + } else { + kDebug() << "invalid unit" << unit; + m_unit = QLatin1String("Unknown"); + } +} + +KTemperature::KTemperature(const double number, const QString &unit) + : d(new KTemperaturePrivate(number, unit)) +{ +} + +KTemperature::~KTemperature() +{ + delete d; +} + +double KTemperature::number() const +{ + return d->m_number; +} + +QString KTemperature::unit() const +{ + return d->m_unit; +} + +QString KTemperature::toString() const +{ + return QString::fromLatin1("%1 %2").arg(QString::number(d->m_number), d->m_unit); +} + +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); + } + return 0.0; +} + +double KTemperature::round(const double number, const uint digits) +{ + const double poweroften = qPow(10, digits); + return qRound(number * poweroften) / poweroften; +} diff --git a/kdecore/util/kunitconversion.h b/kdecore/util/kunitconversion.h new file mode 100644 index 00000000..dc4dc922 --- /dev/null +++ b/kdecore/util/kunitconversion.h @@ -0,0 +1,74 @@ +/* This file is part of the KDE libraries + Copyright (C) 2021 Ivailo Monev + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License version 2, as published by the Free Software Foundation. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#ifndef KUNITCONVERSION_H +#define KUNITCONVERSION_H + +#include + +class KTemperaturePrivate; + +/*! + Temperature conversion class + @since 4.20 + @todo implement other classes to substitute KUnitConversion +*/ +class KTemperature { +public: + enum KTempUnit { + Invalid, + Celsius, + Fahrenheit + }; + + /*! + @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" + */ + KTemperature(const double number, const QString &unit); + ~KTemperature(); + + /*! + @return Same number as the value passed to the constructor + */ + double number() const; + /*! + @return Short string representing the unit passed to the constructor, e.g. "°C" or "°F" + */ + QString unit() const; + /*! + @return Combination of the number and unit as string, e.g. "12 °C" or "123 °F" + */ + QString toString() const; + /*! + @return Number converted to different unit + */ + double convertTo(const KTempUnit unit) const; + + /*! + @return Rounded number up to number of decimal digits specified by @p digits + */ + static double round(const double number, const uint digits); + +private: + Q_DISABLE_COPY(KTemperature); + KTemperaturePrivate * const d; +}; + +#endif