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 #include "timeformat.h" 0008 0009 #include <QTime> 0010 0011 TimeFormat::TimeFormat(bool showHours, bool showMinutes, SecondFraction fractions) : 0012 m_showHours {showHours}, 0013 m_showMinutes {showMinutes}, 0014 m_fractions {fractions} 0015 { 0016 setupFormat(); 0017 } 0018 0019 QString TimeFormat::format(const QTime& time) const 0020 { 0021 const auto h = formatHours(time); 0022 const auto m = formatMinutes(time); 0023 const auto s = formatSeconds(time); 0024 const auto f = formatFractions(time); 0025 0026 return h + m + s + f; 0027 } 0028 0029 QString TimeFormat::formatHours(const QTime& time) const 0030 { 0031 if (!m_showHours) { 0032 return {}; 0033 } 0034 0035 return time.toString(m_hourFormat); 0036 } 0037 0038 QString TimeFormat::formatMinutes(const QTime& time) const 0039 { 0040 if (!m_showMinutes) { 0041 return {}; 0042 } 0043 0044 return time.toString(m_minFormat); 0045 } 0046 0047 QString TimeFormat::formatSeconds(const QTime& time) const 0048 { 0049 return time.toString(m_secFormat); 0050 } 0051 0052 QString TimeFormat::formatFractions(const QTime& time) const 0053 { 0054 auto fracFormat = QStringLiteral("zzz"); 0055 auto temp = QString {}; 0056 0057 switch (m_fractions) { 0058 case UpToTenths: 0059 temp = time.toString(fracFormat); 0060 return temp.left(temp.size() - 2); 0061 case UpToHundredths: 0062 temp = time.toString(fracFormat); 0063 return temp.left(temp.size() - 1); 0064 case UpToMilliseconds: 0065 return time.toString(fracFormat); 0066 default: 0067 return QString(); 0068 } 0069 } 0070 0071 void TimeFormat::overrideHours() 0072 { 0073 if (m_showHours) 0074 return; 0075 0076 m_showHours = true; 0077 setupFormat(); 0078 } 0079 0080 void TimeFormat::overrideMinutes() 0081 { 0082 if (m_showMinutes) 0083 return; 0084 0085 m_showMinutes = true; 0086 setupFormat(); 0087 } 0088 0089 bool TimeFormat::hasHours() const 0090 { 0091 return m_showHours; 0092 } 0093 0094 bool TimeFormat::hasMinutes() const 0095 { 0096 return m_showMinutes; 0097 } 0098 0099 bool TimeFormat::hasFractions() const 0100 { 0101 return m_fractions != NoFractions; 0102 } 0103 0104 TimeFormat::SecondFraction TimeFormat::secondFractions() const 0105 { 0106 return m_fractions; 0107 } 0108 0109 void TimeFormat::showDividers(bool show) 0110 { 0111 m_showDividers = show; 0112 setupFormat(); 0113 } 0114 0115 bool TimeFormat::operator==(const TimeFormat& right) const 0116 { 0117 return m_showHours == right.m_showHours && 0118 m_showMinutes == right.m_showMinutes && 0119 m_fractions == right.m_fractions && 0120 m_showDividers == right.m_showDividers; 0121 } 0122 0123 bool TimeFormat::operator!=(const TimeFormat& right) const 0124 { 0125 return !(*this == right); 0126 } 0127 0128 void TimeFormat::setupFormat() 0129 { 0130 if (m_showHours) { 0131 if (m_showDividers) { 0132 m_hourFormat = QStringLiteral("hh:"); 0133 } 0134 else { 0135 m_hourFormat = QStringLiteral("hh"); 0136 } 0137 } 0138 0139 if (m_showMinutes) { 0140 if (m_showDividers) { 0141 m_minFormat = QStringLiteral("mm:"); 0142 } 0143 else { 0144 m_minFormat = QStringLiteral("mm"); 0145 } 0146 } 0147 0148 if (m_showDividers && m_fractions != NoFractions) { 0149 m_secFormat = QStringLiteral("ss."); 0150 } 0151 else { 0152 m_secFormat = QStringLiteral("ss"); 0153 } 0154 }