2021-03-05 02:35:15 +03:00
|
|
|
// SPDX-FileCopyrightText: 2021 Nheko Contributors
|
2022-01-01 06:57:53 +03:00
|
|
|
// SPDX-FileCopyrightText: 2022 Nheko Contributors
|
2021-03-05 02:35:15 +03:00
|
|
|
//
|
|
|
|
// 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;
|
|
|
|
|
2021-04-18 21:21:03 +03:00
|
|
|
int
|
|
|
|
EmojiModel::categoryToIndex(int category)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
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; }));
|
2021-04-18 21:21:03 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
return static_cast<int>(dist);
|
2021-04-18 21:21:03 +03:00
|
|
|
}
|
|
|
|
|
2020-05-13 07:35:26 +03:00
|
|
|
QHash<int, QByteArray>
|
|
|
|
EmojiModel::roleNames() const
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
static QHash<int, QByteArray> roles;
|
2020-05-13 07:35:26 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (roles.isEmpty()) {
|
2022-04-22 19:49:57 +03:00
|
|
|
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::UnicodeName)] = QByteArrayLiteral("unicodeName");
|
|
|
|
roles[static_cast<int>(EmojiModel::Roles::Category)] = QByteArrayLiteral("category");
|
|
|
|
roles[static_cast<int>(EmojiModel::Roles::Emoji)] = QByteArrayLiteral("emoji");
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2020-05-13 07:35:26 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
return roles;
|
2020-05-13 07:35:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
EmojiModel::rowCount(const QModelIndex &parent) const
|
|
|
|
{
|
2022-10-02 00:57:32 +03:00
|
|
|
return parent == QModelIndex() ? (qsizetype)Provider::emoji.size() : 0;
|
2020-05-13 07:35:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QVariant
|
|
|
|
EmojiModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
if (hasIndex(index.row(), index.column(), index.parent())) {
|
|
|
|
switch (role) {
|
|
|
|
case Qt::DisplayRole:
|
|
|
|
case CompletionModel::CompletionRole:
|
|
|
|
case static_cast<int>(EmojiModel::Roles::Unicode):
|
|
|
|
return Provider::emoji[index.row()].unicode;
|
2020-05-13 07:35:26 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
case Qt::ToolTipRole:
|
2022-05-06 17:14:47 +03:00
|
|
|
return Provider::emoji[index.row()].shortName + ", " +
|
|
|
|
Provider::emoji[index.row()].unicodeName;
|
2022-04-23 04:42:29 +03:00
|
|
|
case CompletionModel::SearchRole2:
|
2022-04-22 19:49:57 +03:00
|
|
|
case static_cast<int>(EmojiModel::Roles::UnicodeName):
|
|
|
|
return Provider::emoji[index.row()].unicodeName;
|
2022-04-23 04:42:29 +03:00
|
|
|
case CompletionModel::SearchRole:
|
2021-09-18 01:22:33 +03:00
|
|
|
case static_cast<int>(EmojiModel::Roles::ShortName):
|
|
|
|
return Provider::emoji[index.row()].shortName;
|
|
|
|
case static_cast<int>(EmojiModel::Roles::Category):
|
|
|
|
return QVariant::fromValue(Provider::emoji[index.row()].category);
|
2020-05-13 07:35:26 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
case static_cast<int>(EmojiModel::Roles::Emoji):
|
|
|
|
return QVariant::fromValue(Provider::emoji[index.row()]);
|
2020-05-13 07:35:26 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2020-05-13 07:35:26 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
return {};
|
2020-05-13 07:35:26 +03:00
|
|
|
}
|