KTextAddons

emoticontexteditselector.cpp
1/*
2 SPDX-FileCopyrightText: 2012-2025 Laurent Montel <montel@kde.org>
3 based on code from kopete
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#include "emoticontexteditselector.h"
9#include "emoticoncategorybuttons.h"
10#include "emoticonlistview.h"
11
12#include <KLocalizedString>
13#include <QLineEdit>
14#include <QVBoxLayout>
15#include <TextEmoticonsCore/EmojiModel>
16#include <TextEmoticonsCore/EmojiModelManager>
17#include <TextEmoticonsCore/EmojiProxyModel>
18#include <TextEmoticonsCore/EmoticonUnicodeUtils>
19#include <TextEmoticonsCore/UnicodeEmoticonManager>
20
21using namespace TextEmoticonsWidgets;
22
23class EmoticonTextEditSelector::EmoticonTextEditSelectorPrivate
24{
25public:
26 EmoticonTextEditSelectorPrivate(EmoticonTextEditSelector *q)
27 : mSearchUnicodeLineEdit(new QLineEdit(q))
28 , mCategoryButtons(new EmoticonCategoryButtons(q))
29 , mEmoticonListView(new EmoticonListView(q))
30 , mEmojiProxyModel(new TextEmoticonsCore::EmojiProxyModel(q))
31 , qq(q)
32 {
33 }
34 void slotUsedIdentifierChanged(const QStringList &lst)
35 {
36 mEmojiProxyModel->setRecentEmoticons(lst);
37 }
38 void slotCategorySelected(const QString &category)
39 {
40 mSearchUnicodeLineEdit->setText({});
41 mEmojiProxyModel->setCategory(category);
42 mEmoticonListView->setIsRecentView(category == TextEmoticonsCore::EmoticonUnicodeUtils::recentIdentifier());
43 }
44
45 void slotSearchUnicode(const QString &str)
46 {
47 mEmojiProxyModel->setSearchIdentifier(str);
48 }
49
50 void slotItemSelected(const QString &str, const QString &identifier)
51 {
52 TextEmoticonsCore::EmojiModelManager::self()->addIdentifier(identifier);
53 Q_EMIT qq->insertEmoji(str);
54 Q_EMIT qq->insertEmojiIdentifier(identifier);
55 if (qq->isVisible() && qq->parentWidget() && qq->parentWidget()->inherits("QMenu")) {
56 qq->parentWidget()->close();
57 }
58 }
59 QLineEdit *const mSearchUnicodeLineEdit;
60 EmoticonCategoryButtons *const mCategoryButtons;
61 EmoticonListView *const mEmoticonListView;
62 TextEmoticonsCore::EmojiProxyModel *const mEmojiProxyModel;
63 bool mCustomEmojiSupport = false;
64 EmoticonTextEditSelector *const qq;
65};
66
67EmoticonTextEditSelector::EmoticonTextEditSelector(QWidget *parent)
68 : QWidget(parent)
69 , d(new EmoticonTextEditSelectorPrivate(this))
70{
71 const int defaultFontSize{22};
72 QFont f;
73 f.setPointSize(defaultFontSize);
74 f.setFamily(TextEmoticonsCore::EmoticonUnicodeUtils::emojiFontName());
75 d->mEmoticonListView->setFontSize(defaultFontSize);
76 d->mCategoryButtons->setFont(f);
77 auto mainLayout = new QVBoxLayout(this);
78 mainLayout->setObjectName(QStringLiteral("mainLayout"));
79 mainLayout->setSpacing(0);
80 mainLayout->setContentsMargins({});
81 d->mSearchUnicodeLineEdit->setObjectName(QStringLiteral("mSearchUnicodeLineEdit"));
82 d->mSearchUnicodeLineEdit->setClearButtonEnabled(true);
83 d->mSearchUnicodeLineEdit->setPlaceholderText(i18nc("@info:placeholder", "Search Emoticon…"));
84 mainLayout->addWidget(d->mSearchUnicodeLineEdit);
85
86 d->mCategoryButtons->setObjectName(QStringLiteral("mCategoryButtons"));
87 mainLayout->addWidget(d->mCategoryButtons);
88 d->mEmoticonListView->setObjectName(QStringLiteral("mEmoticonListView"));
89 mainLayout->addWidget(d->mEmoticonListView);
90
91 d->mEmojiProxyModel->setObjectName(QStringLiteral("mEmoticonProxyModel"));
92 d->mEmoticonListView->setModel(d->mEmojiProxyModel);
93 connect(d->mEmoticonListView, &EmoticonListView::fontSizeChanged, d->mEmoticonListView, &EmoticonListView::setFontSize);
94 connect(d->mEmoticonListView, &EmoticonListView::emojiItemSelected, this, [this](const QString &str, const QString &identifier) {
95 d->slotItemSelected(str, identifier);
96 });
97 connect(d->mCategoryButtons, &EmoticonCategoryButtons::categorySelected, this, [this](const QString &category) {
98 d->slotCategorySelected(category);
99 });
100 connect(d->mSearchUnicodeLineEdit, &QLineEdit::textChanged, this, [this](const QString &str) {
101 d->slotSearchUnicode(str);
102 });
103 connect(TextEmoticonsCore::EmojiModelManager::self(), &TextEmoticonsCore::EmojiModelManager::usedIdentifierChanged, this, [this](const QStringList &lst) {
104 d->slotUsedIdentifierChanged(lst);
105 });
106
107 connect(d->mEmoticonListView, &EmoticonListView::clearHistory, this, []() {
108 TextEmoticonsCore::EmojiModelManager::self()->setRecentIdentifier(QStringList());
109 });
110
111 const QSize popupMenuSize = QSize(400, 250);
112 setMinimumSize(popupMenuSize);
113}
114
116{
117 d->mSearchUnicodeLineEdit->setFocus();
118}
119
120EmoticonTextEditSelector::~EmoticonTextEditSelector() = default;
121
123{
124 if (!d->mCategoryButtons->wasLoaded()) {
125 TextEmoticonsCore::UnicodeEmoticonManager *emojiManager = TextEmoticonsCore::UnicodeEmoticonManager::self();
126 d->mEmojiProxyModel->setSourceModel(TextEmoticonsCore::EmojiModelManager::self()->emojiModel());
127 const QList<TextEmoticonsCore::EmoticonCategory> categories = emojiManager->categories();
128 d->mCategoryButtons->setCategories(categories, d->mCustomEmojiSupport);
129 d->mEmojiProxyModel->setRecentEmoticons(TextEmoticonsCore::EmojiModelManager::self()->recentIdentifier());
130 }
131}
132
134{
135 d->mCustomEmojiSupport = b;
136}
137
139{
140 return d->mCustomEmojiSupport;
141}
142
143#include "moc_emoticontexteditselector.cpp"
void forceLineEditFocus()
Focus the line edit for search.
bool customEmojiSupport() const
Return whether the custom emoji category is enabled.
void loadEmoticons()
Do all the magic of creating an TextEmoticonsCore::UnicodeEmoticonManager and loading the categories ...
void setCustomEmojiSupport(bool b)
Set whether the custom emoji category should be shown.
QString i18nc(const char *context, const char *text, const TYPE &arg...)
void setFamily(const QString &family)
void setPointSize(int pointSize)
void textChanged(const QString &text)
Q_EMITQ_EMIT
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Jan 24 2025 11:49:24 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.