1/*
2 Copyright (C) 2014 by Elvis Angelaccio <elvis.angelaccio@kdemail.net>
3
4 This file is part of Kronometer.
5
6 Kronometer is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 2 of the License, or
9 (at your option) any later version.
10
11 Kronometer is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Kronometer. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20#ifndef LAPMODEL_H
21#define LAPMODEL_H
22
23#include <QAbstractTableModel>
24
25#include "timeformat.h"
26
27class QTime;
28class QDomElement;
29
30/**
31 * @brief A LapModel is a Model for lap times.
32 * A LapModel holds a list of times. Every time is meant to be the absolute time of a lap.
33 * Then the model show the relative time of the lap, computing the difference between two consecutive absolute times.
34 */
35class LapModel : public QAbstractTableModel
36{
37 Q_OBJECT
38
39public:
40
41 explicit LapModel(QObject *parent = nullptr);
42
43 int rowCount(const QModelIndex& parent) const;
44 int columnCount(const QModelIndex& parent) const;
45 QVariant data(const QModelIndex& index, int role) const;
46 QVariant headerData(int section, Qt::Orientation orientation, int role) const;
47
48 void setTimeFormat(const TimeFormat& format);
49
50 /**
51 * Serialize the suggested lap time to the given XML DOM element.
52 * The serialization is implemented by adding an attribute (with the given name) to the element.
53 * @param element The XML DOM element to be used as serialization output.
54 * @param attributeName The name of the attribute to be added to the element.
55 * @param lapIndex the index of the lap time to be serialized.
56 * @return true if the serialization succeeds, false otherwise.
57 */
58 bool lapToXml(QDomElement& element, const QString& attributeName, int lapIndex);
59
60 /**
61 * De-serialize the suggested lap time from the given XML DOM element.
62 * The deserialization is implemented by reading an attribute (with the given name) from the element.
63 * @param element The XML DOM element to be used as deserialization input.
64 * @param attributeName The name of the attribute to be read from the element.
65 * @param lapIndex the index of the lap time to be deserialized.
66 * @return true if the deserialization succeeds, false otherwise.
67 */
68 bool lapFromXml(const QDomElement& element, const QString& attributeName);
69
70 /**
71 * Compute the relative time of a lap.
72 * @param lapIndex The index of the lap.
73 * @return Relative time of the lap, formatted as string.
74 */
75 QString relativeLapTime(int lapIndex) const;
76
77 /**
78 * Compute the absolute time of a lap.
79 * @param lapIndex The index of the lap.
80 * @return Absolute time of the lap, formatted as string.
81 */
82 QString absoluteLapTime(int lapIndex) const;
83
84 /**
85 * Whether the model is empty.
86 * @return true if the model holds at least one lap time, false otherwise.
87 */
88 bool isEmpty() const;
89
90 friend QDataStream& operator<<(QDataStream& out, const LapModel& m);
91 friend QDataStream& operator>>(QDataStream& in, LapModel& m);
92
93public slots:
94
95 /**
96 * Add a new absolute lap time to the model.
97 * @param lapTime The absolute time of the new lap.
98 */
99 void onLap(const QTime& lapTime);
100
101 /**
102 * Clear all the model data
103 */
104 void onClear();
105
106private:
107
108 static const int LAP_TAG_NUMBER = 3; /** Number of tag/header in the model */
109
110 enum LapTag
111 {
112 NUMBER = 0, /**< Index of the lap-number column */
113 REL_TIME = 1, /**< Index of the lap relative time column */
114 ABS_TIME = 2 /**< Index of the lap absolute time column */
115 };
116
117 QList<QTime> timeList; /** Absolute lap times */
118 TimeFormat timeFormat; /** Current lap times format */
119
120
121};
122
123
124#endif
125