diff options
author | 2022-02-28 18:37:59 +0530 | |
---|---|---|
committer | 2022-02-28 18:37:59 +0530 | |
commit | 21940ee605e23bed4fcfd944f49a39f5d164d70b (patch) | |
tree | 4be188caab95acdbc37aade2d680481e31724c9f | |
parent | ed88d77268967fd3f4db9106a37739cc2937cf04 (diff) | |
download | whatsie-21940ee605e23bed4fcfd944f49a39f5d164d70b.tar.gz whatsie-21940ee605e23bed4fcfd944f49a39f5d164d70b.zip |
style: code refactor3.0
-rw-r--r-- | src/notificationpopup.h | 193 |
1 files changed, 95 insertions, 98 deletions
diff --git a/src/notificationpopup.h b/src/notificationpopup.h index aed8656..ebb587d 100644 --- a/src/notificationpopup.h +++ b/src/notificationpopup.h @@ -3,148 +3,145 @@ #pragma once +#include "widgets/scrolltext/scrolltext.h" +#include <QApplication> +#include <QDebug> +#include <QDesktopWidget> #include <QHBoxLayout> #include <QLabel> #include <QMouseEvent> +#include <QPropertyAnimation> #include <QPushButton> +#include <QSettings> #include <QSpacerItem> #include <QTimer> #include <QVBoxLayout> #include <QWebEngineNotification> -#include <QPropertyAnimation> -#include <QApplication> -#include <QDesktopWidget> -#include <QDebug> -#include "widgets/scrolltext/scrolltext.h" -#include <QSettings> #include <memory> -class NotificationPopup : public QWidget -{ - Q_OBJECT +class NotificationPopup : public QWidget { + Q_OBJECT - QLabel m_icon, m_title; ScrollText m_message; - std::unique_ptr<QWebEngineNotification> notification; - QSettings settings; + QLabel m_icon, m_title; + ScrollText m_message; + std::unique_ptr<QWebEngineNotification> notification; + QSettings settings; public: - NotificationPopup(QWidget *parent) : QWidget(parent) - { - setWindowFlags(Qt::ToolTip); - auto rootLayout = new QHBoxLayout(this); + NotificationPopup(QWidget *parent) : QWidget(parent) { + setWindowFlags(Qt::ToolTip); + auto rootLayout = new QHBoxLayout(this); - rootLayout->addWidget(&m_icon); + rootLayout->addWidget(&m_icon); - auto bodyLayout = new QVBoxLayout; - rootLayout->addLayout(bodyLayout); + auto bodyLayout = new QVBoxLayout; + rootLayout->addLayout(bodyLayout); - auto titleLayout = new QHBoxLayout; - bodyLayout->addLayout(titleLayout); + auto titleLayout = new QHBoxLayout; + bodyLayout->addLayout(titleLayout); - titleLayout->addWidget(&m_title); - titleLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::Expanding)); + 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); + 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)); + bodyLayout->addWidget(&m_message); + adjustSize(); + } - this->adjustSize(); - qApp->processEvents(); + 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)); - int x = QApplication::desktop()->geometry().width()-(this->width()+10); - int y = 40; + this->adjustSize(); + qApp->processEvents(); + int x = QApplication::desktop()->geometry().width() - (this->width() + 10); + int y = 40; - this->update(); + this->update(); - QTimer::singleShot(settings.value("notificationTimeOut",9000).toInt(),this,[=](){ - onClosed(); - }); + QTimer::singleShot(settings.value("notificationTimeOut", 9000).toInt(), + 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); + 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(); + this->show(); + } + void present(std::unique_ptr<QWebEngineNotification> &newNotification) { + if (notification) { + notification->close(); + notification.reset(); } - void present(std::unique_ptr<QWebEngineNotification> &newNotification) - { - if (notification) { - notification->close(); - notification.reset(); - } + notification.swap(newNotification); - 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())); - m_title.setText("<b>" + notification->title() + "</b>"); - m_message.setText(notification->message()); - m_icon.setPixmap(QPixmap::fromImage(notification->icon()).scaledToHeight(m_icon.height())); + notification->show(); - //show(); - notification->show(); + connect(notification.get(), &QWebEngineNotification::closed, this, + &NotificationPopup::onClosed); + QTimer::singleShot(settings.value("notificationTimeOut", 9000).toInt(), + notification.get(), [&]() { onClosed(); }); - connect(notification.get(), &QWebEngineNotification::closed, this, &NotificationPopup::onClosed); - QTimer::singleShot(settings.value("notificationTimeOut",9000).toInt(), notification.get(), [&] () { onClosed(); }); + this->adjustSize(); + qApp->processEvents(); - this->adjustSize(); - qApp->processEvents(); + int x = QApplication::desktop()->geometry().width() - (this->width() + 10); + int y = 40; - int x = QApplication::desktop()->geometry().width()-(this->width()+10); - int y = 40; + this->update(); - this->update(); - - 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(); - } + 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(); - } + 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(); - } + 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(); + void notification_clicked(); }; #endif // NOTIFICATIONPOPUP_H |