Warning, file /frameworks/kunitconversion/src/unit.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002  *   SPDX-FileCopyrightText: 2007-2009 Petri Damstén <damu@iki.fi>
0003  *   SPDX-FileCopyrightText: 2014 John Layt <jlayt@kde.org>
0004  *
0005  *   SPDX-License-Identifier: LGPL-2.0-or-later
0006  */
0007 
0008 #include "unit.h"
0009 #include "unit_p.h"
0010 #include "unitcategory.h"
0011 
0012 #include <KLocalizedString>
0013 
0014 namespace KUnitConversion
0015 {
0016 UnitPrivate::UnitPrivate()
0017     : m_categoryId(InvalidCategory)
0018     , m_id(InvalidUnit)
0019     , m_multiplier(1.0)
0020 {
0021 }
0022 
0023 UnitPrivate::UnitPrivate(CategoryId categoryId,
0024                          UnitId id,
0025                          qreal multiplier,
0026                          const QString &symbol,
0027                          const QString &description,
0028                          const QString &matchString,
0029                          const KLocalizedString &symbolString,
0030                          const KLocalizedString &realString,
0031                          const KLocalizedString &integerString)
0032     : m_categoryId(categoryId)
0033     , m_id(id)
0034     , m_multiplier(multiplier)
0035     , m_symbol(symbol)
0036     , m_description(description)
0037     , m_matchString(matchString)
0038     , m_symbolString(symbolString)
0039     , m_realString(realString)
0040     , m_integerString(integerString)
0041 {
0042 }
0043 
0044 UnitPrivate::~UnitPrivate()
0045 {
0046 }
0047 
0048 UnitPrivate *UnitPrivate::clone()
0049 {
0050     return new UnitPrivate(*this);
0051 }
0052 
0053 bool UnitPrivate::operator==(const UnitPrivate &other) const
0054 {
0055     return (m_id == other.m_id && m_symbol == other.m_symbol);
0056 }
0057 
0058 bool UnitPrivate::operator!=(const UnitPrivate &other) const
0059 {
0060     return !(*this == other);
0061 }
0062 
0063 void UnitPrivate::setUnitMultiplier(qreal multiplier)
0064 {
0065     m_multiplier = multiplier;
0066 }
0067 
0068 qreal UnitPrivate::unitMultiplier() const
0069 {
0070     return m_multiplier;
0071 }
0072 
0073 qreal UnitPrivate::toDefault(qreal value) const
0074 {
0075     return value * m_multiplier;
0076 }
0077 
0078 qreal UnitPrivate::fromDefault(qreal value) const
0079 {
0080     return value / m_multiplier;
0081 }
0082 
0083 Unit::Unit()
0084     : d(nullptr)
0085 {
0086 }
0087 
0088 Unit::Unit(UnitPrivate *dd)
0089     : d(dd)
0090 {
0091 }
0092 
0093 Unit::Unit(const Unit &other)
0094     : d(other.d)
0095 {
0096 }
0097 
0098 Unit::~Unit()
0099 {
0100 }
0101 
0102 Unit &Unit::operator=(const Unit &other)
0103 {
0104     d = other.d;
0105     return *this;
0106 }
0107 
0108 bool Unit::operator==(const Unit &other) const
0109 {
0110     if (d && other.d) {
0111         return (*d == *other.d);
0112     } else {
0113         return (d == other.d);
0114     }
0115 }
0116 
0117 bool Unit::operator!=(const Unit &other) const
0118 {
0119     if (d && other.d) {
0120         return (*d != *other.d);
0121     } else {
0122         return (d != other.d);
0123     }
0124 }
0125 
0126 bool Unit::isNull() const
0127 {
0128     return !d;
0129 }
0130 
0131 bool Unit::isValid() const
0132 {
0133     return (d && !d->m_symbol.isEmpty());
0134 }
0135 
0136 UnitId Unit::id() const
0137 {
0138     if (d) {
0139         return d->m_id;
0140     }
0141     return InvalidUnit;
0142 }
0143 
0144 CategoryId Unit::categoryId() const
0145 {
0146     if (d) {
0147         return d->m_categoryId;
0148     }
0149     return InvalidCategory;
0150 }
0151 
0152 UnitCategory Unit::category() const
0153 {
0154     if (d) {
0155         return UnitCategory(d->m_category);
0156     }
0157     return UnitCategory();
0158 }
0159 
0160 QString Unit::description() const
0161 {
0162     if (d) {
0163         return d->m_description;
0164     }
0165     return QString();
0166 }
0167 
0168 QString Unit::symbol() const
0169 {
0170     if (d) {
0171         return d->m_symbol;
0172     }
0173     return QString();
0174 }
0175 
0176 void Unit::setUnitMultiplier(qreal multiplier)
0177 {
0178     if (d) {
0179         d->setUnitMultiplier(multiplier);
0180     }
0181 }
0182 
0183 qreal Unit::toDefault(qreal value) const
0184 {
0185     if (d) {
0186         return d->toDefault(value);
0187     }
0188     return 0;
0189 }
0190 
0191 qreal Unit::fromDefault(qreal value) const
0192 {
0193     if (d) {
0194         return d->fromDefault(value);
0195     }
0196     return 0;
0197 }
0198 
0199 QString Unit::toString(qreal value, int fieldWidth, char format, int precision, const QChar &fillChar) const
0200 {
0201     if (isNull()) {
0202         return QString();
0203     }
0204     if ((int)value == value && precision < 1) {
0205         return d->m_integerString.subs((int)value).toString();
0206     }
0207     return d->m_realString.subs(value, fieldWidth, format, precision, fillChar).toString();
0208 }
0209 
0210 QString Unit::toSymbolString(qreal value, int fieldWidth, char format, int precision, const QChar &fillChar) const
0211 {
0212     if (d) {
0213         return d->m_symbolString.subs(value, fieldWidth, format, precision, fillChar).subs(d->m_symbol).toString();
0214     }
0215     return QString();
0216 }
0217 
0218 }