2021-03-05 02:35:15 +03:00
|
|
|
// SPDX-FileCopyrightText: 2021 Nheko Contributors
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2020-05-13 07:35:26 +03:00
|
|
|
#include "EmojiModel.h"
|
|
|
|
|
|
|
|
#include <Cache.h>
|
|
|
|
#include <MatrixClient.h>
|
|
|
|
|
2020-11-20 06:33:11 +03:00
|
|
|
#include "CompletionModelRoles.h"
|
|
|
|
|
2020-05-13 07:35:26 +03:00
|
|
|
using namespace emoji;
|
|
|
|
|
|
|
|
QHash<int, QByteArray>
|
|
|
|
EmojiModel::roleNames() const
|
|
|
|
{
|
|
|
|
static QHash<int, QByteArray> roles;
|
|
|
|
|
|
|
|
if (roles.isEmpty()) {
|
|
|
|
roles = QAbstractListModel::roleNames();
|
|
|
|
roles[static_cast<int>(EmojiModel::Roles::Unicode)] = QByteArrayLiteral("unicode");
|
|
|
|
roles[static_cast<int>(EmojiModel::Roles::ShortName)] =
|
|
|
|
QByteArrayLiteral("shortName");
|
|
|
|
roles[static_cast<int>(EmojiModel::Roles::Category)] =
|
|
|
|
QByteArrayLiteral("category");
|
|
|
|
roles[static_cast<int>(EmojiModel::Roles::Emoji)] = QByteArrayLiteral("emoji");
|
|
|
|
}
|
|
|
|
|
|
|
|
return roles;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
EmojiModel::rowCount(const QModelIndex &parent) const
|
|
|
|
{
|
|
|
|
return parent == QModelIndex() ? Provider::emoji.count() : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant
|
|
|
|
EmojiModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
|
|
|
if (hasIndex(index.row(), index.column(), index.parent())) {
|
|
|
|
switch (role) {
|
|
|
|
case Qt::DisplayRole:
|
2020-11-20 06:33:11 +03:00
|
|
|
case CompletionModel::CompletionRole:
|
2020-05-13 07:35:26 +03:00
|
|
|
case static_cast<int>(EmojiModel::Roles::Unicode):
|
2020-06-11 05:34:14 +03:00
|
|
|
return Provider::emoji[index.row()].unicode;
|
2020-05-13 07:35:26 +03:00
|
|
|
|
|
|
|
case Qt::ToolTipRole:
|
2020-11-20 06:33:11 +03:00
|
|
|
case CompletionModel::SearchRole:
|
2020-05-13 07:35:26 +03:00
|
|
|
case static_cast<int>(EmojiModel::Roles::ShortName):
|
2020-06-11 05:34:14 +03:00
|
|
|
return Provider::emoji[index.row()].shortName;
|
2020-05-13 07:35:26 +03:00
|
|
|
|
|
|
|
case static_cast<int>(EmojiModel::Roles::Category):
|
2020-06-11 05:34:14 +03:00
|
|
|
return QVariant::fromValue(Provider::emoji[index.row()].category);
|
2020-05-13 07:35:26 +03:00
|
|
|
|
|
|
|
case static_cast<int>(EmojiModel::Roles::Emoji):
|
|
|
|
return QVariant::fromValue(Provider::emoji[index.row()]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
EmojiProxyModel::EmojiProxyModel(QObject *parent)
|
|
|
|
: QSortFilterProxyModel(parent)
|
|
|
|
{}
|
|
|
|
|
|
|
|
EmojiProxyModel::~EmojiProxyModel() {}
|
|
|
|
|
2021-01-23 04:07:23 +03:00
|
|
|
Emoji::Category
|
2020-05-13 07:35:26 +03:00
|
|
|
EmojiProxyModel::category() const
|
|
|
|
{
|
|
|
|
return category_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2021-01-23 04:07:23 +03:00
|
|
|
EmojiProxyModel::setCategory(Emoji::Category cat)
|
2020-05-13 07:35:26 +03:00
|
|
|
{
|
|
|
|
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
|
2021-01-23 04:07:23 +03:00
|
|
|
if (category_ != Emoji::Category::Search) {
|
2020-06-11 05:34:14 +03:00
|
|
|
return emoji.category == category_;
|
2020-05-13 07:35:26 +03:00
|
|
|
}
|
|
|
|
|
2020-06-11 05:34:14 +03:00
|
|
|
return filterRegExp().isEmpty() ? true : filterRegExp().indexIn(emoji.shortName) != -1;
|
2020-05-13 07:35:26 +03:00
|
|
|
}
|