KUnitConversion 5.109.0
unitcategory.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_UNITCATEGORY_H
9#define KUNITCONVERSION_UNITCATEGORY_H
10
11#include "kunitconversion/kunitconversion_export.h"
12#include "unit.h"
13#include "value.h"
14#include <QExplicitlySharedDataPointer>
15#include <QString>
16#include <QStringList>
17#include <chrono>
18
19namespace KUnitConversion
20{
21class UnitCategoryPrivate;
22
34class KUNITCONVERSION_EXPORT UnitCategory
35{
36public:
41
46
47 // TODO KF6: remove virtual
48 virtual ~UnitCategory();
49
54
55 // TODO KF6: de-inline
56#ifdef Q_COMPILER_RVALUE_REFS
61 UnitCategory &operator=(UnitCategory &&other)
62 {
63 swap(other);
64 return *this;
65 }
66#endif
67
68 // TODO KF6: remove
72 void swap(UnitCategory &other)
73 {
74 d.swap(other.d);
75 }
76
80 bool operator==(const UnitCategory &other) const;
81
85 bool operator!=(const UnitCategory &other) const;
86
90 bool isNull() const;
91
95 CategoryId id() const;
96
102 QString name() const;
103
107 QString description() const;
108
115
121 bool hasUnit(const QString &unit) const;
122
128 Unit unit(const QString &s) const;
129
135 Unit unit(UnitId unitId) const;
136
142 QList<Unit> units() const;
143
149 QList<Unit> mostCommonUnits() const;
150
156 QStringList allUnits() const;
157
158 // TODO KF6: make const
166 Value convert(const Value &value, const QString &toUnit = QString());
167
168 // TODO KF6: make const
176 Value convert(const Value &value, UnitId toUnit);
177
178 // TODO KF6: make const, remove virtual
186 virtual Value convert(const Value &value, const Unit &toUnit);
187
192
200 void syncConversionTable(std::chrono::seconds updateSkipPeriod);
201
202protected:
203 // TODO KF6: remove those three
204 void addDefaultUnit(const Unit &unit);
205 void addCommonUnit(const Unit &unit);
206 void addUnit(const Unit &unit);
207
208private:
209 friend class Unit;
210 friend class UnitCategoryPrivate;
211
212 KUNITCONVERSION_NO_EXPORT explicit UnitCategory(UnitCategoryPrivate *dd);
213
214protected:
215 QExplicitlySharedDataPointer<UnitCategoryPrivate> d;
216};
217
218} // KUnitConversion namespace
219
220#endif
Class to define a category of units of measurement.
Definition unitcategory.h:35
QStringList allUnits() const
Return all unit names, short names and unit synonyms in this category.
UnitCategory()
Null constructor.
bool operator!=(const UnitCategory &other) const
virtual Value convert(const Value &value, const Unit &toUnit)
Convert value to another unit.
bool hasOnlineConversionTable() const
QList< Unit > units() const
Return units in this category.
bool operator==(const UnitCategory &other) const
Unit unit(UnitId unitId) const
Return unit for unit enum.
Unit unit(const QString &s) const
Return unit for string.
Unit defaultUnit() const
Returns default unit.
Value convert(const Value &value, UnitId toUnit)
Convert value to another unit selected by enum.
QString name() const
Returns name for the unit category.
void syncConversionTable(std::chrono::seconds updateSkipPeriod)
Explicit request to sync conversion table when it is older than updateSkipPeriod.
Value convert(const Value &value, const QString &toUnit=QString())
Convert value to another unit selected by string.
UnitCategory & operator=(const UnitCategory &other)
Assignment operator, assign other to this.
UnitCategory(const UnitCategory &other)
Copy constructor, copy other to this.
void swap(UnitCategory &other)
Swaps this UnitCategory with other.
Definition unitcategory.h:72
QList< Unit > mostCommonUnits() const
Return most common units in this category.
bool hasUnit(const QString &unit) const
Check if unit category has a unit.
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