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 | |
18 | class QVariant; |
19 | |
20 | namespace KUnitConversion |
21 | { |
22 | class ValuePrivate; |
23 | |
24 | /** |
25 | * @short Class to hold a value in a unit of measurement |
26 | * |
27 | * This is a class to hold a value in a unit of measurement. |
28 | * |
29 | * @see Converter, Unit, UnitCategory |
30 | * |
31 | * @author Petri Damstén <damu@iki.fi> |
32 | * @author John Layt <jlayt@kde.org> |
33 | */ |
34 | |
35 | class KUNITCONVERSION_EXPORT Value |
36 | { |
37 | public: |
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 | /** |
60 | * Copy constructor, copy @p other to this. |
61 | **/ |
62 | Value(const Value &other); |
63 | |
64 | /** |
65 | * Destroys this Value instance |
66 | */ |
67 | ~Value(); |
68 | |
69 | /** |
70 | * Assignment operator, assign @p other to this. |
71 | **/ |
72 | Value &operator=(const Value &other); |
73 | |
74 | #ifdef Q_COMPILER_RVALUE_REFS |
75 | /** |
76 | * Move-assigns @p other to this Value instance, transferring the |
77 | * ownership of the managed pointer to this instance. |
78 | **/ |
79 | Value &operator=(Value &&other) |
80 | { |
81 | swap(other); |
82 | return *this; |
83 | } |
84 | #endif |
85 | |
86 | /** |
87 | * Swaps this Value with @p other. This function is very fast and never fails. |
88 | **/ |
89 | void swap(Value &other) |
90 | { |
91 | d.swap(other&: other.d); |
92 | } |
93 | |
94 | /** |
95 | * @return @c true if this Value is equal to the @p other Value. |
96 | **/ |
97 | bool operator==(const Value &other) const; |
98 | |
99 | /** |
100 | * @return @c true if this Value is not equal to the @p other Value. |
101 | **/ |
102 | bool operator!=(const Value &other) const; |
103 | |
104 | /** |
105 | * @return returns true if this Value is null |
106 | **/ |
107 | bool isNull() const; |
108 | |
109 | /** |
110 | * Check if value is valid. |
111 | * |
112 | * @return True if value is valid |
113 | **/ |
114 | bool isValid() const; |
115 | |
116 | /** |
117 | * Number part of the value |
118 | **/ |
119 | qreal number() const; |
120 | |
121 | /** |
122 | * Unit part of the value |
123 | **/ |
124 | Unit unit() const; |
125 | |
126 | /** |
127 | * Convert value to a string |
128 | * @param fieldWidth width of the formatted field, padded by spaces. |
129 | * Positive value aligns right, negative aligns left |
130 | * @param format type of floating point formatting, like in QString::arg |
131 | * @param precision number of digits after the decimal separator |
132 | * @param fillChar the character used to fill up the empty places when |
133 | * field width is greater than argument width |
134 | * @return value as a string |
135 | **/ |
136 | QString toString(int fieldWidth = 0, char format = 'g', int precision = -1, const QChar &fillChar = QLatin1Char(' ')) const; |
137 | |
138 | /** |
139 | * Convert value to a string with symbol |
140 | * @param fieldWidth width of the formatted field, padded by spaces. |
141 | * Positive value aligns right, negative aligns left |
142 | * @param format type of floating point formatting, like in QString::arg |
143 | * @param precision number of digits after the decimal separator |
144 | * @param fillChar the character used to fill up the empty places when |
145 | * field width is greater than argument width |
146 | * @return value as a string |
147 | **/ |
148 | QString toSymbolString(int fieldWidth = 0, char format = 'g', int precision = -1, const QChar &fillChar = QLatin1Char(' ')) const; |
149 | |
150 | /** |
151 | * rounds value to decimal count |
152 | * @param decimals decimal count. |
153 | **/ |
154 | Value &round(uint decimals); |
155 | |
156 | /** |
157 | * convert to another unit |
158 | **/ |
159 | Value convertTo(const Unit &unit) const; |
160 | |
161 | /** |
162 | * convert to another unit |
163 | **/ |
164 | Value convertTo(UnitId unit) const; |
165 | |
166 | /** |
167 | * convert to another unit |
168 | **/ |
169 | Value convertTo(const QString &unit) const; |
170 | |
171 | private: |
172 | QSharedDataPointer<ValuePrivate> d; |
173 | }; |
174 | |
175 | } // KUnitConversion namespace |
176 | |
177 | #endif |
178 | |