File indexing completed on 2024-04-21 05:54:14
0001 // SPDX-FileCopyrightText: 2022 Felipe Kinoshita <kinofhek@gmail.com> 0002 // SPDX-License-Identifier: LGPL-2.1-or-later 0003 0004 #pragma once 0005 0006 #include <QAbstractListModel> 0007 #include <QJsonObject> 0008 0009 #include "task.h" 0010 0011 class TasksModel : public QAbstractListModel 0012 { 0013 Q_OBJECT 0014 Q_PROPERTY(int completedTasks READ completedTasks NOTIFY completedTasksChanged) 0015 0016 public: 0017 enum Roles { 0018 TitleRole = Qt::UserRole + 1, 0019 CheckedRole 0020 }; 0021 0022 explicit TasksModel(QObject *parent = nullptr); 0023 0024 QHash<int, QByteArray> roleNames() const override; 0025 QVariant data(const QModelIndex &index, int role) const override; 0026 int rowCount(const QModelIndex &parent) const final; 0027 bool setData(const QModelIndex &index, const QVariant &value, int role) override; 0028 0029 Q_INVOKABLE void add(const QString &title); 0030 Q_INVOKABLE void remove(const QModelIndex &index); 0031 Q_INVOKABLE void clear(); 0032 0033 [[nodiscard]] int completedTasks() const 0034 { 0035 return m_completedTasks; 0036 } 0037 Q_SIGNAL void completedTasksChanged(); 0038 0039 protected: 0040 bool saveTasks() const; 0041 bool loadTasks(); 0042 0043 private: 0044 QList<Task> m_tasks; 0045 int m_completedTasks = 0; 0046 };