use completion interface in emoji completions

This commit is contained in:
Jussi Kuokkanen 2020-09-04 12:56:36 +03:00 committed by Nicolas Werner
parent 00bc87b550
commit 07259b9cb7
2 changed files with 19 additions and 5 deletions

View file

@ -30,6 +30,7 @@
#include "Cache.h" #include "Cache.h"
#include "ChatPage.h" #include "ChatPage.h"
#include "CompletionModelRoles.h"
#include "CompletionProxyModel.h" #include "CompletionProxyModel.h"
#include "Logging.h" #include "Logging.h"
#include "TextInputWidget.h" #include "TextInputWidget.h"
@ -71,6 +72,7 @@ FilteredTextEdit::FilteredTextEdit(QWidget *parent)
auto model = new emoji::EmojiSearchModel(this); auto model = new emoji::EmojiSearchModel(this);
model->sort(0, Qt::AscendingOrder); model->sort(0, Qt::AscendingOrder);
completer_->setModel((emoji_completion_model_ = new CompletionProxyModel(model, this))); completer_->setModel((emoji_completion_model_ = new CompletionProxyModel(model, this)));
emoji_completion_model_->setFilterRole(CompletionModel::SearchRole);
completer_->setModelSorting(QCompleter::UnsortedModel); completer_->setModelSorting(QCompleter::UnsortedModel);
completer_->popup()->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); completer_->popup()->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
completer_->popup()->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); completer_->popup()->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
@ -79,8 +81,8 @@ FilteredTextEdit::FilteredTextEdit(QWidget *parent)
QOverload<const QModelIndex &>::of(&QCompleter::activated), QOverload<const QModelIndex &>::of(&QCompleter::activated),
[this](auto &index) { [this](auto &index) {
emoji_popup_open_ = false; emoji_popup_open_ = false;
auto emoji = index.data(emoji::EmojiModel::Unicode).toString(); auto text = index.data(CompletionModel::CompletionRole).toString();
insertCompletion(emoji); insertCompletion(text);
}); });
connect(this, &FilteredTextEdit::resultsRetrieved, this, &FilteredTextEdit::showResults); connect(this, &FilteredTextEdit::resultsRetrieved, this, &FilteredTextEdit::showResults);
@ -297,7 +299,9 @@ FilteredTextEdit::keyPressEvent(QKeyEvent *event)
if (emoji_popup_open_ && textAfterPosition(trigger_pos_).length() > 2) { if (emoji_popup_open_ && textAfterPosition(trigger_pos_).length() > 2) {
// Update completion // 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()); completer_->complete(completerRect());
} }

View file

@ -2,6 +2,7 @@
#include "EmojiModel.h" #include "EmojiModel.h"
#include <CompletionModelRoles.h>
#include <QDebug> #include <QDebug>
#include <QEvent> #include <QEvent>
#include <QSortFilterProxyModel> #include <QSortFilterProxyModel>
@ -19,13 +20,22 @@ public:
} }
QVariant data(const QModelIndex &index, int role = Qt::UserRole + 1) const override 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(); auto emoji = QSortFilterProxyModel::data(index, role).toString();
return emoji + " :" + return emoji + " :" +
toShortcode(data(index, EmojiModel::ShortName).toString()) + ":"; toShortcode(data(index, EmojiModel::ShortName).toString()) + ":";
} }
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); return QSortFilterProxyModel::data(index, role);
} }
}
private: private:
QString toShortcode(QString shortname) const QString toShortcode(QString shortname) const