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 }
49 } else {
50 const auto &customEmoji = mCustomEmojiList.at(index.row() - mEmoticonList.count());
51 switch (role) {
52 case AnimatedFileName:
53 if (mCustomEmojiIconManager) {
54 if (customEmoji.isAnimatedEmoji()) {
55 const QString filename = mCustomEmojiIconManager->fileName(customEmoji.identifier());
56 return filename;
57 }
58 return {};
59 } else {
60 qCWarning(TEXTEMOTICONSCORE_LOG) << "mCustomEmojiIconManager is null. It's a bug";
61 return {};
62 }
63 case Animated:
64 return customEmoji.isAnimatedEmoji();
65 case Qt::DecorationRole: {
66 if (mCustomEmojiIconManager) {
67 if (customEmoji.isAnimatedEmoji()) {
68 const QString filename = mCustomEmojiIconManager->fileName(customEmoji.identifier());
69 if (!filename.isEmpty()) {
70 const QIcon icon(filename);
71 return icon;
72 }
73 }
74 const QIcon icon = mCustomEmojiIconManager->generateIcon(customEmoji.identifier());
75 return icon;
76 } else {
77 qCWarning(TEXTEMOTICONSCORE_LOG) << "mCustomEmojiIconManager is null. It's a bug";
78 }
79 return {};
80 }
81 case Category:
82 return customEmoji.category();
83 case Order:
84 return -1;
85 case Qt::ToolTipRole:
86 case Identifier:
87 case UnicodeEmoji:
88 return customEmoji.identifier();
89 }
90 }
91 return {};
92}
93
94const QList<TextEmoticonsCore::UnicodeEmoticon> &EmojiModel::emoticonList() const
95{
96 return mEmoticonList;
97}
98
99void EmojiModel::setUnicodeEmoticonList(const QList<TextEmoticonsCore::UnicodeEmoticon> &newEmoticonList)
100{
102 mEmoticonList = newEmoticonList;
104}
105
106QList<TextEmoticonsCore::CustomEmoji> EmojiModel::customEmojiList() const
107{
108 return mCustomEmojiList;
109}
110
111void EmojiModel::setCustomEmojiList(const QList<TextEmoticonsCore::CustomEmoji> &newCustomEmojiList)
112{
114 mCustomEmojiList = newCustomEmojiList;
116}
117
118TextEmoticonsCore::CustomEmojiIconManager *EmojiModel::customEmojiIconManager() const
119{
120 return mCustomEmojiIconManager;
121}
122
123void EmojiModel::setCustomEmojiIconManager(TextEmoticonsCore::CustomEmojiIconManager *newCustomEmojiIconManager)
124{
125 mCustomEmojiIconManager = newCustomEmojiIconManager;
126}
127
128void EmojiModel::setExcludeEmoticons(const QStringList &emoticons)
129{
130 bool emoticonsRemoved = false;
131 for (const auto &identifier : emoticons) {
132 auto index = std::find_if(mEmoticonList.begin(), mEmoticonList.end(), [identifier](const TextEmoticonsCore::UnicodeEmoticon &emoji) {
133 return (identifier == emoji.identifier());
134 });
135 if (index != mEmoticonList.end()) {
136 mEmoticonList.removeAll(*index);
137 emoticonsRemoved = true;
138 }
139 }
140 if (emoticonsRemoved) {
143 }
144}
145
146QHash<int, QByteArray> EmojiModel::roleNames() const
147{
148 return {{{UnicodeEmoji, QByteArrayLiteral("unicode")}, {Identifier, QByteArrayLiteral("identifier")}, {Category, QByteArrayLiteral("category")}}};
149}
150
151#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 Mon Nov 18 2024 12:19:49 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.