File indexing completed on 2024-05-12 16:27:39

0001 /*
0002    SPDX-FileCopyrightText: 2020-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 #include "notificationwidget.h"
0007 #include <KNotification>
0008 #include <KNotificationReplyAction>
0009 #include <QDebug>
0010 #include <QHBoxLayout>
0011 #include <QLineEdit>
0012 #include <QPushButton>
0013 
0014 NotificationWidget::NotificationWidget(QWidget *parent)
0015     : QWidget(parent)
0016     , mLineEdit(new QLineEdit(this))
0017 {
0018     auto mainLayout = new QHBoxLayout(this);
0019     mainLayout->addWidget(mLineEdit);
0020 
0021     auto sendNotification = new QPushButton(QStringLiteral("Send"), this);
0022     mainLayout->addWidget(sendNotification);
0023 
0024     connect(sendNotification, &QPushButton::clicked, this, &NotificationWidget::slotSendNotification);
0025 }
0026 
0027 NotificationWidget::~NotificationWidget() = default;
0028 
0029 void NotificationWidget::slotSendNotification()
0030 {
0031     const QString str = mLineEdit->text();
0032     if (!str.isEmpty()) {
0033         auto notification = new KNotification(QStringLiteral("new-notification"), KNotification::CloseOnTimeout);
0034         notification->setTitle(QStringLiteral("test"));
0035         notification->setText(str);
0036 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0037         notification->setDefaultAction(QStringLiteral("Open Channel"));
0038         connect(notification, &KNotification::defaultActivated, this, []() {
0039 #else
0040         auto action = notification->addDefaultAction(QStringLiteral("Open Channel"));
0041         connect(action, &KNotificationAction::activated, this, []() {
0042 #endif
0043             qDebug() << " default Activated !!!!!!";
0044         });
0045         connect(notification, &KNotification::closed, this, []() {
0046             qDebug() << " CLOSED!!!!!!!!!!!!!!!!!!!!!!!!!";
0047         });
0048 
0049         std::unique_ptr<KNotificationReplyAction> replyAction(new KNotificationReplyAction(QStringLiteral("Reply")));
0050         replyAction->setPlaceholderText(QStringLiteral("Reply..."));
0051         QObject::connect(replyAction.get(), &KNotificationReplyAction::replied, this, [](const QString &text) {
0052             qDebug() << " reply " << text;
0053         });
0054         notification->setReplyAction(std::move(replyAction));
0055         notification->sendEvent();
0056     }
0057 }
0058 
0059 #include "moc_notificationwidget.cpp"