11#include <QApplication>
22 : QWidget(parent), themeManager_(themeManager ? themeManager : &
ThemeManager::instance())
25 setAttribute(Qt::WA_TransparentForMouseEvents,
false);
27 auto* layout =
new QHBoxLayout(
this);
28 layout->setContentsMargins(12, 10, 8, 10);
29 layout->setSpacing(10);
31 iconLabel_ =
new QLabel(
this);
32 iconLabel_->setFixedWidth(18);
33 iconLabel_->setAlignment(Qt::AlignCenter);
34 layout->addWidget(iconLabel_);
36 messageLabel_ =
new QLabel(message,
this);
37 messageLabel_->setWordWrap(
true);
38 messageLabel_->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
39 layout->addWidget(messageLabel_, 1);
41 closeBtn_ =
new QPushButton(
"\u2715",
this);
42 closeBtn_->setFixedSize(22, 22);
43 closeBtn_->setCursor(Qt::PointingHandCursor);
44 closeBtn_->setFlat(
true);
45 layout->addWidget(closeBtn_);
52 opacityEffect_ =
new QGraphicsOpacityEffect(
this);
53 opacityEffect_->setOpacity(0.0);
54 setGraphicsEffect(opacityEffect_);
59 autoCloseTimer_ =
new QTimer(
this);
60 autoCloseTimer_->setSingleShot(
true);
61 autoCloseTimer_->setInterval(
static_cast<int>(duration * 1000));
68 const auto& c = themeManager_->
colors();
78 borderColor = c.success;
79 iconColor = c.success;
83 borderColor = c.warning;
84 iconColor = c.warning;
88 borderColor = c.error;
93 borderColor = c.accent;
98 iconLabel_->setText(icon);
99 iconLabel_->setStyleSheet(
100 QString(
"color: %1; font-size: %2px; font-weight: bold; background: transparent;")
101 .arg(iconColor.name())
102 .arg(themeManager_->scaledFontSize(14)));
104 messageLabel_->setStyleSheet(QString(
"color: %1; font-size: %2px; background: transparent;")
105 .arg(c.textPrimary.name())
106 .arg(themeManager_->scaledFontSize(12)));
108 closeBtn_->setStyleSheet(
109 QString(
"QPushButton { color: %1; background: transparent; border: none; font-size: %3px; }"
110 "QPushButton:hover { color: %2; }")
111 .arg(c.textSecondary.name(), c.textPrimary.name())
112 .arg(themeManager_->scaledFontSize(11)));
114 setStyleSheet(QString(
"ToastWidget {"
115 " background-color: %1;"
116 " border: 1px solid %2;"
117 " border-left: 3px solid %3;"
118 " border-radius: 6px;"
120 .arg(c.cardBackground.name(), c.border.name(), borderColor.name()));
128 auto* fadeIn =
new QPropertyAnimation(opacityEffect_,
"opacity",
this);
129 fadeIn->setDuration(200);
130 fadeIn->setStartValue(0.0);
131 fadeIn->setEndValue(1.0);
132 fadeIn->setEasingCurve(QEasingCurve::OutCubic);
133 fadeIn->start(QAbstractAnimation::DeleteWhenStopped);
136 autoCloseTimer_->start();
146 autoCloseTimer_->stop();
148 auto* fadeOut =
new QPropertyAnimation(opacityEffect_,
"opacity",
this);
149 fadeOut->setDuration(200);
150 fadeOut->setStartValue(opacityEffect_->opacity());
151 fadeOut->setEndValue(0.0);
152 fadeOut->setEasingCurve(QEasingCurve::InCubic);
154 connect(fadeOut, &QPropertyAnimation::finished,
this, [
this]() { emit
dismissed(); });
156 fadeOut->start(QAbstractAnimation::DeleteWhenStopped);
164 : QWidget(parent), themeManager_(themeManager ? themeManager : &
ThemeManager::instance())
166 setAttribute(Qt::WA_TransparentForMouseEvents,
true);
167 setAttribute(Qt::WA_TranslucentBackground);
168 setStyleSheet(
"background: transparent;");
173 parent->installEventFilter(
this);
180 if (obj == parent() && event->type() == QEvent::Resize)
184 return QWidget::eventFilter(obj, event);
192 if (activeToasts_.size() >= kMaxToasts)
194 activeToasts_.first()->dismissAnimated();
197 auto* toast =
new ToastWidget(message, type, duration, parentWidget(), themeManager_);
198 activeToasts_.append(toast);
202 toast->showAnimated();
208 for (
auto* toast : activeToasts_)
210 toast->dismissAnimated();
214void ToastContainer::removeToast(
ToastWidget* toast)
216 activeToasts_.removeOne(toast);
217 toast->deleteLater();
221void ToastContainer::repositionToasts()
223 auto* parent = parentWidget();
227 int parentW = parent->width();
228 int parentH = parent->height();
230 int y = parentH - kMarginBottom;
233 for (qsizetype i = activeToasts_.size() - 1; i >= 0; --i)
235 auto* toast = activeToasts_[i];
237 int toastH = toast->sizeHint().height();
240 int x = parentW - kToastWidth - kMarginRight;
253 : QObject(parent), themeManager_(themeManager ? themeManager : &
ThemeManager::instance())
256 &QtNotificationService::processNotification, Qt::QueuedConnection);
263 parentWidget_ = widget;
294 static_cast<int>(notification.
type), notification.
duration);
300 container_->clearAll();
303void QtNotificationService::processNotification(
const QString& message,
int type,
float duration)
void info(const std::string &message, float duration=3.0f) override
Show an info notification.
void error(const std::string &message, float duration=5.0f) override
Show an error notification.
void clearAll() override
Clear all notifications.
QtNotificationService(QObject *parent=nullptr, ThemeManager *themeManager=nullptr)
void warning(const std::string &message, float duration=5.0f) override
Show a warning notification.
void setParentWidget(QWidget *widget)
~QtNotificationService() override
void success(const std::string &message, float duration=3.0f) override
Show a success notification.
void notificationReceived(const QString &message, int type, float duration)
void show(const core::Notification ¬ification) override
Show a notification with full control.
const ThemeColors & colors() const
Get effective colors (with accent customization applied).
void addToast(const QString &message, core::NotificationType type, float duration)
bool eventFilter(QObject *obj, QEvent *event) override
ToastContainer(QWidget *parent, ThemeManager *themeManager=nullptr)