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