File indexing completed on 2024-05-19 16:51:56

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 #ifndef LAPMODEL_H
0021 #define LAPMODEL_H
0022 
0023 #include "lap.h"
0024 #include "timeformat.h"
0025 
0026 #include <QAbstractTableModel>
0027 
0028 class QTime;
0029 
0030 /**
0031  * @brief A LapModel is a Model for lap times.
0032  * A LapModel holds a list of times. Every time is meant to be the absolute time of a lap.
0033  * The model can also show the relative time of the lap, computing the difference between two consecutive absolute times.
0034  */
0035 class LapModel : public QAbstractTableModel
0036 {
0037     Q_OBJECT
0038 
0039 public:
0040 
0041     enum class Roles
0042     {
0043         LapIdRole = Qt::UserRole,
0044         RelativeTimeRole,
0045         AbsoluteTimeRole,
0046         NoteRole,
0047         LapRole
0048     };
0049 
0050     explicit LapModel(QObject *parent = nullptr);
0051 
0052     int rowCount(const QModelIndex& parent = {}) const override;
0053     int columnCount(const QModelIndex& parent = {}) const override;
0054     QVariant data(const QModelIndex& index, int role) const override;
0055     QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
0056     bool setData(const QModelIndex& index, const QVariant& value, int role) override;
0057     Qt::ItemFlags flags(const QModelIndex& index) const override;
0058 
0059     /**
0060      * Update the lap times format used by the model.
0061      * @param format The times format to be used.
0062      */
0063     void setTimeFormat(const TimeFormat& format);
0064 
0065     /**
0066      * Insert a new Lap object to the end of the model.
0067      * @param lap The new Lap object.
0068      */
0069     void append(const Lap& lap);
0070 
0071     /**
0072      * @return Whether the model does not hold any lap.
0073      */
0074     bool isEmpty() const;
0075 
0076     /**
0077      * @return The index of the column for the given role.
0078      */
0079     int columnForRole(Roles role) const;
0080 
0081 public slots:
0082 
0083     /**
0084      * Add a new absolute lap time to the model.
0085      * @param lapTime The absolute time of the new lap.
0086      */
0087     void addLap(const QTime& lapTime);
0088 
0089     /**
0090      * Clear all the model data
0091      */
0092     void clear();
0093 
0094 private:
0095 
0096     const QVector<Roles> m_roles {Roles::LapIdRole, Roles::RelativeTimeRole, Roles::AbsoluteTimeRole, Roles::NoteRole};
0097 
0098     QVector<Lap> m_laps;             /** Lap times */
0099     TimeFormat m_timeFormat;          /** Current lap times format */
0100 
0101     /**
0102      *  Reload the model data.
0103      */
0104     void reload();
0105 
0106     /**
0107      * @return The role for the given column.
0108      */
0109     Roles roleForColumn(int column) const;
0110 
0111     Q_DISABLE_COPY(LapModel)
0112 };
0113 
0114 
0115 #endif