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

source code of kunitconversion/src/unit.h