File indexing completed on 2024-05-19 16:51:55
0001 /* 0002 Copyright (C) 2015 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 "session.h" 0021 0022 #include <QJsonArray> 0023 #include <QJsonObject> 0024 0025 Session::Session(int time, const QDateTime& date) : 0026 m_time {time}, 0027 m_date {date} 0028 {} 0029 0030 void Session::setName(const QString& name) 0031 { 0032 m_name = name; 0033 } 0034 0035 void Session::setNote(const QString& note) 0036 { 0037 m_note = note; 0038 } 0039 0040 void Session::setTime(int time) 0041 { 0042 m_time = time; 0043 } 0044 0045 void Session::setDate(const QDateTime& date) 0046 { 0047 m_date = date; 0048 } 0049 0050 void Session::setIsOutdated(bool isOutdated) 0051 { 0052 m_isOutdated = isOutdated; 0053 } 0054 0055 QString Session::name() const 0056 { 0057 return m_name; 0058 } 0059 0060 QString Session::note() const 0061 { 0062 return m_note; 0063 } 0064 0065 int Session::time() const 0066 { 0067 return m_time; 0068 } 0069 0070 QDateTime Session::date() const 0071 { 0072 return m_date; 0073 } 0074 0075 bool Session::isOutdated() const 0076 { 0077 return m_isOutdated; 0078 } 0079 0080 QVector<Lap> Session::laps() const 0081 { 0082 return m_laps; 0083 } 0084 0085 bool Session::isEmpty() const 0086 { 0087 return m_time == 0; 0088 } 0089 0090 void Session::addLap(const Lap& lap) 0091 { 0092 m_laps.append(lap); 0093 } 0094 0095 void Session::clear() 0096 { 0097 m_time = 0; 0098 m_laps.clear(); 0099 } 0100 0101 void Session::write(QJsonObject& json) const 0102 { 0103 json[QStringLiteral("name")] = m_name; 0104 json[QStringLiteral("note")] = m_note; 0105 json[QStringLiteral("time")] = m_time; 0106 json[QStringLiteral("date")] = m_date.toString(Qt::ISODate); 0107 0108 auto laps = QJsonArray {}; 0109 0110 for (const auto& lap : m_laps) { 0111 auto object = QJsonObject {}; 0112 lap.write(object); 0113 laps.append(object); 0114 } 0115 0116 json[QStringLiteral("laps")] = laps; 0117 } 0118 0119 Session Session::fromJson(const QJsonObject& json) 0120 { 0121 auto session = Session {}; 0122 0123 session.m_name = json[QStringLiteral("name")].toString(); 0124 session.m_note = json[QStringLiteral("note")].toString(); 0125 session.m_time = json[QStringLiteral("time")].toInt(); 0126 session.m_date = QDateTime::fromString(json[QStringLiteral("date")].toString(), Qt::ISODate); 0127 0128 const auto laps = json[QStringLiteral("laps")].toArray(); 0129 for (const auto& lap : laps) { 0130 session.addLap(Lap::fromJson(lap.toObject())); 0131 } 0132 0133 return session; 0134 } 0135 0136 bool Session::operator==(const Session& right) const 0137 { 0138 return m_date == right.m_date; 0139 } 0140 0141 bool Session::operator!=(const Session& right) const 0142 { 0143 return not (*this == right); 0144 } 0145