KTextAddons

emojimodel.cpp
1/*
2 SPDX-FileCopyrightText: 2021-2025 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 case DiversityChildren:
51 return unicodeEmoti.diversityChildren();
52 }
53 } else {
54 const auto &customEmoji = mCustomEmojiList.at(index.row() - mEmoticonList.count());
55 switch (role) {
56 case AnimatedFileName:
57 if (mCustomEmojiIconManager) {
58 if (customEmoji.isAnimatedEmoji()) {
59 const QString filename = mCustomEmojiIconManager->fileName(customEmoji.identifier());
60 return filename;
61 }
62 return {};
63 } else {
64 qCWarning(TEXTEMOTICONSCORE_LOG) << "mCustomEmojiIconManager is null. It's a bug";
65 return {};
66 }
67 case Animated:
68 return customEmoji.isAnimatedEmoji();
69 case Qt::DecorationRole: {
70 if (mCustomEmojiIconManager) {
71 if (customEmoji.isAnimatedEmoji()) {
72 const QString filename = mCustomEmojiIconManager->fileName(customEmoji.identifier());
73 if (!filename.isEmpty()) {
74 const QIcon icon(filename);
75 return icon;
76 }
77 }
78 const QIcon icon = mCustomEmojiIconManager->generateIcon(customEmoji.identifier());
79 return icon;
80 } else {
81 qCWarning(TEXTEMOTICONSCORE_LOG) << "mCustomEmojiIconManager is null. It's a bug";
82 }
83 return {};
84 }
85 case Category:
86 return customEmoji.category();
87 case Order:
88 return -1;
89 case Qt::ToolTipRole:
90 case Identifier:
91 case UnicodeEmoji:
92 return customEmoji.identifier();
93 case IsCustom:
94 return true;
95 case FileName: {
96 if (mCustomEmojiIconManager) {
97 return mCustomEmojiIconManager->fileName(customEmoji.identifier());
98 }
99 return customEmoji.fileName();
100 }
101 case DiversityChildren:
102 return false;
103 }
104 }
105 return {};
106}
107
108const QList<TextEmoticonsCore::UnicodeEmoticon> &EmojiModel::emoticonList() const
109{
110 return mEmoticonList;
111}
112
113void EmojiModel::setUnicodeEmoticonList(const QList<TextEmoticonsCore::UnicodeEmoticon> &newEmoticonList)
114{
116 mEmoticonList = newEmoticonList;
118}
119
120QList<TextEmoticonsCore::CustomEmoji> EmojiModel::customEmojiList() const
121{
122 return mCustomEmojiList;
123}
124
125void EmojiModel::setCustomEmojiList(const QList<TextEmoticonsCore::CustomEmoji> &newCustomEmojiList)
126{
128 mCustomEmojiList = newCustomEmojiList;
130}
131
132TextEmoticonsCore::CustomEmojiIconManager *EmojiModel::customEmojiIconManager() const
133{
134 return mCustomEmojiIconManager;
135}
136
137void EmojiModel::setCustomEmojiIconManager(TextEmoticonsCore::CustomEmojiIconManager *newCustomEmojiIconManager)
138{
139 mCustomEmojiIconManager = newCustomEmojiIconManager;
140}
141
142void EmojiModel::setExcludeEmoticons(const QStringList &emoticons)
143{
144 bool emoticonsRemoved = false;
145 for (const auto &identifier : emoticons) {
146 auto index = std::find_if(mEmoticonList.begin(), mEmoticonList.end(), [identifier](const TextEmoticonsCore::UnicodeEmoticon &emoji) {
147 return (identifier == emoji.identifier());
148 });
149 if (index != mEmoticonList.end()) {
150 mEmoticonList.removeAll(*index);
151 emoticonsRemoved = true;
152 }
153 }
154 if (emoticonsRemoved) {
157 }
158}
159
160QHash<int, QByteArray> EmojiModel::roleNames() const
161{
162 return {{{UnicodeEmoji, QByteArrayLiteral("unicode")},
163 {Identifier, QByteArrayLiteral("identifier")},
164 {Category, QByteArrayLiteral("category")},
165 {IsCustom, QByteArrayLiteral("isCustom")},
166 {FileName, QByteArrayLiteral("fileName")},
167 {DiversityChildren, QByteArrayLiteral("diversityChildren")}}};
168}
169
170#include "moc_emojimodel.cpp"
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const=0
virtual QModelIndex parent(const QModelIndex &index) const const=0
bool isEmpty() const const
DisplayRole
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Feb 21 2025 11:46:43 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.