1/*
2 * Copyright (C) 2007-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 "value.h"
21#include "converter.h"
22#include <qmath.h>
23
24namespace KUnitConversion
25{
26
27class Value::Private
28{
29public:
30 Private(double n = 0.0, int u = InvalidUnit)
31 : number(n)
32 {
33 unit = converter.unit(u);
34 }
35
36 Private(double n, UnitPtr u)
37 : number(n)
38 , unit(u)
39 {
40 }
41
42 Private(double n, const QString& u)
43 : number(n)
44 {
45 unit = converter.unit(u);
46 }
47
48 ~Private()
49 {
50 }
51
52 double number;
53 UnitPtr unit;
54 Converter converter;
55};
56
57Value::Value()
58: d(new Value::Private())
59{
60}
61
62Value::Value(double n, UnitPtr u)
63: d(new Value::Private(n, u))
64{
65}
66
67Value::Value(double n, const QString& u)
68: d(new Value::Private(n, u))
69{
70}
71
72Value::Value(double n, int u)
73: d(new Value::Private(n, u))
74{
75}
76
77Value::Value(const QVariant& n, const QString& u)
78: d(new Value::Private(n.toDouble(), u))
79{
80}
81
82Value::~Value()
83{
84 delete d;
85}
86
87bool Value::isValid() const
88{
89 return (d->unit && d->unit->isValid());
90}
91
92QString Value::toString(int fieldWidth, char format, int precision, const QChar& fillChar) const
93{
94 if (isValid()) {
95 return d->unit->toString(d->number, fieldWidth, format, precision, fillChar);
96 }
97 return QString();
98}
99
100QString Value::toSymbolString(int fieldWidth, char format, int precision,
101 const QChar& fillChar) const
102{
103 if (isValid()) {
104 return d->unit->toSymbolString(d->number, fieldWidth, format, precision, fillChar);
105 }
106 return QString();
107}
108
109Value& Value::round(uint decimals)
110{
111 uint div = qPow(10, decimals);
112 double add = 0.5 / (double)div;
113
114 d->number = (int)((d->number + add) * div) / (double)div;
115 return *this;
116}
117
118double Value::number() const
119{
120 return d->number;
121}
122
123UnitPtr Value::unit() const
124{
125 if (!d->unit) {
126 d->unit = d->converter.unit(InvalidUnit);
127 }
128 return d->unit;
129}
130
131Value& Value::operator=(const Value& value)
132{
133 d->number = value.d->number;
134 d->unit = value.d->unit;
135 return *this;
136}
137
138Value Value::convertTo(UnitPtr unit) const
139{
140 return d->converter.convert(*this, unit);
141}
142
143Value Value::convertTo(int unit) const
144{
145 return d->converter.convert(*this, unit);
146}
147
148Value Value::convertTo(const QString& unit) const
149{
150 return d->converter.convert(*this, unit);
151}
152
153}
154