File indexing completed on 2025-01-05 05:14:51

0001 /*
0002 SPDX-FileCopyrightText: 2021 Hamed Masafi <hamed.masfi@gmail.com>
0003 
0004 SPDX-License-Identifier: GPL-3.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include <QTextEdit>
0010 
0011 class QCompleter;
0012 class QStringListModel;
0013 class CompletionTextEdit : public QTextEdit
0014 {
0015     Q_OBJECT
0016 
0017 public:
0018     explicit CompletionTextEdit(QWidget *parent = nullptr);
0019 
0020     void setCompleter(QCompleter *c);
0021     QCompleter *completer() const;
0022 
0023     void addWord(const QString &word);
0024     void addWords(const QStringList &words);
0025 
0026     const QStringList &words() const;
0027     void setWords(const QStringList &newWords);
0028 
0029     void begin();
0030 
0031 protected:
0032     void keyPressEvent(QKeyEvent *e) override;
0033     void focusInEvent(QFocusEvent *e) override;
0034 
0035 private Q_SLOTS:
0036     void insertCompletion(const QString &completion);
0037 
0038 private:
0039     QString textUnderCursor() const;
0040 
0041 private:
0042     QStringList mWords;
0043     QStringListModel *const mCompletionModel;
0044     QCompleter *const mCompleter;
0045 };