This commit is contained in:
Jussi Kuokkanen 2020-08-28 23:59:27 +03:00
parent 5e344d2685
commit 7acd4b3307
4 changed files with 135 additions and 122 deletions

View file

@ -4,12 +4,16 @@
#include <QSortFilterProxyModel>
class CompletionModel : public QSortFilterProxyModel {
class CompletionModel : public QSortFilterProxyModel
{
public:
CompletionModel(QAbstractItemModel *model, QObject *parent = nullptr) : QSortFilterProxyModel(parent) {
CompletionModel(QAbstractItemModel *model, QObject *parent = nullptr)
: QSortFilterProxyModel(parent)
{
setSourceModel(model);
}
int rowCount(const QModelIndex &parent) const override {
int rowCount(const QModelIndex &parent) const override
{
auto row_count = QSortFilterProxyModel::rowCount(parent);
return (row_count < 7) ? row_count : 7;
}

View file

@ -77,14 +77,14 @@ FilteredTextEdit::FilteredTextEdit(QWidget *parent)
completer_->popup()->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
completer_->popup()->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
connect(completer_, QOverload<const QModelIndex&>::of(&QCompleter::activated),
connect(completer_,
QOverload<const QModelIndex &>::of(&QCompleter::activated),
[this](auto &index) {
emoji_popup_open_ = false;
auto emoji = index.data(emoji::EmojiModel::Unicode).toString();
insertCompletion(emoji);
});
typingTimer_ = new QTimer(this);
typingTimer_->setInterval(1000);
typingTimer_->setSingleShot(true);
@ -126,7 +126,8 @@ FilteredTextEdit::FilteredTextEdit(QWidget *parent)
}
void
FilteredTextEdit::insertCompletion(QString completion) {
FilteredTextEdit::insertCompletion(QString completion)
{
// Paint the current word and replace it with 'completion'
auto cur_word = wordUnderCursor();
auto tc = textCursor();
@ -249,7 +250,7 @@ FilteredTextEdit::keyPressEvent(QKeyEvent *event)
QTextEdit::keyPressEvent(event);
emoji_popup_open_ = true;
emoji_completion_model_->setFilterRegExp(wordUnderCursor());
//completer_->setCompletionPrefix(wordUnderCursor());
// completer_->setCompletionPrefix(wordUnderCursor());
completer_->popup()->setCurrentIndex(completer_->completionModel()->index(0, 0));
completer_->complete(completerRect());
break;
@ -307,19 +308,21 @@ FilteredTextEdit::keyPressEvent(QKeyEvent *event)
if (isModifier)
return;
if (emoji_popup_open_) {
// Update completion
emoji_completion_model_->setFilterRegExp(wordUnderCursor());
//completer_->setCompletionPrefix(wordUnderCursor());
completer_->popup()->setCurrentIndex(completer_->completionModel()->index(0, 0));
// completer_->setCompletionPrefix(wordUnderCursor());
completer_->popup()->setCurrentIndex(
completer_->completionModel()->index(0, 0));
completer_->complete(completerRect());
}
if (emoji_popup_open_ && (completer_->completionCount() < 1 ||
if (emoji_popup_open_ &&
(completer_->completionCount() < 1 ||
!wordUnderCursor().contains(QRegExp(":[^\r\n\t\f\v :]+$")))) {
// No completions for this word or another word than the completer was started with
// No completions for this word or another word than the completer was
// started with
emoji_popup_open_ = false;
completer_->popup()->hide();
}
@ -447,8 +450,9 @@ FilteredTextEdit::completerRect()
auto item_height = completer_->popup()->sizeHintForRow(0);
auto max_height = item_height * completer_->maxVisibleItems();
auto height = (completer_->completionCount() > completer_->maxVisibleItems()) ? max_height :
completer_->completionCount() * item_height;
auto height = (completer_->completionCount() > completer_->maxVisibleItems())
? max_height
: completer_->completionCount() * item_height;
rect.setWidth(completer_->popup()->sizeHintForColumn(0));
rect.moveBottom(-height);
return rect;

View file

@ -11,16 +11,20 @@
namespace emoji {
// Map emoji data to searchable data
class EmojiSearchModel : public QSortFilterProxyModel {
class EmojiSearchModel : public QSortFilterProxyModel
{
public:
EmojiSearchModel(QObject *parent = nullptr) : QSortFilterProxyModel(parent) {
EmojiSearchModel(QObject *parent = nullptr)
: QSortFilterProxyModel(parent)
{
setSourceModel(new EmojiModel(this));
}
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) {
auto emoji = QSortFilterProxyModel::data(index, role).toString();
return emoji + " :" + toShortcode(data(index, EmojiModel::ShortName).toString())
+ ":";
return emoji + " :" +
toShortcode(data(index, EmojiModel::ShortName).toString()) + ":";
}
return QSortFilterProxyModel::data(index, role);
}
@ -29,7 +33,8 @@ public:
return (row_count < 7) ? row_count : 7;
}*/
private:
QString toShortcode(QString shortname) const {
QString toShortcode(QString shortname) const
{
return shortname.replace(" ", "-").replace(":", "-").replace("--", "-").toLower();
}
};