~ubuntu-branches/ubuntu/wily/kunitconversion/wily-proposed

« back to all changes in this revision

Viewing changes to src/unit.h

  • Committer: Package Import Robot
  • Author(s): Scarlett Clark
  • Date: 2014-07-14 17:56:16 UTC
  • Revision ID: package-import@ubuntu.com-20140714175616-n92z93yphd8vce6b
Tags: upstream-5.0.0
Import upstream version 5.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *   Copyright (C) 2007-2009 Petri Damstén <damu@iki.fi>
 
3
 *   Copyright (C) 2014 John Layt <jlayt@kde.org>
 
4
 *
 
5
 *   This program is free software; you can redistribute it and/or modify
 
6
 *   it under the terms of the GNU Library General Public License as
 
7
 *   published by the Free Software Foundation; either version 2, or
 
8
 *   (at your option) any later version.
 
9
 *
 
10
 *   This program is distributed in the hope that it will be useful,
 
11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 *   GNU General Public License for more details
 
14
 *
 
15
 *   You should have received a copy of the GNU Library General Public
 
16
 *   License along with this program; if not, write to the
 
17
 *   Free Software Foundation, Inc.,
 
18
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
19
 */
 
20
 
 
21
#ifndef KUNITCONVERSION_UNIT_H
 
22
#define KUNITCONVERSION_UNIT_H
 
23
 
 
24
#include "kunitconversion/kunitconversion_export.h"
 
25
 
 
26
#include <QtCore/QString>
 
27
#include <QtCore/QExplicitlySharedDataPointer>
 
28
 
 
29
class KLocalizedString;
 
30
 
 
31
namespace KUnitConversion
 
