From 07259b9cb736469262acbb450fd451233e150c2c Mon Sep 17 00:00:00 2001 From: Jussi Kuokkanen Date: Fri, 4 Sep 2020 12:56:36 +0300 Subject: [PATCH] use completion interface in emoji completions --- src/TextInputWidget.cpp | 10 +++++++--- src/emoji/EmojiSearchModel.h | 14 ++++++++++++-- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/TextInputWidget.cpp b/src/TextInputWidget.cpp index b46f0e5a..6d1fdc7a 100644 --- a/src/TextInputWidget.cpp +++ b/src/TextInputWidget.cpp @@ -30,6 +30,7 @@ #include "Cache.h" #include "ChatPage.h" +#include "CompletionModelRoles.h" #include "CompletionProxyModel.h" #include "Logging.h" #include "TextInputWidget.h" @@ -71,6 +72,7 @@ FilteredTextEdit::FilteredTextEdit(QWidget *parent) auto model = new emoji::EmojiSearchModel(this); model->sort(0, Qt::AscendingOrder); completer_->setModel((emoji_completion_model_ = new CompletionProxyModel(model, this))); + emoji_completion_model_->setFilterRole(CompletionModel::SearchRole); completer_->setModelSorting(QCompleter::UnsortedModel); completer_->popup()->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); completer_->popup()->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); @@ -79,8 +81,8 @@ FilteredTextEdit::FilteredTextEdit(QWidget *parent) QOverload::of(&QCompleter::activated), [this](auto &index) { emoji_popup_open_ = false; - auto emoji = index.data(emoji::EmojiModel::Unicode).toString(); - insertCompletion(emoji); + auto text = index.data(CompletionModel::CompletionRole).toString(); + insertCompletion(text); }); connect(this, &FilteredTextEdit::resultsRetrieved, this, &FilteredTextEdit::showResults); @@ -297,7 +299,9 @@ FilteredTextEdit::keyPressEvent(QKeyEvent *event) if (emoji_popup_open_ && textAfterPosition(trigger_pos_).length() > 2) { // Update completion - emoji_completion_model_->setFilterRegExp(textAfterPosition(trigger_pos_)); + // Don't include the trigger token in the search + emoji_completion_model_->setFilterWildcard( + textAfterPosition(trigger_pos_).remove(0, 1)); completer_->complete(completerRect()); } diff --git a/src/emoji/EmojiSearchModel.h b/src/emoji/EmojiSearchModel.h index 13a03934..7dbe6732 100644 --- a/src/emoji/EmojiSearchModel.h +++ b/src/emoji/EmojiSearchModel.h @@ -2,6 +2,7 @@ #include "EmojiModel.h" +#include #include #include #include @@ -19,12 +20,21 @@ public: } QVariant data(const QModelIndex &index, int role = Qt::UserRole + 1) const override { - if (role == Qt::DisplayRole) { + switch (role) { + case Qt::DisplayRole: { auto emoji = QSortFilterProxyModel::data(index, role).toString(); return emoji + " :" + toShortcode(data(index, EmojiModel::ShortName).toString()) + ":"; } - return QSortFilterProxyModel::data(index, role); + case CompletionModel::CompletionRole: + return QSortFilterProxyModel::data(index, EmojiModel::Unicode); + case CompletionModel::SearchRole: { + return toShortcode( + QSortFilterProxyModel::data(index, EmojiModel::ShortName).toString()); + } + default: + return QSortFilterProxyModel::data(index, role); + } } private: