diff options
author | Michal Klocek <michal.klocek@qt.io> | 2022-12-19 15:50:13 +0100 |
---|---|---|
committer | Michal Klocek <michal.klocek@qt.io> | 2022-12-22 14:30:46 +0100 |
commit | 9907b4cbadfb3104f5617724f10e19e910c61e2e (patch) | |
tree | eebb0c173b42627310d3f1c5704b9eb971cd0003 /examples/webenginewidgets/push-notifications/notificationpopup.h | |
parent | ef9de55d78b5acd3b12d4a967c5822008179ac3b (diff) |
Update push example after setPushServiceEndpoint change
We have now extra call to enable push messaging, therefore
drop usage of simple browser in example and use
the same code as in Notification Example but add new settings
and fix profile to be not off-the-record.
Task-number: QTBUG-107442
Pick-to: 6.5
Change-Id: I3069c4cf2b6a6ce864c1e4c578ab03adb67984e4
Reviewed-by: Leena Miettinen <riitta-leena.miettinen@qt.io>
Diffstat (limited to 'examples/webenginewidgets/push-notifications/notificationpopup.h')
-rw-r--r-- | examples/webenginewidgets/push-notifications/notificationpopup.h | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/examples/webenginewidgets/push-notifications/notificationpopup.h b/examples/webenginewidgets/push-notifications/notificationpopup.h new file mode 100644 index 000000000..cf53ded45 --- /dev/null +++ b/examples/webenginewidgets/push-notifications/notificationpopup.h @@ -0,0 +1,91 @@ +// Copyright (C) 2019 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#pragma once + +#include <QHBoxLayout> +#include <QLabel> +#include <QMouseEvent> +#include <QPushButton> +#include <QSpacerItem> +#include <QTimer> +#include <QVBoxLayout> +#include <QWebEngineNotification> + +#include <memory> + +class NotificationPopup : public QWidget +{ + Q_OBJECT + + QLabel m_icon, m_title, m_message; + std::unique_ptr<QWebEngineNotification> notification; + +public: + NotificationPopup(QWidget *parent) : QWidget(parent) + { + setWindowFlags(Qt::ToolTip); + auto rootLayout = new QHBoxLayout(this); + + rootLayout->addWidget(&m_icon); + + auto bodyLayout = new QVBoxLayout; + rootLayout->addLayout(bodyLayout); + + auto titleLayout = new QHBoxLayout; + bodyLayout->addLayout(titleLayout); + + titleLayout->addWidget(&m_title); + titleLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::Expanding)); + + auto close = new QPushButton(tr("Close")); + titleLayout->addWidget(close); + connect(close, &QPushButton::clicked, this, &NotificationPopup::onClosed); + + bodyLayout->addWidget(&m_message); + adjustSize(); + } + + void present(std::unique_ptr<QWebEngineNotification> &newNotification) + { + if (notification) { + notification->close(); + notification.reset(); + } + + notification.swap(newNotification); + + m_title.setText("<b>" + notification->title() + "</b>"); + m_message.setText(notification->message()); + m_icon.setPixmap(QPixmap(":/icon.png").scaledToHeight(m_icon.height())); + + show(); + notification->show(); + + connect(notification.get(), &QWebEngineNotification::closed, this, + &NotificationPopup::onClosed); + QTimer::singleShot(10000, notification.get(), [&]() { onClosed(); }); + + // position our popup in the right corner of its parent widget + move(parentWidget()->mapToGlobal(parentWidget()->rect().bottomRight() + - QPoint(width() + 10, height() + 10))); + } + +protected slots: + void onClosed() + { + hide(); + notification->close(); + notification.reset(); + } + +protected: + void mouseReleaseEvent(QMouseEvent *event) override + { + QWidget::mouseReleaseEvent(event); + if (notification && event->button() == Qt::LeftButton) { + notification->click(); + onClosed(); + } + } +}; |