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

KUnitConversion

value.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 "value.h"
00021 #include "converter.h"
00022 #include <qmath.h>
00023 
00024 namespace KUnitConversion
00025 {
00026 
00027 class Value::Private
00028 {
00029 public:
00030     Private(double n = 0.0, int u = InvalidUnit)
00031     : number(n)
00032     {
00033         unit = converter.unit(u);
00034     }
00035 
00036     Private(double n, UnitPtr u)
00037     : number(n)
00038     , unit(u)
00039     {
00040     }
00041 
00042     Private(double n, const QString& u)
00043     : number(n)
00044     {
00045         unit = converter.unit(u);
00046     }
00047 
00048     ~Private()
00049     {
00050     }
00051 
00052     double number;
00053     UnitPtr unit;
00054     Converter converter;
00055 };
00056 
00057 Value::Value()
00058 : d(new Value::Private())
00059 {
00060 }
00061 
00062 Value::Value(double n, UnitPtr u)
00063 : d(new Value::Private(n, u))
00064 {
00065 }
00066 
00067 Value::Value(double n, const QString& u)
00068 : d(new Value::Private(n, u))
00069 {
00070 }
00071 
00072 Value::Value(double n, int u)
00073 : d(new Value::Private(n, u))
00074 {
00075 }
00076 
00077 Value::Value(const QVariant& n, const QString& u)
00078 : d(new Value::Private(n.toDouble(), u))
00079 {
00080 }
00081 
00082 Value::~Value()
00083 {
00084     delete d;
00085 }
00086 
00087 bool Value::isValid() const
00088 {
00089     return (d->unit && d->unit->isValid());
00090 }
00091 
00092 QString Value::toString(int fieldWidth, char format, int precision, const QChar& fillChar) const
00093 {
00094     if (isValid()) {
00095         return d->unit->toString(d->number, fieldWidth, format, precision, fillChar);
00096     }
00097     return QString();
00098 }
00099 
00100 QString Value::toSymbolString(int fieldWidth, char format, int precision,
00101                               const QChar& fillChar) const
00102 {
00103     if (isValid()) {
00104         return d->unit->toSymbolString(d->number, fieldWidth, format, precision, fillChar);
00105     }
00106     return QString();
00107 }
00108 
00109 Value& Value::round(uint decimals)
00110 {
00111     uint div = qPow(10, decimals);
00112     double add = 0.5 / (double)div;
00113 
00114     d->number = (int)((d->number + add) * div) / (double)div;
00115     return *this;
00116 }
00117 
00118 double Value::number() const
00119 {
00120     return d->number;
00121 }
00122 
00123 UnitPtr Value::unit() const
00124 {
00125     if (!d->unit) {
00126        d->unit = d->converter.unit(InvalidUnit);
00127     }
00128     return d->unit;
00129 }
00130 
00131 Value& Value::operator=(const Value& value)
00132 {
00133     d->number = value.d->number;
00134     d->unit = value.d->unit;
00135     return *this;
00136 }
00137 
00138 Value Value::convertTo(UnitPtr unit) const
00139 {
00140     return d->converter.convert(*this, unit);
00141 }
00142 
00143 Value Value::convertTo(int unit) const
00144 {
00145     return d->converter.convert(*this, unit);
00146 }
00147 
00148 Value Value::convertTo(const QString& unit) const
00149 {
00150     return d->converter.convert(*this, unit);
00151 }
00152 
00153 }

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