File indexing completed on 2024-04-21 11:37:10

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 #ifndef KUNITCONVERSION_UNITCATEGORY_H
0009 #define KUNITCONVERSION_UNITCATEGORY_H
0010 
0011 #include "kunitconversion/kunitconversion_export.h"
0012 #include "unit.h"
0013 #include "value.h"
0014 #include <QExplicitlySharedDataPointer>
0015 #include <QString>
0016 #include <QStringList>
0017 #include <chrono>
0018 
0019 namespace KUnitConversion
0020 {
0021 class UnitCategoryPrivate;
0022 
0023 /**
0024  * @short Class to define a category of units of measurement
0025  *
0026  * This is a class to define a category of units of measurement.
0027  *
0028  * @see Converter, Unit, Value
0029  *
0030  * @author Petri Damstén <damu@iki.fi>
0031  * @author John Layt <jlayt@kde.org>
0032  */
0033 
0034 class KUNITCONVERSION_EXPORT UnitCategory
0035 {
0036 public:
0037     /**
0038      * Null constructor
0039      **/
0040     UnitCategory();
0041 
0042     /**
0043      * Copy constructor, copy @p other to this.
0044      **/
0045     UnitCategory(const UnitCategory &other);
0046 
0047     // TODO KF6: remove virtual
0048     virtual ~UnitCategory();
0049 
0050     /**
0051      * Assignment operator, assign @p other to this.
0052      **/
0053     UnitCategory &operator=(const UnitCategory &other);
0054 
0055     // TODO KF6: de-inline
0056 #ifdef Q_COMPILER_RVALUE_REFS
0057     /**
0058      * Move-assigns @p other to this UnitCategory instance, transferring the
0059      * ownership of the managed pointer to this instance.
0060      **/
0061     UnitCategory &operator=(UnitCategory &&other)
0062     {
0063         swap(other);
0064         return *this;
0065     }
0066 #endif
0067 
0068     // TODO KF6: remove
0069     /**
0070      * Swaps this UnitCategory with @p other. This function is very fast and never fails.
0071      **/
0072     void swap(UnitCategory &other)
0073     {
0074         d.swap(other.d);
0075     }
0076 
0077     /**
0078      * @return @c true if this UnitCategory is equal to the @p other UnitCategory.
0079      **/
0080     bool operator==(const UnitCategory &other) const;
0081 
0082     /**
0083      * @return @c true if this UnitCategory is not equal to the @p other UnitCategory.
0084      **/
0085     bool operator!=(const UnitCategory &other) const;
0086 
0087     /**
0088      * @return returns true if this UnitCategory is null
0089      **/
0090     bool isNull() const;
0091 
0092     /**
0093      * @return category id.
0094      **/
0095     CategoryId id() const;
0096 
0097     /**
0098      * Returns name for the unit category.
0099      *
0100      * @return Translated name for category.
0101      **/
0102     QString name() const;
0103 
0104     /**
0105      * @return unit category description
0106      **/
0107     QString description() const;
0108 
0109     /**
0110      * Returns default unit.
0111      *
0112      * @return default unit.
0113      **/
0114     Unit defaultUnit() const;
0115 
0116     /**
0117      * Check if unit category has a unit.
0118      *
0119      * @return True if unit is found
0120      **/
0121     bool hasUnit(const QString &unit) const;
0122 
0123     /**
0124      * Return unit for string.
0125      *
0126      * @return Pointer to unit class.
0127      **/
0128     Unit unit(const QString &s) const;
0129 
0130     /**
0131      * Return unit for unit enum.
0132      *
0133      * @return Pointer to unit class.
0134      **/
0135     Unit unit(UnitId unitId) const;
0136 
0137     /**
0138      * Return units in this category.
0139      *
0140      * @return list of units.
0141      **/
0142     QList<Unit> units() const;
0143 
0144     /**
0145      * Return most common units in this category.
0146      *
0147      * @return list of units.
0148      **/
0149     QList<Unit> mostCommonUnits() const;
0150 
0151     /**
0152      * Return all unit names, short names and unit synonyms in this category.
0153      *
0154      * @return list of units.
0155      **/
0156     QStringList allUnits() const;
0157 
0158     // TODO KF6: make const
0159     /**
0160      * Convert value to another unit selected by string.
0161      *
0162      * @param value value to convert
0163      * @param toUnit unit to convert to. If empty default unit is used.
0164      * @return converted value
0165      **/
0166     Value convert(const Value &value, const QString &toUnit = QString());
0167 
0168     // TODO KF6: make const
0169     /**
0170      * Convert value to another unit selected by enum.
0171      *
0172      * @param value value to convert
0173      * @param toUnit unit to convert to.
0174      * @return converted value
0175      **/
0176     Value convert(const Value &value, UnitId toUnit);
0177 
0178     // TODO KF6: make const, remove virtual
0179     /**
0180      * Convert value to another unit.
0181      *
0182      * @param value value to convert
0183      * @param toUnit unit to be used for conversion
0184      * @return converted value
0185      **/
0186     virtual Value convert(const Value &value, const Unit &toUnit);
0187 
0188     /**
0189      * @return true if category has conversion table that needs to be updated via online access, otherwise false
0190      */
0191     bool hasOnlineConversionTable() const;
0192 
0193     /**
0194      * Explicit request to sync conversion table when it is older than @p updateSkipPeriod.
0195      *
0196      * This method is supposed to be called at a convenient time at application startup. Yet it is
0197      * safe to already do unit conversions that require an up-to-date conversion table (like currency).
0198      * Those conversions yet block internally until the table is up-to-date.
0199      */
0200     void syncConversionTable(std::chrono::seconds updateSkipPeriod);
0201 
0202 protected:
0203     // TODO KF6: remove those three
0204     void addDefaultUnit(const Unit &unit);
0205     void addCommonUnit(const Unit &unit);
0206     void addUnit(const Unit &unit);
0207 
0208 private:
0209     friend class Unit;
0210     friend class UnitCategoryPrivate;
0211 
0212     KUNITCONVERSION_NO_EXPORT explicit UnitCategory(UnitCategoryPrivate *dd);
0213 
0214 protected:
0215     QExplicitlySharedDataPointer<UnitCategoryPrivate> d;
0216 };
0217 
0218 } // KUnitConversion namespace
0219 
0220 #endif