File indexing completed on 2024-05-05 05:54:31

0001 /*
0002     SPDX-FileCopyrightText: 2014 Elvis Angelaccio <elvis.angelaccio@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #ifndef TIMEFORMAT_H
0008 #define TIMEFORMAT_H
0009 
0010 #include <QString>
0011 
0012 class QTime;
0013 
0014 /**
0015  * @brief A wrapper for a QTime-like format time string.
0016  * A TimeFormat is an abstraction for a QTime-like string used for time formats.
0017  * A TimeFormat can be customized using booleans in the constructor.
0018  * The QTime-syntax used is the following:
0019  * "hh:" whether to show hours (00 to 24)
0020  * "mm:" whether to show minutes (00 to 59)
0021  * "ss." whether to show seconds (00 to 59). Seconds are always showed.
0022  * "zzz" whether to show second fractions (tenths or hundredths or milliseconds)
0023  * An example of time formatted with the complete syntax might be the following: 0:05:38.582
0024  */
0025 class TimeFormat
0026 {
0027 
0028 public:
0029 
0030     enum SecondFraction
0031     {
0032         UpToTenths,          /**< Second fraction is tenths of second. */
0033         UpToHundredths,      /**< Second fraction is hundrdths of second. */
0034         UpToMilliseconds,    /**< Second fraction is milliseconds. */
0035         NoFractions          /**< Second fraction disabled. */
0036     };
0037 
0038     explicit TimeFormat(bool showHours = false, bool showMinutes = true, SecondFraction fractions = UpToHundredths);
0039 
0040     /**
0041      * Format the given time with the current time format.
0042      * @param time The time to be formatted.
0043      * @return The time formatted as string.
0044      */
0045     QString format(const QTime& time) const;
0046 
0047     /**
0048      * Format the given time's hours with the current time format.
0049      * @param time The time to be formatted.
0050      * @return The time's hours formatted as string, or empty string if hour is not in the format.
0051      */
0052     QString formatHours(const QTime& time) const;
0053 
0054     /**
0055      * Format the given time's minutes with the current time format.
0056      * @param time The time to be formatted.
0057      * @return The time's minutes formatted as string, or empty string if minute is not in the format.
0058      */
0059     QString formatMinutes(const QTime& time) const;
0060 
0061     /**
0062      * Format the given time's seconds with the current time format.
0063      * @param time The time to be formatted.
0064      * @return The time's seconds formatted as string, or empty string if second is not in the format.
0065      */
0066     QString formatSeconds(const QTime& time) const;
0067 
0068     /**
0069      * Format the given time's second fractions with the current time format.
0070      * @param time The time to be formatted.
0071      * @return The time's second fractions formatted as string, or empty string if second fraction is not in the format.
0072      */
0073     QString formatFractions(const QTime& time) const;
0074 
0075     /**
0076      * Enable the hours in the time format.
0077      */
0078     void overrideHours();
0079 
0080     /**
0081      * Enable minutes in the time format.
0082      */
0083     void overrideMinutes();
0084 
0085     /**
0086      * Whether the hour is in the time format.
0087      * @return true if hour is in the format, false otherwise.
0088      */
0089     bool hasHours() const;
0090 
0091     /**
0092      * Whether the minute is in the time format.
0093      * @return true if minute is in the format, false otherwise.
0094      */
0095     bool hasMinutes() const;
0096 
0097     /**
0098      * Whether the second fraction is in the time format.
0099      * @return true if second fraction is in the format, false otherwise.
0100      */
0101     bool hasFractions() const;
0102 
0103     /**
0104      * @return The current FractionType in the time format.
0105      */
0106     SecondFraction secondFractions() const;
0107 
0108     /**
0109      * Whether to show the symbols ':' and '.' used as dividers in the time format.
0110      * @param show true Whether to show the dividers.
0111      */
0112     void showDividers(bool show);
0113 
0114     bool operator==(const TimeFormat& right) const;
0115     bool operator!=(const TimeFormat& right) const;
0116 
0117 private:
0118 
0119     bool m_showHours;             /** Whether hour is in the internal time format */
0120     bool m_showMinutes;           /** Whether minute is in the internal time format */
0121     bool m_showDividers = true;   /** Whether to show the symbols used as dividers */
0122 
0123     QString m_hourFormat;         /** Hour string format */
0124     QString m_minFormat;          /** Minute string format */
0125     QString m_secFormat;          /** Secondstring format */
0126     SecondFraction m_fractions;   /** Second fraction internal time format */
0127 
0128     /**
0129      * Setup the format strings based on the internal formats
0130      */
0131     void setupFormat();
0132 };
0133 
0134 #endif