mirror of
https://bitbucket.org/smil3y/kdelibs.git
synced 2025-02-24 19:02:48 +00:00
Merge branch 'master' of https://github.com/fluxer/kdelibs into devinfo
This commit is contained in:
commit
76a47c5651
3 changed files with 170 additions and 0 deletions
|
@ -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
|
||||
|
|
94
kdecore/util/kunitconversion.cpp
Normal file
94
kdecore/util/kunitconversion.cpp
Normal file
|
@ -0,0 +1,94 @@
|
|||
/* This file is part of the KDE libraries
|
||||
Copyright (C) 2021 Ivailo Monev <xakepa10@gmail.com>
|
||||
|
||||
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 <qmath.h>
|
||||
|
||||
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;
|
||||
}
|
74
kdecore/util/kunitconversion.h
Normal file
74
kdecore/util/kunitconversion.h
Normal file
|
@ -0,0 +1,74 @@
|
|||
/* This file is part of the KDE libraries
|
||||
Copyright (C) 2021 Ivailo Monev <xakepa10@gmail.com>
|
||||
|
||||
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 <QString>
|
||||
|
||||
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
|
Loading…
Add table
Reference in a new issue