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 "lap.h"
0021 
0022 #include <QJsonObject>
0023 
0024 Lap::Lap(const QTime& lap) :
0025     m_time {lap}
0026 {}
0027 
0028 QTime Lap::time() const
0029 {
0030     return m_time;
0031 }
0032 
0033 QTime Lap::timeTo(const Lap& lap) const
0034 {
0035     if (lap.time() < m_time)
0036         return {0, 0};
0037 
0038     const auto zero = QTime {0, 0};
0039     return zero.addMSecs(m_time.msecsTo(lap.time()));
0040 }
0041 
0042 void Lap::setRelativeTime(const QString& rel)
0043 {
0044     m_relativeTime = rel;
0045 }
0046 
0047 QString Lap::relativeTime() const
0048 {
0049     return m_relativeTime;
0050 }
0051 
0052 void Lap::setAbsoluteTime(const QString& abs)
0053 {
0054     m_absoluteTime = abs;
0055 }
0056 
0057 QString Lap::absoluteTime() const
0058 {
0059     return m_absoluteTime;
0060 }
0061 
0062 void Lap::setNote(const QString& note)
0063 {
0064     m_note = note;
0065 }
0066 
0067 QString Lap::note() const
0068 {
0069     return m_note;
0070 }
0071 
0072 int Lap::raw() const
0073 {
0074     const auto zero = QTime {0, 0};
0075     return zero.msecsTo(m_time);
0076 }
0077 
0078 void Lap::write(QJsonObject& json) const
0079 {
0080     json[QStringLiteral("time")] = raw();
0081     json[QStringLiteral("reltime")] = m_relativeTime;
0082     json[QStringLiteral("abstime")] = m_absoluteTime;
0083     json[QStringLiteral("note")] = m_note;
0084 }
0085 
0086 Lap Lap::fromJson(const QJsonObject& json)
0087 {
0088     auto lap = fromRawData(json[QStringLiteral("time")].toInt());
0089     lap.m_relativeTime = json[QStringLiteral("reltime")].toString();
0090     lap.m_absoluteTime = json[QStringLiteral("abstime")].toString();
0091     lap.m_note = json[QStringLiteral("note")].toString();
0092 
0093     return lap;
0094 }
0095 
0096 Lap Lap::fromRawData(int rawData)
0097 {
0098     if (rawData < 0) {
0099         return Lap {};
0100     }
0101 
0102     const auto zero = QTime {0, 0};
0103     return Lap {zero.addMSecs(rawData)};
0104 }