KCoreAddons

formats.h
1/*
2 SPDX-FileCopyrightText: 2014 Bhushan Shah <bhush94@gmail.com>
3
4 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5*/
6
7#ifndef FORMATS_H
8#define FORMATS_H
9
10#include <KFormat>
11#include <QObject>
12#include <QQmlEngine>
13
14namespace FormatTypes
15{
16Q_NAMESPACE
17QML_NAMED_ELEMENT(FormatTypes)
18QML_FOREIGN_NAMESPACE(KFormat)
19}
20
21class Formats : public QObject
22{
24 QML_NAMED_ELEMENT(Format)
25 QML_SINGLETON
26
27public:
28 /**
29 * Converts size from bytes to the appropriate string representation
30 */
31 Q_INVOKABLE QString formatByteSize(double size, int precision = 1) const;
32
33 /**
34 * Given a number of milliseconds, converts that to a string containing
35 * the localized equivalent, e.g. 1:23:45
36 */
37 Q_INVOKABLE QString formatDuration(quint64 msecs, KFormat::DurationFormatOptions options = KFormat::DefaultDuration) const;
38
39 /**
40 * Given a number of milliseconds, converts that to a string containing
41 * the localized equivalent to the requested decimal places.
42 *
43 * e.g. given formatDuration(60000), returns "1.0 minutes"
44 */
45 Q_INVOKABLE QString formatDecimalDuration(quint64 msecs, int decimalPlaces = 2) const;
46
47 /**
48 * Given a number of milliseconds, converts that to a spell-out string containing
49 * the localized equivalent.
50 *
51 * e.g. given formatSpelloutDuration(60001) returns "1 minute"
52 * given formatSpelloutDuration(62005) returns "1 minute and 2 seconds"
53 * given formatSpelloutDuration(90060000) returns "1 day and 1 hour"
54 *
55 * Units not interesting to the user, for example seconds or minutes when the first
56 * unit is day, are not returned because they are irrelevant. The same applies for
57 * seconds when the first unit is hour.
58 *
59 */
60 Q_INVOKABLE QString formatSpelloutDuration(quint64 msecs) const;
61
62 /**
63 * Returns a string formatted to a relative date style.
64 *
65 * If the date falls within one week before or after the current date
66 * then a relative date string will be returned, such as:
67 * * Yesterday
68 * * Today
69 * * Tomorrow
70 * * Last Tuesday
71 * * Next Wednesday
72 *
73 * If the date falls outside this period then the format is used
74 */
75 Q_INVOKABLE QString formatRelativeDate(const QDate &date, QLocale::FormatType format) const;
76
77 /**
78 * Returns a string formatted to a relative datetime style.
79 *
80 * If the dateTime falls within one week before or after the current date
81 * then a relative date string will be returned, such as:
82 * * Yesterday, 3:00pm
83 * * Today, 3:00pm
84 * * Tomorrow, 3:00pm
85 * * Last Tuesday, 3:00pm
86 * * Next Wednesday, 3:00pm
87 *
88 * If the datetime falls outside this period then the format is used
89 */
90 Q_INVOKABLE QString formatRelativeDateTime(const QDateTime &dateTime, QLocale::FormatType format) const;
91
92 /**
93 * Formats a distance value given in meters in appropriate units for
94 * displaying.
95 *
96 * Unless explicitly forced to metric units this uses units approrpiate for
97 * the current locale.
98 */
99 Q_INVOKABLE [[nodiscard]] QString formatDistance(double distance, KFormat::DistanceFormatOptions options = KFormat::LocaleDistanceUnits) const;
100
101private:
102 const KFormat m_format;
103};
104
105#endif
@ LocaleDistanceUnits
Automatically select metric or imperial units based on the current locale.
Definition kformat.h:204
@ DefaultDuration
Default formatting in localized 1:23:45 format.
Definition kformat.h:184
QFlags< DurationFormatOption > DurationFormatOptions
Stores a combination of DurationFormatOption values.
Definition kformat.h:195
Q_INVOKABLEQ_INVOKABLE
Q_OBJECTQ_OBJECT
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Feb 28 2025 12:00:49 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.