File indexing completed on 2024-04-14 03:56:12
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 Unit &Unit::operator=(Unit &&other) 0109 { 0110 d.swap(other.d); 0111 return *this; 0112 } 0113 0114 bool Unit::operator==(const Unit &other) const 0115 { 0116 if (d && other.d) { 0117 return (*d == *other.d); 0118 } else { 0119 return (d == other.d); 0120 } 0121 } 0122 0123 bool Unit::operator!=(const Unit &other) const 0124 { 0125 if (d && other.d) { 0126 return (*d != *other.d); 0127 } else { 0128 return (d != other.d); 0129 } 0130 } 0131 0132 bool Unit::isNull() const 0133 { 0134 return !d; 0135 } 0136 0137 bool Unit::isValid() const 0138 { 0139 return (d && !d->m_symbol.isEmpty()); 0140 } 0141 0142 UnitId Unit::id() const 0143 { 0144 if (d) { 0145 return d->m_id; 0146 } 0147 return InvalidUnit; 0148 } 0149 0150 CategoryId Unit::categoryId() const 0151 { 0152 if (d) { 0153 return d->m_categoryId; 0154 } 0155 return InvalidCategory; 0156 } 0157 0158 UnitCategory Unit::category() const 0159 { 0160 if (d) { 0161 return UnitCategory(d->m_category); 0162 } 0163 return UnitCategory(); 0164 } 0165 0166 QString Unit::description() const 0167 { 0168 if (d) { 0169 return d->m_description; 0170 } 0171 return QString(); 0172 } 0173 0174 QString Unit::symbol() const 0175 { 0176 if (d) { 0177 return d->m_symbol; 0178 } 0179 return QString(); 0180 } 0181 0182 void Unit::setUnitMultiplier(qreal multiplier) 0183 { 0184 if (d) { 0185 d->setUnitMultiplier(multiplier); 0186 } 0187 } 0188 0189 qreal Unit::toDefault(qreal value) const 0190 { 0191 if (d) { 0192 return d->toDefault(value); 0193 } 0194 return 0; 0195 } 0196 0197 qreal Unit::fromDefault(qreal value) const 0198 { 0199 if (d) { 0200 return d->fromDefault(value); 0201 } 0202 return 0; 0203 } 0204 0205 QString Unit::toString(qreal value, int fieldWidth, char format, int precision, const QChar &fillChar) const 0206 { 0207 if (isNull()) { 0208 return QString(); 0209 } 0210 if ((int)value == value && precision < 1) { 0211 return d->m_integerString.subs((int)value).toString(); 0212 } 0213 return d->m_realString.subs(value, fieldWidth, format, precision, fillChar).toString(); 0214 } 0215 0216 QString Unit::toSymbolString(qreal value, int fieldWidth, char format, int precision, const QChar &fillChar) const 0217 { 0218 if (d) { 0219 return d->m_symbolString.subs(value, fieldWidth, format, precision, fillChar).subs(d->m_symbol).toString(); 0220 } 0221 return QString(); 0222 } 0223 0224 }