File indexing completed on 2025-01-05 04:55:01

0001 /*
0002     Copyright (c) 2018 Christian Mollekopf <mollekopf@kolabsys.com>
0003 
0004     This library is free software; you can redistribute it and/or modify it
0005     under the terms of the GNU Library General Public License as published by
0006     the Free Software Foundation; either version 2 of the License, or (at your
0007     option) any later version.
0008 
0009     This library is distributed in the hope that it will be useful, but WITHOUT
0010     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0011     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
0012     License for more details.
0013 
0014     You should have received a copy of the GNU Library General Public License
0015     along with this library; see the file COPYING.LIB.  If not, write to the
0016     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
0017     02110-1301, USA.
0018 */
0019 
0020 #pragma once
0021 #include "kube_export.h"
0022 
0023 #include <QAbstractItemModel>
0024 #include <QSortFilterProxyModel>
0025 #include <QList>
0026 #include <QSet>
0027 #include <QSharedPointer>
0028 #include <QDateTime>
0029 #include "debouncer.h"
0030 
0031 namespace KCalendarCore {
0032     class Incidence;
0033 }
0034 namespace Sink {
0035     namespace ApplicationDomain {
0036         struct Todo;
0037     }
0038 }
0039 class EntityCacheInterface;
0040 
0041 class KUBE_EXPORT TodoSourceModel : public QAbstractItemModel
0042 {
0043     Q_OBJECT
0044 public:
0045     enum Roles {
0046         Summary = Qt::UserRole + 1,
0047         Description,
0048         StartDate,
0049         DueDate,
0050         CompletedDate,
0051         Date,
0052         Color,
0053         Calendar,
0054         Status,
0055         Complete,
0056         Doing,
0057         Important,
0058         Relevance,
0059         Todo,
0060         SortDate,
0061         LastRole
0062     };
0063     Q_ENUM(Roles);
0064 
0065     TodoSourceModel(QObject *parent = nullptr);
0066     ~TodoSourceModel() = default;
0067 
0068     QModelIndex index(int row, int column, const QModelIndex &parent = {}) const override;
0069     QModelIndex parent(const QModelIndex &index) const override;
0070 
0071     int rowCount(const QModelIndex &parent = {}) const override;
0072     int columnCount(const QModelIndex &parent) const override;
0073 
0074     QVariant data(const QModelIndex &index, int role) const override;
0075     QHash<int, QByteArray> roleNames() const override;
0076 
0077     void setFilter(const QVariantMap &);
0078 
0079 private:
0080     void refreshView();
0081     void updateFromSource();
0082     QByteArray getColor(const QByteArray &calendar) const;
0083     QString getCalendarName(const QByteArray &calendar) const;
0084 
0085     QSharedPointer<QAbstractItemModel> mSourceModel;
0086     QSet<QByteArray> mCalendarFilter;
0087     QSharedPointer<EntityCacheInterface> mCalendarCache;
0088 
0089     Debouncer mUpdateFromSourceDebouncer;
0090 
0091     struct Occurrence {
0092         QDateTime start;
0093         QDateTime due;
0094         QDateTime completed;
0095         QSharedPointer<KCalendarCore::Incidence> incidence;
0096         QByteArray color;
0097         QString calendarName;
0098         QString status;
0099         QSharedPointer<Sink::ApplicationDomain::Todo> domainObject;
0100         int priority;
0101     };
0102 
0103     QList<Occurrence> mTodos;
0104 };
0105 
0106 class KUBE_EXPORT TodoModel : public QSortFilterProxyModel
0107 {
0108     Q_OBJECT
0109     Q_PROPERTY(QVariantMap filter WRITE setFilter)
0110 
0111 public:
0112     TodoModel(QObject *parent = nullptr);
0113     ~TodoModel() = default;
0114 
0115     QHash<int, QByteArray> roleNames() const override;
0116 
0117     void setFilter(const QVariantMap &);
0118 
0119 protected:
0120     bool lessThan(const QModelIndex &left, const QModelIndex &right) const Q_DECL_OVERRIDE;
0121     bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const Q_DECL_OVERRIDE;
0122 
0123 private:
0124     bool mFilterNotStarted{false};
0125 };