1/*
2 * Copyright (C) 2008-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 "converter.h"
21
22#include <kglobal.h>
23#include <klocale.h>
24
25#include "unitcategory.h"
26#include "area.h"
27#include "length.h"
28#include "currency.h"
29#include "density.h"
30#include "energy.h"
31#include "fuel_efficiency.h"
32#include "mass.h"
33#include "power.h"
34#include "pressure.h"
35#include "temperature.h"
36#include "timeunit.h"
37#include "unit.h"
38#include "velocity.h"
39#include "volume.h"
40#include "acceleration.h"
41#include "force.h"
42#include "angle.h"
43#include "frequency.h"
44
45namespace KUnitConversion
46{
47
48class Invalid : public UnitCategory
49{
50public:
51 Invalid() : UnitCategory(InvalidCategory)
52 {
53 const QString s;
54 const KLocalizedString ls;
55 setName(i18n("Invalid"));
56 setDefaultUnit(UP(InvalidUnit, 1.0, s, s, s, ls, ls));
57 setSymbolStringFormat(ki18nc("%1 value, %2 unit symbol (default)", "%1 %2"));
58 };
59};
60
61class ConverterPrivate
62{
63public:
64 QMap<int, UnitCategory *> categories;
65 ConverterPrivate()
66 {
67 KGlobal::locale()->insertCatalog("libkunitconversion");
68
69 categories[InvalidCategory] = new Invalid;
70 categories[LengthCategory] = new Length;
71 categories[AreaCategory] = new Area();
72 categories[VolumeCategory] = new Volume;
73 categories[TemperatureCategory] = new Temperature;
74 categories[VelocityCategory] = new Velocity;
75 categories[MassCategory] = new Mass;
76 categories[PressureCategory] = new Pressure;
77 categories[EnergyCategory] = new Energy;
78 categories[CurrencyCategory] = new Currency;
79 categories[PowerCategory] = new Power;
80 categories[TimeCategory] = new Time;
81 categories[FuelEfficiencyCategory] = new FuelEfficiency;
82 categories[DensityCategory] = new Density;
83 categories[AccelerationCategory] = new Acceleration;
84 categories[ForceCategory] = new Force;
85 categories[AngleCategory] = new Angle;
86 categories[FrequencyCategory] = new Frequency;
87 };
88
89 ~ConverterPrivate()
90 {
91 qDeleteAll(categories);
92 };
93};
94
95K_GLOBAL_STATIC(ConverterPrivate, static_d)
96
97Converter::Converter(QObject* parent)
98: QObject(parent), d(static_cast<ConverterPrivate *>(static_d))
99{
100}
101
102Converter::~Converter()
103{
104}
105
106Value Converter::convert(const Value& value, const QString& toUnit) const
107{
108 if (!value.unit().isNull()) {
109 UnitCategory* category = value.unit()->category();
110 if (category) {
111 return category->convert(value, toUnit);
112 }
113 }
114 return Value();
115}
116
117Value Converter::convert(const Value& value, int toUnit) const
118{
119 if (!value.unit().isNull()) {
120 UnitCategory* category = value.unit()->category();
121 if (category) {
122 return category->convert(value, toUnit);
123 }
124 }
125 return Value();
126}
127
128Value Converter::convert(const Value& value, UnitPtr toUnit) const
129{
130 if (!toUnit.isNull() && !value.unit().isNull() && value.unit()->isValid()) {
131 UnitCategory* category = value.unit()->category();
132 if (category) {
133 return category->convert(value, toUnit);
134 }
135 }
136 return Value();
137}
138
139UnitCategory* Converter::categoryForUnit(const QString& unit) const
140{
141 foreach (UnitCategory* u, categories()) {
142 if (u->hasUnit(unit)) {
143 return u;
144 }
145 }
146 return d->categories[InvalidCategory];
147}
148
149UnitPtr Converter::unit(const QString& unitString) const
150{
151 foreach (UnitCategory* u, d->categories) {
152 UnitPtr unitClass = u->unit(unitString);
153 if (unitClass) {
154 return unitClass;
155 }
156 }
157 return unit(InvalidUnit);
158}
159
160UnitPtr Converter::unit(int unitId) const
161{
162 foreach (UnitCategory* u, d->categories) {
163 UnitPtr unitClass = u->unit(unitId);
164 if (unitClass) {
165 return unitClass;
166 }
167 }
168 return d->categories[InvalidCategory]->defaultUnit();
169}
170
171UnitCategory* Converter::category(const QString& category) const
172{
173 foreach (UnitCategory *u, d->categories) {
174 if (u->name() == category)
175 return u;
176 }
177 // not found
178 return d->categories[InvalidCategory];
179}
180
181UnitCategory* Converter::category(int categoryId) const
182{
183 if (d->categories.contains(categoryId)) {
184 return d->categories[categoryId];
185 }
186 // not found
187 return d->categories[InvalidCategory];
188}
189
190QList<UnitCategory*> Converter::categories() const
191{
192 QList<UnitCategory*> categories = d->categories.values();
193 categories.removeAt(0);
194 return categories;
195}
196
197}
198
199#include "converter.moc"
200