File: emojicategorybuttons.cpp

package info (click to toggle)
ktextaddons 1.9.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,856 kB
  • sloc: cpp: 62,045; ansic: 6,520; xml: 2,630; sh: 11; makefile: 7
file content (102 lines) | stat: -rw-r--r-- 3,459 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
  SPDX-FileCopyrightText: 2023-2026 Laurent Montel <montel@kde.org>

  SPDX-License-Identifier: LGPL-2.0-or-later
*/

#include "emojicategorybuttons.h"

#include "emojicategorybutton.h"
#include "emoticonunicodeutils.h"
#include <KLocalizedString>
#include <QButtonGroup>
#include <QHBoxLayout>
#include <QToolButton>
#include <QWheelEvent>
#include <TextEmoticonsCore/EmoticonCategory>
#include <TextEmoticonsCore/EmoticonUnicodeUtils>

using namespace Qt::Literals::StringLiterals;
using namespace TextEmoticonsWidgets;
EmojiCategoryButtons::EmojiCategoryButtons(QWidget *parent)
    : QWidget{parent}
    , mMainLayout(new QHBoxLayout(this))
    , mButtonGroup(new QButtonGroup(this))
{
    mMainLayout->setObjectName(u"mMainLayout"_s);
    mMainLayout->setContentsMargins({});
    mButtonGroup->setObjectName(u"mButtonGroup"_s);
}

EmojiCategoryButtons::~EmojiCategoryButtons() = default;

void EmojiCategoryButtons::wheelEvent(QWheelEvent *event)
{
    auto button = mButtonGroup->checkedButton();
    if (button) {
        const int index = mButtonGroup->buttons().indexOf(button);
        if (index != -1) {
            QAbstractButton *nextButton = nullptr;
            if (event->angleDelta().y() > 0) {
                if (index > 0) {
                    nextButton = mButtonGroup->buttons().at(index - 1);
                } else {
                    nextButton = mButtonGroup->buttons().constLast();
                }
            } else if (event->angleDelta().y() < 0) {
                if (index == (mButtonGroup->buttons().count() - 1)) {
                    nextButton = mButtonGroup->buttons().constFirst();
                } else {
                    nextButton = mButtonGroup->buttons().at(index + 1);
                }
            }
            if (nextButton) {
                nextButton->setChecked(true);
                Q_EMIT nextButton->clicked(true);
            }
        }
    }

    QWidget::wheelEvent(event);
}

void EmojiCategoryButtons::addButton(const QString &name, const QString &category, const QString &toolTip)
{
    auto button = new EmojiCategoryButton(this);
    button->setText(name);
    button->setToolTip(toolTip);
    mMainLayout->addWidget(button);
    mButtonGroup->addButton(button);
    connect(button, &QToolButton::clicked, this, [this, category](bool clicked) {
        if (clicked) {
            Q_EMIT categorySelected(category);
        }
    });
}

bool EmojiCategoryButtons::wasLoaded() const
{
    return mWasLoaded;
}

void EmojiCategoryButtons::setCategories(const QList<TextEmoticonsCore::EmoticonCategory> &categories, bool hasCustomSupport)
{
    addButton(TextEmoticonsCore::EmoticonUnicodeUtils::recentName(),
              TextEmoticonsCore::EmoticonUnicodeUtils::recentIdentifier(),
              i18nc("Previously used emojis", "History"));
    if (hasCustomSupport) {
        addButton(TextEmoticonsCore::EmoticonUnicodeUtils::customName(),
                  TextEmoticonsCore::EmoticonUnicodeUtils::customIdentifier(),
                  i18nc("'Custom' is a category of emoji", "Custom"));
    }
    for (const auto &cat : categories) {
        addButton(cat.name(), cat.category(), cat.i18nName());
    }
    // Initialize first button.
    auto button = mButtonGroup->buttons().constFirst();
    button->setChecked(true);
    Q_EMIT categorySelected(TextEmoticonsCore::EmoticonUnicodeUtils::recentIdentifier());
    mWasLoaded = true;
}

#include "moc_emojicategorybuttons.cpp"