1/*
2 * SPDX-FileCopyrightText: 2007-2009 Petri Damstén <damu@iki.fi>
3 * SPDX-FileCopyrightText: 2014 John Layt <jlayt@kde.org>
4 *
5 * SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7
8#ifndef KUNITCONVERSION_VALUE_H
9#define KUNITCONVERSION_VALUE_H
10
11#include "kunitconversion/kunitconversion_export.h"
12
13#include "unit.h"
14
15#include <QSharedDataPointer>
16#include <QString>
17
18class QVariant;
19
20namespace KUnitConversion
21{
22class ValuePrivate;
23
24/*!
25 * \class KUnitConversion::Value
26 * \inmodule KUnitConversion
27 *
28 * \brief Class to hold a value in a unit of measurement.
29 *
30 * This is a class to hold a value in a unit of measurement.
31 *
32 * \sa Converter, Unit, UnitCategory
33 */
34
35class KUNITCONVERSION_EXPORT Value
36{
37public:
38 /*!
39 * Creates a null value.
40 */
41 Value();
42 /*!
43 * Creates a value with a unit instance
44 */
45 Value(qreal number, const Unit &unit);
46 /*!
47 * Creates a value with a unit (as a string).
48 */
49 Value(qreal number, const QString &unitString);
50 /*!
51 * Creates a value with a unit (as a enum value).
52 */
53 Value(qreal number, UnitId unitId);
54 /*!
55 * Creates a value based on a QVariant (calling toReal() on it) with a unit (as a string).
56 */
57 Value(const QVariant &number, const QString &unitString);
58
59 Value(const Value &other);
60
61 ~Value();
62
63 Value &operator=(const Value &other);
64
65#ifdef Q_COMPILER_RVALUE_REFS
66 Value &operator=(Value &&other)
67 {
68 swap(other);
69 return *this;
70 }
71#endif
72
73 void swap(Value &other)
74 {
75 d.swap(other&: other.d);
76 }
77
78 /*!
79 * Returns true if this Value is equal to the \a other Value.
80 **/
81 bool operator==(const Value &other) const;
82
83 bool operator!=(const Value &other) const;
84
85 /*!
86 * Returns returns true if this Value is null
87 **/
88 bool isNull() const;
89
90 /*!
91 * Returns true if value is valid
92 **/
93 bool isValid() const;
94
95 /*!
96 * Number part of the value
97 **/
98 qreal number() const;
99
100 /*!
101 * Unit part of the value
102 **/
103 Unit unit() const;
104
105 /*!
106 * Convert value to a string
107 *
108 * \a fieldWidth width of the formatted field, padded by spaces.
109 * Positive value aligns right, negative aligns left
110 *
111 * \a format type of floating point formatting, like in \l QString::arg
112 *
113 * \a precision number of digits after the decimal separator
114 *
115 * \a fillChar the character used to fill up the empty places when
116 * field width is greater than argument width
117 *
118 * Returns value as a string
119 **/
120 QString toString(int fieldWidth = 0, char format = 'g', int precision = -1, const QChar &fillChar = QLatin1Char(' ')) const;
121
122 /*!
123 * Convert value to a string with symbol
124 *
125 * \a fieldWidth width of the formatted field, padded by spaces.
126 * Positive value aligns right, negative aligns left
127 * \a format type of floating point formatting, like in \l QString::arg
128 *
129 * \a precision number of digits after the decimal separator
130 *
131 * \a fillChar the character used to fill up the empty places when
132 * field width is greater than argument width
133 *
134 * Returns value as a string
135 **/
136 QString toSymbolString(int fieldWidth = 0, char format = 'g', int precision = -1, const QChar &fillChar = QLatin1Char(' ')) const;
137
138 /*!
139 * Rounds value to decimal count
140 *
141 * \a decimals decimal count.
142 **/
143 Value &round(uint decimals);
144
145 /*!
146 * Convert to another unit
147 **/
148 Value convertTo(const Unit &unit) const;
149
150 /*!
151 * Convert to another unit
152 **/
153 Value convertTo(UnitId unit) const;
154
155 /*!
156 * Convert to another unit
157 **/
158 Value convertTo(const QString &unit) const;
159
160private:
161 QSharedDataPointer<ValuePrivate> d;
162};
163
164} // KUnitConversion namespace
165
166#endif
167

source code of kunitconversion/src/value.h