1/*
2 * Copyright (C) 2007-2009 Petri Damstén <damu@iki.fi>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Library General Public License as
6 * published by the Free Software Foundation; either version 2, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19
20#ifndef KUNITCONVERSION_UNIT_H
21#define KUNITCONVERSION_UNIT_H
22
23#include <QtCore/QString>
24#include <ksharedptr.h>
25#include "kunitconversion_export.h"
26
27class KLocalizedString;
28
29namespace KUnitConversion
30{
31
32class UnitCategory;
33
34class KUNITCONVERSION_EXPORT Complex
35{
36public:
37 Complex();
38 virtual ~Complex();
39 virtual double toDefault(double) const = 0;
40 virtual double fromDefault(double) const = 0;
41};
42
43class KUNITCONVERSION_EXPORT Unit : public QSharedData
44{
45public:
46 Unit(UnitCategory* category, int id, double multiplier, const QString& symbol,
47 const QString& description, const QString& match,
48 const KLocalizedString& real, const KLocalizedString& integer);
49 Unit(UnitCategory* category, int id, const Complex* complex, const QString& symbol,
50 const QString& description, const QString& match,
51 const KLocalizedString& real, const KLocalizedString& integer);
52 virtual ~Unit();
53
54 /**
55 * @return translated name for unit.
56 **/
57 QString description() const;
58
59 /**
60 * @return symbol for the unit.
61 **/
62 QString symbol() const;
63
64 /**
65 * @param value number value
66 * @param fieldWidth width of the formatted field, padded by spaces.
67 * Positive value aligns right, negative aligns left
68 * @param format type of floating point formating, like in QString::arg
69 * @param precision number of digits after the decimal separator
70 * @param fillChar the character used to fill up the empty places when
71 * field width is greater than argument width
72 * @return value + unit string
73 **/
74 QString toString(double value, int fieldWidth = 0, char format = 'g', int precision = -1,
75 const QChar& fillChar = QLatin1Char(' ')) const;
76
77 /**
78 * @param value number value
79 * @param fieldWidth width of the formatted field, padded by spaces.
80 * Positive value aligns right, negative aligns left
81 * @param format type of floating point formating, like in QString::arg
82 * @param precision number of digits after the decimal separator
83 * @param fillChar the character used to fill up the empty places when
84 * field width is greater than argument width
85 * @return value + unit string
86 **/
87 QString toSymbolString(double value, int fieldWidth = 0, char format = 'g',
88 int precision = -1, const QChar& fillChar = QLatin1Char(' ')) const;
89
90 /**
91 * @return unit multiplier.
92 **/
93 double multiplier() const;
94
95 /**
96 * Set unit multiplier.
97 **/
98 void setMultiplier(double multiplier);
99
100 /**
101 * @return unit category.
102 **/
103 UnitCategory* category() const;
104
105 /**
106 * @return if unit is valid.
107 **/
108 bool isValid() const;
109
110 /**
111 * @return unit id.
112 **/
113 int id() const;
114
115protected:
116 double toDefault(double value) const;
117 double fromDefault(double value) const;
118
119private:
120 friend class UnitCategory;
121 class Private;
122 Private* const d;
123};
124
125typedef KSharedPtr<Unit> UnitPtr;
126
127#define UP(id, m, s, d, sy, r, i) \
128 (KUnitConversion::UnitPtr(new KUnitConversion::Unit(this, id, m, s, d, sy, r, i)))
129#define U(id, m, s, d, sy, r, i) (new KUnitConversion::Unit(this, id, m, s, d, sy, r, i))
130
131} // KUnitConversion namespace
132
133#endif
134