File indexing completed on 2024-05-12 03:55:02

0001 /*
0002     SPDX-FileCopyrightText: 2014 Bhushan Shah <bhush94@gmail.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #ifndef FORMATS_H
0008 #define FORMATS_H
0009 
0010 #include <KFormat>
0011 #include <QObject>
0012 
0013 class Formats : public QObject
0014 {
0015     Q_OBJECT
0016 
0017 public:
0018     /**
0019      * Converts size from bytes to the appropriate string representation
0020      */
0021     Q_INVOKABLE QString formatByteSize(double size, int precision = 1) const;
0022 
0023     /**
0024      * Given a number of milliseconds, converts that to a string containing
0025      * the localized equivalent, e.g. 1:23:45
0026      */
0027     Q_INVOKABLE QString formatDuration(quint64 msecs, KFormat::DurationFormatOptions options = KFormat::DefaultDuration) const;
0028 
0029     Q_DECLARE_FLAGS(DurationFormatOptions, KFormat::DurationFormatOption)
0030 
0031     /**
0032      * This overload exists so it can be called from QML, which does
0033      * not support calling Q_INVOKABLEs with Q_ENUMS from different classes
0034      *
0035      * This is mentioned in the docs and also in https://bugreports.qt.io/browse/QTBUG-20639
0036      * Until that bug is fixed, we'll need this
0037      */
0038     Q_INVOKABLE QString formatDuration(quint64 msecs, int options) const;
0039 
0040     /**
0041      * Given a number of milliseconds, converts that to a string containing
0042      * the localized equivalent to the requested decimal places.
0043      *
0044      * e.g. given formatDuration(60000), returns "1.0 minutes"
0045      */
0046     Q_INVOKABLE QString formatDecimalDuration(quint64 msecs, int decimalPlaces = 2) const;
0047 
0048     /**
0049      * Given a number of milliseconds, converts that to a spell-out string containing
0050      * the localized equivalent.
0051      *
0052      * e.g. given formatSpelloutDuration(60001) returns "1 minute"
0053      *      given formatSpelloutDuration(62005) returns "1 minute and 2 seconds"
0054      *      given formatSpelloutDuration(90060000) returns "1 day and 1 hour"
0055      *
0056      * Units not interesting to the user, for example seconds or minutes when the first
0057      * unit is day, are not returned because they are irrelevant. The same applies for
0058      * seconds when the first unit is hour.
0059      *
0060      */
0061     Q_INVOKABLE QString formatSpelloutDuration(quint64 msecs) const;
0062 
0063     /**
0064      * Returns a string formatted to a relative date style.
0065      *
0066      * If the date falls within one week before or after the current date
0067      * then a relative date string will be returned, such as:
0068      * * Yesterday
0069      * * Today
0070      * * Tomorrow
0071      * * Last Tuesday
0072      * * Next Wednesday
0073      *
0074      * If the date falls outside this period then the format is used
0075      */
0076     Q_INVOKABLE QString formatRelativeDate(const QDate &date, QLocale::FormatType format) const;
0077 
0078     /**
0079      * Returns a string formatted to a relative datetime style.
0080      *
0081      * If the dateTime falls within one week before or after the current date
0082      * then a relative date string will be returned, such as:
0083      * * Yesterday, 3:00pm
0084      * * Today, 3:00pm
0085      * * Tomorrow, 3:00pm
0086      * * Last Tuesday, 3:00pm
0087      * * Next Wednesday, 3:00pm
0088      *
0089      * If the datetime falls outside this period then the format is used
0090      */
0091     Q_INVOKABLE QString formatRelativeDateTime(const QDateTime &dateTime, QLocale::FormatType format) const;
0092 
0093 private:
0094     const KFormat m_format;
0095 };
0096 
0097 #endif