matrixion/src/emoji/EmojiModel.cpp

74 lines
2.2 KiB
C++
Raw Normal View History

2021-03-05 02:35:15 +03:00
// SPDX-FileCopyrightText: 2021 Nheko Contributors
//
// SPDX-License-Identifier: GPL-3.0-or-later
#include "EmojiModel.h"
#include <Cache.h>
#include <MatrixClient.h>
2020-11-20 06:33:11 +03:00
#include "CompletionModelRoles.h"
using namespace emoji;
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-09-18 01:22:33 +03:00
return static_cast<int>(dist);
}
QHash<int, QByteArray>
EmojiModel::roleNames() const
{
2021-09-18 01:22:33 +03:00
static QHash<int, QByteArray> roles;
2021-09-18 01:22:33 +03:00
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");
}
2021-09-18 01:22:33 +03:00
return roles;
}
int
EmojiModel::rowCount(const QModelIndex &parent) const
{
2021-09-18 01:22:33 +03:00
return parent == QModelIndex() ? Provider::emoji.count() : 0;
}
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;
2021-09-18 01:22:33 +03:00
case Qt::ToolTipRole:
case CompletionModel::SearchRole:
case static_cast<int>(EmojiModel::Roles::ShortName):
return Provider::emoji[index.row()].shortName;
2021-09-18 01:22:33 +03:00
case static_cast<int>(EmojiModel::Roles::Category):
return QVariant::fromValue(Provider::emoji[index.row()].category);
2021-09-18 01:22:33 +03:00
case static_cast<int>(EmojiModel::Roles::Emoji):
return QVariant::fromValue(Provider::emoji[index.row()]);
}
2021-09-18 01:22:33 +03:00
}
2021-09-18 01:22:33 +03:00
return {};
}