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

KDE's Doxygen guidelines are available online.