KTextAddons

emojimodel.cpp
1/*
2 SPDX-FileCopyrightText: 2021-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "emojimodel.h"
8#include "textemoticonscore_debug.h"
9#include <TextEmoticonsCore/CustomEmojiIconManager>
10using namespace TextEmoticonsCore;
11EmojiModel::EmojiModel(QObject *parent)
12 : QAbstractListModel(parent)
13{
14}
15
16EmojiModel::~EmojiModel() = default;
17
18int EmojiModel::rowCount(const QModelIndex &parent) const
19{
20 if (parent.isValid()) {
21 return 0; // flat model
22 }
23 return mEmoticonList.count() + mCustomEmojiList.count();
24}
25
26QVariant EmojiModel::data(const QModelIndex &index, int role) const
27{
28 if (index.row() < 0 || index.row() >= (mEmoticonList.count() + mCustomEmojiList.count())) {
29 return {};
30 }
31 if (index.row() < mEmoticonList.count()) {
32 const auto &unicodeEmoti = mEmoticonList.at(index.row());
33 switch (role) {
34 case AnimatedFileName:
35 return {};
36 case Animated:
37 return false;
38 case Qt::DisplayRole:
39 case UnicodeEmoji:
40 return unicodeEmoti.unicode();
41 case Category:
42 return unicodeEmoti.category();
43 case Order:
44 return unicodeEmoti.order();
45 case Identifier:
46 case Qt::ToolTipRole:
47 return unicodeEmoti.identifier();
48 case IsCustom:
49 return false;
50 }
51 } else {
52 const auto &customEmoji = mCustomEmojiList.at(index.row() - mEmoticonList.count());
53 switch (role) {
54 case AnimatedFileName:
55 if (mCustomEmojiIconManager) {
56 if (customEmoji.isAnimatedEmoji()) {
57 const QString filename = mCustomEmojiIconManager->fileName(customEmoji.identifier());
58 return filename;
59 }
60 return {};
61 } else {
62 qCWarning(TEXTEMOTICONSCORE_LOG) << "mCustomEmojiIconManager is null. It's a bug";
63 return {};
64 }
65 case Animated:
66 return customEmoji.isAnimatedEmoji();
67 case Qt::DecorationRole: {
68 if (mCustomEmojiIconManager) {
69 if (customEmoji.isAnimatedEmoji()) {
70 const QString filename = mCustomEmojiIconManager->fileName(customEmoji.identifier());
71 if (!filename.isEmpty()) {
72 const QIcon icon(filename);
73 return icon;
74 }
75 }
76 const QIcon icon = mCustomEmojiIconManager->generateIcon(customEmoji.identifier());
77 return icon;
78 } else {
79 qCWarning(TEXTEMOTICONSCORE_LOG) << "mCustomEmojiIconManager is null. It's a bug";
80 }
81 return {};
82 }
83 case Category:
84 return customEmoji.category();
85 case Order:
86 return -1;
87 case Qt::ToolTipRole:
88 case Identifier:
89 case UnicodeEmoji:
90 return customEmoji.identifier();
91 case IsCustom:
92 return true;
93 case FileName: {
94 if (mCustomEmojiIconManager) {
95 return mCustomEmojiIconManager->fileName(customEmoji.identifier());
96 }
97 return customEmoji.fileName();
98 }
99 }
100 }
101 return {};
102}
103
104const QList<TextEmoticonsCore::UnicodeEmoticon> &EmojiModel::emoticonList() const
105{
106 return mEmoticonList;
107}
108
109void EmojiModel::setUnicodeEmoticonList(const QList<TextEmoticonsCore::UnicodeEmoticon> &newEmoticonList)
110{
112 mEmoticonList = newEmoticonList;
114}
115
116QList<TextEmoticonsCore::CustomEmoji> EmojiModel::customEmojiList() const
117{
118 return mCustomEmojiList;
119}
120
121void EmojiModel::setCustomEmojiList(const QList<TextEmoticonsCore::CustomEmoji> &newCustomEmojiList)
122{
124 mCustomEmojiList = newCustomEmojiList;
126}
127
128TextEmoticonsCore::CustomEmojiIconManager *EmojiModel::customEmojiIconManager() const
129{
130 return mCustomEmojiIconManager;
131}
132
133void EmojiModel::setCustomEmojiIconManager(TextEmoticonsCore::CustomEmojiIconManager *newCustomEmojiIconManager)
134{
135 mCustomEmojiIconManager = newCustomEmojiIconManager;
136}
137
138void EmojiModel::setExcludeEmoticons(const QStringList &emoticons)
139{
140 bool emoticonsRemoved = false;
141 for (const auto &identifier : emoticons) {
142 auto index = std::find_if(mEmoticonList.begin(), mEmoticonList.end(), [identifier](const TextEmoticonsCore::UnicodeEmoticon &emoji) {
143 return (identifier == emoji.identifier());
144 });
145 if (index != mEmoticonList.end()) {
146 mEmoticonList.removeAll(*index);
147 emoticonsRemoved = true;
148 }
149 }
150 if (emoticonsRemoved) {
153 }
154}
155
156QHash<int, QByteArray> EmojiModel::roleNames() const
157{
158 return {{
159 {UnicodeEmoji, QByteArrayLiteral("unicode")},
160 {Identifier, QByteArrayLiteral("identifier")},
161 {Category, QByteArrayLiteral("category")},
162 {IsCustom, QByteArrayLiteral("isCustom")},
163 {FileName, QByteArrayLiteral("fileName")},
164 }};
165}
166
167#include "moc_emojimodel.cpp"
The UnicodeEmoticon class.
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
const_reference at(qsizetype i) const const
iterator begin()
qsizetype count() const const
iterator end()
qsizetype removeAll(const AT &t)
int row() const const
QObject * parent() const const
bool isEmpty() const const
DisplayRole
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:56:02 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.