|
| 1 | + |
| 2 | +/****************************************************************************** |
| 3 | + * MODULE : qt_floating_toast.cpp |
| 4 | + * DESCRIPTION: Floating toast implementation |
| 5 | + * COPYRIGHT : (C) 2026 Yuki Lu |
| 6 | + ******************************************************************************* |
| 7 | + * This software falls under the GNU general public license version 3 or later. |
| 8 | + * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE |
| 9 | + * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>. |
| 10 | + ******************************************************************************/ |
| 11 | + |
| 12 | +#include "qt_floating_toast.hpp" |
| 13 | +#include "qt_dpi_utils.hpp" |
| 14 | + |
| 15 | +#include <QApplication> |
| 16 | +#include <QHBoxLayout> |
| 17 | +#include <QLabel> |
| 18 | +#include <QPainter> |
| 19 | +#include <QPropertyAnimation> |
| 20 | +#include <QScreen> |
| 21 | +#include <QTimer> |
| 22 | + |
| 23 | +QtFloatingToast::QtFloatingToast (QWidget* parent) |
| 24 | + : QWidget (parent, |
| 25 | + Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint | Qt::Tool) { |
| 26 | + setAttribute (Qt::WA_TranslucentBackground); |
| 27 | + |
| 28 | + label_= new QLabel (this); |
| 29 | + label_->setAlignment (Qt::AlignCenter); |
| 30 | + label_->setWordWrap (true); |
| 31 | + label_->setStyleSheet ("QLabel { color: #ffffff; }"); |
| 32 | + label_->setFont (DpiUtils::scaledFont (label_->font (), 14)); |
| 33 | + |
| 34 | + layout_= new QHBoxLayout (this); |
| 35 | + layout_->setContentsMargins (0, 0, 0, 0); |
| 36 | + layout_->addWidget (label_, 0, Qt::AlignCenter); |
| 37 | + |
| 38 | + hideTimer_= new QTimer (this); |
| 39 | + hideTimer_->setSingleShot (true); |
| 40 | + connect (hideTimer_, &QTimer::timeout, this, &QtFloatingToast::startFadeOut); |
| 41 | + |
| 42 | + fadeAnimation_= new QPropertyAnimation (this, "windowOpacity"); |
| 43 | + fadeAnimation_->setDuration (200); |
| 44 | +} |
| 45 | + |
| 46 | +QtFloatingToast::~QtFloatingToast ()= default; |
| 47 | + |
| 48 | +void |
| 49 | +QtFloatingToast::showAbove (QWidget* anchorWidget, const QString& message, |
| 50 | + int durationMs, Type type) { |
| 51 | + if (!anchorWidget) return; |
| 52 | + |
| 53 | + type_= type; |
| 54 | + label_->setText (message); |
| 55 | + label_->adjustSize (); |
| 56 | + |
| 57 | + int padX= DpiUtils::scaled (20); |
| 58 | + int padY= DpiUtils::scaled (10); |
| 59 | + layout_->setContentsMargins (padX, padY, padX, padY); |
| 60 | + |
| 61 | + adjustSize (); |
| 62 | + updatePosition (anchorWidget); |
| 63 | + |
| 64 | + setWindowOpacity (0.0); |
| 65 | + show (); |
| 66 | + raise (); |
| 67 | + startFadeIn (); |
| 68 | + |
| 69 | + hideTimer_->start (durationMs); |
| 70 | +} |
| 71 | + |
| 72 | +void |
| 73 | +QtFloatingToast::showToast (QWidget* anchorWidget, const QString& message, |
| 74 | + int durationMs, Type type) { |
| 75 | + if (!anchorWidget) return; |
| 76 | + auto* toast= new QtFloatingToast (anchorWidget->window ()); |
| 77 | + toast->showAbove (anchorWidget, message, durationMs, type); |
| 78 | +} |
| 79 | + |
| 80 | +void |
| 81 | +QtFloatingToast::updatePosition (QWidget* anchorWidget) { |
| 82 | + if (!anchorWidget) return; |
| 83 | + QWidget* window= anchorWidget->window (); |
| 84 | + QRect geo = window->geometry (); |
| 85 | + int x = geo.x () + (geo.width () - width ()) / 2; |
| 86 | + int y = geo.y () + (geo.height () - height ()) / 8; |
| 87 | + move (x, y); |
| 88 | +} |
| 89 | + |
| 90 | +void |
| 91 | +QtFloatingToast::startFadeIn () { |
| 92 | + fadeAnimation_->stop (); |
| 93 | + fadeAnimation_->setStartValue (0.0); |
| 94 | + fadeAnimation_->setEndValue (1.0); |
| 95 | + fadeAnimation_->start (); |
| 96 | +} |
| 97 | + |
| 98 | +void |
| 99 | +QtFloatingToast::startFadeOut () { |
| 100 | + fadeAnimation_->stop (); |
| 101 | + fadeAnimation_->setStartValue (1.0); |
| 102 | + fadeAnimation_->setEndValue (0.0); |
| 103 | + if (fadeConnection_) disconnect (fadeConnection_); |
| 104 | + fadeConnection_= connect (fadeAnimation_, &QPropertyAnimation::finished, this, |
| 105 | + &QObject::deleteLater); |
| 106 | + fadeAnimation_->start (); |
| 107 | +} |
| 108 | + |
| 109 | +void |
| 110 | +QtFloatingToast::paintEvent (QPaintEvent* event) { |
| 111 | + QPainter painter (this); |
| 112 | + painter.setRenderHint (QPainter::Antialiasing); |
| 113 | + |
| 114 | + QRectF rect = this->rect ().adjusted (1, 1, -1, -1); |
| 115 | + int radius= DpiUtils::scaled (8); |
| 116 | + |
| 117 | + painter.setPen (Qt::NoPen); |
| 118 | + QColor bg; |
| 119 | + switch (type_) { |
| 120 | + case Success: |
| 121 | + bg= QColor (46, 125, 50, 220); |
| 122 | + break; |
| 123 | + case Warning: |
| 124 | + bg= QColor (245, 124, 0, 220); |
| 125 | + break; |
| 126 | + case Error: |
| 127 | + bg= QColor (198, 40, 40, 220); |
| 128 | + break; |
| 129 | + default: |
| 130 | + bg= QColor (50, 50, 50, 220); |
| 131 | + } |
| 132 | + painter.setBrush (bg); |
| 133 | + painter.drawRoundedRect (rect, radius, radius); |
| 134 | +} |
0 commit comments