File indexing completed on 2024-04-21 03:58:29

0001 /*
0002  *   SPDX-FileCopyrightText: 2008-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_CONVERTER_H
0009 #define KUNITCONVERSION_CONVERTER_H
0010 
0011 #include <kunitconversion/kunitconversion_export.h>
0012 
0013 #include "unitcategory.h"
0014 
0015 #include <QExplicitlySharedDataPointer>
0016 
0017 namespace KUnitConversion
0018 {
0019 class Value;
0020 class UnitCategory;
0021 class ConverterPrivate;
0022 
0023 /**
0024  * @short Class for converting values between units of measurement
0025  *
0026  * This is a class to convert values between different units of measurement.
0027  *
0028  * @see Unit, UnitCategory, Value
0029  *
0030  * @author Petri Damstén <damu@iki.fi>
0031  * @author John Layt <jlayt@kde.org>
0032  */
0033 
0034 class KUNITCONVERSION_EXPORT Converter
0035 {
0036 public:
0037     /**
0038      * Creates a Converter instance.
0039      */
0040     Converter();
0041     /**
0042      * Destroys this Converter instance.
0043      */
0044     ~Converter();
0045     /**
0046      * Copy constructor.
0047      * @param other existing Converter instance.
0048      */
0049     Converter(const Converter &other);
0050 
0051     /**
0052      * Assignment operator, assign @p other to this.
0053      **/
0054     Converter &operator=(const Converter &other);
0055 
0056     /**
0057      * Move-assigns @p other to this Converter instance, transferring the
0058      * ownership of the managed pointer to this instance.
0059      **/
0060     Converter &operator=(Converter &&other);
0061 
0062     /**
0063      * Convert value to another unit.
0064      *
0065      * @param value value to convert
0066      * @param toUnit unit to convert to. If empty default unit is used.
0067      * @return converted value
0068      **/
0069     Value convert(const Value &value, const QString &toUnit = QString()) const;
0070     Value convert(const Value &value, UnitId toUnit) const;
0071     Value convert(const Value &value, const Unit &toUnit) const;
0072 
0073     /**
0074      * Find unit category for unit.
0075      *
0076      * @param unit unit to find category for.
0077      * @return unit category for unit
0078      **/
0079     UnitCategory categoryForUnit(const QString &unit) const;
0080 
0081     /**
0082      * Find unit for string unit.
0083      *
0084      * @param unitString unit string to find unit for.
0085      * @return unit for string unit
0086      **/
0087     Unit unit(const QString &unitString) const;
0088 
0089     /**
0090      * Find unit for unit enum.
0091      *
0092      * @param unitId unit enum to find unit for.
0093      * @return unit for string unit
0094      **/
0095     Unit unit(UnitId unitId) const;
0096 
0097     /**
0098      * Find unit category.
0099      *
0100      * @param category name of the category to find (length, area, mass, etc.).
0101      * @return unit category named category or invalid category.
0102      **/
0103     UnitCategory category(const QString &category) const;
0104 
0105     /**
0106      * Find unit category.
0107      *
0108      * @param categoryId id of the category to find (LengthCategory, AreaCategory, etc.).
0109      * @return unit category which id is categoryId or invalid category.
0110      **/
0111     UnitCategory category(CategoryId categoryId) const;
0112 
0113     /**
0114      * Returns a list of all unit categories.
0115      *
0116      * @return list of unit categories.
0117      **/
0118     QList<UnitCategory> categories() const;
0119 
0120 private:
0121     QExplicitlySharedDataPointer<ConverterPrivate> d;
0122 };
0123 
0124 } // KUnitConversion namespace
0125 
0126 #endif