KTextAddons

emojiproxymodel.cpp
1/*
2 SPDX-FileCopyrightText: 2021-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6#include "emojiproxymodel.h"
7#include "emojimodel.h"
8#include "emoticonunicodeutils.h"
9
10using namespace TextEmoticonsCore;
11
12class EmojiProxyModel::EmojiProxyModelPrivate
13{
14public:
15 EmojiProxyModelPrivate(EmojiProxyModel *qq)
16 : q(qq)
17 {
18 }
19 void clearSearch()
20 {
21 q->setSearchIdentifier(QString());
22 }
23 QString mCategory;
24 QStringList mRecentEmoticons;
25 QString mSearchIdentifier;
26 EmojiProxyModel *const q;
27};
28
29EmojiProxyModel::EmojiProxyModel(QObject *parent)
30 : QSortFilterProxyModel(parent)
31 , d(new EmojiProxyModel::EmojiProxyModelPrivate(this))
32{
33 setFilterCaseSensitivity(Qt::CaseInsensitive);
34 setFilterRole(EmojiModel::Identifier);
35 sort(0);
36}
37
38EmojiProxyModel::~EmojiProxyModel() = default;
39
40bool EmojiProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
41{
42 if (d->mCategory.isEmpty()) {
43 return QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent);
44 return false;
45 }
46 if (!d->mSearchIdentifier.isEmpty()) {
47 const QModelIndex sourceIndex = sourceModel()->index(source_row, 0, source_parent);
48 const QString identifier = sourceIndex.data(EmojiModel::Identifier).toString();
49 if (identifier.contains(d->mSearchIdentifier)) {
50 return true;
51 }
52 return false;
53 }
54 if (d->mCategory == TextEmoticonsCore::EmoticonUnicodeUtils::recentIdentifier()) {
55 const QModelIndex sourceIndex = sourceModel()->index(source_row, 0, source_parent);
56 const QString identifier = sourceIndex.data(EmojiModel::Identifier).toString();
57 if (d->mRecentEmoticons.contains(identifier)) {
58 return true;
59 }
60 } else {
61 const QModelIndex sourceIndex = sourceModel()->index(source_row, 0, source_parent);
62 const auto category = sourceIndex.data(EmojiModel::Category).toString();
63 if (d->mCategory == category) {
64 return true;
65 }
66 }
67 return false;
68}
69
70QString EmojiProxyModel::searchIdentifier() const
71{
72 return d->mSearchIdentifier;
73}
74
75void EmojiProxyModel::setSearchIdentifier(const QString &newSearchIdentifier)
76{
77 if (d->mSearchIdentifier != newSearchIdentifier) {
78 d->mSearchIdentifier = newSearchIdentifier;
80 }
81}
82
83QStringList EmojiProxyModel::recentEmoticons() const
84{
85 return d->mRecentEmoticons;
86}
87
88void EmojiProxyModel::setRecentEmoticons(const QStringList &newRecentEmoticons)
89{
90 if (d->mRecentEmoticons != newRecentEmoticons) {
91 d->mRecentEmoticons = newRecentEmoticons;
92 if (TextEmoticonsCore::EmoticonUnicodeUtils::recentIdentifier() == d->mCategory) {
93 invalidate();
94 }
95 }
96}
97
98QString EmojiProxyModel::category() const
99{
100 return d->mCategory;
101}
102
103void EmojiProxyModel::setCategory(const QString &newCategorie)
104{
105 if (d->mCategory != newCategorie) {
106 d->mCategory = newCategorie;
107 if (!d->mSearchIdentifier.isEmpty()) {
108 d->clearSearch();
109 } else {
111 }
112 if ((TextEmoticonsCore::EmoticonUnicodeUtils::recentIdentifier() == d->mCategory)
113 || (TextEmoticonsCore::EmoticonUnicodeUtils::customIdentifier() == d->mCategory)) {
114 // Make sure that we reorder recent/custom category
115 invalidate();
116 }
117 Q_EMIT categoryChanged();
118 }
119}
120
121bool EmojiProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
122{
123 if (TextEmoticonsCore::EmoticonUnicodeUtils::recentIdentifier() == d->mCategory) {
124 const QString leftIdentifier = sourceModel()->data(left, EmojiModel::Identifier).toString();
125 const QString rightIdentifier = sourceModel()->data(right, EmojiModel::Identifier).toString();
126 const int positionIdentifierLeft = d->mRecentEmoticons.indexOf(leftIdentifier);
127 const int positionIdentifierRight = d->mRecentEmoticons.indexOf(rightIdentifier);
128 // qDebug() << " leftIdentifier " << leftIdentifier << " rightIdentifier " << rightIdentifier << " positionIdentifierLeft " <<
129 // positionIdentifierLeft
130 // << " positionIdentifierRight " << positionIdentifierRight;
131 // qDebug() << "mRecentEmoticons " << mRecentEmoticons;
132 return positionIdentifierLeft < positionIdentifierRight;
133 } else {
134 const int leftOrder = sourceModel()->data(left, EmojiModel::Order).toInt();
135 const int rightOrder = sourceModel()->data(right, EmojiModel::Order).toInt();
136
137 return leftOrder < rightOrder;
138 }
139}
140
141#include "moc_emojiproxymodel.cpp"
The EmojiProxyModel class.
QVariant data(int role) const const
Q_EMITQ_EMIT
virtual bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const const
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
QChar * data()
CaseInsensitive
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:29:59 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.