summary refs log tree commit diff
path: root/src/emoji/EmojiModel.cpp
blob: cf535bf54598805ff6b5eddc2797770e22abe7ec (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// SPDX-FileCopyrightText: 2021 Nheko Contributors
// SPDX-FileCopyrightText: 2022 Nheko Contributors
//
// SPDX-License-Identifier: GPL-3.0-or-later

#include "EmojiModel.h"

#include <Cache.h>
#include <MatrixClient.h>

#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<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
{
    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:
        case CompletionModel::CompletionRole:
        case static_cast<int>(EmojiModel::Roles::Unicode):
            return Provider::emoji[index.row()].unicode;

        case Qt::ToolTipRole:
        case CompletionModel::SearchRole:
        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);

        case static_cast<int>(EmojiModel::Roles::Emoji):
            return QVariant::fromValue(Provider::emoji[index.row()]);
        }
    }

    return {};
}