Libksysguard

Formatter.cpp
1/*
2 SPDX-FileCopyrightText: 2019 Vlad Zahorodnii <vlad.zahorodnii@kde.org>
3
4 formatBootTimestamp is based on TimeUtil class:
5 SPDX-FileCopyrightText: 2014 Gregor Mi <codestruct@posteo.org>
6
7 SPDX-License-Identifier: LGPL-2.0-or-later
8*/
9
10#include "Formatter.h"
11
12#include <KFormat>
13#include <KLocalizedString>
14
15#include <QFontMetrics>
16#include <QLocale>
17#include <QTime>
18
19#include <cmath>
20#include <ctime>
21
22#include <time.h>
23#include <unistd.h>
24
25#include "formatter_debug.h"
26
27namespace KSysGuard
28{
29// TODO: Is there a bit nicer way to handle formatting?
30
31static KLocalizedString unitFormat(Unit unit)
32{
33 // The strings here use \u200B (zero-width space) to allow the unit symbol
34 // to be wrapped to a new line when there is little space and potentially
35 // even be hidden that way. Since the prefix is part of the unit it risks
36 // hiding the prefix, so the zero-width space is placed after the prefix and
37 // we use \u202F (narrow no-break space) to ensure we have some separation
38 // but do not wrap it. For the prefix-less units where we *do* want to allow
39 // wrapping, we use \u2009 (thin space) to ensure we have the same spacing
40 // between prefix-less and prefixed units.
41
42 // i18n: Use \u2009 (thin space) to separate value and unit
43 const static KLocalizedString B = ki18nc("Bytes unit symbol", "%1\u2009B");
44 // i18n: Use \u202F (thin non-break space) to separate value and prefixed unit; Use \u200B (zero-width space) to separate prefix and unit.
45 const static KLocalizedString KiB = ki18nc("Kilobytes unit symbol", "%1\u202FKi\u200BB");
46 // i18n: Use \u202F (thin non-break space) to separate value and prefixed unit; Use \u200B (zero-width space) to separate prefix and unit.
47 const static KLocalizedString MiB = ki18nc("Megabytes unit symbol", "%1\u202FMi\u200BB");
48 // i18n: Use \u202F (thin non-break space) to separate value and prefixed unit; Use \u200B (zero-width space) to separate prefix and unit.
49 const static KLocalizedString GiB = ki18nc("Gigabytes unit symbol", "%1\u202FGi\u200BB");
50 // i18n: Use \u202F (thin non-break space) to separate value and prefixed unit; Use \u200B (zero-width space) to separate prefix and unit.
51 const static KLocalizedString TiB = ki18nc("Terabytes unit symbol", "%1\u202FTi\u200BB");
52 // i18n: Use \u202F (thin non-break space) to separate value and prefixed unit; Use \u200B (zero-width space) to separate prefix and unit.
53 const static KLocalizedString PiB = ki18nc("Petabytes unit symbol", "%1\u202FPi\u200BB");
54
55 // i18n: Use \u2009 (thin space) to separate value and unit
56 const static KLocalizedString bps = ki18nc("Bytes per second unit symbol", "%1\u2009B/s");
57 // i18n: Use \u202F (thin non-break space) to separate value and prefixed unit; Use \u200B (zero-width space) to separate prefix and unit.
58 const static KLocalizedString Kbps = ki18nc("Kilobytes per second unit symbol", "%1\u202FKi\u200BB/s");
59 // i18n: Use \u202F (thin non-break space) to separate value and prefixed unit; Use \u200B (zero-width space) to separate prefix and unit.
60 const static KLocalizedString Mbps = ki18nc("Megabytes per second unit symbol", "%1\u202FMi\u200BB/s");
61 // i18n: Use \u202F (thin non-break space) to separate value and prefixed unit; Use \u200B (zero-width space) to separate prefix and unit.
62 const static KLocalizedString Gbps = ki18nc("Gigabytes per second unit symbol", "%1\u202FGi\u200BB/s");
63 // i18n: Use \u202F (thin non-break space) to separate value and prefixed unit; Use \u200B (zero-width space) to separate prefix and unit.
64 const static KLocalizedString Tbps = ki18nc("Terabytes per second unit symbol", "%1\u202FTi\u200BB/s");
65 // i18n: Use \u202F (thin non-break space) to separate value and prefixed unit; Use \u200B (zero-width space) to separate prefix and unit.
66 const static KLocalizedString Pbps = ki18nc("Petabytes per second unit symbol", "%1\u202FPi\u200BB/s");
67
68 // i18n: Use \u2009 (thin space) to separate value and unit
69 const static KLocalizedString bitsps = ki18nc("Bits per second unit symbol", "%1\u2009bps");
70 // i18n: Use \u202F (thin non-break space) to separate value and prefixed unit; Use \u200B (zero-width space) to separate prefix and unit.
71 const static KLocalizedString Kbitsps = ki18nc("Kilobits per second unit symbol", "%1\u202Fk\u200Bbps");
72 // i18n: Use \u202F (thin non-break space) to separate value and prefixed unit; Use \u200B (zero-width space) to separate prefix and unit.
73 const static KLocalizedString Mbitsps = ki18nc("Megabits per second unit symbol", "%1\u202FM\u200Bbps");
74 // i18n: Use \u202F (thin non-break space) to separate value and prefixed unit; Use \u200B (zero-width space) to separate prefix and unit.
75 const static KLocalizedString Gbitsps = ki18nc("Gigabits per second unit symbol", "%1\u202FG\u200Bbps");
76 // i18n: Use \u202F (thin non-break space) to separate value and prefixed unit; Use \u200B (zero-width space) to separate prefix and unit.
77 const static KLocalizedString Tbitsps = ki18nc("Terabits per second unit symbol", "%1\u202FT\u200Bbps");
78 // i18n: Use \u202F (thin non-break space) to separate value and prefixed unit; Use \u200B (zero-width space) to separate prefix and unit.
79 const static KLocalizedString Pbitsps = ki18nc("Petabits per second unit symbol", "%1\u202FP\u200Bbps");
80
81 // i18n: Use \u2009 (thin space) to separate value and unit
82 const static KLocalizedString Hz = ki18nc("Hertz unit symbol", "%1\u2009Hz");
83 // i18n: Use \u202F (thin non-break space) to separate value and prefixed unit; Use \u200B (zero-width space) to separate prefix and unit.
84 const static KLocalizedString kHz = ki18nc("Kilohertz unit symbol", "%1\u202Fk\u200BHz");
85 // i18n: Use \u202F (thin non-break space) to separate value and prefixed unit; Use \u200B (zero-width space) to separate prefix and unit.
86 const static KLocalizedString MHz = ki18nc("Megahertz unit symbol", "%1\u202FM\u200BHz");
87 // i18n: Use \u202F (thin non-break space) to separate value and prefixed unit; Use \u200B (zero-width space) to separate prefix and unit.
88 const static KLocalizedString GHz = ki18nc("Gigahertz unit symbol", "%1\u202FG\u200BHz");
89 // i18n: Use \u202F (thin non-break space) to separate value and prefixed unit; Use \u200B (zero-width space) to separate prefix and unit.
90 const static KLocalizedString THz = ki18nc("Terahertz unit symbol", "%1\u202FT\u200BHz");
91 // i18n: Use \u202F (thin non-break space) to separate value and prefixed unit; Use \u200B (zero-width space) to separate prefix and unit.
92 const static KLocalizedString PHz = ki18nc("Petahertz unit symbol", "%1\u202FP\u200BHz");
93
94 // i18n: Use \u2009 (thin space) to separate value and unit
95 const static KLocalizedString V = ki18nc("Volts unit symbol", "%1\u2009V");
96 // i18n: Use \u202F (thin non-break space) to separate value and prefixed unit; Use \u200B (zero-width space) to separate prefix and unit.
97 const static KLocalizedString kV = ki18nc("Kilovolts unit symbol", "%1\u202Fk\u200BV");
98 // i18n: Use \u202F (thin non-break space) to separate value and prefixed unit; Use \u200B (zero-width space) to separate prefix and unit.
99 const static KLocalizedString MV = ki18nc("Megavolts unit symbol", "%1\u202FM\u200BV");
100 // i18n: Use \u202F (thin non-break space) to separate value and prefixed unit; Use \u200B (zero-width space) to separate prefix and unit.
101 const static KLocalizedString GV = ki18nc("Gigavolts unit symbol", "%1\u202FG\u200BV");
102 // i18n: Use \u202F (thin non-break space) to separate value and prefixed unit; Use \u200B (zero-width space) to separate prefix and unit.
103 const static KLocalizedString TV = ki18nc("Teravolts unit symbol", "%1\u202FT\u200BV");
104 // i18n: Use \u202F (thin non-break space) to separate value and prefixed unit; Use \u200B (zero-width space) to separate prefix and unit.
105 const static KLocalizedString PV = ki18nc("Petavolts unit symbol", "%1\u202FP\u200BV");
106
107 // i18n: Use \u2009 (thin space) to separate value and unit
108 const static KLocalizedString W = ki18nc("Watts unit symbol", "%1\u2009W");
109 // i18n: Use \u202F (thin non-break space) to separate value and prefixed unit; Use \u200B (zero-width space) to separate prefix and unit.
110 const static KLocalizedString kW = ki18nc("Kilowatts unit symbol", "%1\u202Fk\u200BW");
111 // i18n: Use \u202F (thin non-break space) to separate value and prefixed unit; Use \u200B (zero-width space) to separate prefix and unit.
112 const static KLocalizedString MW = ki18nc("Megawatts unit symbol", "%1\u202FM\u200BW");
113 // i18n: Use \u202F (thin non-break space) to separate value and prefixed unit; Use \u200B (zero-width space) to separate prefix and unit.
114 const static KLocalizedString GW = ki18nc("Gigawatts unit symbol", "%1\u202FG\u200BW");
115 // i18n: Use \u202F (thin non-break space) to separate value and prefixed unit; Use \u200B (zero-width space) to separate prefix and unit.
116 const static KLocalizedString TW = ki18nc("Terawatts unit symbol", "%1\u202FT\u200BW");
117 // i18n: Use \u202F (thin non-break space) to separate value and prefixed unit; Use \u200B (zero-width space) to separate prefix and unit.
118 const static KLocalizedString PW = ki18nc("Petawatts unit symbol", "%1\u202FP\u200BW");
119
120 // i18n: Use \u2009 (thin space) to separate value and unit
121 const static KLocalizedString Wh = ki18nc("Watt-hours unit symbol", "%1\u2009Wh");
122 // i18n: Use \u202F (thin non-break space) to separate value and prefixed unit; Use \u200B (zero-width space) to separate prefix and unit.
123 const static KLocalizedString kWh = ki18nc("Kilowatt-hours unit symbol", "%1\u202Fk\u200BWh");
124 // i18n: Use \u202F (thin non-break space) to separate value and prefixed unit; Use \u200B (zero-width space) to separate prefix and unit.
125 const static KLocalizedString MWh = ki18nc("Megawatt-hours unit symbol", "%1\u202FM\u200BWh");
126 // i18n: Use \u202F (thin non-break space) to separate value and prefixed unit; Use \u200B (zero-width space) to separate prefix and unit.
127 const static KLocalizedString GWh = ki18nc("Gigawatt-hours unit symbol", "%1\u202FG\u200BWh");
128 // i18n: Use \u202F (thin non-break space) to separate value and prefixed unit; Use \u200B (zero-width space) to separate prefix and unit.
129 const static KLocalizedString TWh = ki18nc("Terawatt-hours unit symbol", "%1\u202FT\u200BWh");
130 // i18n: Use \u202F (thin non-break space) to separate value and prefixed unit; Use \u200B (zero-width space) to separate prefix and unit.
131 const static KLocalizedString PWh = ki18nc("Petawatt-hours unit symbol", "%1\u202FP\u200BWh");
132
133 // i18n: Use \u2009 (thin space) to separate value and unit
134 const static KLocalizedString A = ki18nc("Ampere unit symbol", "%1\u2009A");
135 // i18n: Use \u202F (thin non-break space) to separate value and prefixed unit; Use \u200B (zero-width space) to separate prefix and unit.
136 const static KLocalizedString kA = ki18nc("Kiloamperes unit symbol", "%1\u202Fk\u200BA");
137 // i18n: Use \u202F (thin non-break space) to separate value and prefixed unit; Use \u200B (zero-width space) to separate prefix and unit.
138 const static KLocalizedString MA = ki18nc("Megaamperes unit symbol", "%1\u202FM\u200BA");
139 // i18n: Use \u202F (thin non-break space) to separate value and prefixed unit; Use \u200B (zero-width space) to separate prefix and unit.
140 const static KLocalizedString GA = ki18nc("Gigaamperes unit symbol", "%1\u202FG\u200BA");
141 // i18n: Use \u202F (thin non-break space) to separate value and prefixed unit; Use \u200B (zero-width space) to separate prefix and unit.
142 const static KLocalizedString TA = ki18nc("Teraamperes unit symbol", "%1\u202FT\u200BA");
143 // // i18n: Use \u202F (thin non-break space) to separate value and prefixed unit; Use \u200B (zero-width space) to separate prefix and unit.
144 const static KLocalizedString PA = ki18nc("Petaamperes unit symbol", "%1\u202FP\u200BA");
145
146 // i18n: Use \u200B (zero width space) to separate value and unit
147 const static KLocalizedString percent = ki18nc("Percent unit", "%1\u200B%");
148 // i18n: Use \u2009 (thin space) to separate value and unit
149 const static KLocalizedString RPM = ki18nc("Revolutions per minute unit symbol", "%1\u2009RPM");
150 // i18n: Use \u2009 (thin space) to separate value and unit
151 const static KLocalizedString C = ki18nc("Celsius unit symbol", "%1\u2009°C");
152 // i18n: Use \u2009 (thin space) to separate value and unit
153 const static KLocalizedString dBm = ki18nc("Decibels unit symbol", "%1\u2009dBm");
154 // i18n: Use \u200B (zero width space) to separate value and unit
155 const static KLocalizedString s = ki18nc("Seconds unit symbol", "%1\u200Bs");
156 // i18n: Use \u2009 (thin space) to separate value and unit
157 const static KLocalizedString rate = ki18nc("Rate unit symbol", "%1\u2009s⁻¹");
158 const static KLocalizedString unitless = ki18nc("Unitless", "%1");
159
160 switch (unit) {
161 case UnitByte:
162 return B;
163 case UnitKiloByte:
164 return KiB;
165 case UnitMegaByte:
166 return MiB;
167 case UnitGigaByte:
168 return GiB;
169 case UnitTeraByte:
170 return TiB;
171 case UnitPetaByte:
172 return PiB;
173
174 case UnitByteRate:
175 return bps;
176 case UnitKiloByteRate:
177 return Kbps;
178 case UnitMegaByteRate:
179 return Mbps;
180 case UnitGigaByteRate:
181 return Gbps;
182 case UnitTeraByteRate:
183 return Tbps;
184 case UnitPetaByteRate:
185 return Pbps;
186
187 case UnitBitRate:
188 return bitsps;
189 case UnitKiloBitRate:
190 return Kbitsps;
191 case UnitMegaBitRate:
192 return Mbitsps;
193 case UnitGigaBitRate:
194 return Gbitsps;
195 case UnitTeraBitRate:
196 return Tbitsps;
197 case UnitPetaBitRate:
198 return Pbitsps;
199
200 case UnitHertz:
201 return Hz;
202 case UnitKiloHertz:
203 return kHz;
204 case UnitMegaHertz:
205 return MHz;
206 case UnitGigaHertz:
207 return GHz;
208 case UnitTeraHertz:
209 return THz;
210 case UnitPetaHertz:
211 return PHz;
212
213 case UnitVolt:
214 return V;
215 case UnitKiloVolt:
216 return kV;
217 case UnitMegaVolt:
218 return MV;
219 case UnitGigaVolt:
220 return GV;
221 case UnitTeraVolt:
222 return TV;
223 case UnitPetaVolt:
224 return PV;
225
226 case UnitWatt:
227 return W;
228 case UnitKiloWatt:
229 return kW;
230 case UnitMegaWatt:
231 return MW;
232 case UnitGigaWatt:
233 return GW;
234 case UnitTeraWatt:
235 return TW;
236 case UnitPetaWatt:
237 return PV;
238
239 case UnitWattHour:
240 return Wh;
241 case UnitKiloWattHour:
242 return kWh;
243 case UnitMegaWattHour:
244 return MWh;
245 case UnitGigaWattHour:
246 return GWh;
247 case UnitTeraWattHour:
248 return TWh;
249 case UnitPetaWattHour:
250 return PWh;
251
252 case UnitAmpere:
253 return A;
254 case UnitKiloAmpere:
255 return kA;
256 case UnitMegaAmpere:
257 return MA;
258 case UnitGigaAmpere:
259 return GA;
260 case UnitTeraAmpere:
261 return TA;
262 case UnitPetaAmpere:
263 return PA;
264
265 case UnitCelsius:
266 return C;
267 case UnitDecibelMilliWatts:
268 return dBm;
269 case UnitPercent:
270 return percent;
271 case UnitRate:
272 return rate;
273 case UnitRpm:
274 return RPM;
275 case UnitSecond:
276 return s;
277
278 default:
279 return unitless;
280 }
281}
282
283static int unitOrder(Unit unit)
284{
285 switch (unit) {
286 case UnitByte:
287 case UnitKiloByte:
288 case UnitMegaByte:
289 case UnitGigaByte:
290 case UnitTeraByte:
291 case UnitPetaByte:
292 case UnitByteRate:
293 case UnitKiloByteRate:
294 case UnitMegaByteRate:
295 case UnitGigaByteRate:
296 case UnitTeraByteRate:
297 case UnitPetaByteRate:
298 case UnitBitRate:
299 case UnitKiloBitRate:
300 case UnitMegaBitRate:
301 case UnitGigaBitRate:
302 case UnitTeraBitRate:
303 case UnitPetaBitRate:
304 return 1024;
305
306 case UnitHertz:
307 case UnitKiloHertz:
308 case UnitMegaHertz:
309 case UnitGigaHertz:
310 case UnitTeraHertz:
311 case UnitPetaHertz:
312
313 case UnitWatt:
314 case UnitKiloWatt:
315 case UnitMegaWatt:
316 case UnitGigaWatt:
317 case UnitTeraWatt:
318 case UnitPetaWatt:
319
320 case UnitWattHour:
321 case UnitKiloWattHour:
322 case UnitMegaWattHour:
323 case UnitGigaWattHour:
324 case UnitTeraWattHour:
325 case UnitPetaWattHour:
326
327 case UnitAmpere:
328 case UnitKiloAmpere:
329 case UnitMegaAmpere:
330 case UnitGigaAmpere:
331 case UnitTeraAmpere:
332 case UnitPetaAmpere:
333
334 case UnitVolt:
335 case UnitKiloVolt:
336 case UnitMegaVolt:
337 case UnitGigaVolt:
338 case UnitTeraVolt:
339 case UnitPetaVolt:
340 return 1000;
341
342 default:
343 return 0;
344 }
345}
346
347static Unit unitBase(Unit unit)
348{
349 switch (unit) {
350 case UnitByte:
351 case UnitKiloByte:
352 case UnitMegaByte:
353 case UnitGigaByte:
354 case UnitTeraByte:
355 case UnitPetaByte:
356 return UnitByte;
357
358 case UnitByteRate:
359 case UnitKiloByteRate:
360 case UnitMegaByteRate:
361 case UnitGigaByteRate:
362 case UnitTeraByteRate:
363 case UnitPetaByteRate:
364 return UnitByteRate;
365
366 case UnitBitRate:
367 case UnitKiloBitRate:
368 case UnitMegaBitRate:
369 case UnitGigaBitRate:
370 case UnitTeraBitRate:
371 case UnitPetaBitRate:
372 return UnitBitRate;
373
374 case UnitHertz:
375 case UnitKiloHertz:
376 case UnitMegaHertz:
377 case UnitGigaHertz:
378 case UnitTeraHertz:
379 case UnitPetaHertz:
380 return UnitHertz;
381
382 case UnitVolt:
383 case UnitKiloVolt:
384 case UnitMegaVolt:
385 case UnitGigaVolt:
386 case UnitTeraVolt:
387 case UnitPetaVolt:
388 return UnitVolt;
389
390 case UnitWatt:
391 case UnitKiloWatt:
392 case UnitMegaWatt:
393 case UnitGigaWatt:
394 case UnitTeraWatt:
395 case UnitPetaWatt:
396 return UnitWatt;
397
398 case UnitWattHour:
399 case UnitKiloWattHour:
400 case UnitMegaWattHour:
401 case UnitGigaWattHour:
402 case UnitTeraWattHour:
403 case UnitPetaWattHour:
404 return UnitWattHour;
405
406 case UnitAmpere:
407 case UnitKiloAmpere:
408 case UnitMegaAmpere:
409 case UnitGigaAmpere:
410 case UnitTeraAmpere:
411 case UnitPetaAmpere:
412 return UnitAmpere;
413
414 default:
415 return unit;
416 }
417}
418
419static Unit adjustedUnit(qreal value, Unit unit, MetricPrefix prefix)
420{
421 const int order = unitOrder(unit);
422 if (!order) {
423 return unit;
424 }
425
426 const Unit baseUnit = unitBase(unit);
427 const MetricPrefix basePrefix = MetricPrefix(unit - baseUnit);
428
429 if (prefix == MetricPrefixAutoAdjust) {
430 const qreal absoluteValue = value * std::pow(order, int(basePrefix));
431 if (absoluteValue > 0) {
432 const int targetPrefix = std::log2(absoluteValue) / std::log2(order);
433 if (targetPrefix <= MetricPrefixLast) {
434 prefix = MetricPrefix(targetPrefix);
435 }
436 }
437 if (prefix == MetricPrefixAutoAdjust) {
438 prefix = basePrefix;
439 }
440 }
441
442 const Unit newUnit = Unit(int(prefix) + int(baseUnit));
443 // If there is no prefixed unit,
444 // don't overflow into the following unrelated units.
445 if (unitBase(newUnit) != baseUnit) {
446 return unit;
447 }
448
449 return newUnit;
450}
451
452static QString formatNumber(const QVariant &value, Unit unit, MetricPrefix prefix, FormatOptions options, int precision)
453{
454 qreal amount = value.toDouble();
455
456 if (!options.testFlag(FormatOptionShowNull) && (qFuzzyIsNull(amount) || qIsNaN(amount))) {
457 return QString();
458 }
459
460 const Unit adjusted = adjustedUnit(amount, unit, prefix);
461 if (adjusted != unit) {
462 amount /= std::pow(unitOrder(unit), adjusted - unit);
463 }
464
465 if (precision < 0) {
466 precision = (value.typeId() != QMetaType::Double && adjusted <= unit) ? 0 : 1;
467 }
468 const QString text = QLocale().toString(amount, 'f', precision);
469
470 return unitFormat(adjusted).subs(text).toString();
471}
472
473static QString formatTime(const QVariant &value)
474{
475 return KFormat().formatDuration(value.toLongLong() * 1000);
476}
477
478static QString formatTicks(const QVariant &value)
479{
480 auto seconds = value.toLongLong() / sysconf(_SC_CLK_TCK);
481 return KFormat().formatDuration(seconds * 1000);
482}
483
484static QString formatBootTimestamp(const QVariant &value)
485{
486 timespec tp;
487#ifdef Q_OS_LINUX
488 clock_gettime(CLOCK_BOOTTIME, &tp);
489#else
490 clock_gettime(CLOCK_MONOTONIC, &tp);
491#endif
492
493 const QDateTime systemBootTime = QDateTime::currentDateTime().addSecs(-tp.tv_sec);
494
495 const qreal secondsSinceSystemBoot = value.toReal() / sysconf(_SC_CLK_TCK);
496 const QDateTime absoluteTimeSinceBoot = systemBootTime.addSecs(secondsSinceSystemBoot);
497
498 return KFormat().formatRelativeDateTime(absoluteTimeSinceBoot, QLocale::ShortFormat);
499}
500
501qreal Formatter::scaleDownFactor(const QVariant &value, Unit unit, MetricPrefix targetPrefix)
502{
503 const Unit adjusted = adjustedUnit(value.toDouble(), unit, targetPrefix);
504 if (adjusted == unit) {
505 return 1;
506 }
507
508 return std::pow(unitOrder(unit), adjusted - unit);
509}
510
511KLocalizedString Formatter::localizedString(const QVariant &value, Unit unit, MetricPrefix targetPrefix)
512{
513 const Unit adjusted = adjustedUnit(value.toDouble(), unit, targetPrefix);
514 return unitFormat(adjusted);
515}
516
517QString Formatter::formatValue(const QVariant &value, Unit unit, MetricPrefix targetPrefix, FormatOptions options, int precision)
518{
519 switch (unit) {
520 case UnitByte:
521 case UnitKiloByte:
522 case UnitMegaByte:
523 case UnitGigaByte:
524 case UnitTeraByte:
525 case UnitPetaByte:
526 case UnitByteRate:
527 case UnitKiloByteRate:
528 case UnitMegaByteRate:
529 case UnitGigaByteRate:
530 case UnitTeraByteRate:
531 case UnitPetaByteRate:
532 case UnitBitRate:
533 case UnitKiloBitRate:
534 case UnitMegaBitRate:
535 case UnitGigaBitRate:
536 case UnitTeraBitRate:
537 case UnitPetaBitRate:
538 case UnitHertz:
539 case UnitKiloHertz:
540 case UnitMegaHertz:
541 case UnitGigaHertz:
542 case UnitTeraHertz:
543 case UnitPetaHertz:
544 case UnitVolt:
545 case UnitKiloVolt:
546 case UnitMegaVolt:
547 case UnitGigaVolt:
548 case UnitTeraVolt:
549 case UnitPetaVolt:
550 case UnitWatt:
551 case UnitKiloWatt:
552 case UnitMegaWatt:
553 case UnitGigaWatt:
554 case UnitTeraWatt:
555 case UnitPetaWatt:
556 case UnitWattHour:
557 case UnitKiloWattHour:
558 case UnitMegaWattHour:
559 case UnitGigaWattHour:
560 case UnitTeraWattHour:
561 case UnitPetaWattHour:
562 case UnitAmpere:
563 case UnitKiloAmpere:
564 case UnitMegaAmpere:
565 case UnitGigaAmpere:
566 case UnitTeraAmpere:
567 case UnitPetaAmpere:
568 case UnitSecond:
569 case UnitPercent:
570 case UnitRate:
571 case UnitRpm:
572 case UnitCelsius:
573 case UnitDecibelMilliWatts:
574 return formatNumber(value, unit, targetPrefix, options, precision);
575
576 case UnitBootTimestamp:
577 return formatBootTimestamp(value);
578 case UnitTime:
579 return formatTime(value);
580 case UnitNone:
581 return formatNumber(value, unit, MetricPrefix::MetricPrefixUnity, options, precision);
582 case UnitTicks:
583 return formatTicks(value);
584
585 default:
586 return value.toString();
587 }
588}
589
591{
592 // TODO: Is it possible to avoid duplication of these symbols?
593 switch (unit) {
594 case UnitByte:
595 return i18nc("Bytes unit symbol", "B");
596 case UnitKiloByte:
597 return i18nc("Kilobytes unit symbol", "KiB");
598 case UnitMegaByte:
599 return i18nc("Megabytes unit symbol", "MiB");
600 case UnitGigaByte:
601 return i18nc("Gigabytes unit symbol", "GiB");
602 case UnitTeraByte:
603 return i18nc("Terabytes unit symbol", "TiB");
604 case UnitPetaByte:
605 return i18nc("Petabytes unit symbol", "PiB");
606
607 case UnitByteRate:
608 return i18nc("Bytes per second unit symbol", "B/s");
609 case UnitKiloByteRate:
610 return i18nc("Kilobytes per second unit symbol", "KiB/s");
611 case UnitMegaByteRate:
612 return i18nc("Megabytes per second unit symbol", "MiB/s");
613 case UnitGigaByteRate:
614 return i18nc("Gigabytes per second unit symbol", "GiB/s");
615 case UnitTeraByteRate:
616 return i18nc("Terabytes per second unit symbol", "TiB/s");
617 case UnitPetaByteRate:
618 return i18nc("Petabytes per second unit symbol", "PiB/s");
619
620 case UnitBitRate:
621 return i18nc("Bits per second unit symbol", "bps");
622 case UnitKiloBitRate:
623 return i18nc("Kilobits per second unit symbol", "Kbps");
624 case UnitMegaBitRate:
625 return i18nc("Megabits per second unit symbol", "Mbps");
626 case UnitGigaBitRate:
627 return i18nc("Gigabits per second unit symbol", "Gbps");
628 case UnitTeraBitRate:
629 return i18nc("Terabits per second unit symbol", "Tbps");
630 case UnitPetaBitRate:
631 return i18nc("Petabits per second unit symbol", "Pbps");
632
633 case UnitHertz:
634 return i18nc("Hertz unit symbol", "Hz");
635 case UnitKiloHertz:
636 return i18nc("Kilohertz unit symbol", "kHz");
637 case UnitMegaHertz:
638 return i18nc("Megahertz unit symbol", "MHz");
639 case UnitGigaHertz:
640 return i18nc("Gigahertz unit symbol", "GHz");
641 case UnitTeraHertz:
642 return i18nc("Terahertz unit symbol", "THz");
643 case UnitPetaHertz:
644 return i18nc("Petahertz unit symbol", "PHz");
645
646 case UnitVolt:
647 return i18nc("Volts unit symbol", "V");
648 case UnitKiloVolt:
649 return i18nc("Kilovolts unit symbol", "kV");
650 case UnitMegaVolt:
651 return i18nc("Megavolts unit symbol", "MV");
652 case UnitGigaVolt:
653 return i18nc("Gigavolts unit symbol", "GV");
654 case UnitTeraVolt:
655 return i18nc("Teravolts unit symbol", "TV");
656 case UnitPetaVolt:
657 return i18nc("Petavolts unit symbol", "PV");
658
659 case UnitWatt:
660 return i18nc("Watts unit symbol", "W");
661 case UnitKiloWatt:
662 return i18nc("Kilowatts unit symbol", "kW");
663 case UnitMegaWatt:
664 return i18nc("Megawatts unit symbol", "MW");
665 case UnitGigaWatt:
666 return i18nc("Gigawatts unit symbol", "GW");
667 case UnitTeraWatt:
668 return i18nc("Terawatts unit symbol", "TW");
669 case UnitPetaWatt:
670 return i18nc("Petawatts unit symbol", "PW");
671
672 case UnitWattHour:
673 return i18nc("Watt-hours unit symbol", "Wh");
674 case UnitKiloWattHour:
675 return i18nc("Kilo-watthours unit symbol", "kWh");
676 case UnitMegaWattHour:
677 return i18nc("Mega-watthours unit symbol", "MWh");
678 case UnitGigaWattHour:
679 return i18nc("Giga-watthours unit symbol", "GWh");
680 case UnitTeraWattHour:
681 return i18nc("Tera-watthours unit symbol", "TWh");
682 case UnitPetaWattHour:
683 return i18nc("Peta-watthours unit symbol", "PWh");
684
685 case UnitAmpere:
686 return i18nc("Ampere unit symbol", "A");
687 case UnitKiloAmpere:
688 return i18nc("Kiloamperes unit symbol", "kA");
689 case UnitMegaAmpere:
690 return i18nc("Megaamperes unit symbol", "MA");
691 case UnitGigaAmpere:
692 return i18nc("Gigaamperes unit symbol", "GA");
693 case UnitTeraAmpere:
694 return i18nc("Teraamperes unit symbol", "TA");
695 case UnitPetaAmpere:
696 return i18nc("Petaamperes unit symbol", "PA");
697
698 case UnitPercent:
699 return i18nc("Percent unit", "%");
700 case UnitRpm:
701 return i18nc("Revolutions per minute unit symbol", "RPM");
702 case UnitCelsius:
703 return i18nc("Celsius unit symbol", "°C");
704 case UnitDecibelMilliWatts:
705 return i18nc("Decibels unit symbol", "dBm");
706 case UnitSecond:
707 return i18nc("Seconds unit symbol", "s");
708
709 case UnitRate:
710 return i18nc("Rate unit symbol", "s⁻¹");
711
712 default:
713 return QString();
714 }
715}
716
717qreal Formatter::maximumLength(Unit unit, const QFont &font)
718{
719 auto order = unitOrder(unit);
720
721 QString maximum;
722 switch (unitBase(unit)) {
723 case UnitByte:
724 maximum = formatValue(order - 0.5, UnitMegaByte, MetricPrefixMega);
725 break;
726 case UnitByteRate:
727 maximum = formatValue(order - 0.5, UnitMegaByteRate, MetricPrefixMega);
728 break;
729 case UnitBitRate:
730 maximum = formatValue(order - 0.5, UnitMegaBitRate, MetricPrefixMega);
731 break;
732 case UnitHertz:
733 maximum = formatValue(order - 0.5, UnitMegaHertz, MetricPrefixMega);
734 break;
735 case UnitPercent:
736 maximum = formatValue(999.9, UnitPercent);
737 break;
738 default:
739 return -1.0;
740 }
741
742 auto metrics = QFontMetrics{font};
743 return metrics.horizontalAdvance(maximum);
744}
745
746} // namespace KSysGuard
QString formatRelativeDateTime(const QDateTime &dateTime, QLocale::FormatType format) const
QString formatDuration(quint64 msecs, KFormat::DurationFormatOptions options=KFormat::DefaultDuration) const
QString toString() const
KLocalizedString subs(const KLocalizedString &a, int fieldWidth=0, QChar fillChar=QLatin1Char(' ')) const
static QString formatValue(const QVariant &value, Unit unit, MetricPrefix targetPrefix=MetricPrefixAutoAdjust, FormatOptions options=FormatOptionNone, int precision=-1)
Converts value to the appropriate displayable string.
static KLocalizedString localizedString(const QVariant &value, Unit unit, MetricPrefix targetPrefix=MetricPrefixAutoAdjust)
Returns localized string that is suitable for display.
static qreal maximumLength(Unit unit, const QFont &font)
Return the maximum length of a formatted string for the specified unit and font.
static QString symbol(Unit unit)
Returns a symbol that corresponds to the given unit.
static qreal scaleDownFactor(const QVariant &value, Unit unit, MetricPrefix targetPrefix=MetricPrefixAutoAdjust)
Returns the scale factor suitable for display.
QString i18nc(const char *context, const char *text, const TYPE &arg...)
KLocalizedString KI18N_EXPORT ki18nc(const char *context, const char *text)
QDateTime addSecs(qint64 s) const const
QDateTime currentDateTime()
int horizontalAdvance(QChar ch) const const
QString toString(QDate date, FormatType format) const const
double toDouble(bool *ok) const const
qlonglong toLongLong(bool *ok) const const
qreal toReal(bool *ok) const const
QString toString() const const
int typeId() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:02:48 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.