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> #include <QSortFilterProxyModel>
class CompletionModel : public QSortFilterProxyModel { class CompletionModel : public QSortFilterProxyModel
{
public: public:
CompletionModel(QAbstractItemModel *model, QObject *parent = nullptr) : QSortFilterProxyModel(parent) { CompletionModel(QAbstractItemModel *model, QObject *parent = nullptr)
: QSortFilterProxyModel(parent)
{
setSourceModel(model); setSourceModel(model);
} }
int rowCount(const QModelIndex &parent) const override { int rowCount(const QModelIndex &parent) const override
{
auto row_count = QSortFilterProxyModel::rowCount(parent); auto row_count = QSortFilterProxyModel::rowCount(parent);
return (row_count < 7) ? row_count : 7; return (row_count < 7) ? row_count : 7;
} }

View file

@ -77,14 +77,14 @@ FilteredTextEdit::FilteredTextEdit(QWidget *parent)
completer_->popup()->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); completer_->popup()->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
completer_->popup()->setVerticalScrollBarPolicy(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) { [this](auto &index) {
emoji_popup_open_ = false; emoji_popup_open_ = false;
auto emoji = index.data(emoji::EmojiModel::Unicode).toString(); auto emoji = index.data(emoji::EmojiModel::Unicode).toString();
insertCompletion(emoji); insertCompletion(emoji);
}); });
typingTimer_ = new QTimer(this); typingTimer_ = new QTimer(this);
typingTimer_->setInterval(1000); typingTimer_->setInterval(1000);
typingTimer_->setSingleShot(true); typingTimer_->setSingleShot(true);
@ -126,7 +126,8 @@ FilteredTextEdit::FilteredTextEdit(QWidget *parent)
} }
void void
FilteredTextEdit::insertCompletion(QString completion) { FilteredTextEdit::insertCompletion(QString completion)
{
// Paint the current word and replace it with 'completion' // Paint the current word and replace it with 'completion'
auto cur_word = wordUnderCursor(); auto cur_word = wordUnderCursor();
auto tc = textCursor(); auto tc = textCursor();
@ -307,19 +308,21 @@ FilteredTextEdit::keyPressEvent(QKeyEvent *event)
if (isModifier) if (isModifier)
return; return;
if (emoji_popup_open_) { if (emoji_popup_open_) {
// Update completion // Update completion
emoji_completion_model_->setFilterRegExp(wordUnderCursor()); emoji_completion_model_->setFilterRegExp(wordUnderCursor());
// completer_->setCompletionPrefix(wordUnderCursor()); // completer_->setCompletionPrefix(wordUnderCursor());
completer_->popup()->setCurrentIndex(completer_->completionModel()->index(0, 0)); completer_->popup()->setCurrentIndex(
completer_->completionModel()->index(0, 0));
completer_->complete(completerRect()); 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 :]+$")))) { !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; emoji_popup_open_ = false;
completer_->popup()->hide(); completer_->popup()->hide();
} }
@ -447,8 +450,9 @@ FilteredTextEdit::completerRect()
auto item_height = completer_->popup()->sizeHintForRow(0); auto item_height = completer_->popup()->sizeHintForRow(0);
auto max_height = item_height * completer_->maxVisibleItems(); auto max_height = item_height * completer_->maxVisibleItems();
auto height = (completer_->completionCount() > completer_->maxVisibleItems()) ? max_height : auto height = (completer_->completionCount() > completer_->maxVisibleItems())
completer_->completionCount() * item_height; ? max_height
: completer_->completionCount() * item_height;
rect.setWidth(completer_->popup()->sizeHintForColumn(0)); rect.setWidth(completer_->popup()->sizeHintForColumn(0));
rect.moveBottom(-height); rect.moveBottom(-height);
return rect; return rect;

View file

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