summary refs log tree commit diff
path: root/src/emoji
diff options
context:
space:
mode:
authorNicolas Werner <nicolas.werner@hotmail.de>2021-04-18 20:21:03 +0200
committerNicolas Werner <nicolas.werner@hotmail.de>2021-04-18 21:53:39 +0200
commitc2e625756ce74b2691ec10fc7c67d0920b6749ac (patch)
tree89b92cf4c1b043788fc3a30e2a2af6036c79f179 /src/emoji
parentPrevent warning on empty user requests (diff)
downloadnheko-c2e625756ce74b2691ec10fc7c67d0920b6749ac.tar.xz
Use one CompletionProxy for everything including EmojiPicker
Diffstat (limited to 'src/emoji')
-rw-r--r--src/emoji/EmojiModel.cpp70
-rw-r--r--src/emoji/EmojiModel.h32
-rw-r--r--src/emoji/EmojiSearchModel.h50
3 files changed, 16 insertions, 136 deletions
diff --git a/src/emoji/EmojiModel.cpp b/src/emoji/EmojiModel.cpp

index 70b85934..66e7aeda 100644 --- a/src/emoji/EmojiModel.cpp +++ b/src/emoji/EmojiModel.cpp
@@ -11,6 +11,20 @@ using namespace emoji; +int +EmojiModel::categoryToIndex(int category) +{ + auto dist = std::distance(Provider::emoji.begin(), + std::lower_bound(Provider::emoji.begin(), + Provider::emoji.end(), + static_cast<Emoji::Category>(category), + [](const struct Emoji &e, Emoji::Category c) { + return e.category < c; + })); + + return static_cast<int>(dist); +} + QHash<int, QByteArray> EmojiModel::roleNames() const { @@ -60,59 +74,3 @@ EmojiModel::data(const QModelIndex &index, int role) const return {}; } - -EmojiProxyModel::EmojiProxyModel(QObject *parent) - : QSortFilterProxyModel(parent) -{} - -EmojiProxyModel::~EmojiProxyModel() {} - -Emoji::Category -EmojiProxyModel::category() const -{ - return category_; -} - -void -EmojiProxyModel::setCategory(Emoji::Category cat) -{ - if (category_ == cat) { - return; - } - - category_ = cat; - emit categoryChanged(); - - invalidateFilter(); -} - -QString -EmojiProxyModel::filter() const -{ - return filterRegExp().pattern(); -} - -void -EmojiProxyModel::setFilter(const QString &filter) -{ - if (filterRegExp().pattern() == filter) { - return; - } - - setFilterWildcard(filter); - emit filterChanged(); -} - -bool -EmojiProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const -{ - const QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent); - const Emoji emoji = index.data(static_cast<int>(EmojiModel::Roles::Emoji)).value<Emoji>(); - - // TODO: Add favorites / recently used - if (category_ != Emoji::Category::Search) { - return emoji.category == category_; - } - - return filterRegExp().isEmpty() ? true : filterRegExp().indexIn(emoji.shortName) != -1; -} diff --git a/src/emoji/EmojiModel.h b/src/emoji/EmojiModel.h
index 1a8bf029..679563f1 100644 --- a/src/emoji/EmojiModel.h +++ b/src/emoji/EmojiModel.h
@@ -30,38 +30,10 @@ public: using QAbstractListModel::QAbstractListModel; + Q_INVOKABLE int categoryToIndex(int category); + QHash<int, QByteArray> roleNames() const override; int rowCount(const QModelIndex &parent = QModelIndex()) const override; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; }; - -class EmojiProxyModel : public QSortFilterProxyModel -{ - Q_OBJECT - - Q_PROPERTY( - emoji::Emoji::Category category READ category WRITE setCategory NOTIFY categoryChanged) - Q_PROPERTY(QString filter READ filter WRITE setFilter NOTIFY filterChanged) - -public: - explicit EmojiProxyModel(QObject *parent = nullptr); - ~EmojiProxyModel() override; - - Emoji::Category category() const; - void setCategory(Emoji::Category cat); - - QString filter() const; - void setFilter(const QString &filter); - -signals: - void categoryChanged(); - void filterChanged(); - -protected: - bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override; - -private: - Emoji::Category category_ = Emoji::Category::Search; - emoji::Provider emoji_provider_; -}; } diff --git a/src/emoji/EmojiSearchModel.h b/src/emoji/EmojiSearchModel.h deleted file mode 100644
index 64af83dd..00000000 --- a/src/emoji/EmojiSearchModel.h +++ /dev/null
@@ -1,50 +0,0 @@ -// SPDX-FileCopyrightText: 2021 Nheko Contributors -// -// SPDX-License-Identifier: GPL-3.0-or-later - -#pragma once - -#include "EmojiModel.h" - -#include <CompletionModelRoles.h> -#include <QDebug> -#include <QEvent> -#include <QSortFilterProxyModel> - -namespace emoji { - -// Map emoji data to searchable data -class EmojiSearchModel : public QSortFilterProxyModel -{ -public: - EmojiSearchModel(QObject *parent = nullptr) - : QSortFilterProxyModel(parent) - { - setSourceModel(new EmojiModel(this)); - } - QVariant data(const QModelIndex &index, int role = Qt::UserRole + 1) const override - { - 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 - { - return shortname.replace(" ", "-").replace(":", "-").replace("--", "-").toLower(); - } -}; -}