File indexing completed on 2024-05-19 16:51:56
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 #ifndef SESSIONMODEL_H 0021 #define SESSIONMODEL_H 0022 0023 #include "session.h" 0024 0025 #include <QAbstractTableModel> 0026 0027 /** 0028 * @brief A SessionModel is a Model for sessions. 0029 */ 0030 class SessionModel : public QAbstractTableModel 0031 { 0032 Q_OBJECT 0033 0034 public: 0035 0036 enum class Roles 0037 { 0038 SessionIdRole = Qt::UserRole, 0039 NameRole, 0040 DateRole, 0041 NoteRole, 0042 SessionRole 0043 }; 0044 0045 explicit SessionModel(QObject *parent = nullptr); 0046 0047 int rowCount(const QModelIndex& parent = {}) const override; 0048 int columnCount(const QModelIndex& parent = {}) const override; 0049 QVariant data(const QModelIndex& index, int role) const override; 0050 QVariant headerData(int section, Qt::Orientation orientation, int role) const override; 0051 bool setData(const QModelIndex& index, const QVariant& value, int role) override; 0052 Qt::ItemFlags flags(const QModelIndex& index) const override; 0053 bool removeRows(int row, int count, const QModelIndex& parent) override; 0054 0055 /** 0056 * Insert a new Session object to the end of the model. 0057 * @param session The new Session object. 0058 */ 0059 void append(const Session& session); 0060 0061 /** 0062 * Update the given session in the model. 0063 * If the session does not belong to the model, nothing happens. 0064 * @param session The session to be updated. 0065 */ 0066 void update(const Session& session); 0067 0068 /** 0069 * @return Whether the model does not hold any session. 0070 */ 0071 bool isEmpty() const; 0072 0073 /** 0074 * Whether the given index refers to an editable value in the model. 0075 * @param index An index of the model. 0076 * @return True if the underlying data is editable, false otherwise. 0077 */ 0078 bool isEditable(const QModelIndex& index) const; 0079 0080 /** 0081 * Load the model from the given JSON object. 0082 * @param json A JSON object. 0083 */ 0084 void read(const QJsonObject& json); 0085 0086 private slots: 0087 0088 /** 0089 * Update the global sessions file with the data in the model. 0090 */ 0091 void slotWriteData(); 0092 0093 private: 0094 0095 /** 0096 * @return The index of the column for the given role. 0097 */ 0098 int columnForRole(Roles role) const; 0099 0100 /** 0101 * @return The role for the given column. 0102 */ 0103 Roles roleForColumn(int column) const; 0104 0105 /** 0106 * @return The list of sessions as json array. 0107 */ 0108 QJsonArray jsonSessions() const; 0109 0110 const QVector<Roles> m_roles {Roles::SessionIdRole, Roles::NameRole, Roles::DateRole, Roles::NoteRole}; 0111 0112 QVector<Session> m_sessionList; /** Sessions in the model. */ 0113 0114 Q_DISABLE_COPY(SessionModel) 0115 }; 0116 0117 0118 #endif