KUnitConversion

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

KDE's Doxygen guidelines are available online.