aboutsummaryrefslogtreecommitdiff
path: root/src/notificationpopup.h
diff options
context:
space:
mode:
authorLibravatar keshavbhatt <keshavnrj@gmail.com>2021-04-05 02:11:25 +0530
committerLibravatar keshavbhatt <keshavnrj@gmail.com>2021-04-05 02:11:25 +0530
commit9ea334d08f42f3c362e86499dc3c0ed658bb428c (patch)
tree85eb15c519bcff8a5e3c4f8406f068b7c412b789 /src/notificationpopup.h
parente79b447b31ad9ab1ed42fd232f8789fad38d780b (diff)
downloadwhatsie-9ea334d08f42f3c362e86499dc3c0ed658bb428c.tar.gz
whatsie-9ea334d08f42f3c362e86499dc3c0ed658bb428c.zip
src init
Diffstat (limited to 'src/notificationpopup.h')
-rw-r--r--src/notificationpopup.h144
1 files changed, 144 insertions, 0 deletions
diff --git a/src/notificationpopup.h b/src/notificationpopup.h
new file mode 100644
index 0000000..2246129
--- /dev/null
+++ b/src/notificationpopup.h
@@ -0,0 +1,144 @@
+#ifndef NOTIFICATIONPOPUP_H
+#define NOTIFICATIONPOPUP_H
+
+#pragma once
+
+#include <QHBoxLayout>
+#include <QLabel>
+#include <QMouseEvent>
+#include <QPushButton>
+#include <QSpacerItem>
+#include <QTimer>
+#include <QVBoxLayout>
+#include <QWebEngineNotification>
+#include <QPropertyAnimation>
+#include <QApplication>
+#include <QDesktopWidget>
+#include <QDebug>
+#include "widgets/scrolltext/scrolltext.h"
+
+#include <memory>
+
+class NotificationPopup : public QWidget
+{
+ Q_OBJECT
+
+ QLabel m_icon, m_title; ScrollText 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(QString title,QString message,const QPixmap image)
+ {
+ m_title.setText("<b>" + title + "</b>");
+ m_message.setText(message);
+ m_icon.setPixmap(image.scaledToHeight(m_icon.height(),Qt::SmoothTransformation));
+
+ this->adjustSize();
+ qApp->processEvents();
+
+ int x = QApplication::desktop()->geometry().width()-(this->width()+10);
+ int y = 40;
+
+
+ QTimer::singleShot(10000,this,[=](){
+ onClosed();
+ });
+
+ QPropertyAnimation *a = new QPropertyAnimation(this,"pos");
+ a->setDuration(200);
+ a->setStartValue(QApplication::desktop()->mapToGlobal(QPoint(x+this->width(),y)));
+ a->setEndValue(QApplication::desktop()->mapToGlobal(QPoint(x,y)));
+ a->setEasingCurve(QEasingCurve::Linear);
+ a->start(QPropertyAnimation::DeleteWhenStopped);
+
+ this->show();
+
+ }
+
+ 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::fromImage(notification->icon()).scaledToHeight(m_icon.height()));
+
+ //show();
+ notification->show();
+
+ connect(notification.get(), &QWebEngineNotification::closed, this, &NotificationPopup::onClosed);
+ QTimer::singleShot(10000, notification.get(), [&] () { onClosed(); });
+
+ this->adjustSize();
+ qApp->processEvents();
+
+ int x = QApplication::desktop()->geometry().width()-(this->width()+10);
+ int y = 40;
+
+ QPropertyAnimation *a = new QPropertyAnimation(this,"pos");
+ a->setDuration(200);
+ a->setStartValue(QApplication::desktop()->mapToGlobal(QPoint(x+this->width(),y)));
+ a->setEndValue(QApplication::desktop()->mapToGlobal(QPoint(x,y)));
+ a->setEasingCurve(QEasingCurve::Linear);
+ a->start(QPropertyAnimation::DeleteWhenStopped);
+ this->show();
+ }
+
+protected slots:
+ void onClosed()
+ {
+ hide();
+ if(notification){
+ notification->close();
+ notification.reset();
+ }else{
+ this->deleteLater();
+ }
+ }
+
+protected:
+ void mouseReleaseEvent(QMouseEvent *event) override
+ {
+ QWidget::mouseReleaseEvent(event);
+ if (event->button() == Qt::LeftButton) {
+ qDebug()<<"noti clicked";
+ emit notification_clicked();
+ if(notification )
+ notification->click();
+ onClosed();
+ }
+ }
+signals:
+ void notification_clicked();
+};
+
+#endif // NOTIFICATIONPOPUP_H