// SPDX-FileCopyrightText: 2021 Nheko Contributors // // SPDX-License-Identifier: GPL-3.0-or-later #include "EmojiModel.h" #include #include #include "CompletionModelRoles.h" 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(category), [](const struct Emoji &e, Emoji::Category c) { return e.category < c; })); return static_cast(dist); } QHash EmojiModel::roleNames() const { static QHash roles; if (roles.isEmpty()) { roles = QAbstractListModel::roleNames(); roles[static_cast(EmojiModel::Roles::Unicode)] = QByteArrayLiteral("unicode"); roles[static_cast(EmojiModel::Roles::ShortName)] = QByteArrayLiteral("shortName"); roles[static_cast(EmojiModel::Roles::Category)] = QByteArrayLiteral("category"); roles[static_cast(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: case CompletionModel::CompletionRole: case static_cast(EmojiModel::Roles::Unicode): return Provider::emoji[index.row()].unicode; case Qt::ToolTipRole: case CompletionModel::SearchRole: case static_cast(EmojiModel::Roles::ShortName): return Provider::emoji[index.row()].shortName; case static_cast(EmojiModel::Roles::Category): return QVariant::fromValue(Provider::emoji[index.row()].category); case static_cast(EmojiModel::Roles::Emoji): return QVariant::fromValue(Provider::emoji[index.row()]); } } return {}; }