File indexing completed on 2024-05-05 16:58:37

0001 // SPDX-FileCopyrightText: 2022 Tobias Fella <tobias.fella@kde.org>
0002 // SPDX-License-Identifier: LGPL-2.0-or-later
0003 
0004 #pragma once
0005 
0006 #include <QConcatenateTablesProxyModel>
0007 #include <QSortFilterProxyModel>
0008 
0009 #include "roomlistmodel.h"
0010 
0011 class CompletionProxyModel;
0012 class UserListModel;
0013 class NeoChatRoom;
0014 class RoomListModel;
0015 
0016 /**
0017  * @class CompletionModel
0018  *
0019  * This class defines the model for suggesting completions in chat text.
0020  *
0021  * This model is able to select the appropriate completion type for the input text
0022  * and present a list of options that can be presented to the user.
0023  */
0024 class CompletionModel : public QAbstractListModel
0025 {
0026     Q_OBJECT
0027 
0028     /**
0029      * @brief The current text to search for completions.
0030      */
0031     Q_PROPERTY(QString text READ text NOTIFY textChanged)
0032 
0033     /**
0034      * @brief The current room that the model is getting completions for.
0035      */
0036     Q_PROPERTY(NeoChatRoom *room READ room WRITE setRoom NOTIFY roomChanged)
0037 
0038     /**
0039      * @brief The current type of completion being done on the entered text.
0040      *
0041      * @sa AutoCompletionType
0042      */
0043     Q_PROPERTY(AutoCompletionType autoCompletionType READ autoCompletionType NOTIFY autoCompletionTypeChanged);
0044 
0045     /**
0046      * @brief The RoomListModel to be used for room completions.
0047      */
0048     Q_PROPERTY(RoomListModel *roomListModel READ roomListModel WRITE setRoomListModel NOTIFY roomListModelChanged);
0049 
0050 public:
0051     /**
0052      * @brief Defines the different types of completion available.
0053      */
0054     enum AutoCompletionType {
0055         User, /**< A user in the current room. */
0056         Room, /**< A matrix room. */
0057         Emoji, /**< An emoji. */
0058         Command, /**< A / command. */
0059         None, /**< No available completion for the current text. */
0060     };
0061     Q_ENUM(AutoCompletionType)
0062 
0063     /**
0064      * @brief Defines the model roles.
0065      */
0066     enum Roles {
0067         DisplayNameRole = Qt::DisplayRole, /**< The main text to show. */
0068         SubtitleRole, /**< The subtitle text to show. */
0069         IconNameRole, /**< The icon to show. */
0070         ReplacedTextRole, /**< The text to replace the input text with for the completion. */
0071     };
0072     Q_ENUM(Roles)
0073 
0074     explicit CompletionModel(QObject *parent = nullptr);
0075 
0076     /**
0077      * @brief Get the given role value at the given index.
0078      *
0079      * @sa QAbstractItemModel::data
0080      */
0081     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
0082 
0083     /**
0084      * @brief Number of rows in the model.
0085      *
0086      * @sa  QAbstractItemModel::rowCount
0087      */
0088     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0089 
0090     /**
0091      * @brief Returns a mapping from Role enum values to role names.
0092      *
0093      * @sa EventRoles, QAbstractItemModel::roleNames()
0094      */
0095     QHash<int, QByteArray> roleNames() const override;
0096 
0097     QString text() const;
0098     void setText(const QString &text, const QString &fullText);
0099 
0100     NeoChatRoom *room() const;
0101     void setRoom(NeoChatRoom *room);
0102 
0103     RoomListModel *roomListModel() const;
0104     void setRoomListModel(RoomListModel *roomListModel);
0105 
0106     AutoCompletionType autoCompletionType() const;
0107     void setAutoCompletionType(AutoCompletionType autoCompletionType);
0108 
0109 Q_SIGNALS:
0110     void textChanged();
0111     void roomChanged();
0112     void autoCompletionTypeChanged();
0113     void roomListModelChanged();
0114 
0115 private:
0116     QString m_text;
0117     QString m_fullText;
0118     CompletionProxyModel *m_filterModel;
0119     NeoChatRoom *m_room = nullptr;
0120     AutoCompletionType m_autoCompletionType = None;
0121 
0122     void updateCompletion();
0123 
0124     UserListModel *m_userListModel;
0125     RoomListModel *m_roomListModel;
0126     QConcatenateTablesProxyModel *m_emojiModel;
0127 };
0128 Q_DECLARE_METATYPE(CompletionModel::AutoCompletionType);