File indexing completed on 2024-12-08 07:34:23

0001 /*
0002    SPDX-FileCopyrightText: 2018-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include <QObject>
0010 
0011 #include "model/inputcompletermodel.h"
0012 
0013 #include "libruqolacore_export.h"
0014 
0015 class QAbstractItemModel;
0016 class RocketChatAccount;
0017 class CommandsModelFilterProxyModel;
0018 class EmoticonFilterProxyModel;
0019 class LIBRUQOLACORE_EXPORT InputTextManager : public QObject
0020 {
0021     Q_OBJECT
0022 public:
0023     enum CompletionForType {
0024         Channel = 0,
0025         User,
0026         Emoji,
0027         Command,
0028         None,
0029     };
0030     Q_ENUM(CompletionForType)
0031 
0032     explicit InputTextManager(RocketChatAccount *account, QObject *parent = nullptr);
0033     ~InputTextManager() override;
0034 
0035     void setEmoticonModel(QAbstractItemModel *model);
0036 
0037     [[nodiscard]] InputCompleterModel *inputCompleterModel() const;
0038     [[nodiscard]] QAbstractItemModel *emojiCompleterModel() const;
0039 
0040     void inputTextCompleter(const QJsonObject &obj);
0041 
0042     [[nodiscard]] QString applyCompletion(const QString &newWord, const QString &str, int *pPosition);
0043 
0044     void setInputTextChanged(const QString &roomId, const QString &str, int position);
0045 
0046     void clearCompleter();
0047 
0048     // Only for autotests
0049     [[nodiscard]] QString searchWord(const QString &text, int position, int &start);
0050 
0051     void setCommandModel(QAbstractItemModel *model);
0052     [[nodiscard]] QAbstractItemModel *commandModel() const;
0053 
0054 Q_SIGNALS:
0055     // Trigger autocompletion request in DDPClient (via RocketChatAccount)
0056     // Emitted with Channel and User, never Emoji or None
0057     void completionRequested(const QString &roomId, const QString &pattern, const QString &exceptions, InputTextManager::CompletionForType type);
0058     void completionTypeChanged(InputTextManager::CompletionForType type);
0059     void selectFirstTextCompleter();
0060 
0061 private:
0062     LIBRUQOLACORE_NO_EXPORT void setCompletionType(CompletionForType type);
0063 
0064     InputCompleterModel *const mInputCompleterModel;
0065     EmoticonFilterProxyModel *const mEmoticonFilterProxyModel;
0066     CommandsModelFilterProxyModel *const mCommandFilterProxyModel;
0067     RocketChatAccount *const mRocketChatAccount;
0068     CompletionForType mCurrentCompletionType = None;
0069     QString mCurrentCompletionPattern;
0070 };
0071 
0072 Q_DECLARE_METATYPE(InputTextManager::CompletionForType)