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#include "unit.h"
21
22#include <klocalizedstring.h>
23
24#include "unitcategory.h"
25
26namespace KUnitConversion
27{
28
29Complex::Complex()
30{
31}
32
33Complex::~Complex()
34{
35}
36
37class Unit::Private
38{
39public:
40 Private(UnitCategory* category, const Complex* complex = 0)
41 : multiplier(1.0)
42 , complex(complex)
43 , category(category)
44 {
45 };
46
47 ~Private()
48 {
49 };
50
51 QString symbol;
52 QString description;
53 double multiplier;
54 KLocalizedString real;
55 KLocalizedString integer;
56 const Complex* complex;
57 UnitCategory* category;
58 int id;
59};
60
61Unit::Unit(UnitCategory* category, int id, double multiplier, const QString& symbol,
62 const QString& description, const QString& match,
63 const KLocalizedString& real, const KLocalizedString& integer)
64: d(new Unit::Private(category))
65{
66 if (category) {
67 category->addUnitMapValues(UnitPtr(this), match);
68 category->addIdMapValue(UnitPtr(this), id);
69 }
70 d->multiplier = multiplier;
71 d->real = real;
72 d->integer = integer;
73 d->symbol = symbol;
74 d->description = description;
75 d->id = id;
76}
77
78Unit::Unit(UnitCategory* category, int id, const Complex* complex, const QString& symbol,
79 const QString& description, const QString& match,
80 const KLocalizedString& real, const KLocalizedString& integer)
81: d(new Unit::Private(category, complex))
82{
83 if (category) {
84 category->addUnitMapValues(UnitPtr(this), match);
85 category->addIdMapValue(UnitPtr(this), id);
86 }
87 d->real = real;
88 d->integer = integer;
89 d->symbol = symbol;
90 d->description = description;
91 d->id = id;
92}
93
94Unit::~Unit()
95{
96 delete d;
97}
98
99UnitCategory* Unit::category() const
100{
101 return d->category;
102}
103
104QString Unit::description() const
105{
106 return d->description;
107}
108
109QString Unit::symbol() const
110{
111 return d->symbol;
112}
113
114double Unit::multiplier() const
115{
116 return d->multiplier;
117}
118
119void Unit::setMultiplier(double multiplier)
120{
121 d->multiplier = multiplier;
122}
123
124double Unit::toDefault(double value) const
125{
126 if (d->complex) {
127 return d->complex->toDefault(value);
128 } else {
129 return value * d->multiplier;
130 }
131}
132
133double Unit::fromDefault(double value) const
134{
135 if (d->complex) {
136 return d->complex->fromDefault(value);
137 } else {
138 return value / d->multiplier;
139 }
140}
141
142QString Unit::toString(double value, int fieldWidth, char format, int precision,
143 const QChar& fillChar) const
144{
145 if ((int)value == value && precision < 1) {
146 return d->integer.subs((int)value).toString();
147 }
148 return d->real.subs(value, fieldWidth, format, precision, fillChar).toString();
149}
150
151QString Unit::toSymbolString(double value, int fieldWidth, char format, int precision,
152 const QChar& fillChar) const
153{
154 return category()->symbolStringFormat().subs(value, fieldWidth, format, precision, fillChar)
155 .subs(d->symbol).toString();
156}
157
158bool Unit::isValid() const
159{
160 return !d->symbol.isEmpty();
161}
162
163int Unit::id() const
164{
165 return d->id;
166}
167
168}
169
170