32
{
 
33
 
 
34
enum CategoryId {
 
35
    InvalidCategory = -1,
 
36
    LengthCategory,
 
37
    AreaCategory,
 
38
    VolumeCategory,
 
39
    TemperatureCategory,
 
40
    VelocityCategory,
 
41
    MassCategory,
 
42
    PressureCategory,
 
43
    EnergyCategory,
 
44
    CurrencyCategory,
 
45
    PowerCategory,
 
46
    TimeCategory,
 
47
    FuelEfficiencyCategory,
 
48
    DensityCategory,
 
49
    AccelerationCategory,
 
50
    AngleCategory,
 
51
    FrequencyCategory,
 
52
    ForceCategory
 
53
};
 
54
 
 
55
enum UnitId {
 
56
    InvalidUnit = -1,
 
57
    NoUnit = 0,
 
58
    Percent = 1,
 
59
 
 
60
    // Area
 
61
    SquareYottameter = 1000, SquareZettameter, SquareExameter, SquarePetameter, SquareTerameter,
 
62
    SquareGigameter, SquareMegameter, SquareKilometer, SquareHectometer, SquareDecameter,
 
63
    SquareMeter, SquareDecimeter, SquareCentimeter, SquareMillimeter, SquareMicrometer,
 
64
    SquareNanometer, SquarePicometer, SquareFemtometer, SquareAttometer, SquareZeptometer,
 
65
    SquareYoctometer, Acre, SquareFoot, SquareInch, SquareMile,
 
66
 
 
67
    // Length
 
68
    Yottameter = 2000, Zettameter, Exameter, Petameter, Terameter, Gigameter, Megameter,
 
69
    Kilometer, Hectometer, Decameter, Meter, Decimeter, Centimeter, Millimeter, Micrometer,
 
70
    Nanometer, Picometer, Femtometer, Attometer, Zeptometer, Yoctometer, Inch, Foot, Yard,
 
71
    Mile, NauticalMile, LightYear, Parsec, AstronomicalUnit, Thou, Angstrom,
 
72
 
 
73
    // Volume
 
74
    CubicYottameter = 3000, CubicZettameter, CubicExameter, CubicPetameter, CubicTerameter,
 
75
    CubicGigameter, CubicMegameter, CubicKilometer, CubicHectometer, CubicDecameter, CubicMeter,
 
76
    CubicDecimeter, CubicCentimeter, CubicMillimeter, CubicMicrometer, CubicNanometer,
 
77
    CubicPicometer, CubicFemtometer, CubicAttometer, CubicZeptometer, CubicYoctometer,
 
78
    Yottaliter, Zettaliter, Exaliter, Petaliter, Teraliter, Gigaliter, Megaliter, Kiloliter,
 
79
    Hectoliter, Decaliter, Liter, Deciliter, Centiliter, Milliliter, Microliter, Nanoliter,
 
80
    Picoliter, Femtoliter, Attoliter, Zeptoliter, Yoctoliter, CubicFoot, CubicInch, CubicMile,
 
81
    FluidOunce, Cup, GallonUS, PintImperial,
 
82
 
 
83
    // Mass
 
84
    Yottagram = 4000, Zettagram, Exagram, Petagram, Teragram, Gigagram, Megagram, Kilogram,
 
85
    Hectogram, Decagram, Gram, Decigram, Centigram, Milligram, Microgram, Nanogram, Picogram,
 
86
    Femtogram, Attogram, Zeptogram, Yoctogram, Ton, Carat, Pound, Ounce, TroyOunce, MassNewton,
 
87
    Kilonewton,
 
88
 
 
89
    // Pressure
 
90
    Yottapascal = 5000, Zettapascal, Exapascal, Petapascal, Terapascal, Gigapascal, Megapascal,
 
91
    Kilopascal, Hectopascal, Decapascal, Pascal, Decipascal, Centipascal, Millipascal,
 
92
    Micropascal, Nanopascal, Picopascal, Femtopascal, Attopascal, Zeptopascal, Yoctopascal,
 
93
    Bar, Millibar, Decibar, Torr, TechnicalAtmosphere, Atmosphere, PoundForcePerSquareInch,
 
94
    InchesOfMercury, MillimetersOfMercury,
 
95
 
 
96
    // Temperature
 
97
    Kelvin = 6000, Celsius, Fahrenheit, Rankine, Delisle, TemperatureNewton, Reaumur, Romer,
 
98
 
 
99
    // Energy
 
100
    Yottajoule = 7000, Zettajoule, Exajoule, Petajoule, Terajoule, Gigajoule, Megajoule,
 
101
    Kilojoule, Hectojoule, Decajoule, Joule, Decijoule, Centijoule, Millijoule, Microjoule,
 
102
    Nanojoule, Picojoule, Femtojoule, Attojoule, Zeptojoule, Yoctojoule, GuidelineDailyAmount,
 
103
    Electronvolt, Rydberg, Kilocalorie, PhotonWavelength, KiloJoulePerMole, JoulePerMole,
 
104
 
 
105
    // Currency
 
106
    Eur = 8000, Ats, Bef, Nlg, Fim, Frf, Dem, Iep, Itl, Luf, Pte, Esp, Grd, Sit, Cyp, Mtl, Skk,
 
107
    Usd, Jpy, Bgn, Czk, Dkk, Eek, Gbp, Huf, Ltl, Lvl, Pln, Ron, Sek, Chf, Nok, Hrk, Rub, Try,
 
108
    Aud, Brl, Cad, Cny, Hkd, Idr, Inr, Krw, Mxn, Myr, Nzd, Php, Sgd, Thb, Zar,
 
109
 
 
110
    // Velocity
 
111
    MeterPerSecond = 9000, KilometerPerHour, MilePerHour, FootPerSecond, InchPerSecond, Knot,
 
112
    Mach, SpeedOfLight, Beaufort,
 
113
 
 
114
    // Power
 
115
    Yottawatt = 10000, Zettawatt, Exawatt, Petawatt, Terawatt, Gigawatt, Megawatt, Kilowatt,
 
116
    Hectowatt, Decawatt, Watt, Deciwatt, Centiwatt, Milliwatt, Microwatt, Nanowatt, Picowatt,
 
117
    Femtowatt, Attowatt, Zeptowatt, Yoctowatt, Horsepower,
 
118
 
 
119
    // Time
 
120
    Yottasecond = 11000, Zettasecond, Exasecond, Petasecond, Terasecond, Gigasecond, Megasecond,
 
121
    Kilosecond, Hectosecond, Decasecond, Second, Decisecond, Centisecond, Millisecond,
 
122
    Microsecond, Nanosecond, Picosecond, Femtosecond, Attosecond, Zeptosecond, Yoctosecond,
 
123
    Minute, Hour, Day, Week, JulianYear, LeapYear, Year,
 
124
 
 
125
    // FuelEfficiency
 
126
    LitersPer100Kilometers = 12000, MilePerUsGallon, MilePerImperialGallon, KilometrePerLitre,
 
127
 
 
128
    // Density
 
129
    YottakilogramsPerCubicMeter = 13000, ZettakilogramPerCubicMeter, ExakilogramPerCubicMeter,
 
130
    PetakilogramPerCubicMeter, TerakilogramPerCubicMeter, GigakilogramPerCubicMeter,
 
131
    MegakilogramPerCubicMeter, KilokilogramPerCubicMeter, HectokilogramsPerCubicMeter,
 
132
    DecakilogramsPerCubicMeter, KilogramsPerCubicMeter, DecikilogramsPerCubicMeter,
 
133
    CentikilogramsPerCubicMeter, MillikilogramsPerCubicMeter, MicrokilogramsPerCubicMeter,
 
134
    NanokilogramsPerCubicMeter, PicokilogramsPerCubicMeter, FemtokilogramsPerCubicMeter,
 
135
    AttokilogramsPerCubicMeter, ZeptokilogramsPerCubicMeter, YoctokilogramsPerCubicMeter,
 
136
    KilogramPerLiter, GramPerLiter, GramPerMilliliter,
 
137
    OuncePerCubicInch, OuncePerCubicFoot, OuncePerCubicYard,
 
138
    PoundPerCubicInch, PoundPerCubicFoot, PoundPerCubicYard,
 
139
 
 
140
    // Acceleration
 
141
    MetresPerSecondSquared = 14000, FeetPerSecondSquared, StandardGravity,
 
142
 
 
143
    // Force
 
144
    Yottanewton = 15000, Zettanewton, Exanewton, Petanewton, Teranewton, Giganewton, Meganewton,
 
145
    KilonewtonForce, Hectonewton, Decanewton, Newton, Decinewton, Centinewton, Millinewton, Micronewton,
 
146
    Nanonewton, Piconewton, Femtonewton, Attonewton, Zeptonewton, Yoctonewton, Dyne, Kilopond,
 
147
    PoundForce, Poundal,
 
148
 
 
149
    // Angle
 
150
    Degree = 16000, Radian, Gradian, ArcMinute, ArcSecond,
 
151
 
 
152
    // Frequency
 
153
    Yottahertz = 17000, Zettahertz, Exahertz, Petahertz, Terahertz, Gigahertz, Megahertz,
 
154
    Kilohertz, Hectohertz, Decahertz, Hertz, Decihertz, Centihertz, Millihertz, Microhertz,
 
155
    Nanohertz, Picohertz, Femtohertz, Attohertz, Zeptohertz, Yoctohertz, RPM
 
156
};
 
157
 
 
158
class UnitCategory;
 
159
class UnitPrivate;
 
160
 
 
161
/**
 
162
 * @short Class to define a unit of measurement
 
163
 *
 
164
 * @since 4.4
 
165
 *
 
166
 * This is a class to define a unit of measurement.
 
167
 *
 
168
 * @b license GNU-LGPL v.2 or later
 
169
 *
 
170
 * @see Converter, UnitCategory, Value
 
171
 *
 
172
 * @author Petri Damstén <damu@iki.fi>
 
173
 * @author John Layt <jlayt@kde.org>
 
174
 */
 
175
 
 
176
class KUNITCONVERSION_EXPORT Unit
 
177
{
 
178
public:
 
179
    /**
 
180
     * Null constructor
 
181
     **/
 
182
    Unit();
 
183
 
 
184
    /**
 
185
     * Copy constructor, copy @param other to this.
 
186
     **/
 
187
    Unit(const Unit &other);
 
188
 
 
189
    virtual ~Unit();
 
190
 
 
191
    /**
 
192
     * Assignment operator, assign @param other to this.
 
193
     **/
 
194
    Unit &operator=(const Unit &other);
 
195
 
 
196
#ifdef Q_COMPILER_RVALUE_REFS
 
197
    /**
 
198
     * Move-assigns \a other to this Unit instance, transferring the
 
199
     * ownership of the managed pointer to this instance.
 
200
     **/
 
201
    Unit &operator=(Unit &&other) { swap(other); return *this; }
 
202
#endif
 
203
 
 
204
    /**
 
205
     * Swaps this Unit with \a other. This function is very fast and never fails.
 
206
     **/
 
207
    void swap(Unit &other) { d.swap(other.d); }
 
208
 
 
209
    /**
 
210
     * @return Returns true if this Unit is equal to the @param other Unit.
 
211
     **/
 
212
    bool operator==(const Unit &other) const;
 
213
 
 
214
    /**
 
215
     * @return Returns true if this Unit is not equal to the @param other Unit.
 
216
     **/
 
217
    bool operator!=(const Unit &other) const;
 
218
 
 
219
    /**
 
220
     * @return returns true if this Unit is null
 
221
     **/
 
222
    bool isNull() const;
 
223
 
 
224
    /**
 
225
     * @return if unit is valid.
 
226
     **/
 
227
    bool isValid() const;
 
228
 
 
229
    /**
 
230
     * @return unit id.
 
231
     **/
 
232
    UnitId id() const;
 
233
 
 
234
    /**
 
235
     * @return category id.
 
236
     **/
 
237
    CategoryId categoryId() const;
 
238
 
 
239
    /**
 
240
     * @return unit category.
 
241
     **/
 
242
    UnitCategory category() const;
 
243
 
 
244
    /**
 
245
     * @return translated name for unit.
 
246
     **/
 
247
    QString description() const;
 
248
 
 
249
    /**
 
250
     * @return symbol for the unit.
 
251
     **/
 
252
    QString symbol() const;
 
253
 
 
254
    /**
 
255
     * @param value number value
 
256
     * @param fieldWidth width of the formatted field, padded by spaces.
 
257
     *                   Positive value aligns right, negative aligns left
 
258
     * @param format type of floating point formating, like in QString::arg
 
259
     * @param precision number of digits after the decimal separator
 
260
     * @param fillChar the character used to fill up the empty places when
 
261
     *                 field width is greater than argument width
 
262
     * @return value + unit string
 
263
     **/
 
264
    QString toString(qreal value, int fieldWidth = 0, char format = 'g', int precision = -1,
 
265
                     const QChar &fillChar = QLatin1Char(' ')) const;
 
266
 
 
267
    /**
 
268
     * @param value number value
 
269
     * @param fieldWidth width of the formatted field, padded by spaces.
 
270
     *                   Positive value aligns right, negative aligns left
 
271
     * @param format type of floating point formating, like in QString::arg
 
272
     * @param precision number of digits after the decimal separator
 
273
     * @param fillChar the character used to fill up the empty places when
 
274
     *                 field width is greater than argument width
 
275
     * @return value + unit string
 
276
     **/
 
277
    QString toSymbolString(qreal value, int fieldWidth = 0, char format = 'g', int precision = -1,
 
278
                           const QChar &fillChar = QLatin1Char(' ')) const;
 
279
 
 
280
 
 
281
protected:
 
282
    qreal toDefault(qreal value) const;
 
283
    qreal fromDefault(qreal value) const;
 
284
 
 
285
private:
 
286
    friend class CustomUnit;
 
287
    friend class UnitCategory;
 
288
    friend class UnitCategoryPrivate;
 
289
    friend class CurrencyCategoryPrivate;
 
290
 
 
291
    Unit(UnitPrivate *dd);
 
292
    void setUnitMultiplier(qreal multiplier);
 
293
 
 
294
    QExplicitlySharedDataPointer<UnitPrivate> d;
 
295
};
 
296
 
 
297
#define U(id, m, s, d, sy, r, i) (KUnitConversion::Unit(*this, id, m, s, d, sy, r, i))
 
298
 
 
299
} // KUnitConversion namespace
 
300
 
 
301
#endif