KUnitConversion 5.109.0
value.h
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
35class KUNITCONVERSION_EXPORT Value
36{
37public:
45 Value(qreal number, const Unit &unit);
49 Value(qreal number, const QString &unitString);
53 Value(qreal number, UnitId unitId);
57 Value(const QVariant &number, const QString &unitString);
58
62 Value(const Value &other);
63
68
72 Value &operator=(const Value &other);
73
74#ifdef Q_COMPILER_RVALUE_REFS
79 Value &operator=(Value &&other)
80 {
81 swap(other);
82 return *this;
83 }
84#endif
85
89 void swap(Value &other)
90 {
91 d.swap(other.d);
92 }
93
97 bool operator==(const Value &other) const;
98
102 bool operator!=(const Value &other) const;
103
107 bool isNull() const;
108
114 bool isValid() const;
115
119 qreal number() const;
120
124 Unit unit() const;
125
136 QString toString(int fieldWidth = 0, char format = 'g', int precision = -1, const QChar &fillChar = QLatin1Char(' ')) const;
137
148 QString toSymbolString(int fieldWidth = 0, char format = 'g', int precision = -1, const QChar &fillChar = QLatin1Char(' ')) const;
149
154 Value &round(uint decimals);
155
159 Value convertTo(const Unit &unit) const;
160
164 Value convertTo(UnitId unit) const;
165
169 Value convertTo(const QString &unit) const;
170
171private:
172 QSharedDataPointer<ValuePrivate> d;
173};
174
175} // KUnitConversion namespace
176
177#endif
Class to define a unit of measurement.
Definition unit.h:758
Class to hold a value in a unit of measurement.
Definition value.h:36
Value convertTo(const QString &unit) const
convert to another unit
Value(qreal number, UnitId unitId)
Creates a value with a unit (as a enum value).
Value(const Value &other)
Copy constructor, copy other to this.
Value(const QVariant &number, const QString &unitString)
Creates a value based on a QVariant (calling toReal() on it) with a unit (as a string).
QString toSymbolString(int fieldWidth=0, char format='g', int precision=-1, const QChar &fillChar=QLatin1Char(' ')) const
Convert value to a string with symbol.
Value(qreal number, const QString &unitString)
Creates a value with a unit (as a string).
QString toString(int fieldWidth=0, char format='g', int precision=-1, const QChar &fillChar=QLatin1Char(' ')) const
Convert value to a string.
qreal number() const
Number part of the value.
bool isValid() const
Check if value is valid.
Value & operator=(const Value &other)
Assignment operator, assign other to this.
bool operator==(const Value &other) const
Value convertTo(UnitId unit) const
convert to another unit
Unit unit() const
Unit part of the value.
Value convertTo(const Unit &unit) const
convert to another unit
Value(qreal number, const Unit &unit)
Creates a value with a unit instance.
Value()
Creates a null value.
void swap(Value &other)
Swaps this Value with other.
Definition value.h:89
Value & round(uint decimals)
rounds value to decimal count
~Value()
Destroys this Value instance.
bool operator!=(const Value &other) const