KTextAddons

emoticoncategorybuttons.cpp
1/*
2 SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "emoticoncategorybuttons.h"
8#include "emoticoncategorybutton.h"
9#include "emoticonunicodeutils.h"
10#include <KLocalizedString>
11#include <QButtonGroup>
12#include <QHBoxLayout>
13#include <QToolButton>
14#include <QWheelEvent>
15#include <TextEmoticonsCore/EmoticonCategory>
16#include <TextEmoticonsCore/EmoticonUnicodeUtils>
17
18using namespace TextEmoticonsWidgets;
19EmoticonCategoryButtons::EmoticonCategoryButtons(QWidget *parent)
20 : QWidget{parent}
21 , mMainLayout(new QHBoxLayout(this))
22 , mButtonGroup(new QButtonGroup(this))
23{
24 mMainLayout->setObjectName(QStringLiteral("mMainLayout"));
25 mMainLayout->setContentsMargins({});
26 mButtonGroup->setObjectName(QStringLiteral("mButtonGroup"));
27}
28
29EmoticonCategoryButtons::~EmoticonCategoryButtons() = default;
30
31void EmoticonCategoryButtons::wheelEvent(QWheelEvent *event)
32{
33 auto button = mButtonGroup->checkedButton();
34 if (button) {
35 const int index = mButtonGroup->buttons().indexOf(button);
36 if (index != -1) {
37 QAbstractButton *nextButton = nullptr;
38 if (event->angleDelta().y() > 0) {
39 if (index > 0) {
40 nextButton = mButtonGroup->buttons().at(index - 1);
41 } else {
42 nextButton = mButtonGroup->buttons().constLast();
43 }
44 } else if (event->angleDelta().y() < 0) {
45 if (index == (mButtonGroup->buttons().count() - 1)) {
46 nextButton = mButtonGroup->buttons().constFirst();
47 } else {
48 nextButton = mButtonGroup->buttons().at(index + 1);
49 }
50 }
51 if (nextButton) {
52 nextButton->setChecked(true);
53 nextButton->clicked(true);
54 }
55 }
56 }
57
59}
60
61void EmoticonCategoryButtons::addButton(const QString &name, const QString &category, const QString &toolTip)
62{
63 auto button = new EmoticonCategoryButton(this);
64 button->setText(name);
65 button->setToolTip(toolTip);
66 mMainLayout->addWidget(button);
67 mButtonGroup->addButton(button);
68 connect(button, &QToolButton::clicked, this, [this, category](bool clicked) {
69 if (clicked) {
70 Q_EMIT categorySelected(category);
71 }
72 });
73}
74
75bool EmoticonCategoryButtons::wasLoaded() const
76{
77 return mWasLoaded;
78}
79
80void EmoticonCategoryButtons::setCategories(const QList<TextEmoticonsCore::EmoticonCategory> &categories, bool hasCustomSupport)
81{
82 addButton(TextEmoticonsCore::EmoticonUnicodeUtils::recentName(),
83 TextEmoticonsCore::EmoticonUnicodeUtils::recentIdentifier(),
84 i18nc("Previously used emojis", "History"));
85 if (hasCustomSupport) {
86 addButton(TextEmoticonsCore::EmoticonUnicodeUtils::customName(),
87 TextEmoticonsCore::EmoticonUnicodeUtils::customIdentifier(),
88 i18nc("'Custom' is a category of emoji", "Custom"));
89 }
90 for (const auto &cat : categories) {
91 addButton(cat.name(), cat.category(), cat.i18nName());
92 }
93 // Initialize first button.
94 auto button = mButtonGroup->buttons().constFirst();
95 button->setChecked(true);
96 Q_EMIT categorySelected(TextEmoticonsCore::EmoticonUnicodeUtils::recentIdentifier());
97 mWasLoaded = true;
98}
99
100#include "moc_emoticoncategorybuttons.cpp"
QString i18nc(const char *context, const char *text, const TYPE &arg...)
void setChecked(bool)
void clicked(bool checked)
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
void addButton(QAbstractButton *button, int id)
QList< QAbstractButton * > buttons() const const
QAbstractButton * checkedButton() const const
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void setObjectName(QAnyStringView name)
virtual bool event(QEvent *event) override
virtual void wheelEvent(QWheelEvent *event)
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.