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