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 #include "emojiproxymodel.h" 0007 #include "emojimodel.h" 0008 #include "emoticonunicodeutils.h" 0009 0010 using namespace TextEmoticonsCore; 0011 0012 class EmojiProxyModel::EmojiProxyModelPrivate 0013 { 0014 public: 0015 EmojiProxyModelPrivate(EmojiProxyModel *qq) 0016 : q(qq) 0017 { 0018 } 0019 void clearSearch() 0020 { 0021 q->setSearchIdentifier(QString()); 0022 } 0023 QString mCategory; 0024 QStringList mRecentEmoticons; 0025 QString mSearchIdentifier; 0026 EmojiProxyModel *const q; 0027 }; 0028 0029 EmojiProxyModel::EmojiProxyModel(QObject *parent) 0030 : QSortFilterProxyModel(parent) 0031 , d(new EmojiProxyModel::EmojiProxyModelPrivate(this)) 0032 { 0033 setFilterCaseSensitivity(Qt::CaseInsensitive); 0034 setFilterRole(EmojiModel::Identifier); 0035 sort(0); 0036 } 0037 0038 EmojiProxyModel::~EmojiProxyModel() = default; 0039 0040 bool EmojiProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const 0041 { 0042 if (d->mCategory.isEmpty()) { 0043 return QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent); 0044 return false; 0045 } 0046 if (!d->mSearchIdentifier.isEmpty()) { 0047 const QModelIndex sourceIndex = sourceModel()->index(source_row, 0, source_parent); 0048 const QString identifier = sourceIndex.data(EmojiModel::Identifier).toString(); 0049 if (identifier.contains(d->mSearchIdentifier)) { 0050 return true; 0051 } 0052 return false; 0053 } 0054 if (d->mCategory == TextEmoticonsCore::EmoticonUnicodeUtils::recentIdentifier()) { 0055 const QModelIndex sourceIndex = sourceModel()->index(source_row, 0, source_parent); 0056 const QString identifier = sourceIndex.data(EmojiModel::Identifier).toString(); 0057 if (d->mRecentEmoticons.contains(identifier)) { 0058 return true; 0059 } 0060 } else { 0061 const QModelIndex sourceIndex = sourceModel()->index(source_row, 0, source_parent); 0062 const auto category = sourceIndex.data(EmojiModel::Category).toString(); 0063 if (d->mCategory == category) { 0064 return true; 0065 } 0066 } 0067 return false; 0068 } 0069 0070 QString EmojiProxyModel::searchIdentifier() const 0071 { 0072 return d->mSearchIdentifier; 0073 } 0074 0075 void EmojiProxyModel::setSearchIdentifier(const QString &newSearchIdentifier) 0076 { 0077 if (d->mSearchIdentifier != newSearchIdentifier) { 0078 d->mSearchIdentifier = newSearchIdentifier; 0079 invalidateFilter(); 0080 } 0081 } 0082 0083 QStringList EmojiProxyModel::recentEmoticons() const 0084 { 0085 return d->mRecentEmoticons; 0086 } 0087 0088 void EmojiProxyModel::setRecentEmoticons(const QStringList &newRecentEmoticons) 0089 { 0090 if (d->mRecentEmoticons != newRecentEmoticons) { 0091 d->mRecentEmoticons = newRecentEmoticons; 0092 if (TextEmoticonsCore::EmoticonUnicodeUtils::recentIdentifier() == d->mCategory) { 0093 invalidate(); 0094 } 0095 } 0096 } 0097 0098 QString EmojiProxyModel::category() const 0099 { 0100 return d->mCategory; 0101 } 0102 0103 void EmojiProxyModel::setCategory(const QString &newCategorie) 0104 { 0105 if (d->mCategory != newCategorie) { 0106 d->mCategory = newCategorie; 0107 if (!d->mSearchIdentifier.isEmpty()) { 0108 d->clearSearch(); 0109 } else { 0110 invalidateFilter(); 0111 } 0112 if ((TextEmoticonsCore::EmoticonUnicodeUtils::recentIdentifier() == d->mCategory) 0113 || (TextEmoticonsCore::EmoticonUnicodeUtils::customIdentifier() == d->mCategory)) { 0114 // Make sure that we reorder recent/custom category 0115 invalidate(); 0116 } 0117 } 0118 } 0119 0120 bool EmojiProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const 0121 { 0122 if (TextEmoticonsCore::EmoticonUnicodeUtils::recentIdentifier() == d->mCategory) { 0123 const QString leftIdentifier = sourceModel()->data(left, EmojiModel::Identifier).toString(); 0124 const QString rightIdentifier = sourceModel()->data(right, EmojiModel::Identifier).toString(); 0125 const int positionIdentifierLeft = d->mRecentEmoticons.indexOf(leftIdentifier); 0126 const int positionIdentifierRight = d->mRecentEmoticons.indexOf(rightIdentifier); 0127 // qDebug() << " leftIdentifier " << leftIdentifier << " rightIdentifier " << rightIdentifier << " positionIdentifierLeft " << 0128 // positionIdentifierLeft 0129 // << " positionIdentifierRight " << positionIdentifierRight; 0130 // qDebug() << "mRecentEmoticons " << mRecentEmoticons; 0131 return positionIdentifierLeft < positionIdentifierRight; 0132 } else { 0133 const int leftOrder = sourceModel()->data(left, EmojiModel::Order).toInt(); 0134 const int rightOrder = sourceModel()->data(right, EmojiModel::Order).toInt(); 0135 0136 return leftOrder < rightOrder; 0137 } 0138 } 0139 0140 #include "moc_emojiproxymodel.cpp"