File indexing completed on 2024-05-12 04:58:10

0001 /* ============================================================
0002 * Falkon - Qt web browser
0003 * Copyright (C) 2010-2018 David Rosca <nowrep@gmail.com>
0004 *
0005 * This program is free software: you can redistribute it and/or modify
0006 * it under the terms of the GNU General Public License as published by
0007 * the Free Software Foundation, either version 3 of the License, or
0008 * (at your option) any later version.
0009 *
0010 * This program is distributed in the hope that it will be useful,
0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013 * GNU General Public License for more details.
0014 *
0015 * You should have received a copy of the GNU General Public License
0016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0017 * ============================================================ */
0018 #include "desktopnotification.h"
0019 #include "ui_desktopnotification.h"
0020 
0021 #include <QTimer>
0022 #include <QMouseEvent>
0023 
0024 DesktopNotification::DesktopNotification(bool setPosition)
0025     : QWidget(nullptr)
0026     , ui(new Ui::DesktopNotification)
0027     , m_settingPosition(setPosition)
0028     , m_timeout(6000)
0029     , m_timer(new QTimer(this))
0030 {
0031     ui->setupUi(this);
0032     setAttribute(Qt::WA_DeleteOnClose);
0033     setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint | Qt::X11BypassWindowManagerHint);
0034 
0035     m_timer->setSingleShot(true);
0036     connect(m_timer, &QTimer::timeout, this, &QWidget::close);
0037 
0038     if (m_settingPosition) {
0039         setCursor(Qt::OpenHandCursor);
0040     }
0041 }
0042 
0043 void DesktopNotification::show()
0044 {
0045     ui->icon->setPixmap(m_icon);
0046     ui->icon->setVisible(!m_icon.isNull());
0047     ui->heading->setText(m_heading);
0048     ui->text->setText(m_text);
0049 
0050     if (!m_settingPosition) {
0051         m_timer->setInterval(m_timeout);
0052         m_timer->start();
0053     }
0054 
0055     QWidget::show();
0056 }
0057 
0058 void DesktopNotification::mousePressEvent(QMouseEvent* e)
0059 {
0060     if (!m_settingPosition) {
0061         close();
0062         return;
0063     }
0064 
0065     if (e->buttons() == Qt::LeftButton) {
0066         m_dragPosition = e->globalPosition().toPoint() - frameGeometry().topLeft();
0067         e->accept();
0068     }
0069 }
0070 
0071 void DesktopNotification::mouseMoveEvent(QMouseEvent* e)
0072 {
0073     if (e->buttons() & Qt::LeftButton) {
0074         move(e->globalPosition().toPoint() - m_dragPosition);
0075         e->accept();
0076     }
0077 }
0078 
0079 DesktopNotification::~DesktopNotification()
0080 {
0081     delete ui;
0082 }