File indexing completed on 2024-06-23 04:40:22

0001 /*
0002   SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.org>
0003 
0004   SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "emoticoncategorybuttons.h"
0008 #include "emoticoncategorybutton.h"
0009 #include "emoticonunicodeutils.h"
0010 #include <KLocalizedString>
0011 #include <QButtonGroup>
0012 #include <QHBoxLayout>
0013 #include <QToolButton>
0014 #include <QWheelEvent>
0015 #include <TextEmoticonsCore/EmoticonCategory>
0016 #include <TextEmoticonsCore/EmoticonUnicodeUtils>
0017 
0018 using namespace TextEmoticonsWidgets;
0019 EmoticonCategoryButtons::EmoticonCategoryButtons(QWidget *parent)
0020     : QWidget{parent}
0021     , mMainLayout(new QHBoxLayout(this))
0022     , mButtonGroup(new QButtonGroup(this))
0023 {
0024     mMainLayout->setObjectName(QStringLiteral("mMainLayout"));
0025     mMainLayout->setContentsMargins({});
0026     mButtonGroup->setObjectName(QStringLiteral("mButtonGroup"));
0027 }
0028 
0029 EmoticonCategoryButtons::~EmoticonCategoryButtons() = default;
0030 
0031 void EmoticonCategoryButtons::wheelEvent(QWheelEvent *event)
0032 {
0033     auto button = mButtonGroup->checkedButton();
0034     if (button) {
0035         const int index = mButtonGroup->buttons().indexOf(button);
0036         if (index != -1) {
0037             QAbstractButton *nextButton = nullptr;
0038             if (event->angleDelta().y() > 0) {
0039                 if (index > 0) {
0040                     nextButton = mButtonGroup->buttons().at(index - 1);
0041                 } else {
0042                     nextButton = mButtonGroup->buttons().constLast();
0043                 }
0044             } else if (event->angleDelta().y() < 0) {
0045                 if (index == (mButtonGroup->buttons().count() - 1)) {
0046                     nextButton = mButtonGroup->buttons().constFirst();
0047                 } else {
0048                     nextButton = mButtonGroup->buttons().at(index + 1);
0049                 }
0050             }
0051             if (nextButton) {
0052                 nextButton->setChecked(true);
0053                 nextButton->clicked(true);
0054             }
0055         }
0056     }
0057 
0058     QWidget::wheelEvent(event);
0059 }
0060 
0061 void EmoticonCategoryButtons::addButton(const QString &name, const QString &category, const QString &toolTip)
0062 {
0063     auto button = new EmoticonCategoryButton(this);
0064     button->setText(name);
0065     button->setToolTip(toolTip);
0066     mMainLayout->addWidget(button);
0067     mButtonGroup->addButton(button);
0068     connect(button, &QToolButton::clicked, this, [this, category](bool clicked) {
0069         if (clicked) {
0070             Q_EMIT categorySelected(category);
0071         }
0072     });
0073 }
0074 
0075 bool EmoticonCategoryButtons::wasLoaded() const
0076 {
0077     return mWasLoaded;
0078 }
0079 
0080 void EmoticonCategoryButtons::setCategories(const QList<TextEmoticonsCore::EmoticonCategory> &categories, bool hasCustomSupport)
0081 {
0082     addButton(QStringLiteral("⌛️"), TextEmoticonsCore::EmoticonUnicodeUtils::recentIdentifier(), i18nc("Previously used emojis", "History"));
0083     if (hasCustomSupport) {
0084         addButton(QStringLiteral("🖼️"), TextEmoticonsCore::EmoticonUnicodeUtils::customIdentifier(), i18nc("'Custom' is a category of emoji", "Custom"));
0085     }
0086     for (const auto &cat : categories) {
0087         addButton(cat.name(), cat.category(), cat.i18nName());
0088     }
0089     // Initialize first button.
0090     auto button = mButtonGroup->buttons().constFirst();
0091     button->setChecked(true);
0092     Q_EMIT categorySelected(TextEmoticonsCore::EmoticonUnicodeUtils::recentIdentifier());
0093     mWasLoaded = true;
0094 }
0095 
0096 #include "moc_emoticoncategorybuttons.cpp"