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 "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<const QModelIndex &>::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());
}

View file

@ -2,6 +2,7 @@
#include "EmojiModel.h"
#include <CompletionModelRoles.h>
#include <QDebug>
#include <QEvent>
#include <QSortFilterProxyModel>
@ -19,13 +20,22 @@ 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()) + ":";
}
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:
QString toShortcode(QString shortname) const