File indexing completed on 2024-05-19 16:51:55
0001 /* 0002 Copyright (C) 2014 by Elvis Angelaccio <elvis.angelaccio@kde.org> 0003 0004 This file is part of Kronometer. 0005 0006 Kronometer is free software: you can redistribute it and/or modify 0007 it under the terms of the GNU General Public License as published by 0008 the Free Software Foundation, either version 2 of the License, or 0009 (at your option) any later version. 0010 0011 Kronometer is distributed in the hope that it will be useful, 0012 but WITHOUT ANY WARRANTY; without even the implied warranty of 0013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0014 GNU General Public License for more details. 0015 0016 You should have received a copy of the GNU General Public License 0017 along with Kronometer. If not, see <http://www.gnu.org/licenses/>. 0018 */ 0019 0020 #ifndef TIMEFORMAT_H 0021 #define TIMEFORMAT_H 0022 0023 #include <QString> 0024 0025 class QTime; 0026 0027 /** 0028 * @brief A wrapper for a QTime-like format time string. 0029 * A TimeFormat is an abstraction for a QTime-like string used for time formats. 0030 * A TimeFormat can be customized using booleans in the constructor. 0031 * The QTime-syntax used is the following: 0032 * "hh:" whether to show hours (00 to 24) 0033 * "mm:" whether to show minutes (00 to 59) 0034 * "ss." whether to show seconds (00 to 59). Seconds are always showed. 0035 * "zzz" whether to show second fractions (tenths or hundredths or milliseconds) 0036 * An example of time formatted with the complete syntax might be the following: 0:05:38.582 0037 */ 0038 class TimeFormat 0039 { 0040 0041 public: 0042 0043 enum SecondFraction 0044 { 0045 UpToTenths, /**< Second fraction is tenths of second. */ 0046 UpToHundredths, /**< Second fraction is hundrdths of second. */ 0047 UpToMilliseconds, /**< Second fraction is milliseconds. */ 0048 NoFractions /**< Second fraction disabled. */ 0049 }; 0050 0051 explicit TimeFormat(bool showHours = false, bool showMinutes = true, SecondFraction fractions = UpToHundredths); 0052 0053 /** 0054 * Format the given time with the current time format. 0055 * @param time The time to be formatted. 0056 * @return The time formatted as string. 0057 */ 0058 QString format(const QTime& time) const; 0059 0060 /** 0061 * Format the given time's hours with the current time format. 0062 * @param time The time to be formatted. 0063 * @return The time's hours formatted as string, or empty string if hour is not in the format. 0064 */ 0065 QString formatHours(const QTime& time) const; 0066 0067 /** 0068 * Format the given time's minutes with the current time format. 0069 * @param time The time to be formatted. 0070 * @return The time's minutes formatted as string, or empty string if minute is not in the format. 0071 */ 0072 QString formatMinutes(const QTime& time) const; 0073 0074 /** 0075 * Format the given time's seconds with the current time format. 0076 * @param time The time to be formatted. 0077 * @return The time's seconds formatted as string, or empty string if second is not in the format. 0078 */ 0079 QString formatSeconds(const QTime& time) const; 0080 0081 /** 0082 * Format the given time's second fractions with the current time format. 0083 * @param time The time to be formatted. 0084 * @return The time's second fractions formatted as string, or empty string if second fraction is not in the format. 0085 */ 0086 QString formatFractions(const QTime& time) const; 0087 0088 /** 0089 * Enable the hours in the time format. 0090 */ 0091 void overrideHours(); 0092 0093 /** 0094 * Enable minutes in the time format. 0095 */ 0096 void overrideMinutes(); 0097 0098 /** 0099 * Whether the hour is in the time format. 0100 * @return true if hour is in the format, false otherwise. 0101 */ 0102 bool hasHours() const; 0103 0104 /** 0105 * Whether the minute is in the time format. 0106 * @return true if minute is in the format, false otherwise. 0107 */ 0108 bool hasMinutes() const; 0109 0110 /** 0111 * Whether the second fraction is in the time format. 0112 * @return true if second fraction is in the format, false otherwise. 0113 */ 0114 bool hasFractions() const; 0115 0116 /** 0117 * @return The current FractionType in the time format. 0118 */ 0119 SecondFraction secondFractions() const; 0120 0121 /** 0122 * Whether to show the symbols ':' and '.' used as dividers in the time format. 0123 * @param show true Whether to show the dividers. 0124 */ 0125 void showDividers(bool show); 0126 0127 bool operator==(const TimeFormat& right) const; 0128 bool operator!=(const TimeFormat& right) const; 0129 0130 private: 0131 0132 bool m_showHours; /** Whether hour is in the internal time format */ 0133 bool m_showMinutes; /** Whether minute is in the internal time format */ 0134 bool m_showDividers = true; /** Whether to show the symbols used as dividers */ 0135 0136 QString m_hourFormat; /** Hour string format */ 0137 QString m_minFormat; /** Minute string format */ 0138 QString m_secFormat; /** Secondstring format */ 0139 SecondFraction m_fractions; /** Second fraction internal time format */ 0140 0141 /** 0142 * Setup the format strings based on the internal formats 0143 */ 0144 void setupFormat(); 0145 }; 0146 0147 #endif