File indexing completed on 2024-12-22 04:28:16

0001 /*
0002    SPDX-FileCopyrightText: 2021-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "emojimodel.h"
0008 #include "textemoticonscore_debug.h"
0009 #include <TextEmoticonsCore/CustomEmojiIconManager>
0010 using namespace TextEmoticonsCore;
0011 EmojiModel::EmojiModel(QObject *parent)
0012     : QAbstractListModel(parent)
0013 {
0014 }
0015 
0016 EmojiModel::~EmojiModel() = default;
0017 
0018 int EmojiModel::rowCount(const QModelIndex &parent) const
0019 {
0020     if (parent.isValid()) {
0021         return 0; // flat model
0022     }
0023     return mEmoticonList.count() + mCustomEmojiList.count();
0024 }
0025 
0026 QVariant EmojiModel::data(const QModelIndex &index, int role) const
0027 {
0028     if (index.row() < 0 || index.row() >= (mEmoticonList.count() + mCustomEmojiList.count())) {
0029         return {};
0030     }
0031     if (index.row() < mEmoticonList.count()) {
0032         const auto &unicodeEmoti = mEmoticonList.at(index.row());
0033         switch (role) {
0034         case AnimatedFileName:
0035             return {};
0036         case Animated:
0037             return false;
0038         case Qt::DisplayRole:
0039         case UnicodeEmoji:
0040             return unicodeEmoti.unicode();
0041         case Category:
0042             return unicodeEmoti.category();
0043         case Order:
0044             return unicodeEmoti.order();
0045         case Identifier:
0046         case Qt::ToolTipRole:
0047             return unicodeEmoti.identifier();
0048         }
0049     } else {
0050         const auto &customEmoji = mCustomEmojiList.at(index.row() - mEmoticonList.count());
0051         switch (role) {
0052         case AnimatedFileName:
0053             if (mCustomEmojiIconManager) {
0054                 if (customEmoji.isAnimatedEmoji()) {
0055                     const QString filename = mCustomEmojiIconManager->fileName(customEmoji.identifier());
0056                     return filename;
0057                 }
0058                 return {};
0059             } else {
0060                 qCWarning(TEXTEMOTICONSCORE_LOG) << "mCustomEmojiIconManager is null. It's a bug";
0061                 return {};
0062             }
0063         case Animated:
0064             return customEmoji.isAnimatedEmoji();
0065         case Qt::DecorationRole: {
0066             if (mCustomEmojiIconManager) {
0067                 if (customEmoji.isAnimatedEmoji()) {
0068                     const QString filename = mCustomEmojiIconManager->fileName(customEmoji.identifier());
0069                     if (!filename.isEmpty()) {
0070                         const QIcon icon(filename);
0071                         return icon;
0072                     }
0073                 }
0074                 const QIcon icon = mCustomEmojiIconManager->generateIcon(customEmoji.identifier());
0075                 return icon;
0076             } else {
0077                 qCWarning(TEXTEMOTICONSCORE_LOG) << "mCustomEmojiIconManager is null. It's a bug";
0078             }
0079             return {};
0080         }
0081         case Category:
0082             return customEmoji.category();
0083         case Order:
0084             return -1;
0085         case Qt::ToolTipRole:
0086         case Identifier:
0087         case UnicodeEmoji:
0088             return customEmoji.identifier();
0089         }
0090     }
0091     return {};
0092 }
0093 
0094 const QList<TextEmoticonsCore::UnicodeEmoticon> &EmojiModel::emoticonList() const
0095 {
0096     return mEmoticonList;
0097 }
0098 
0099 void EmojiModel::setUnicodeEmoticonList(const QList<TextEmoticonsCore::UnicodeEmoticon> &newEmoticonList)
0100 {
0101     beginResetModel();
0102     mEmoticonList = newEmoticonList;
0103     endResetModel();
0104 }
0105 
0106 QList<TextEmoticonsCore::CustomEmoji> EmojiModel::customEmojiList() const
0107 {
0108     return mCustomEmojiList;
0109 }
0110 
0111 void EmojiModel::setCustomEmojiList(const QList<TextEmoticonsCore::CustomEmoji> &newCustomEmojiList)
0112 {
0113     beginResetModel();
0114     mCustomEmojiList = newCustomEmojiList;
0115     endResetModel();
0116 }
0117 
0118 TextEmoticonsCore::CustomEmojiIconManager *EmojiModel::customEmojiIconManager() const
0119 {
0120     return mCustomEmojiIconManager;
0121 }
0122 
0123 void EmojiModel::setCustomEmojiIconManager(TextEmoticonsCore::CustomEmojiIconManager *newCustomEmojiIconManager)
0124 {
0125     mCustomEmojiIconManager = newCustomEmojiIconManager;
0126 }
0127 
0128 void EmojiModel::setExcludeEmoticons(const QStringList &emoticons)
0129 {
0130     bool emoticonsRemoved = false;
0131     for (const auto &identifier : emoticons) {
0132         auto index = std::find_if(mEmoticonList.begin(), mEmoticonList.end(), [identifier](const TextEmoticonsCore::UnicodeEmoticon &emoji) {
0133             return (identifier == emoji.identifier());
0134         });
0135         if (index != mEmoticonList.end()) {
0136             mEmoticonList.removeAll(*index);
0137             emoticonsRemoved = true;
0138         }
0139     }
0140     if (emoticonsRemoved) {
0141         beginResetModel();
0142         endResetModel();
0143     }
0144 }
0145 
0146 #include "moc_emojimodel.cpp"