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

0001 /*
0002     SPDX-FileCopyrightText: 2015 Elvis Angelaccio <elvis.angelaccio@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #ifndef SESSIONMODEL_H
0008 #define SESSIONMODEL_H
0009 
0010 #include "session.h"
0011 
0012 #include <QAbstractTableModel>
0013 
0014 /**
0015  * @brief A SessionModel is a Model for sessions.
0016  */
0017 class SessionModel : public QAbstractTableModel
0018 {
0019     Q_OBJECT
0020 
0021 public:
0022 
0023     enum class Roles
0024     {
0025         SessionIdRole = Qt::UserRole,
0026         NameRole,
0027         DateRole,
0028         NoteRole,
0029         SessionRole
0030     };
0031 
0032     explicit SessionModel(QObject *parent = nullptr);
0033 
0034     int rowCount(const QModelIndex& parent = {}) const override;
0035     int columnCount(const QModelIndex& parent = {}) const override;
0036     QVariant data(const QModelIndex& index, int role) const override;
0037     QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
0038     bool setData(const QModelIndex& index, const QVariant& value, int role) override;
0039     Qt::ItemFlags flags(const QModelIndex& index) const override;
0040     bool removeRows(int row, int count, const QModelIndex& parent) override;
0041 
0042     /**
0043      * Insert a new Session object to the end of the model.
0044      * @param session The new Session object.
0045      */
0046     void append(const Session& session);
0047 
0048     /**
0049      * Update the given session in the model and updates its date.
0050      * If the session does not belong to the model, nothing happens.
0051      * @param session The session to be updated.
0052      */
0053     void update(Session& session);
0054 
0055     /**
0056      * @return Whether the model does not hold any session.
0057      */
0058     bool isEmpty() const;
0059 
0060     /**
0061      * Whether the given index refers to an editable value in the model.
0062      * @param index An index of the model.
0063      * @return True if the underlying data is editable, false otherwise.
0064      */
0065     bool isEditable(const QModelIndex& index) const;
0066 
0067     /**
0068      * Load the model from the given JSON object.
0069      * @param json A JSON object.
0070      */
0071     void read(const QJsonObject& json);
0072 
0073 private Q_SLOTS:
0074 
0075     /**
0076      * Update the global sessions file with the data in the model.
0077      */
0078     void slotWriteData();
0079 
0080 private:
0081 
0082     /**
0083      * @return The index of the column for the given role.
0084      */
0085     int columnForRole(Roles role) const;
0086 
0087     /**
0088      * @return The role for the given column.
0089      */
0090     Roles roleForColumn(int column) const;
0091 
0092     /**
0093      * @return The list of sessions as json array.
0094      */
0095     QJsonArray jsonSessions() const;
0096 
0097     const QVector<Roles> m_roles {Roles::SessionIdRole, Roles::NameRole, Roles::DateRole, Roles::NoteRole};
0098 
0099     QVector<Session> m_sessionList;    /** Sessions in the model. */
0100 
0101     Q_DISABLE_COPY(SessionModel)
0102 };
0103 
0104 
0105 #endif