KCoreAddons

kformat.h
Go to the documentation of this file.
1 /*
2  This file is part of the KDE Frameworks
3 
4  SPDX-FileCopyrightText: 2013 Alex Merry <[email protected]>
5  SPDX-FileCopyrightText: 2013 John Layt <[email protected]>
6  SPDX-FileCopyrightText: 2010 Michael Leupold <[email protected]>
7  SPDX-FileCopyrightText: 2009 Michael Pyne <[email protected]>
8  SPDX-FileCopyrightText: 2008 Albert Astals Cid <[email protected]>
9 
10  SPDX-License-Identifier: LGPL-2.0-or-later
11 */
12 
13 #ifndef KFORMAT_H
14 #define KFORMAT_H
15 
16 #include <kcoreaddons_export.h>
17 
18 #include <QString>
19 #include <QLocale>
20 #include <QSharedPointer>
21 
22 class QDate;
23 class QDateTime;
24 
25 class KFormatPrivate;
26 
31 /*
32  The code in this class was copied from the old KLocale and modified
33  by John Layt (and also Alex Merry) in the KDELIBS 4 to KDE
34  Frameworks 5 transition in 2013.
35 
36  Albert Astals Cid is the original author of formatSpelloutDuration()
37  originally named KLocale::prettyFormatDuration().
38 
39  Michael Pyne is the original author of formatByteSize().
40 
41  Michael Leupold is the original author of formatRelativeDate(()
42  originally part of KFormat::formatDate().
43 */
44 
58 class KCOREADDONS_EXPORT KFormat Q_DECL_FINAL
59 {
60  Q_GADGET
61 
62 public:
86  DefaultBinaryUnits = -1,
87 
88  // The first real unit must be 0 for the current implementation!
98  UnitLastUnit = UnitYottaByte
99  };
100 
107  enum class Unit {
108  Other,
109  Bit,
110  Byte,
111  Meter,
112  Hertz,
113  };
114 
126  enum class UnitPrefix {
128  AutoAdjust = -128,
129 
130  Yocto = 0,
131  Zepto,
132  Atto,
133  Femto,
134  Pico,
135  Nano,
136  Micro,
137  Milli,
138  Centi,
139  Deci,
140  Unity,
141  Deca,
142  Hecto,
143  Kilo,
144  Mega,
145  Giga,
146  Tera,
147  Peta,
148  Exa,
149  Zetta,
150  Yotta,
151  };
152 
172  DefaultBinaryDialect = -1,
176  LastBinaryDialect = MetricBinaryDialect
177  };
178 
184  DefaultDuration = 0x0,
185  InitialDuration = 0x1,
186  ShowMilliseconds = 0x2,
187  HideSeconds = 0x4,
188  FoldHours = 0x8
189  };
194  Q_FLAG(DurationFormatOption)
195 
201  explicit KFormat(const QLocale &locale = QLocale());
202 
206  KFormat(const KFormat &other);
207 
208  KFormat& operator=(const KFormat &other);
209 
213  ~KFormat();
214 
246  QString formatByteSize(double size,
247  int precision = 1,
248  KFormat::BinaryUnitDialect dialect = KFormat::DefaultBinaryDialect,
249  KFormat::BinarySizeUnits units = KFormat::DefaultBinaryUnits) const;
250 
260  QString formatDuration(quint64 msecs,
261  KFormat::DurationFormatOptions options = KFormat::DefaultDuration) const;
262 
274  QString formatDecimalDuration(quint64 msecs,
275  int decimalPlaces = 2) const;
276 
291  QString formatSpelloutDuration(quint64 msecs) const;
292 
311  QString formatRelativeDate(const QDate &date,
312  QLocale::FormatType format) const;
313 
332  QString formatRelativeDateTime(const QDateTime &dateTime,
333  QLocale::FormatType format) const;
334 
361  QString formatValue(double value,
362  KFormat::Unit unit,
363  int precision = 1,
364  KFormat::UnitPrefix prefix = KFormat::UnitPrefix::AutoAdjust,
365  KFormat::BinaryUnitDialect dialect = KFormat::DefaultBinaryDialect) const;
366 
390  QString formatValue(double value,
391  const QString& unit,
392  int precision = 1,
393  KFormat::UnitPrefix prefix = KFormat::UnitPrefix::AutoAdjust) const;
420  QString formatValue(double value,
421  const QString& unit,
422  int precision,
423  KFormat::UnitPrefix prefix,
424  KFormat::BinaryUnitDialect dialect) const;
425 
426 
427 private:
428  QSharedDataPointer<KFormatPrivate> d;
429 };
430 
431 #endif // KFORMAT_H
PiB/PB/PB 2^50/10^15 bytes.
Definition: kformat.h:94
KiB, MiB, etc. 2^(10*n)
Definition: kformat.h:173
GiB/GB/GB 2^30/10^09 bytes.
Definition: kformat.h:92
UnitPrefix
These prefixes are used in KDE by the formatValue() function.
Definition: kformat.h:126
BinarySizeUnits
These binary units are used in KDE by the formatByteSize() function.
Definition: kformat.h:84
KiB/KB/kB 1024/1000 bytes.
Definition: kformat.h:90
DurationFormatOption
Format flags for formatDuration()
Definition: kformat.h:183
MiB/MB/MB 2^20/10^06 bytes.
Definition: kformat.h:91
KB, MB, etc. 2^(10*n)
Definition: kformat.h:174
YiB/YB/YB 2^80/10^24 bytes.
Definition: kformat.h:97
EiB/EB/EB 2^60/10^18 bytes.
Definition: kformat.h:95
ZiB/ZB/ZB 2^70/10^21 bytes.
Definition: kformat.h:96
Unit
These units are used in KDE by the formatValue() function.
Definition: kformat.h:107
BinaryUnitDialect
This enum chooses what dialect is used for binary units.
Definition: kformat.h:171
TiB/TB/TB 2^40/10^12 bytes.
Definition: kformat.h:93
KFormat provides support for formatting numbers and datetimes in formats that are not supported by QL...
Definition: kformat.h:58
SI Units, kB, MB, etc. 10^(3*n)
Definition: kformat.h:175
B 1 byte.
Definition: kformat.h:89
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Fri Sep 25 2020 23:03:07 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.