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 "unitcategory.h"
21#include <klocalizedstring.h>
22
23namespace KUnitConversion
24{
25
26class UnitCategory::Private
27{
28public:
29 Private() :
30 defaultUnit(0)
31 {
32 };
33
34 ~Private()
35 {
36 };
37 QString name;
38 UnitPtr defaultUnit;
39 QMap<QString, UnitPtr> unitMap;
40 QMap<int, UnitPtr> idMap;
41 QList<UnitPtr> units;
42 QList<UnitPtr> mostCommonUnits;
43 QString description;
44 KUrl url;
45 KLocalizedString symbolStringFormat;
46 int id;
47};
48
49UnitCategory::UnitCategory(int id)
50 : d(new UnitCategory::Private)
51{
52 d->id = id;
53}
54
55UnitCategory::~UnitCategory()
56{
57 delete d;
58}
59
60void UnitCategory::setSymbolStringFormat(const KLocalizedString& symbolStringFormat)
61{
62 d->symbolStringFormat = symbolStringFormat;
63}
64
65KLocalizedString UnitCategory::symbolStringFormat() const
66{
67 return d->symbolStringFormat;
68}
69
70QList<UnitPtr> UnitCategory::units() const
71{
72 return d->units;
73}
74
75QList<UnitPtr> UnitCategory::mostCommonUnits() const
76{
77 return d->mostCommonUnits;
78}
79
80void UnitCategory::setMostCommonUnits(const QList<int>& units)
81{
82 d->mostCommonUnits.clear();
83 foreach (int u, units) {
84 d->mostCommonUnits.append(unit(u));
85 }
86}
87
88QStringList UnitCategory::allUnits() const
89{
90 return d->unitMap.keys();
91}
92
93bool UnitCategory::hasUnit(const QString &unit) const
94{
95 return d->unitMap.contains(unit);
96}
97
98Value UnitCategory::convert(const Value& value, const QString& toUnit)
99{
100 if ((toUnit.isEmpty() || d->unitMap.contains(toUnit)) && value.unit()->isValid()) {
101 UnitPtr to = toUnit.isEmpty() ? defaultUnit() : d->unitMap[toUnit];
102 return convert(value, to);
103 }
104 return Value();
105}
106
107Value UnitCategory::convert(const Value& value, int toUnit)
108{
109 if (d->idMap.contains(toUnit) && value.unit()->isValid()) {
110 return convert(value, d->idMap[toUnit]);
111 }
112 return Value();
113}
114
115Value UnitCategory::convert(const Value& value, UnitPtr toUnit)
116{
117 if (toUnit) {
118 double v = toUnit->fromDefault(value.unit()->toDefault(value.number()));
119 return Value(v, toUnit);
120 }
121 return Value();
122}
123
124void UnitCategory::addUnitMapValues(UnitPtr unit, const QString& names)
125{
126 const QStringList list = names.split(';');
127 foreach (const QString& name, list) {
128 d->unitMap[name] = unit;
129 }
130}
131
132void UnitCategory::addIdMapValue(UnitPtr unit, int id)
133{
134 d->idMap[id] = unit;
135 d->units.append(unit);
136}
137
138UnitPtr UnitCategory::unit(const QString& s) const
139{
140 return d->unitMap.value(s);
141}
142
143UnitPtr UnitCategory::unit(int unitId) const
144{
145 if (d->idMap.contains(unitId)) {
146 return d->idMap[unitId];
147 }
148 return UnitPtr();
149}
150
151QString UnitCategory::name() const
152{
153 return d->name;
154}
155
156void UnitCategory::setName(const QString& name)
157{
158 d->name = name;
159}
160
161void UnitCategory::setDefaultUnit(UnitPtr defaultUnit)
162{
163 d->defaultUnit = defaultUnit;
164}
165
166UnitPtr UnitCategory::defaultUnit() const
167{
168 return d->defaultUnit;
169}
170
171QString UnitCategory::description() const
172{
173 return d->description;
174}
175
176void UnitCategory::setDescription(const QString& description)
177{
178 d->description = description;
179}
180
181KUrl UnitCategory::url() const
182{
183 return d->url;
184}
185
186void UnitCategory::setUrl(const KUrl& url)
187{
188 d->url = url;
189}
190
191int UnitCategory::id() const
192{
193 return d->id;
194}
195
196}
197