KUnitConversion
unit.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 "unit.h" 00021 00022 #include <klocalizedstring.h> 00023 00024 #include "unitcategory.h" 00025 00026 namespace KUnitConversion 00027 { 00028 00029 Complex::Complex() 00030 { 00031 } 00032 00033 Complex::~Complex() 00034 { 00035 } 00036 00037 class Unit::Private 00038 { 00039 public: 00040 Private(UnitCategory* category, const Complex* complex = 0) 00041 : multiplier(1.0) 00042 , complex(complex) 00043 , category(category) 00044 { 00045 }; 00046 00047 ~Private() 00048 { 00049 }; 00050 00051 QString symbol; 00052 QString description; 00053 double multiplier; 00054 KLocalizedString real; 00055 KLocalizedString integer; 00056 const Complex* complex; 00057 UnitCategory* category; 00058 int id; 00059 }; 00060 00061 Unit::Unit(UnitCategory* category, int id, double multiplier, const QString& symbol, 00062 const QString& description, const QString& match, 00063 const KLocalizedString& real, const KLocalizedString& integer) 00064 : d(new Unit::Private(category)) 00065 { 00066 if (category) { 00067 category->addUnitMapValues(UnitPtr(this), match); 00068 category->addIdMapValue(UnitPtr(this), id); 00069 } 00070 d->multiplier = multiplier; 00071 d->real = real; 00072 d->integer = integer; 00073 d->symbol = symbol; 00074 d->description = description; 00075 d->id = id; 00076 } 00077 00078 Unit::Unit(UnitCategory* category, int id, const Complex* complex, const QString& symbol, 00079 const QString& description, const QString& match, 00080 const KLocalizedString& real, const KLocalizedString& integer) 00081 : d(new Unit::Private(category, complex)) 00082 { 00083 if (category) { 00084 category->addUnitMapValues(UnitPtr(this), match); 00085 category->addIdMapValue(UnitPtr(this), id); 00086 } 00087 d->real = real; 00088 d->integer = integer; 00089 d->symbol = symbol; 00090 d->description = description; 00091 d->id = id; 00092 } 00093 00094 Unit::~Unit() 00095 { 00096 delete d; 00097 } 00098 00099 UnitCategory* Unit::category() const 00100 { 00101 return d->category; 00102 } 00103 00104 QString Unit::description() const 00105 { 00106 return d->description; 00107 } 00108 00109 QString Unit::symbol() const 00110 { 00111 return d->symbol; 00112 } 00113 00114 double Unit::multiplier() const 00115 { 00116 return d->multiplier; 00117 } 00118 00119 void Unit::setMultiplier(double multiplier) 00120 { 00121 d->multiplier = multiplier; 00122 } 00123 00124 double Unit::toDefault(double value) const 00125 { 00126 if (d->complex) { 00127 return d->complex->toDefault(value); 00128 } else { 00129 return value * d->multiplier; 00130 } 00131 } 00132 00133 double Unit::fromDefault(double value) const 00134 { 00135 if (d->complex) { 00136 return d->complex->fromDefault(value); 00137 } else { 00138 return value / d->multiplier; 00139 } 00140 } 00141 00142 QString Unit::toString(double value, int fieldWidth, char format, int precision, 00143 const QChar& fillChar) const 00144 { 00145 if ((int)value == value && precision < 1) { 00146 return d->integer.subs((int)value).toString(); 00147 } 00148 return d->real.subs(value, fieldWidth, format, precision, fillChar).toString(); 00149 } 00150 00151 QString Unit::toSymbolString(double value, int fieldWidth, char format, int precision, 00152 const QChar& fillChar) const 00153 { 00154 return category()->symbolStringFormat().subs(value, fieldWidth, format, precision, fillChar) 00155 .subs(d->symbol).toString(); 00156 } 00157 00158 bool Unit::isValid() const 00159 { 00160 return !d->symbol.isEmpty(); 00161 } 00162 00163 int Unit::id() const 00164 { 00165 return d->id; 00166 } 00167 00168 } 00169
KDE 4.6 API Reference