File indexing completed on 2024-04-28 04:59:35

0001 // SPDX-FileCopyrightText: 2020 Tobias Fella <tobias.fella@kde.org>
0002 // SPDX-License-Identifier: GPL-2.0-or-later
0003 
0004 #pragma once
0005 
0006 #include <QQmlEngine>
0007 #include <QSortFilterProxyModel>
0008 
0009 /**
0010  * @class SortFilterRoomListModel
0011  *
0012  * This model sorts and filters the room list.
0013  *
0014  * There are numerous room sort orders available:
0015  *  - Categories - sort rooms by their NeoChatRoomType and then by last activty within
0016  *                 each category.
0017  *  - LastActivity - sort rooms by the last active time in the room.
0018  *  - Alphabetical - sort the rooms alphabetically by room name.
0019  *
0020  * The model can be given a filter string that will only show rooms who's name includes
0021  * the text.
0022  *
0023  * The model can also be given an active space ID and will only show rooms within
0024  * that space.
0025  *
0026  * All space rooms and upgraded rooms will also be filtered out.
0027  */
0028 class SortFilterRoomListModel : public QSortFilterProxyModel
0029 {
0030     Q_OBJECT
0031     QML_ELEMENT
0032 
0033     /**
0034      * @brief The order by which the rooms will be sorted.
0035      *
0036      * @sa RoomSortOrder
0037      */
0038     Q_PROPERTY(RoomSortOrder roomSortOrder READ roomSortOrder WRITE setRoomSortOrder NOTIFY roomSortOrderChanged)
0039 
0040     /**
0041      * @brief The text to use to filter room names.
0042      */
0043     Q_PROPERTY(QString filterText READ filterText READ filterText WRITE setFilterText NOTIFY filterTextChanged)
0044 
0045     /**
0046      * @brief Set the ID of the space to show rooms for.
0047      */
0048     Q_PROPERTY(QString activeSpaceId READ activeSpaceId WRITE setActiveSpaceId NOTIFY activeSpaceIdChanged)
0049 
0050     /**
0051      * @brief Whether only direct chats should be shown.
0052      */
0053     Q_PROPERTY(Mode mode READ mode WRITE setMode NOTIFY modeChanged)
0054 
0055 public:
0056     enum RoomSortOrder {
0057         Alphabetical,
0058         LastActivity,
0059         Categories,
0060     };
0061     Q_ENUM(RoomSortOrder)
0062 
0063     enum Mode {
0064         Rooms,
0065         DirectChats,
0066         All,
0067     };
0068     Q_ENUM(Mode)
0069 
0070     explicit SortFilterRoomListModel(QObject *parent = nullptr);
0071 
0072     void setRoomSortOrder(RoomSortOrder sortOrder);
0073     [[nodiscard]] RoomSortOrder roomSortOrder() const;
0074 
0075     void setFilterText(const QString &text);
0076     [[nodiscard]] QString filterText() const;
0077 
0078     QString activeSpaceId() const;
0079     void setActiveSpaceId(const QString &spaceId);
0080 
0081     Mode mode() const;
0082     void setMode(Mode mode);
0083 
0084 protected:
0085     /**
0086      * @brief Returns true if the value of source_left is less than source_right.
0087      *
0088      * @sa QSortFilterProxyModel::lessThan
0089      */
0090     [[nodiscard]] bool lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const override;
0091 
0092     /**
0093      * @brief Whether a row should be shown out or not.
0094      *
0095      * @sa QSortFilterProxyModel::filterAcceptsRow
0096      */
0097     [[nodiscard]] bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override;
0098 
0099 Q_SIGNALS:
0100     void roomSortOrderChanged();
0101     void filterTextChanged();
0102     void activeSpaceIdChanged();
0103     void modeChanged();
0104 
0105 private:
0106     RoomSortOrder m_sortOrder = Categories;
0107     Mode m_mode = All;
0108     QString m_filterText;
0109     QString m_activeSpaceId;
0110 };