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

KUnitConversion

  • kunitconversion
value.cpp
Go to the documentation of this file.
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}
KUnitConversion::Value
Definition: value.h:33
KUnitConversion::Value::convertTo
Value convertTo(UnitPtr unit) const
convert to another unit
Definition: value.cpp:138
KUnitConversion::Value::toSymbolString
QString toSymbolString(int fieldWidth=0, char format='g', int precision=-1, const QChar &fillChar=QLatin1Char(' ')) const
Convert value to a string with symbol.
Definition: value.cpp:100
KUnitConversion::Value::toString
QString toString(int fieldWidth=0, char format='g', int precision=-1, const QChar &fillChar=QLatin1Char(' ')) const
Convert value to a string.
Definition: value.cpp:92
KUnitConversion::Value::isValid
bool isValid() const
Check if value is valid.
Definition: value.cpp:87
KUnitConversion::Value::operator=
Value & operator=(const Value &)
Definition: value.cpp:131
KUnitConversion::Value::Value
Value()
Definition: value.cpp:57
KUnitConversion::Value::round
Value & round(uint decimals)
rounds value to decimal count
Definition: value.cpp:109
KUnitConversion::Value::unit
UnitPtr unit() const
Unit part of the value.
Definition: value.cpp:123
KUnitConversion::Value::number
double number() const
Number part of the value.
Definition: value.cpp:118
KUnitConversion::Value::~Value
~Value()
Definition: value.cpp:82
converter.h
KUnitConversion
Definition: converter.cpp:46
KUnitConversion::UnitPtr
KSharedPtr< Unit > UnitPtr
Definition: unit.h:125
KUnitConversion::InvalidUnit
@ InvalidUnit
Definition: converter.h:36
value.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KUnitConversion

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

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • 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
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal