KTextAddons

emoticonlistview.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 "emoticonlistview.h"
8#include "emojimodel.h"
9#include "emoticonitemdelegate.h"
10
11#include <TextEmoticonsCore/EmojiModel>
12#include <TextEmoticonsCore/EmoticonUnicodeUtils>
13
14#include <KLocalizedString>
15
16#include <QKeyEvent>
17#include <QMenu>
18
19using namespace TextEmoticonsWidgets;
20EmoticonListView::EmoticonListView(QWidget *parent)
21 : QListView(parent)
22{
23 // setUniformItemSizes(true);
24 setViewMode(QListView::IconMode);
25 setDragEnabled(false);
26 setMouseTracking(true);
27 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
28 setContextMenuPolicy(Qt::DefaultContextMenu);
29 auto emoticonDelegate = new EmoticonItemDelegate(this);
30 emoticonDelegate->setObjectName(QStringLiteral("emoticonDelegate"));
31 setItemDelegate(emoticonDelegate);
32 connect(this, &QListView::clicked, this, &EmoticonListView::selectEmoji);
33}
34
35EmoticonListView::~EmoticonListView() = default;
36
37void EmoticonListView::selectEmoji(const QModelIndex &index)
38{
39 const QString emojiIdentifier = index.data(TextEmoticonsCore::EmojiModel::Identifier).toString();
40 const QString emojiStr = index.data().toString();
41 Q_EMIT emojiItemSelected(emojiStr, emojiIdentifier);
42}
43
44void EmoticonListView::keyPressEvent(QKeyEvent *event)
45{
46 const bool isControlClicked = event->modifiers() & Qt::ControlModifier;
47 int fontSize = mFontSize;
48 if (isControlClicked) {
49 if (event->key() == Qt::Key_Plus) {
50 Q_EMIT fontSizeChanged(++fontSize);
51 } else if (event->key() == Qt::Key_Minus) {
52 Q_EMIT fontSizeChanged(--fontSize);
53 }
54 } else {
55 if (event->key() == Qt::Key_Enter || event->key() == Qt::Key_Return) {
56 auto index = currentIndex();
57 if (index.isValid()) {
58 selectEmoji(index);
59 }
60 }
62 }
63}
64
65void EmoticonListView::wheelEvent(QWheelEvent *e)
66{
67 int fontSize = mFontSize;
68 if (e->modifiers() == Qt::ControlModifier) {
69 const int y = e->angleDelta().y();
70 if (y < 0) {
71 Q_EMIT fontSizeChanged(--fontSize);
72 } else if (y > 0) {
73 Q_EMIT fontSizeChanged(++fontSize);
74 } // else: y == 0 => horizontal scroll => do not handle
75 } else {
77 }
78}
79
80void EmoticonListView::setFontSize(int newFontSize)
81{
82 if (newFontSize < 10 || newFontSize > 30) {
83 return;
84 }
85 if (mFontSize != newFontSize) {
86 mFontSize = newFontSize;
87 applyFontSize();
88 }
89}
90
91void EmoticonListView::applyFontSize()
92{
93 QFont f = font();
94 f.setPointSize(mFontSize);
95 f.setFamily(TextEmoticonsCore::EmoticonUnicodeUtils::emojiFontName());
96
97 mRowSize = QFontMetrics(f).height();
98 setFont(f);
99}
100
101bool EmoticonListView::isRecentView() const
102{
103 return mIsRecentView;
104}
105
106void EmoticonListView::setIsRecentView(bool newIsRecentView)
107{
108 mIsRecentView = newIsRecentView;
109}
110
111void EmoticonListView::contextMenuEvent(QContextMenuEvent *event)
112{
113 if (mIsRecentView) {
114 if (model()->rowCount() > 0) {
115 QMenu menu(this);
116 auto clearRecent = new QAction(i18nc("@action", "Clear Recents"), &menu);
117 connect(clearRecent, &QAction::triggered, this, &EmoticonListView::clearHistory);
118 menu.addAction(clearRecent);
119 menu.exec(event->globalPos());
120 }
121 }
122}
123
124#include "moc_emoticonlistview.cpp"
QString i18nc(const char *context, const char *text, const TYPE &arg...)
void clicked(const QModelIndex &index)
QModelIndex currentIndex() const const
virtual void keyPressEvent(QKeyEvent *event) override
QAbstractItemModel * model() const const
void triggered(bool checked)
void setFamily(const QString &family)
void setPointSize(int pointSize)
int height() const const
Qt::KeyboardModifiers modifiers() const const
virtual bool event(QEvent *e) override
virtual void wheelEvent(QWheelEvent *e) override
QVariant data(int role) const const
bool isValid() const const
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
int y() const const
DefaultContextMenu
Key_Plus
ControlModifier
ScrollBarAlwaysOff
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QString toString() const const
QPoint angleDelta() const const
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.