File indexing completed on 2024-05-12 09:02:07

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 public:
0051     enum RoomSortOrder {
0052         Alphabetical,
0053         LastActivity,
0054         Categories,
0055     };
0056     Q_ENUM(RoomSortOrder)
0057 
0058     explicit SortFilterRoomListModel(QObject *parent = nullptr);
0059 
0060     void setRoomSortOrder(RoomSortOrder sortOrder);
0061     [[nodiscard]] RoomSortOrder roomSortOrder() const;
0062 
0063     void setFilterText(const QString &text);
0064     [[nodiscard]] QString filterText() const;
0065 
0066     QString activeSpaceId() const;
0067     void setActiveSpaceId(const QString &spaceId);
0068 
0069 protected:
0070     /**
0071      * @brief Returns true if the value of source_left is less than source_right.
0072      *
0073      * @sa QSortFilterProxyModel::lessThan
0074      */
0075     [[nodiscard]] bool lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const override;
0076 
0077     /**
0078      * @brief Whether a row should be shown out or not.
0079      *
0080      * @sa QSortFilterProxyModel::filterAcceptsRow
0081      */
0082     [[nodiscard]] bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override;
0083 
0084 Q_SIGNALS:
0085     void roomSortOrderChanged();
0086     void filterTextChanged();
0087     void activeSpaceIdChanged();
0088 
0089 private:
0090     RoomSortOrder m_sortOrder = Categories;
0091     QString m_filterText;
0092     QString m_activeSpaceId;
0093 };