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

KUnitConversion

  • sources
  • kde-4.14
  • kdelibs
  • 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 
24 namespace KUnitConversion
25 {
26 
27 class Value::Private
28 {
29 public:
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 
57 Value::Value()
58 : d(new Value::Private())
59 {
60 }
61 
62 Value::Value(double n, UnitPtr u)
63 : d(new Value::Private(n, u))
64 {
65 }
66 
67 Value::Value(double n, const QString& u)
68 : d(new Value::Private(n, u))
69 {
70 }
71 
72 Value::Value(double n, int u)
73 : d(new Value::Private(n, u))
74 {
75 }
76 
77 Value::Value(const QVariant& n, const QString& u)
78 : d(new Value::Private(n.toDouble(), u))
79 {
80 }
81 
82 Value::~Value()
83 {
84  delete d;
85 }
86 
87 bool Value::isValid() const
88 {
89  return (d->unit && d->unit->isValid());
90 }
91 
92 QString 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 
100 QString 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 
109 Value& 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 
118 double Value::number() const
119 {
120  return d->number;
121 }
122 
123 UnitPtr Value::unit() const
124 {
125  if (!d->unit) {
126  d->unit = d->converter.unit(InvalidUnit);
127  }
128  return d->unit;
129 }
130 
131 Value& Value::operator=(const Value& value)
132 {
133  d->number = value.d->number;
134  d->unit = value.d->unit;
135  return *this;
136 }
137 
138 Value Value::convertTo(UnitPtr unit) const
139 {
140  return d->converter.convert(*this, unit);
141 }
142 
143 Value Value::convertTo(int unit) const
144 {
145  return d->converter.convert(*this, unit);
146 }
147 
148 Value Value::convertTo(const QString& unit) const
149 {
150  return d->converter.convert(*this, unit);
151 }
152 
153 }
KUnitConversion::InvalidUnit
Definition: converter.h:36
KUnitConversion::Value::round
Value & round(uint decimals)
rounds value to decimal count
Definition: value.cpp:109
KUnitConversion::Value::Value
Value()
Definition: value.cpp:57
QChar
KUnitConversion::Value::isValid
bool isValid() const
Check if value is valid.
Definition: value.cpp:87
KUnitConversion::Value
Definition: value.h:32
KUnitConversion::Value::~Value
~Value()
Definition: value.cpp:82
KUnitConversion::Value::operator=
Value & operator=(const Value &)
Definition: value.cpp:131
converter.h
QString
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::number
double number() const
Number part of the value.
Definition: value.cpp:118
KUnitConversion::UnitPtr
KSharedPtr< Unit > UnitPtr
Definition: unit.h:125
value.h
KUnitConversion::Value::unit
UnitPtr unit() const
Unit part of the value.
Definition: value.cpp:123
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
QVariant
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:23:00 by doxygen 1.8.7 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 API Reference

Skip menu "kdelibs 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

Search



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