• Skip to content
  • Skip to link menu
KDE 4.6 API Reference
  • KDE API Reference
  • kdelibs
  • KDE Home
  • Contact Us
 

KUnitConversion

converter.cpp

Go to the documentation of this file.
00001 /*
00002  *   Copyright (C) 2008-2009 Petri Damstén <damu@iki.fi>
00003  *
00004  *   This program is free software; you can redistribute it and/or modify
00005  *   it under the terms of the GNU Library General Public License as
00006  *   published by the Free Software Foundation; either version 2, or
00007  *   (at your option) any later version.
00008  *
00009  *   This program is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details
00013  *
00014  *   You should have received a copy of the GNU Library General Public
00015  *   License along with this program; if not, write to the
00016  *   Free Software Foundation, Inc.,
00017  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00018  */
00019 
00020 #include "converter.h"
00021 
00022 #include <kglobal.h>
00023 #include <klocale.h>
00024 
00025 #include "unitcategory.h"
00026 #include "area.h"
00027 #include "length.h"
00028 #include "currency.h"
00029 #include "density.h"
00030 #include "energy.h"
00031 #include "fuel_efficiency.h"
00032 #include "mass.h"
00033 #include "power.h"
00034 #include "pressure.h"
00035 #include "temperature.h"
00036 #include "timeunit.h"
00037 #include "unit.h"
00038 #include "velocity.h"
00039 #include "volume.h"
00040 #include "acceleration.h"
00041 #include "force.h"
00042 #include "angle.h"
00043 #include "frequency.h"
00044 
00045 namespace KUnitConversion
00046 {
00047 
00048 class Invalid : public UnitCategory
00049 {
00050 public:
00051     Invalid() : UnitCategory(InvalidCategory)
00052     {
00053         const QString s;
00054         const KLocalizedString ls;
00055         setName(i18n("Invalid"));
00056         setDefaultUnit(UP(InvalidUnit, 1.0, s, s, s, ls, ls));
00057         setSymbolStringFormat(ki18nc("%1 value, %2 unit symbol (default)", "%1 %2"));
00058     };
00059 };
00060 
00061 class ConverterPrivate
00062 {
00063 public:
00064     QMap<int, UnitCategory *> categories;
00065     ConverterPrivate()
00066     {
00067         KGlobal::locale()->insertCatalog("libkunitconversion");
00068 
00069         categories[InvalidCategory] = new Invalid;
00070         categories[LengthCategory] = new Length;
00071         categories[AreaCategory] = new Area();
00072         categories[VolumeCategory] = new Volume;
00073         categories[TemperatureCategory] = new Temperature;
00074         categories[VelocityCategory] = new Velocity;
00075         categories[MassCategory] = new Mass;
00076         categories[PressureCategory] = new Pressure;
00077         categories[EnergyCategory] = new Energy;
00078         categories[CurrencyCategory] = new Currency;
00079         categories[PowerCategory] = new Power;
00080         categories[TimeCategory] = new Time;
00081         categories[FuelEfficiencyCategory] = new FuelEfficiency;
00082         categories[DensityCategory] = new Density;
00083         categories[AccelerationCategory] = new Acceleration;
00084         categories[ForceCategory] = new Force;
00085         categories[AngleCategory] = new Angle;
00086         categories[FrequencyCategory] = new Frequency;
00087     };
00088 
00089     ~ConverterPrivate()
00090     {
00091         qDeleteAll(categories);
00092     };
00093 };
00094 
00095 K_GLOBAL_STATIC(ConverterPrivate, static_d)
00096 
00097 Converter::Converter(QObject* parent)
00098 : QObject(parent), d(static_cast<ConverterPrivate *>(static_d))
00099 {
00100 }
00101 
00102 Converter::~Converter()
00103 {
00104 }
00105 
00106 Value Converter::convert(const Value& value, const QString& toUnit) const
00107 {
00108     if (!value.unit().isNull()) {
00109         UnitCategory* category = value.unit()->category();
00110         if (category) {
00111             return category->convert(value, toUnit);
00112         }
00113     }
00114     return Value();
00115 }
00116 
00117 Value Converter::convert(const Value& value, int toUnit) const
00118 {
00119     if (!value.unit().isNull()) {
00120         UnitCategory* category = value.unit()->category();
00121         if (category) {
00122             return category->convert(value, toUnit);
00123         }
00124     }
00125     return Value();
00126 }
00127 
00128 Value Converter::convert(const Value& value, UnitPtr toUnit) const
00129 {
00130     if (!toUnit.isNull() && !value.unit().isNull() && value.unit()->isValid()) {
00131         UnitCategory* category = value.unit()->category();
00132         if (category) {
00133             return category->convert(value, toUnit);
00134         }
00135     }
00136     return Value();
00137 }
00138 
00139 UnitCategory* Converter::categoryForUnit(const QString& unit) const
00140 {
00141     foreach (UnitCategory* u, categories()) {
00142         if (u->hasUnit(unit)) {
00143             return u;
00144         }
00145     }
00146     return d->categories[InvalidCategory];
00147 }
00148 
00149 UnitPtr Converter::unit(const QString& unitString) const
00150 {
00151     foreach (UnitCategory* u, d->categories) {
00152         UnitPtr unitClass = u->unit(unitString);
00153         if (unitClass) {
00154             return unitClass;
00155         }
00156     }
00157     return unit(InvalidUnit);
00158 }
00159 
00160 UnitPtr Converter::unit(int unitId) const
00161 {
00162     foreach (UnitCategory* u, d->categories) {
00163         UnitPtr unitClass = u->unit(unitId);
00164         if (unitClass) {
00165             return unitClass;
00166         }
00167     }
00168     return unit(InvalidUnit);
00169 }
00170 
00171 UnitCategory* Converter::category(const QString& category) const
00172 {
00173     foreach (UnitCategory *u, d->categories) {
00174         if (u->name() == category)
00175             return u;
00176     }
00177     // not found
00178     return d->categories[InvalidCategory];
00179 }
00180 
00181 UnitCategory* Converter::category(int categoryId) const
00182 {
00183     if (d->categories.contains(categoryId)) {
00184         return d->categories[categoryId];
00185     }
00186     // not found
00187     return d->categories[InvalidCategory];
00188 }
00189 
00190 QList<UnitCategory*> Converter::categories() const
00191 {
00192     QList<UnitCategory*> categories = d->categories.values();
00193     categories.removeAt(0);
00194     return categories;
00195 }
00196 
00197 }
00198 
00199 #include "converter.moc"

KUnitConversion

Skip menu "KUnitConversion"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.7.3
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal