File indexing completed on 2024-04-21 15:04:39

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_VALUE_H
0009 #define KUNITCONVERSION_VALUE_H
0010 
0011 #include "kunitconversion/kunitconversion_export.h"
0012 
0013 #include "unit.h"
0014 
0015 #include <QSharedDataPointer>
0016 #include <QString>
0017 
0018 class QVariant;
0019 
0020 namespace KUnitConversion
0021 {
0022 class ValuePrivate;
0023 
0024 /**
0025  * @short Class to hold a value in a unit of measurement
0026  *
0027  * This is a class to hold a value in a unit of measurement.
0028  *
0029  * @see Converter, Unit, UnitCategory
0030  *
0031  * @author Petri Damstén <damu@iki.fi>
0032  * @author John Layt <jlayt@kde.org>
0033  */
0034 
0035 class KUNITCONVERSION_EXPORT Value
0036 {
0037 public:
0038     /**
0039      * Creates a null value.
0040      */
0041     Value();
0042     /**
0043      * Creates a value with a unit instance
0044      */
0045     Value(qreal number, const Unit &unit);
0046     /**
0047      * Creates a value with a unit (as a string).
0048      */
0049     Value(qreal number, const QString &unitString);
0050     /**
0051      * Creates a value with a unit (as a enum value).
0052      */
0053     Value(qreal number, UnitId unitId);
0054     /**
0055      * Creates a value based on a QVariant (calling toReal() on it) with a unit (as a string).
0056      */
0057     Value(const QVariant &number, const QString &unitString);
0058 
0059     /**
0060      * Copy constructor, copy @p other to this.
0061      **/
0062     Value(const Value &other);
0063 
0064     /**
0065      * Destroys this Value instance
0066      */
0067     ~Value();
0068 
0069     /**
0070      * Assignment operator, assign @p other to this.
0071      **/
0072     Value &operator=(const Value &other);
0073 
0074 #ifdef Q_COMPILER_RVALUE_REFS
0075     /**
0076      * Move-assigns @p other to this Value instance, transferring the
0077      * ownership of the managed pointer to this instance.
0078      **/
0079     Value &operator=(Value &&other)
0080     {
0081         swap(other);
0082         return *this;
0083     }
0084 #endif
0085 
0086     /**
0087      * Swaps this Value with @p other. This function is very fast and never fails.
0088      **/
0089     void swap(Value &other)
0090     {
0091         d.swap(other.d);
0092     }
0093 
0094     /**
0095      * @return @c true if this Value is equal to the @p other Value.
0096      **/
0097     bool operator==(const Value &other) const;
0098 
0099     /**
0100      * @return @c true if this Value is not equal to the @p other Value.
0101      **/
0102     bool operator!=(const Value &other) const;
0103 
0104     /**
0105      * @return returns true if this Value is null
0106      **/
0107     bool isNull() const;
0108 
0109     /**
0110      * Check if value is valid.
0111      *
0112      * @return True if value is valid
0113      **/
0114     bool isValid() const;
0115 
0116     /**
0117      * Number part of the value
0118      **/
0119     qreal number() const;
0120 
0121     /**
0122      * Unit part of the value
0123      **/
0124     Unit unit() const;
0125 
0126     /**
0127      * Convert value to a string
0128      * @param fieldWidth width of the formatted field, padded by spaces.
0129      *                   Positive value aligns right, negative aligns left
0130      * @param format type of floating point formatting, like in QString::arg
0131      * @param precision number of digits after the decimal separator
0132      * @param fillChar the character used to fill up the empty places when
0133      *                 field width is greater than argument width
0134      * @return value as a string
0135      **/
0136     QString toString(int fieldWidth = 0, char format = 'g', int precision = -1, const QChar &fillChar = QLatin1Char(' ')) const;
0137 
0138     /**
0139      * Convert value to a string with symbol
0140      * @param fieldWidth width of the formatted field, padded by spaces.
0141      *                   Positive value aligns right, negative aligns left
0142      * @param format type of floating point formatting, like in QString::arg
0143      * @param precision number of digits after the decimal separator
0144      * @param fillChar the character used to fill up the empty places when
0145      *                 field width is greater than argument width
0146      * @return value as a string
0147      **/
0148     QString toSymbolString(int fieldWidth = 0, char format = 'g', int precision = -1, const QChar &fillChar = QLatin1Char(' ')) const;
0149 
0150     /**
0151      * rounds value to decimal count
0152      * @param decimals decimal count.
0153      **/
0154     Value &round(uint decimals);
0155 
0156     /**
0157      * convert to another unit
0158      **/
0159     Value convertTo(const Unit &unit) const;
0160 
0161     /**
0162      * convert to another unit
0163      **/
0164     Value convertTo(UnitId unit) const;
0165 
0166     /**
0167      * convert to another unit
0168      **/
0169     Value convertTo(const QString &unit) const;
0170 
0171 private:
0172     QSharedDataPointer<ValuePrivate> d;
0173 };
0174 
0175 } // KUnitConversion namespace
0176 
0177 #endif