1 /*
2  *   SPDX-FileCopyrightText: 2007-2009 Petri Damstén <damu@iki.fi>
3  *   SPDX-FileCopyrightText: 2014 John Layt <jlayt@kde.org>
4  *
5  *   SPDX-License-Identifier: LGPL-2.0-or-later
6  */
7 
8 #ifndef KUNITCONVERSION_UNIT_H
9 #define KUNITCONVERSION_UNIT_H
10 
11 #include "kunitconversion/kunitconversion_export.h"
12 
13 #include <QExplicitlySharedDataPointer>
14 #include <QString>
15 
16 namespace KUnitConversion
17 {
18 enum CategoryId {
19     InvalidCategory = -1,
20     LengthCategory,
21     AreaCategory,
22     VolumeCategory,
23     TemperatureCategory,
24     VelocityCategory,
25     MassCategory,
26     PressureCategory,
27     EnergyCategory,
28     CurrencyCategory,
29     PowerCategory,
30     TimeCategory,
31     FuelEfficiencyCategory,
32     DensityCategory,
33     AccelerationCategory,
34     AngleCategory,
35     FrequencyCategory,
36     ForceCategory,
37     /** @since 5.27 */
38     ThermalConductivityCategory,
39     /** @since 5.27 */
40     ThermalFluxCategory,
41     /** @since 5.27 */
42     ThermalGenerationCategory,
43     /** @since 5.27 */
44     VoltageCategory,
45     /** @since 5.27 */
46     ElectricalCurrentCategory,
47     /** @since 5.27 */
48     ElectricalResistanceCategory,
49     /** @since 5.53 */
50     PermeabilityCategory,
51     /** @since 5.61 **/
52     BinaryDataCategory
53 };
54 
55 enum UnitId {
56     InvalidUnit = -1,
57     NoUnit = 0,
58     Percent = 1,
59 
60     // Area
61     SquareYottameter = 1000,
62     SquareZettameter,
63     SquareExameter,
64     SquarePetameter,
65     SquareTerameter,
66     SquareGigameter,
67     SquareMegameter,
68     SquareKilometer,
69     SquareHectometer,
70     SquareDecameter,
71     SquareMeter,
72     SquareDecimeter,
73     SquareCentimeter,
74     SquareMillimeter,
75     SquareMicrometer,
76     SquareNanometer,
77     SquarePicometer,
78     SquareFemtometer,
79     SquareAttometer,
80     SquareZeptometer,
81     SquareYoctometer,
82     Acre,
83     SquareFoot,
84     SquareInch,
85     SquareMile,
86 
87     // Length
88     Yottameter = 2000,
89     Zettameter,
90     Exameter,
91     Petameter,
92     Terameter,
93     Gigameter,
94     Megameter,
95     Kilometer,
96     Hectometer,
97     Decameter,
98     Meter,
99     Decimeter,
100     Centimeter,
101     Millimeter,
102     Micrometer,
103     Nanometer,
104     Picometer,
105     Femtometer,
106     Attometer,
107     Zeptometer,
108     Yoctometer,
109     Inch,
110     Foot,
111     Yard,
112     Mile,
113     NauticalMile,
114     LightYear,
115     Parsec,
116     AstronomicalUnit,
117     Thou,
118     Angstrom,
119 
120     // Volume
121     CubicYottameter = 3000,
122     CubicZettameter,
123     CubicExameter,
124     CubicPetameter,
125     CubicTerameter,
126     CubicGigameter,
127     CubicMegameter,
128     CubicKilometer,
129     CubicHectometer,
130     CubicDecameter,
131     CubicMeter,
132     CubicDecimeter,
133     CubicCentimeter,
134     CubicMillimeter,
135     CubicMicrometer,
136     CubicNanometer,
137     CubicPicometer,
138     CubicFemtometer,
139     CubicAttometer,
140     CubicZeptometer,
141     CubicYoctometer,
142     Yottaliter,
143     Zettaliter,
144     Exaliter,
145     Petaliter,
146     Teraliter,
147     Gigaliter,
148     Megaliter,
149     Kiloliter,
150     Hectoliter,
151     Decaliter,
152     Liter,
153     Deciliter,
154     Centiliter,
155     Milliliter,
156     Microliter,
157     Nanoliter,
158     Picoliter,
159     Femtoliter,
160     Attoliter,
161     Zeptoliter,
162     Yoctoliter,
163     CubicFoot,
164     CubicInch,
165     CubicMile,
166     FluidOunce,
167     Cup,
168     GallonUS,
169     PintImperial,
170     /** @since 5.53 */
171     OilBarrel,
172     /** @since 5.70 */
173     GallonImperial,
174     PintUS,
175 
176     // Mass
177     Yottagram = 4000,
178     Zettagram,
179     Exagram,
180     Petagram,
181     Teragram,
182     Gigagram,
183     Megagram,
184     Kilogram,
185     Hectogram,
186     Decagram,
187     Gram,
188     Decigram,
189     Centigram,
190     Milligram,
191     Microgram,
192     Nanogram,
193     Picogram,
194     Femtogram,
195     Attogram,
196     Zeptogram,
197     Yoctogram,
198     Ton,
199     Carat,
200     Pound,
201     Ounce,
202     TroyOunce,
203     MassNewton,
204     Kilonewton,
205 
206     /** @since 5.26 */
207     Stone,
208 
209     // Pressure
210     Yottapascal = 5000,
211     Zettapascal,
212     Exapascal,
213     Petapascal,
214     Terapascal,
215     Gigapascal,
216     Megapascal,
217     Kilopascal,
218     Hectopascal,
219     Decapascal,
220     Pascal,
221     Decipascal,
222     Centipascal,
223     Millipascal,
224     Micropascal,
225     Nanopascal,
226     Picopascal,
227     Femtopascal,
228     Attopascal,
229     Zeptopascal,
230     Yoctopascal,
231     Bar,
232     Millibar,
233     Decibar,
234     Torr,
235     TechnicalAtmosphere,
236     Atmosphere,
237     PoundForcePerSquareInch,
238     InchesOfMercury,
239     MillimetersOfMercury,
240 
241     // Temperature
242     Kelvin = 6000,
243     Celsius,
244     Fahrenheit,
245     Rankine,
246     Delisle,
247     TemperatureNewton,
248     Reaumur,
249     Romer,
250 
251     // Energy
252     Yottajoule = 7000,
253     Zettajoule,
254     Exajoule,
255     Petajoule,
256     Terajoule,
257     Gigajoule,
258     Megajoule,
259     Kilojoule,
260     Hectojoule,
261     Decajoule,
262     Joule,
263     Decijoule,
264     Centijoule,
265     Millijoule,
266     Microjoule,
267     Nanojoule,
268     Picojoule,
269     Femtojoule,
270     Attojoule,
271     Zeptojoule,
272     Yoctojoule,
273     GuidelineDailyAmount,
274     Electronvolt,
275     Rydberg,
276     Kilocalorie,
277     PhotonWavelength,
278     KiloJoulePerMole,
279     JoulePerMole,
280     /** @since 5.27 */
281     Btu,
282     /** @since 5.27 */
283     Erg,
284 
285     // Currency
286     Eur = 8000,
287     Ats,
288     Bef,
289     Nlg,
290     Fim,
291     Frf,
292     Dem,
293     Iep,
294     Itl,
295     Luf,
296     Pte,
297     Esp,
298     Grd,
299     Sit,
300     Cyp,
301     Mtl,
302     Skk,
303     Usd,
304     Jpy,
305     Bgn,
306     Czk,
307     Dkk,
308     Eek,
309     Gbp,
310     Huf,
311     Ltl,
312     Lvl,
313     Pln,
314     Ron,
315     Sek,
316     Chf,
317     Nok,
318     Hrk,
319     Rub,
320     Try,
321     Aud,
322     Brl,
323     Cad,
324     Cny,
325     Hkd,
326     Idr,
327     Inr,
328     Krw,
329     Mxn,
330     Myr,
331     Nzd,
332     Php,
333     Sgd,
334     Thb,
335     Zar,
336     Ils,
337     Isk,
338 
339     // Velocity
340     MeterPerSecond = 9000,
341     KilometerPerHour,
342     MilePerHour,
343     FootPerSecond,
344     InchPerSecond,
345     Knot,
346     Mach,
347     SpeedOfLight,
348     Beaufort,
349 
350     // Power
351     Yottawatt = 10000,
352     Zettawatt,
353     Exawatt,
354     Petawatt,
355     Terawatt,
356     Gigawatt,
357     Megawatt,
358     Kilowatt,
359     Hectowatt,
360     Decawatt,
361     Watt,
362     Deciwatt,
363     Centiwatt,
364     Milliwatt,
365     Microwatt,
366     Nanowatt,
367     Picowatt,
368     Femtowatt,
369     Attowatt,
370     Zeptowatt,
371     Yoctowatt,
372     Horsepower,
373     /** @since 5.62 */
374     DecibelKilowatt,
375     DecibelWatt,
376     DecibelMilliwatt,
377     DecibelMicrowatt,
378 
379     // Time
380     Yottasecond = 11000,
381     Zettasecond,
382     Exasecond,
383     Petasecond,
384     Terasecond,
385     Gigasecond,
386     Megasecond,
387     Kilosecond,
388     Hectosecond,
389     Decasecond,
390     Second,
391     Decisecond,
392     Centisecond,
393     Millisecond,
394     Microsecond,
395     Nanosecond,
396     Picosecond,
397     Femtosecond,
398     Attosecond,
399     Zeptosecond,
400     Yoctosecond,
401     Minute,
402     Hour,
403     Day,
404     Week,
405     JulianYear,
406     LeapYear,
407     Year,
408 
409     // FuelEfficiency
410     LitersPer100Kilometers = 12000,
411     MilePerUsGallon,
412     MilePerImperialGallon,
413     KilometrePerLitre,
414 
415     // Density
416     YottakilogramsPerCubicMeter = 13000,
417     ZettakilogramPerCubicMeter,
418     ExakilogramPerCubicMeter,
419     PetakilogramPerCubicMeter,
420     TerakilogramPerCubicMeter,
421     GigakilogramPerCubicMeter,
422     MegakilogramPerCubicMeter,
423     KilokilogramPerCubicMeter,
424     HectokilogramsPerCubicMeter,
425     DecakilogramsPerCubicMeter,
426     KilogramsPerCubicMeter,
427     DecikilogramsPerCubicMeter,
428     CentikilogramsPerCubicMeter,
429     MillikilogramsPerCubicMeter,
430     MicrokilogramsPerCubicMeter,
431     NanokilogramsPerCubicMeter,
432     PicokilogramsPerCubicMeter,
433     FemtokilogramsPerCubicMeter,
434     AttokilogramsPerCubicMeter,
435     ZeptokilogramsPerCubicMeter,
436     YoctokilogramsPerCubicMeter,
437     KilogramPerLiter,
438     GramPerLiter,
439     GramPerMilliliter,
440     OuncePerCubicInch,
441     OuncePerCubicFoot,
442     OuncePerCubicYard,
443     PoundPerCubicInch,
444     PoundPerCubicFoot,
445     PoundPerCubicYard,
446 
447     // Acceleration
448     MetresPerSecondSquared = 14000,
449     FeetPerSecondSquared,
450     StandardGravity,
451 
452     // Force
453     Yottanewton = 15000,
454     Zettanewton,
455     Exanewton,
456     Petanewton,
457     Teranewton,
458     Giganewton,
459     Meganewton,
460     KilonewtonForce,
461     Hectonewton,
462     Decanewton,
463     Newton,
464     Decinewton,
465     Centinewton,
466     Millinewton,
467     Micronewton,
468     Nanonewton,
469     Piconewton,
470     Femtonewton,
471     Attonewton,
472     Zeptonewton,
473     Yoctonewton,
474     Dyne,
475     Kilopond,
476     PoundForce,
477     Poundal,
478 
479     // Angle
480     Degree = 16000,
481     Radian,
482     Gradian,
483     ArcMinute,
484     ArcSecond,
485 
486     // Frequency
487     Yottahertz = 17000,
488     Zettahertz,
489     Exahertz,
490     Petahertz,
491     Terahertz,
492     Gigahertz,
493     Megahertz,
494     Kilohertz,
495     Hectohertz,
496     Decahertz,
497     Hertz,
498     Decihertz,
499     Centihertz,
500     Millihertz,
501     Microhertz,
502     Nanohertz,
503     Picohertz,
504     Femtohertz,
505     Attohertz,
506     Zeptohertz,
507     Yoctohertz,
508     RPM,
509 
510     // Thermal Conductivity
511     /** @since 5.27 */
512     WattPerMeterKelvin = 18000,
513     /** @since 5.27 */
514     BtuPerFootHourFahrenheit,
515     /** @since 5.27 */
516     BtuPerSquareFootHourFahrenheitPerInch,
517 
518     // Thermal Flux Density
519     /** @since 5.27 */
520     WattPerSquareMeter = 19000,
521     /** @since 5.27 */
522     BtuPerHourPerSquareFoot,
523 
524     // Thermal Generation per volume
525     /** @since 5.27 */
526     WattPerCubicMeter = 20000,
527     /** @since 5.27 */
528     BtuPerHourPerCubicFoot,
529 
530     // Voltage
531     /** @since 5.27 */
532     Yottavolts = 30000,
533     /** @since 5.27 */
534     Zettavolts,
535     /** @since 5.27 */
536     Exavolts,
537     /** @since 5.27 */
538     Petavolts,
539     /** @since 5.27 */
540     Teravolts,
541     /** @since 5.27 */
542     Gigavolts,
543     /** @since 5.27 */
544     Megavolts,
545     /** @since 5.27 */
546     Kilovolts,
547     /** @since 5.27 */
548     Hectovolts,
549     /** @since 5.27 */
550     Decavolts,
551     /** @since 5.27 */
552     Volts,
553     /** @since 5.27 */
554     Decivolts,
555     /** @since 5.27 */
556     Centivolts,
557     /** @since 5.27 */
558     Millivolts,
559     /** @since 5.27 */
560     Microvolts,
561     /** @since 5.27 */
562     Nanovolts,
563     /** @since 5.27 */
564     Picovolts,
565     /** @since 5.27 */
566     Femtovolts,
567     /** @since 5.27 */
568     Attovolts,
569     /** @since 5.27 */
570     Zeptovolts,
571     /** @since 5.27 */
572     Yoctovolts,
573     /** @since 5.27 */
574     Statvolts,
575 
576     // Electrical Current
577     /** @since 5.27 */
578     Yottaampere = 31000,
579     /** @since 5.27 */
580     Zettaampere,
581     /** @since 5.27 */
582     Exaampere,
583     /** @since 5.27 */
584     Petaampere,
585     /** @since 5.27 */
586     Teraampere,
587     /** @since 5.27 */
588     Gigaampere,
589     /** @since 5.27 */
590     Megaampere,
591     /** @since 5.27 */
592     Kiloampere,
593     /** @since 5.27 */
594     Hectoampere,
595     /** @since 5.27 */
596     Decaampere,
597     /** @since 5.27 */
598     Ampere,
599     /** @since 5.27 */
600     Deciampere,
601     /** @since 5.27 */
602     Centiampere,
603     /** @since 5.27 */
604     Milliampere,
605     /** @since 5.27 */
606     Microampere,
607     /** @since 5.27 */
608     Nanoampere,
609     /** @since 5.27 */
610     Picoampere,
611     /** @since 5.27 */
612     Femtoampere,
613     /** @since 5.27 */
614     Attoampere,
615     /** @since 5.27 */
616     Zeptoampere,
617     /** @since 5.27 */
618     Yoctoampere,
619 
620     // Electrical Resistance
621     /** @since 5.27 */
622     Yottaohms = 32000,
623     /** @since 5.27 */
624     Zettaohms,
625     /** @since 5.27 */
626     Exaohms,
627     /** @since 5.27 */
628     Petaohms,
629     /** @since 5.27 */
630     Teraohms,
631     /** @since 5.27 */
632     Gigaohms,
633     /** @since 5.27 */
634     Megaohms,
635     /** @since 5.27 */
636     Kiloohms,
637     /** @since 5.27 */
638     Hectoohms,
639     /** @since 5.27 */
640     Decaohms,
641     /** @since 5.27 */
642     Ohms,
643     /** @since 5.27 */
644     Deciohms,
645     /** @since 5.27 */
646     Centiohms,
647     /** @since 5.27 */
648     Milliohms,
649     /** @since 5.27 */
650     Microohms,
651     /** @since 5.27 */
652     Nanoohms,
653     /** @since 5.27 */
654     Picoohms,
655     /** @since 5.27 */
656     Femtoohms,
657     /** @since 5.27 */
658     Attoohms,
659     /** @since 5.27 */
660     Zeptoohms,
661     /** @since 5.27 */
662     Yoctoohms,
663 
664     /** @since 5.53 */
665     Darcy = 33000,
666     /** @since 5.53 */
667     MiliDarcy,
668     /** @since 5.53 */
669     PermeabilitySquareMicrometer,
670 
671     /** @since 5.61 */
672     Yobibyte = 34000,
673     /** @since 5.61 */
674     Yobibit,
675     /** @since 5.61 */
676     Yottabyte,
677     /** @since 5.61 */
678     Yottabit,
679     /** @since 5.61 */
680     Zebibyte,
681     /** @since 5.61 */
682     Zebibit,
683     /** @since 5.61 */
684     Zettabyte,
685     /** @since 5.61 */
686     Zettabit,
687     /** @since 5.61 */
688     Exbibyte,
689     /** @since 5.61 */
690     Exbibit,
691     /** @since 5.61 */
692     Exabyte,
693     /** @since 5.61 */
694     Exabit,
695     /** @since 5.61 */
696     Pebibyte,
697     /** @since 5.61 */
698     Pebibit,
699     /** @since 5.61 */
700     Petabyte,
701     /** @since 5.61 */
702     Petabit,
703     /** @since 5.61 */
704     Tebibyte,
705     /** @since 5.61 */
706     Tebibit,
707     /** @since 5.61 */
708     Terabyte,
709     /** @since 5.61 */
710     Terabit,
711     /** @since 5.61 */
712     Gibibyte,
713     /** @since 5.61 */
714     Gibibit,
715     /** @since 5.61 */
716     Gigabyte,
717     /** @since 5.61 */
718     Gigabit,
719     /** @since 5.61 */
720     Mebibyte,
721     /** @since 5.61 */
722     Mebibit,
723     /** @since 5.61 */
724     Megabyte,
725     /** @since 5.61 */
726     Megabit,
727     /** @since 5.61 */
728     Kibibyte,
729     /** @since 5.61 */
730     Kibibit,
731     /** @since 5.61 */
732     Kilobyte,
733     /** @since 5.61 */
734     Kilobit,
735     /** @since 5.61 */
736     Byte,
737     /** @since 5.61 */
738     Bit
739 };
740 
741 class UnitCategory;
742 class UnitPrivate;
743 
744 /**
745  * @short Class to define a unit of measurement
746  *
747  * This is a class to define a unit of measurement.
748  *
749  * @see Converter, UnitCategory, Value
750  *
751  * @author Petri Damstén <damu@iki.fi>
752  * @author John Layt <jlayt@kde.org>
753  */
754 
755 class KUNITCONVERSION_EXPORT Unit
756 {
757 public:
758     /**
759      * Null constructor
760      **/
761     Unit();
762 
763     /**
764      * Copy constructor, copy @p other to this.
765      **/
766     Unit(const Unit &other);
767 
768     virtual ~Unit();
769 
770     /**
771      * Assignment operator, assign @p other to this.
772      **/
773     Unit &operator=(const Unit &other);
774 
775 #ifdef Q_COMPILER_RVALUE_REFS
776     /**
777      * Move-assigns @p other to this Unit instance, transferring the
778      * ownership of the managed pointer to this instance.
779      **/
780     Unit &operator=(Unit &&other)
781     {
782         swap(other);
783         return *this;
784     }
785 #endif
786 
787     /**
788      * Swaps this Unit with @p other. This function is very fast and never fails.
789      **/
swap(Unit & other)790     void swap(Unit &other)
791     {
792         d.swap(other.d);
793     }
794 
795     /**
796      * @return @c true if this Unit is equal to the @p other Unit.
797      **/
798     bool operator==(const Unit &other) const;
799 
800     /**
801      * @return @c true if this Unit is not equal to the @p other Unit.
802      **/
803     bool operator!=(const Unit &other) const;
804 
805     /**
806      * @return returns true if this Unit is null
807      **/
808     bool isNull() const;
809 
810     /**
811      * @return if unit is valid.
812      **/
813     bool isValid() const;
814 
815     /**
816      * @return unit id.
817      **/
818     UnitId id() const;
819 
820     /**
821      * @return category id.
822      **/
823     CategoryId categoryId() const;
824 
825     /**
826      * @return unit category.
827      **/
828     UnitCategory category() const;
829 
830     /**
831      * @return translated name for unit.
832      **/
833     QString description() const;
834 
835     /**
836      * @return symbol for the unit.
837      **/
838     QString symbol() const;
839 
840     /**
841      * @param value number value
842      * @param fieldWidth width of the formatted field, padded by spaces.
843      *                   Positive value aligns right, negative aligns left
844      * @param format type of floating point formatting, like in QString::arg
845      * @param precision number of digits after the decimal separator
846      * @param fillChar the character used to fill up the empty places when
847      *                 field width is greater than argument width
848      * @return value + unit string
849      **/
850     QString toString(qreal value, int fieldWidth = 0, char format = 'g', int precision = -1, const QChar &fillChar = QLatin1Char(' ')) const;
851 
852     /**
853      * @param value number value
854      * @param fieldWidth width of the formatted field, padded by spaces.
855      *                   Positive value aligns right, negative aligns left
856      * @param format type of floating point formatting, like in QString::arg
857      * @param precision number of digits after the decimal separator
858      * @param fillChar the character used to fill up the empty places when
859      *                 field width is greater than argument width
860      * @return value + unit string
861      **/
862     QString toSymbolString(qreal value, int fieldWidth = 0, char format = 'g', int precision = -1, const QChar &fillChar = QLatin1Char(' ')) const;
863 
864 protected:
865     qreal toDefault(qreal value) const;
866     qreal fromDefault(qreal value) const;
867 
868 private:
869     friend class CustomUnit;
870     friend class UnitCategory;
871     friend class UnitCategoryPrivate;
872     friend class CurrencyCategoryPrivate;
873 
874     Unit(UnitPrivate *dd);
875     void setUnitMultiplier(qreal multiplier);
876 
877     QExplicitlySharedDataPointer<UnitPrivate> d;
878 };
879 
880 } // KUnitConversion namespace
881 
882 #endif
883