File indexing completed on 2024-04-14 03:56:12

0001 /*
0002  *   SPDX-FileCopyrightText: 2007-2009 Petri Damstén <damu@iki.fi>
0003  *   SPDX-FileCopyrightText: 2014 John Layt <jlayt@kde.org>
0004  *
0005  *   SPDX-License-Identifier: LGPL-2.0-or-later
0006  */
0007 
0008 #ifndef KUNITCONVERSION_UNIT_H
0009 #define KUNITCONVERSION_UNIT_H
0010 
0011 #include "kunitconversion/kunitconversion_export.h"
0012 
0013 #include <QExplicitlySharedDataPointer>
0014 #include <QString>
0015 
0016 namespace KUnitConversion
0017 {
0018 enum CategoryId {
0019     InvalidCategory = -1,
0020     LengthCategory,
0021     AreaCategory,
0022     VolumeCategory,
0023     TemperatureCategory,
0024     VelocityCategory,
0025     MassCategory,
0026     PressureCategory,
0027     EnergyCategory,
0028     CurrencyCategory,
0029     PowerCategory,
0030     TimeCategory,
0031     FuelEfficiencyCategory,
0032     DensityCategory,
0033     WeightPerAreaCategory,
0034     AccelerationCategory,
0035     AngleCategory,
0036     FrequencyCategory,
0037     ForceCategory,
0038     /** @since 5.27 */
0039     ThermalConductivityCategory,
0040     /** @since 5.27 */
0041     ThermalFluxCategory,
0042     /** @since 5.27 */
0043     ThermalGenerationCategory,
0044     /** @since 5.27 */
0045     VoltageCategory,
0046     /** @since 5.27 */
0047     ElectricalCurrentCategory,
0048     /** @since 5.27 */
0049     ElectricalResistanceCategory,
0050     /** @since 5.53 */
0051     PermeabilityCategory,
0052     /** @since 5.61 **/
0053     BinaryDataCategory
0054 };
0055 
0056 enum UnitId {
0057     InvalidUnit = -1,
0058     NoUnit = 0,
0059     Percent = 1,
0060 
0061     // Area
0062     SquareYottameter = 1000,
0063     SquareZettameter,
0064     SquareExameter,
0065     SquarePetameter,
0066     SquareTerameter,
0067     SquareGigameter,
0068     SquareMegameter,
0069     SquareKilometer,
0070     SquareHectometer,
0071     SquareDecameter,
0072     SquareMeter,
0073     SquareDecimeter,
0074     SquareCentimeter,
0075     SquareMillimeter,
0076     SquareMicrometer,
0077     SquareNanometer,
0078     SquarePicometer,
0079     SquareFemtometer,
0080     SquareAttometer,
0081     SquareZeptometer,
0082     SquareYoctometer,
0083     Acre,
0084     SquareFoot,
0085     SquareInch,
0086     SquareMile,
0087 
0088     // Length
0089     Yottameter = 2000,
0090     Zettameter,
0091     Exameter,
0092     Petameter,
0093     Terameter,
0094     Gigameter,
0095     Megameter,
0096     Kilometer,
0097     Hectometer,
0098     Decameter,
0099     Meter,
0100     Decimeter,
0101     Centimeter,
0102     Millimeter,
0103     Micrometer,
0104     Nanometer,
0105     Picometer,
0106     Femtometer,
0107     Attometer,
0108     Zeptometer,
0109     Yoctometer,
0110     Inch,
0111     Foot,
0112     Yard,
0113     Mile,
0114     NauticalMile,
0115     LightYear,
0116     Parsec,
0117     AstronomicalUnit,
0118     Thou,
0119     Angstrom,
0120 
0121     // Volume
0122     CubicYottameter = 3000,
0123     CubicZettameter,
0124     CubicExameter,
0125     CubicPetameter,
0126     CubicTerameter,
0127     CubicGigameter,
0128     CubicMegameter,
0129     CubicKilometer,
0130     CubicHectometer,
0131     CubicDecameter,
0132     CubicMeter,
0133     CubicDecimeter,
0134     CubicCentimeter,
0135     CubicMillimeter,
0136     CubicMicrometer,
0137     CubicNanometer,
0138     CubicPicometer,
0139     CubicFemtometer,
0140     CubicAttometer,
0141     CubicZeptometer,
0142     CubicYoctometer,
0143     Yottaliter,
0144     Zettaliter,
0145     Exaliter,
0146     Petaliter,
0147     Teraliter,
0148     Gigaliter,
0149     Megaliter,
0150     Kiloliter,
0151     Hectoliter,
0152     Decaliter,
0153     Liter,
0154     Deciliter,
0155     Centiliter,
0156     Milliliter,
0157     Microliter,
0158     Nanoliter,
0159     Picoliter,
0160     Femtoliter,
0161     Attoliter,
0162     Zeptoliter,
0163     Yoctoliter,
0164     CubicFoot,
0165     CubicInch,
0166     CubicMile,
0167     FluidOunce,
0168     Cup,
0169     Teaspoon,
0170     Tablespoon,
0171     GallonUS,
0172     PintImperial,
0173     /** @since 5.53 */
0174     OilBarrel,
0175     /** @since 5.70 */
0176     GallonImperial,
0177     PintUS,
0178 
0179     // Mass
0180     Yottagram = 4000,
0181     Zettagram,
0182     Exagram,
0183     Petagram,
0184     Teragram,
0185     Gigagram,
0186     Megagram,
0187     Kilogram,
0188     Hectogram,
0189     Decagram,
0190     Gram,
0191     Decigram,
0192     Centigram,
0193     Milligram,
0194     Microgram,
0195     Nanogram,
0196     Picogram,
0197     Femtogram,
0198     Attogram,
0199     Zeptogram,
0200     Yoctogram,
0201     Ton,
0202     Carat,
0203     Pound,
0204     Ounce,
0205     TroyOunce,
0206     MassNewton,
0207     Kilonewton,
0208 
0209     /** @since 5.26 */
0210     Stone,
0211 
0212     // Pressure
0213     Yottapascal = 5000,
0214     Zettapascal,
0215     Exapascal,
0216     Petapascal,
0217     Terapascal,
0218     Gigapascal,
0219     Megapascal,
0220     Kilopascal,
0221     Hectopascal,
0222     Decapascal,
0223     Pascal,
0224     Decipascal,
0225     Centipascal,
0226     Millipascal,
0227     Micropascal,
0228     Nanopascal,
0229     Picopascal,
0230     Femtopascal,
0231     Attopascal,
0232     Zeptopascal,
0233     Yoctopascal,
0234     Bar,
0235     Millibar,
0236     Decibar,
0237     Torr,
0238     TechnicalAtmosphere,
0239     Atmosphere,
0240     PoundForcePerSquareInch,
0241     InchesOfMercury,
0242     MillimetersOfMercury,
0243 
0244     // Temperature
0245     Kelvin = 6000,
0246     Celsius,
0247     Fahrenheit,
0248     Rankine,
0249     Delisle,
0250     TemperatureNewton,
0251     Reaumur,
0252     Romer,
0253 
0254     // Energy
0255     Yottajoule = 7000,
0256     Zettajoule,
0257     Exajoule,
0258     Petajoule,
0259     Terajoule,
0260     Gigajoule,
0261     Megajoule,
0262     Kilojoule,
0263     Hectojoule,
0264     Decajoule,
0265     Joule,
0266     Decijoule,
0267     Centijoule,
0268     Millijoule,
0269     Microjoule,
0270     Nanojoule,
0271     Picojoule,
0272     Femtojoule,
0273     Attojoule,
0274     Zeptojoule,
0275     Yoctojoule,
0276     GuidelineDailyAmount,
0277     Electronvolt,
0278     Rydberg,
0279     Kilocalorie,
0280     PhotonWavelength,
0281     KiloJoulePerMole,
0282     JoulePerMole,
0283     /** @since 5.27 */
0284     Btu,
0285     /** @since 5.27 */
0286     Erg,
0287 
0288     // Currency
0289     Eur = 8000,
0290     Ats,
0291     Bef,
0292     Nlg,
0293     Fim,
0294     Frf,
0295     Dem,
0296     Iep,
0297     Itl,
0298     Luf,
0299     Pte,
0300     Esp,
0301     Grd,
0302     Sit,
0303     Cyp,
0304     Mtl,
0305     Skk,
0306     Usd,
0307     Jpy,
0308     Bgn,
0309     Czk,
0310     Dkk,
0311     Eek,
0312     Gbp,
0313     Huf,
0314     Ltl,
0315     Lvl,
0316     Pln,
0317     Ron,
0318     Sek,
0319     Chf,
0320     Nok,
0321     Hrk,
0322     Rub,
0323     Try,
0324     Aud,
0325     Brl,
0326     Cad,
0327     Cny,
0328     Hkd,
0329     Idr,
0330     Inr,
0331     Krw,
0332     Mxn,
0333     Myr,
0334     Nzd,
0335     Php,
0336     Sgd,
0337     Thb,
0338     Zar,
0339     Ils,
0340     Isk,
0341 
0342     // Velocity
0343     MeterPerSecond = 9000,
0344     KilometerPerHour,
0345     MilePerHour,
0346     FootPerSecond,
0347     InchPerSecond,
0348     Knot,
0349     Mach,
0350     SpeedOfLight,
0351     Beaufort,
0352 
0353     // Power
0354     Yottawatt = 10000,
0355     Zettawatt,
0356     Exawatt,
0357     Petawatt,
0358     Terawatt,
0359     Gigawatt,
0360     Megawatt,
0361     Kilowatt,
0362     Hectowatt,
0363     Decawatt,
0364     Watt,
0365     Deciwatt,
0366     Centiwatt,
0367     Milliwatt,
0368     Microwatt,
0369     Nanowatt,
0370     Picowatt,
0371     Femtowatt,
0372     Attowatt,
0373     Zeptowatt,
0374     Yoctowatt,
0375     Horsepower,
0376     /** @since 5.62 */
0377     DecibelKilowatt,
0378     DecibelWatt,
0379     DecibelMilliwatt,
0380     DecibelMicrowatt,
0381 
0382     // Time
0383     Yottasecond = 11000,
0384     Zettasecond,
0385     Exasecond,
0386     Petasecond,
0387     Terasecond,
0388     Gigasecond,
0389     Megasecond,
0390     Kilosecond,
0391     Hectosecond,
0392     Decasecond,
0393     Second,
0394     Decisecond,
0395     Centisecond,
0396     Millisecond,
0397     Microsecond,
0398     Nanosecond,
0399     Picosecond,
0400     Femtosecond,
0401     Attosecond,
0402     Zeptosecond,
0403     Yoctosecond,
0404     Minute,
0405     Hour,
0406     Day,
0407     Week,
0408     JulianYear,
0409     LeapYear,
0410     Year,
0411 
0412     // FuelEfficiency
0413     LitersPer100Kilometers = 12000,
0414     MilePerUsGallon,
0415     MilePerImperialGallon,
0416     KilometrePerLitre,
0417 
0418     // Density
0419     YottakilogramsPerCubicMeter = 13000,
0420     ZettakilogramPerCubicMeter,
0421     ExakilogramPerCubicMeter,
0422     PetakilogramPerCubicMeter,
0423     TerakilogramPerCubicMeter,
0424     GigakilogramPerCubicMeter,
0425     MegakilogramPerCubicMeter,
0426     KilokilogramPerCubicMeter,
0427     HectokilogramsPerCubicMeter,
0428     DecakilogramsPerCubicMeter,
0429     KilogramsPerCubicMeter,
0430     DecikilogramsPerCubicMeter,
0431     CentikilogramsPerCubicMeter,
0432     MillikilogramsPerCubicMeter,
0433     MicrokilogramsPerCubicMeter,
0434     NanokilogramsPerCubicMeter,
0435     PicokilogramsPerCubicMeter,
0436     FemtokilogramsPerCubicMeter,
0437     AttokilogramsPerCubicMeter,
0438     ZeptokilogramsPerCubicMeter,
0439     YoctokilogramsPerCubicMeter,
0440     KilogramPerLiter,
0441     GramPerLiter,
0442     GramPerMilliliter,
0443     OuncePerCubicInch,
0444     OuncePerCubicFoot,
0445     OuncePerCubicYard,
0446     PoundPerCubicInch,
0447     PoundPerCubicFoot,
0448     PoundPerCubicYard,
0449 
0450     // Weight per area
0451     GramsPerSquareMeter,
0452     OuncesPerSquareYard,
0453 
0454     // Acceleration
0455     MetresPerSecondSquared = 14000,
0456     FeetPerSecondSquared,
0457     StandardGravity,
0458 
0459     // Force
0460     Yottanewton = 15000,
0461     Zettanewton,
0462     Exanewton,
0463     Petanewton,
0464     Teranewton,
0465     Giganewton,
0466     Meganewton,
0467     KilonewtonForce,
0468     Hectonewton,
0469     Decanewton,
0470     Newton,
0471     Decinewton,
0472     Centinewton,
0473     Millinewton,
0474     Micronewton,
0475     Nanonewton,
0476     Piconewton,
0477     Femtonewton,
0478     Attonewton,
0479     Zeptonewton,
0480     Yoctonewton,
0481     Dyne,
0482     Kilopond,
0483     PoundForce,
0484     Poundal,
0485 
0486     // Angle
0487     Degree = 16000,
0488     Radian,
0489     Gradian,
0490     ArcMinute,
0491     ArcSecond,
0492 
0493     // Frequency
0494     Yottahertz = 17000,
0495     Zettahertz,
0496     Exahertz,
0497     Petahertz,
0498     Terahertz,
0499     Gigahertz,
0500     Megahertz,
0501     Kilohertz,
0502     Hectohertz,
0503     Decahertz,
0504     Hertz,
0505     Decihertz,
0506     Centihertz,
0507     Millihertz,
0508     Microhertz,
0509     Nanohertz,
0510     Picohertz,
0511     Femtohertz,
0512     Attohertz,
0513     Zeptohertz,
0514     Yoctohertz,
0515     RPM,
0516 
0517     // Thermal Conductivity
0518     /** @since 5.27 */
0519     WattPerMeterKelvin = 18000,
0520     /** @since 5.27 */
0521     BtuPerFootHourFahrenheit,
0522     /** @since 5.27 */
0523     BtuPerSquareFootHourFahrenheitPerInch,
0524 
0525     // Thermal Flux Density
0526     /** @since 5.27 */
0527     WattPerSquareMeter = 19000,
0528     /** @since 5.27 */
0529     BtuPerHourPerSquareFoot,
0530 
0531     // Thermal Generation per volume
0532     /** @since 5.27 */
0533     WattPerCubicMeter = 20000,
0534     /** @since 5.27 */
0535     BtuPerHourPerCubicFoot,
0536 
0537     // Voltage
0538     /** @since 5.27 */
0539     Yottavolts = 30000,
0540     /** @since 5.27 */
0541     Zettavolts,
0542     /** @since 5.27 */
0543     Exavolts,
0544     /** @since 5.27 */
0545     Petavolts,
0546     /** @since 5.27 */
0547     Teravolts,
0548     /** @since 5.27 */
0549     Gigavolts,
0550     /** @since 5.27 */
0551     Megavolts,
0552     /** @since 5.27 */
0553     Kilovolts,
0554     /** @since 5.27 */
0555     Hectovolts,
0556     /** @since 5.27 */
0557     Decavolts,
0558     /** @since 5.27 */
0559     Volts,
0560     /** @since 5.27 */
0561     Decivolts,
0562     /** @since 5.27 */
0563     Centivolts,
0564     /** @since 5.27 */
0565     Millivolts,
0566     /** @since 5.27 */
0567     Microvolts,
0568     /** @since 5.27 */
0569     Nanovolts,
0570     /** @since 5.27 */
0571     Picovolts,
0572     /** @since 5.27 */
0573     Femtovolts,
0574     /** @since 5.27 */
0575     Attovolts,
0576     /** @since 5.27 */
0577     Zeptovolts,
0578     /** @since 5.27 */
0579     Yoctovolts,
0580     /** @since 5.27 */
0581     Statvolts,
0582 
0583     // Electrical Current
0584     /** @since 5.27 */
0585     Yottaampere = 31000,
0586     /** @since 5.27 */
0587     Zettaampere,
0588     /** @since 5.27 */
0589     Exaampere,
0590     /** @since 5.27 */
0591     Petaampere,
0592     /** @since 5.27 */
0593     Teraampere,
0594     /** @since 5.27 */
0595     Gigaampere,
0596     /** @since 5.27 */
0597     Megaampere,
0598     /** @since 5.27 */
0599     Kiloampere,
0600     /** @since 5.27 */
0601     Hectoampere,
0602     /** @since 5.27 */
0603     Decaampere,
0604     /** @since 5.27 */
0605     Ampere,
0606     /** @since 5.27 */
0607     Deciampere,
0608     /** @since 5.27 */
0609     Centiampere,
0610     /** @since 5.27 */
0611     Milliampere,
0612     /** @since 5.27 */
0613     Microampere,
0614     /** @since 5.27 */
0615     Nanoampere,
0616     /** @since 5.27 */
0617     Picoampere,
0618     /** @since 5.27 */
0619     Femtoampere,
0620     /** @since 5.27 */
0621     Attoampere,
0622     /** @since 5.27 */
0623     Zeptoampere,
0624     /** @since 5.27 */
0625     Yoctoampere,
0626 
0627     // Electrical Resistance
0628     /** @since 5.27 */
0629     Yottaohms = 32000,
0630     /** @since 5.27 */
0631     Zettaohms,
0632     /** @since 5.27 */
0633     Exaohms,
0634     /** @since 5.27 */
0635     Petaohms,
0636     /** @since 5.27 */
0637     Teraohms,
0638     /** @since 5.27 */
0639     Gigaohms,
0640     /** @since 5.27 */
0641     Megaohms,
0642     /** @since 5.27 */
0643     Kiloohms,
0644     /** @since 5.27 */
0645     Hectoohms,
0646     /** @since 5.27 */
0647     Decaohms,
0648     /** @since 5.27 */
0649     Ohms,
0650     /** @since 5.27 */
0651     Deciohms,
0652     /** @since 5.27 */
0653     Centiohms,
0654     /** @since 5.27 */
0655     Milliohms,
0656     /** @since 5.27 */
0657     Microohms,
0658     /** @since 5.27 */
0659     Nanoohms,
0660     /** @since 5.27 */
0661     Picoohms,
0662     /** @since 5.27 */
0663     Femtoohms,
0664     /** @since 5.27 */
0665     Attoohms,
0666     /** @since 5.27 */
0667     Zeptoohms,
0668     /** @since 5.27 */
0669     Yoctoohms,
0670 
0671     /** @since 5.53 */
0672     Darcy = 33000,
0673     /** @since 5.53 */
0674     MiliDarcy,
0675     /** @since 5.53 */
0676     PermeabilitySquareMicrometer,
0677 
0678     /** @since 5.61 */
0679     Yobibyte = 34000,
0680     /** @since 5.61 */
0681     Yobibit,
0682     /** @since 5.61 */
0683     Yottabyte,
0684     /** @since 5.61 */
0685     Yottabit,
0686     /** @since 5.61 */
0687     Zebibyte,
0688     /** @since 5.61 */
0689     Zebibit,
0690     /** @since 5.61 */
0691     Zettabyte,
0692     /** @since 5.61 */
0693     Zettabit,
0694     /** @since 5.61 */
0695     Exbibyte,
0696     /** @since 5.61 */
0697     Exbibit,
0698     /** @since 5.61 */
0699     Exabyte,
0700     /** @since 5.61 */
0701     Exabit,
0702     /** @since 5.61 */
0703     Pebibyte,
0704     /** @since 5.61 */
0705     Pebibit,
0706     /** @since 5.61 */
0707     Petabyte,
0708     /** @since 5.61 */
0709     Petabit,
0710     /** @since 5.61 */
0711     Tebibyte,
0712     /** @since 5.61 */
0713     Tebibit,
0714     /** @since 5.61 */
0715     Terabyte,
0716     /** @since 5.61 */
0717     Terabit,
0718     /** @since 5.61 */
0719     Gibibyte,
0720     /** @since 5.61 */
0721     Gibibit,
0722     /** @since 5.61 */
0723     Gigabyte,
0724     /** @since 5.61 */
0725     Gigabit,
0726     /** @since 5.61 */
0727     Mebibyte,
0728     /** @since 5.61 */
0729     Mebibit,
0730     /** @since 5.61 */
0731     Megabyte,
0732     /** @since 5.61 */
0733     Megabit,
0734     /** @since 5.61 */
0735     Kibibyte,
0736     /** @since 5.61 */
0737     Kibibit,
0738     /** @since 5.61 */
0739     Kilobyte,
0740     /** @since 5.61 */
0741     Kilobit,
0742     /** @since 5.61 */
0743     Byte,
0744     /** @since 5.61 */
0745     Bit
0746 };
0747 
0748 class UnitCategory;
0749 class UnitPrivate;
0750 
0751 /**
0752  * @short Class to define a unit of measurement
0753  *
0754  * This is a class to define a unit of measurement.
0755  *
0756  * @see Converter, UnitCategory, Value
0757  *
0758  * @author Petri Damstén <damu@iki.fi>
0759  * @author John Layt <jlayt@kde.org>
0760  */
0761 
0762 class KUNITCONVERSION_EXPORT Unit
0763 {
0764 public:
0765     /**
0766      * Null constructor
0767      **/
0768     Unit();
0769 
0770     /**
0771      * Copy constructor, copy @p other to this.
0772      **/
0773     Unit(const Unit &other);
0774 
0775     ~Unit();
0776 
0777     /**
0778      * Assignment operator, assign @p other to this.
0779      **/
0780     Unit &operator=(const Unit &other);
0781 
0782     /**
0783      * Move-assigns @p other to this Unit instance, transferring the
0784      * ownership of the managed pointer to this instance.
0785      **/
0786     Unit &operator=(Unit &&other);
0787 
0788     /**
0789      * @return @c true if this Unit is equal to the @p other Unit.
0790      **/
0791     bool operator==(const Unit &other) const;
0792 
0793     /**
0794      * @return @c true if this Unit is not equal to the @p other Unit.
0795      **/
0796     bool operator!=(const Unit &other) const;
0797 
0798     /**
0799      * @return returns true if this Unit is null
0800      **/
0801     bool isNull() const;
0802 
0803     /**
0804      * @return if unit is valid.
0805      **/
0806     bool isValid() const;
0807 
0808     /**
0809      * @return unit id.
0810      **/
0811     UnitId id() const;
0812 
0813     /**
0814      * @return category id.
0815      **/
0816     CategoryId categoryId() const;
0817 
0818     /**
0819      * @return unit category.
0820      **/
0821     UnitCategory category() const;
0822 
0823     /**
0824      * @return translated name for unit.
0825      **/
0826     QString description() const;
0827 
0828     /**
0829      * @return symbol for the unit.
0830      **/
0831     QString symbol() const;
0832 
0833     /**
0834      * @param value number value
0835      * @param fieldWidth width of the formatted field, padded by spaces.
0836      *                   Positive value aligns right, negative aligns left
0837      * @param format type of floating point formatting, like in QString::arg
0838      * @param precision number of digits after the decimal separator
0839      * @param fillChar the character used to fill up the empty places when
0840      *                 field width is greater than argument width
0841      * @return value + unit string
0842      **/
0843     QString toString(qreal value, int fieldWidth = 0, char format = 'g', int precision = -1, const QChar &fillChar = QLatin1Char(' ')) const;
0844 
0845     /**
0846      * @param value number value
0847      * @param fieldWidth width of the formatted field, padded by spaces.
0848      *                   Positive value aligns right, negative aligns left
0849      * @param format type of floating point formatting, like in QString::arg
0850      * @param precision number of digits after the decimal separator
0851      * @param fillChar the character used to fill up the empty places when
0852      *                 field width is greater than argument width
0853      * @return value + unit string
0854      **/
0855     QString toSymbolString(qreal value, int fieldWidth = 0, char format = 'g', int precision = -1, const QChar &fillChar = QLatin1Char(' ')) const;
0856 
0857 protected:
0858     qreal toDefault(qreal value) const;
0859     qreal fromDefault(qreal value) const;
0860 
0861 private:
0862     friend class UnitPrivate;
0863     friend class UnitCategory;
0864     friend class UnitCategoryPrivate;
0865     friend class CurrencyCategoryPrivate;
0866 
0867     KUNITCONVERSION_NO_EXPORT explicit Unit(UnitPrivate *dd);
0868 
0869     KUNITCONVERSION_NO_EXPORT void setUnitMultiplier(qreal multiplier);
0870 
0871     QExplicitlySharedDataPointer<UnitPrivate> d;
0872 };
0873 
0874 } // KUnitConversion namespace
0875 
0876 Q_DECLARE_TYPEINFO(KUnitConversion::Unit, Q_RELOCATABLE_TYPE);
0877 
0878 #endif