Warning, file /frameworks/kunitconversion/src/unitcategory.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 "unitcategory.h"
0009 #include "unit_p.h"
0010 #include "unitcategory_p.h"
0011 
0012 #include <KLocalizedString>
0013 
0014 namespace KUnitConversion
0015 {
0016 UnitCategoryPrivate::UnitCategoryPrivate()
0017     : m_id(InvalidCategory)
0018 {
0019 }
0020 
0021 UnitCategoryPrivate::UnitCategoryPrivate(CategoryId id, const QString &name, const QString &description)
0022     : m_id(id)
0023     , m_name(name)
0024     , m_description(description)
0025 {
0026 }
0027 
0028 UnitCategoryPrivate::~UnitCategoryPrivate()
0029 {
0030 }
0031 
0032 UnitCategoryPrivate *UnitCategoryPrivate::clone()
0033 {
0034     return new UnitCategoryPrivate(*this);
0035 }
0036 
0037 bool UnitCategoryPrivate::operator==(const UnitCategoryPrivate &other) const
0038 {
0039     return (m_id == other.m_id);
0040 }
0041 
0042 bool UnitCategoryPrivate::operator!=(const UnitCategoryPrivate &other) const
0043 {
0044     return !(*this == other);
0045 }
0046 
0047 Value UnitCategoryPrivate::convert(const Value &value, const Unit &toUnit)
0048 {
0049     qreal v = value.unit().toDefault(value.number());
0050     return Value(toUnit.fromDefault(v), toUnit);
0051 }
0052 
0053 UnitCategory::UnitCategory()
0054     : d(nullptr)
0055 {
0056 }
0057 
0058 UnitCategory::UnitCategory(UnitCategoryPrivate *dd)
0059     : d(dd)
0060 {
0061 }
0062 
0063 UnitCategory::UnitCategory(const UnitCategory &other)
0064     : d(other.d)
0065 {
0066 }
0067 
0068 UnitCategory::~UnitCategory()
0069 {
0070 }
0071 
0072 UnitCategory &UnitCategory::operator=(const UnitCategory &other)
0073 {
0074     d = other.d;
0075     return *this;
0076 }
0077 
0078 bool UnitCategory::operator==(const UnitCategory &other) const
0079 {
0080     if (d && other.d) {
0081         return (*d == *other.d);
0082     } else {
0083         return (d == other.d);
0084     }
0085 }
0086 
0087 bool UnitCategory::operator!=(const UnitCategory &other) const
0088 {
0089     if (d && other.d) {
0090         return (*d != *other.d);
0091     } else {
0092         return (d != other.d);
0093     }
0094 }
0095 
0096 bool UnitCategory::isNull() const
0097 {
0098     return !d;
0099 }
0100 
0101 CategoryId UnitCategory::id() const
0102 {
0103     if (d) {
0104         return d->m_id;
0105     }
0106     return InvalidCategory;
0107 }
0108 
0109 QList<Unit> UnitCategory::units() const
0110 {
0111     if (d) {
0112         return d->m_units;
0113     }
0114     return QList<Unit>();
0115 }
0116 
0117 QList<Unit> UnitCategory::mostCommonUnits() const
0118 {
0119     if (d) {
0120         return d->m_mostCommonUnits;
0121     }
0122     return QList<Unit>();
0123 }
0124 
0125 QStringList UnitCategory::allUnits() const
0126 {
0127     if (d) {
0128         return d->m_unitMap.keys();
0129     }
0130     return QStringList();
0131 }
0132 
0133 bool UnitCategory::hasUnit(const QString &unit) const
0134 {
0135     if (d) {
0136         return d->m_unitMap.contains(unit);
0137     }
0138     return false;
0139 }
0140 
0141 Value UnitCategory::convert(const Value &value, const QString &toUnit)
0142 {
0143     if (d && (toUnit.isEmpty() || d->m_unitMap.contains(toUnit)) && value.unit().isValid()) {
0144         Unit to = toUnit.isEmpty() ? defaultUnit() : d->m_unitMap[toUnit];
0145         return convert(value, to);
0146     }
0147     return Value();
0148 }
0149 
0150 Value UnitCategory::convert(const Value &value, UnitId toUnit)
0151 {
0152     if (d && d->m_idMap.contains(toUnit) && value.unit().isValid()) {
0153         return convert(value, d->m_idMap[toUnit]);
0154     }
0155     return Value();
0156 }
0157 
0158 Value UnitCategory::convert(const Value &value, const Unit &toUnit)
0159 {
0160     if (d && !toUnit.isNull()) {
0161         return d->convert(value, toUnit);
0162     }
0163     return Value();
0164 }
0165 
0166 Unit UnitCategory::unit(const QString &s) const
0167 {
0168     if (d) {
0169         return d->m_unitMap.value(s);
0170     }
0171     return Unit();
0172 }
0173 
0174 Unit UnitCategory::unit(UnitId unitId) const
0175 {
0176     if (d && d->m_idMap.contains(unitId)) {
0177         return d->m_idMap[unitId];
0178     }
0179     return Unit();
0180 }
0181 
0182 QString UnitCategory::name() const
0183 {
0184     if (d) {
0185         return d->m_name;
0186     }
0187     return QString();
0188 }
0189 
0190 Unit UnitCategory::defaultUnit() const
0191 {
0192     if (d) {
0193         return d->m_defaultUnit;
0194     }
0195     return Unit();
0196 }
0197 
0198 QString UnitCategory::description() const
0199 {
0200     if (d) {
0201         return d->m_description;
0202     }
0203     return QString();
0204 }
0205 
0206 void UnitCategory::addDefaultUnit(const Unit &unit)
0207 {
0208     if (d) {
0209         d->addDefaultUnit(unit);
0210     }
0211 }
0212 
0213 void UnitCategoryPrivate::addDefaultUnit(const Unit &unit)
0214 {
0215     addCommonUnit(unit);
0216     m_defaultUnit = unit;
0217 }
0218 
0219 void UnitCategory::addCommonUnit(const Unit &unit)
0220 {
0221     if (d) {
0222         d->addCommonUnit(unit);
0223     }
0224 }
0225 
0226 void UnitCategoryPrivate::addCommonUnit(const Unit &unit)
0227 {
0228     addUnit(unit);
0229     m_mostCommonUnits.append(unit);
0230 }
0231 
0232 void UnitCategory::addUnit(const Unit &unit)
0233 {
0234     if (d) {
0235         d->addUnit(unit);
0236     }
0237 }
0238 
0239 void UnitCategoryPrivate::addUnit(const Unit &unit)
0240 {
0241     // ### this is emulating a weak_ptr to break a reference cycle between Unit and UnitCategory
0242     // ### even without that, this is slicing the polymorphic part of UnitCategory
0243     // this only works by chance as apart from the ctors those parts contain no logic or data it seems
0244     unit.d->m_category = this;
0245     const QStringList list = unit.d->m_matchString.split(QLatin1Char(';'), Qt::SkipEmptyParts);
0246     Q_ASSERT_X(!list.isEmpty(),
0247                "UnitCategoryPrivate::addUnit",
0248                QLatin1String("Empty match string for unit symbol: '%1' '%2' - fix translation?").arg(unit.symbol(), unit.description()).toUtf8().constData());
0249 
0250     for (const QString &name : list) {
0251         m_unitMap[name] = unit;
0252     }
0253     m_idMap[unit.id()] = unit;
0254     m_units.append(unit);
0255 }
0256 
0257 bool UnitCategory::hasOnlineConversionTable() const
0258 {
0259     return d->hasOnlineConversionTable();
0260 }
0261 
0262 void UnitCategory::syncConversionTable(std::chrono::seconds updateSkipPeriod)
0263 {
0264     d->syncConversionTable(updateSkipPeriod);
0265 }
0266 
0267 }