File indexing completed on 2024-05-12 05:54:14

0001 /*
0002     SPDX-FileCopyrightText: 2014 Elvis Angelaccio <elvis.angelaccio@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #ifndef LAPMODEL_H
0008 #define LAPMODEL_H
0009 
0010 #include "lap.h"
0011 #include "timeformat.h"
0012 
0013 #include <QAbstractTableModel>
0014 
0015 class QTime;
0016 
0017 /**
0018  * @brief A LapModel is a Model for lap times.
0019  * A LapModel holds a list of times. Every time is meant to be the absolute time of a lap.
0020  * The model can also show the relative time of the lap, computing the difference between two consecutive absolute times.
0021  */
0022 class LapModel : public QAbstractTableModel
0023 {
0024     Q_OBJECT
0025 
0026 public:
0027 
0028     enum class Roles
0029     {
0030         LapIdRole = Qt::UserRole,
0031         RelativeTimeRole,
0032         AbsoluteTimeRole,
0033         NoteRole,
0034         LapRole
0035     };
0036 
0037     explicit LapModel(QObject *parent = nullptr);
0038 
0039     int rowCount(const QModelIndex& parent = {}) const override;
0040     int columnCount(const QModelIndex& parent = {}) const override;
0041     QVariant data(const QModelIndex& index, int role) const override;
0042     QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
0043     bool setData(const QModelIndex& index, const QVariant& value, int role) override;
0044     Qt::ItemFlags flags(const QModelIndex& index) const override;
0045 
0046     /**
0047      * Update the lap times format used by the model.
0048      * @param format The times format to be used.
0049      */
0050     void setTimeFormat(const TimeFormat& format);
0051 
0052     /**
0053      * Insert a new Lap object to the end of the model.
0054      * @param lap The new Lap object.
0055      */
0056     void append(const Lap& lap);
0057 
0058     /**
0059      * @return Whether the model does not hold any lap.
0060      */
0061     bool isEmpty() const;
0062 
0063     /**
0064      * @return The index of the column for the given role.
0065      */
0066     int columnForRole(Roles role) const;
0067 
0068 public Q_SLOTS:
0069 
0070     /**
0071      * Add a new absolute lap time to the model.
0072      * @param lapTime The absolute time of the new lap.
0073      */
0074     void addLap(const QTime& lapTime);
0075 
0076     /**
0077      * Clear all the model data
0078      */
0079     void clear();
0080 
0081 private:
0082 
0083     const QVector<Roles> m_roles {Roles::LapIdRole, Roles::RelativeTimeRole, Roles::AbsoluteTimeRole, Roles::NoteRole};
0084 
0085     QVector<Lap> m_laps;             /** Lap times */
0086     TimeFormat m_timeFormat;          /** Current lap times format */
0087 
0088     /**
0089      *  Reload the model data.
0090      */
0091     void reload();
0092 
0093     /**
0094      * @return The role for the given column.
0095      */
0096     Roles roleForColumn(int column) const;
0097 
0098     Q_DISABLE_COPY(LapModel)
0099 };
0100 
0101 
0102 #endif