KUnitConversion
unitcategory.cpp
Go to the documentation of this file.
00001 /* 00002 * Copyright (C) 2007-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 "unitcategory.h" 00021 #include <klocalizedstring.h> 00022 00023 namespace KUnitConversion 00024 { 00025 00026 class UnitCategory::Private 00027 { 00028 public: 00029 Private() : 00030 defaultUnit(0) 00031 { 00032 }; 00033 00034 ~Private() 00035 { 00036 }; 00037 QString name; 00038 UnitPtr defaultUnit; 00039 QMap<QString, UnitPtr> unitMap; 00040 QMap<int, UnitPtr> idMap; 00041 QList<UnitPtr> units; 00042 QList<UnitPtr> mostCommonUnits; 00043 QString description; 00044 KUrl url; 00045 KLocalizedString symbolStringFormat; 00046 int id; 00047 }; 00048 00049 UnitCategory::UnitCategory(int id) 00050 : d(new UnitCategory::Private) 00051 { 00052 d->id = id; 00053 } 00054 00055 UnitCategory::~UnitCategory() 00056 { 00057 delete d; 00058 } 00059 00060 void UnitCategory::setSymbolStringFormat(const KLocalizedString& symbolStringFormat) 00061 { 00062 d->symbolStringFormat = symbolStringFormat; 00063 } 00064 00065 KLocalizedString UnitCategory::symbolStringFormat() const 00066 { 00067 return d->symbolStringFormat; 00068 } 00069 00070 QList<UnitPtr> UnitCategory::units() const 00071 { 00072 return d->units; 00073 } 00074 00075 QList<UnitPtr> UnitCategory::mostCommonUnits() const 00076 { 00077 return d->mostCommonUnits; 00078 } 00079 00080 void UnitCategory::setMostCommonUnits(const QList<int>& units) 00081 { 00082 d->mostCommonUnits.clear(); 00083 foreach (int u, units) { 00084 d->mostCommonUnits.append(unit(u)); 00085 } 00086 } 00087 00088 QStringList UnitCategory::allUnits() const 00089 { 00090 return d->unitMap.keys(); 00091 } 00092 00093 bool UnitCategory::hasUnit(const QString &unit) const 00094 { 00095 return d->unitMap.contains(unit); 00096 } 00097 00098 Value UnitCategory::convert(const Value& value, const QString& toUnit) 00099 { 00100 if ((toUnit.isEmpty() || d->unitMap.contains(toUnit)) && value.unit()->isValid()) { 00101 UnitPtr to = toUnit.isEmpty() ? defaultUnit() : d->unitMap[toUnit]; 00102 return convert(value, to); 00103 } 00104 return Value(); 00105 } 00106 00107 Value UnitCategory::convert(const Value& value, int toUnit) 00108 { 00109 if (d->idMap.contains(toUnit) && value.unit()->isValid()) { 00110 return convert(value, d->idMap[toUnit]); 00111 } 00112 return Value(); 00113 } 00114 00115 Value UnitCategory::convert(const Value& value, UnitPtr toUnit) 00116 { 00117 if (toUnit) { 00118 double v = toUnit->fromDefault(value.unit()->toDefault(value.number())); 00119 return Value(v, toUnit); 00120 } 00121 return Value(); 00122 } 00123 00124 void UnitCategory::addUnitMapValues(UnitPtr unit, const QString& names) 00125 { 00126 const QStringList list = names.split(';'); 00127 foreach (const QString& name, list) { 00128 d->unitMap[name] = unit; 00129 } 00130 } 00131 00132 void UnitCategory::addIdMapValue(UnitPtr unit, int id) 00133 { 00134 d->idMap[id] = unit; 00135 d->units.append(unit); 00136 } 00137 00138 UnitPtr UnitCategory::unit(const QString& s) const 00139 { 00140 return d->unitMap.value(s); 00141 } 00142 00143 UnitPtr UnitCategory::unit(int unitId) const 00144 { 00145 if (d->idMap.contains(unitId)) { 00146 return d->idMap[unitId]; 00147 } 00148 return UnitPtr(); 00149 } 00150 00151 QString UnitCategory::name() const 00152 { 00153 return d->name; 00154 } 00155 00156 void UnitCategory::setName(const QString& name) 00157 { 00158 d->name = name; 00159 } 00160 00161 void UnitCategory::setDefaultUnit(UnitPtr defaultUnit) 00162 { 00163 d->defaultUnit = defaultUnit; 00164 } 00165 00166 UnitPtr UnitCategory::defaultUnit() const 00167 { 00168 return d->defaultUnit; 00169 } 00170 00171 QString UnitCategory::description() const 00172 { 00173 return d->description; 00174 } 00175 00176 void UnitCategory::setDescription(const QString& description) 00177 { 00178 d->description = description; 00179 } 00180 00181 KUrl UnitCategory::url() const 00182 { 00183 return d->url; 00184 } 00185 00186 void UnitCategory::setUrl(const KUrl& url) 00187 { 00188 d->url = url; 00189 } 00190 00191 int UnitCategory::id() const 00192 { 00193 return d->id; 00194 } 00195 00196 }
KDE 4.6 API Reference