8#ifndef QT_NO_SYSTEMTRAYICON
18#include "private/qlabel_p.h"
20#if QT_CONFIG(pushbutton)
21#include "qpushbutton.h"
30#include <private/qhighdpiscaling_p.h>
31#include <qpa/qplatformscreen.h>
33using namespace std::chrono_literals;
37static QIcon messageIcon2qIcon(QSystemTrayIcon::MessageIcon icon)
39 QStyle::StandardPixmap stdIcon = QStyle::SP_CustomBase;
41 case QSystemTrayIcon::Information:
42 stdIcon = QStyle::SP_MessageBoxInformation;
44 case QSystemTrayIcon::Warning:
45 stdIcon = QStyle::SP_MessageBoxWarning;
47 case QSystemTrayIcon::Critical:
48 stdIcon = QStyle::SP_MessageBoxCritical;
50 case QSystemTrayIcon::NoIcon:
53 return QApplication::style()->standardIcon(stdIcon);
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
106
107
108
109
110
111
112
113
114
115
116
119
120
121
122
123
124
125QSystemTrayIcon::QSystemTrayIcon(QObject *parent)
126: QObject(*
new QSystemTrayIconPrivate(), parent)
131
132
133
134
135
136
137QSystemTrayIcon::QSystemTrayIcon(
const QIcon &icon, QObject *parent)
138 : QSystemTrayIcon(parent)
144
145
146QSystemTrayIcon::~QSystemTrayIcon()
148 Q_D(QSystemTrayIcon);
155
156
157
158
159
160
161
162
163
164void QSystemTrayIcon::setContextMenu(QMenu *menu)
166 Q_D(QSystemTrayIcon);
167 QMenu *oldMenu = d->menu.data();
176 if (oldMenu && !oldMenu->platformMenu())
177 QObject::disconnect(d->qpa_sys, &QPlatformSystemTrayIcon::contextMenuRequested, oldMenu,
nullptr);
178 if (menu && !menu->platformMenu()) {
179 QObject::connect(d->qpa_sys, &QPlatformSystemTrayIcon::contextMenuRequested,
181 [menu](QPoint globalNativePos,
const QPlatformScreen *platformScreen)
183 QScreen *screen = platformScreen ? platformScreen->screen() :
nullptr;
184 menu->popup(QHighDpi::fromNativePixels(globalNativePos, screen),
nullptr);
191
192
193QMenu* QSystemTrayIcon::contextMenu()
const
195 Q_D(
const QSystemTrayIcon);
202
203
204
205
206
207
208void QSystemTrayIcon::setIcon(
const QIcon &icon)
210 Q_D(QSystemTrayIcon);
215QIcon QSystemTrayIcon::icon()
const
217 Q_D(
const QSystemTrayIcon);
222
223
224
225
226
227
228void QSystemTrayIcon::setToolTip(
const QString &tooltip)
230 Q_D(QSystemTrayIcon);
231 d->toolTip = tooltip;
232 d->updateToolTip_sys();
235QString QSystemTrayIcon::toolTip()
const
237 Q_D(
const QSystemTrayIcon);
242
243
244
245
246
247
250
251
252
253
254
255
258
259
260
261
262
263QRect QSystemTrayIcon::geometry()
const
265 Q_D(
const QSystemTrayIcon);
268 return d->geometry_sys();
272
273
274
275
276
277
278void QSystemTrayIcon::setVisible(
bool visible)
280 Q_D(QSystemTrayIcon);
281 if (visible == d->visible)
283 if (Q_UNLIKELY(visible && d->icon.isNull()))
284 qWarning(
"QSystemTrayIcon::setVisible: No Icon set");
285 d->visible = visible;
292bool QSystemTrayIcon::isVisible()
const
294 Q_D(
const QSystemTrayIcon);
299
300
301bool QSystemTrayIcon::event(QEvent *e)
303 return QObject::event(e);
307
308
309
310
311
312
313
314
315
316
317
318
319
320
323
324
325
326
327
328
329
330
333
334
335
336
337
338
339
340
341
342
343
347
348
349
350
351
352
354bool QSystemTrayIcon::isSystemTrayAvailable()
356 return QSystemTrayIconPrivate::isSystemTrayAvailable_sys();
360
361
362
363
364bool QSystemTrayIcon::supportsMessages()
366 return QSystemTrayIconPrivate::supportsMessages_sys();
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391void QSystemTrayIcon::showMessage(
const QString &title,
const QString &msg,
392 QSystemTrayIcon::MessageIcon msgIcon,
int msecs)
394 showMessage(title, msg, msgIcon, std::chrono::milliseconds(msecs));
398
399
400
401
402
403
404
405
406
407
408void QSystemTrayIcon::showMessage(
const QString &title,
const QString &msg,
409 QSystemTrayIcon::MessageIcon msgIcon,
410 std::chrono::milliseconds msecs)
412 Q_D(QSystemTrayIcon);
414 d->showMessage_sys(title, msg, messageIcon2qIcon(msgIcon), msgIcon, msecs);
418
419
420
421
422
423
424
425
426
427void QSystemTrayIcon::showMessage(
const QString &title,
const QString &msg,
const QIcon &icon,
430 showMessage(title, msg, icon, std::chrono::milliseconds(msecs));
434
435
436
437
438
439
440
441
442
443void QSystemTrayIcon::showMessage(
const QString &title,
const QString &msg,
const QIcon &icon,
444 std::chrono::milliseconds msecs)
446 Q_D(QSystemTrayIcon);
448 d->showMessage_sys(title, msg, icon, QSystemTrayIcon::NoIcon, msecs);
453 Q_Q(QSystemTrayIcon);
454 emit q->activated(
static_cast<QSystemTrayIcon::ActivationReason>(reason));
460void QBalloonTip::showBalloon(
const QIcon &icon,
const QString &title,
461 const QString &message, QSystemTrayIcon *trayIcon,
462 QPoint pos,
std::chrono::milliseconds timeout,
466 if (message.isEmpty() && title.isEmpty())
497QBalloonTip::QBalloonTip(
const QIcon &icon,
const QString &title,
498 const QString &message, QSystemTrayIcon *ti)
499 : QWidget(
nullptr, Qt::ToolTip),
503 setAttribute(Qt::WA_DeleteOnClose);
504 QObject::connect(ti, SIGNAL(destroyed()),
this, SLOT(close()));
507 QLabel *titleLabel =
new QLabel;
508 titleLabel->installEventFilter(
this);
509 titleLabel->setText(title);
510 QFont f = titleLabel->font();
512 titleLabel->setFont(f);
513 titleLabel->setTextFormat(Qt::PlainText);
516 const int iconSize = 18;
517 const int closeButtonSize = 15;
519#if QT_CONFIG(pushbutton)
520 QPushButton *closeButton =
new QPushButton;
521 closeButton->setIcon(style()->standardIcon(QStyle::SP_TitleBarCloseButton));
522 closeButton->setIconSize(QSize(closeButtonSize, closeButtonSize));
523 closeButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
524 closeButton->setFixedSize(closeButtonSize, closeButtonSize);
525 QObject::connect(closeButton, SIGNAL(clicked()),
this, SLOT(close()));
527 Q_UNUSED(closeButtonSize);
531 QLabel *msgLabel =
new QLabel;
532 msgLabel->installEventFilter(
this);
533 msgLabel->setText(message);
534 msgLabel->setTextFormat(Qt::PlainText);
535 msgLabel->setAlignment(Qt::AlignTop | Qt::AlignLeft);
538 int limit = QWidgetPrivate::availableScreenGeometry(msgLabel).width() / 3;
539 if (msgLabel->sizeHint().width() > limit) {
540 msgLabel->setWordWrap(
true);
541 if (msgLabel->sizeHint().width() > limit) {
542 msgLabel->d_func()->ensureTextControl();
543 if (QWidgetTextControl *control = msgLabel->d_func()->control) {
544 QTextOption opt = control->document()->defaultTextOption();
545 opt.setWrapMode(QTextOption::WrapAnywhere);
546 control->document()->setDefaultTextOption(opt);
551 msgLabel->setFixedSize(limit, msgLabel->heightForWidth(limit));
555 QGridLayout *layout =
new QGridLayout;
557 if (!icon.isNull()) {
558 QLabel *iconLabel =
new QLabel;
559 iconLabel->setPixmap(icon.pixmap(QSize(iconSize, iconSize), devicePixelRatio()));
560 iconLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
561 iconLabel->setMargin(2);
562 layout->addWidget(iconLabel, 0, 0);
563 layout->addWidget(titleLabel, 0, 1);
565 layout->addWidget(titleLabel, 0, 0, 1, 2);
569#if QT_CONFIG(pushbutton)
570 layout->addWidget(closeButton, 0, 2);
574 layout->addWidget(msgLabel, 1, 0, 1, 3);
576 layout->setSizeConstraint(QLayout::SetFixedSize);
577 layout->setContentsMargins(3, 3, 3, 3);
580 QPalette pal = palette();
581 pal.setColor(QPalette::Window, QColor(0xff, 0xff, 0xe1));
582 pal.setColor(QPalette::WindowText, Qt::black);
593 QPainter painter(
this);
594 painter.drawPixmap(rect(), pixmap);
599 QWidget::resizeEvent(ev);
604 this->showArrow = showArrow;
605 QScreen *screen = QGuiApplication::screenAt(pos);
607 screen = QGuiApplication::primaryScreen();
608 QRect screenRect = screen->geometry();
609 QSize sh = sizeHint();
610 const int border = 1;
611 const int ah = 18, ao = 18, aw = 18, rc = 7;
612 bool arrowAtTop = (pos.y() + sh.height() + ah < screenRect.height());
613 bool arrowAtLeft = (pos.x() + sh.width() - ao < screenRect.width());
614 setContentsMargins(border + 3, border + (arrowAtTop ? ah : 0) + 2, border + 3, border + (arrowAtTop ? 0 : ah) + 2);
619 QSize sz = sizeHint();
623 mb = sz.height() - ah - 1;
628 mb = sz.height() - 1;
632 path.moveTo(ml + rc, mt);
633 if (arrowAtTop && arrowAtLeft) {
635 path.lineTo(ml + ao, mt);
636 path.lineTo(ml + ao, mt - ah);
637 path.lineTo(ml + ao + aw, mt);
639 move(qMax(pos.x() - ao, screenRect.left() + 2), pos.y());
640 }
else if (arrowAtTop && !arrowAtLeft) {
642 path.lineTo(mr - ao - aw, mt);
643 path.lineTo(mr - ao, mt - ah);
644 path.lineTo(mr - ao, mt);
646 move(qMin(pos.x() - sh.width() + ao, screenRect.right() - sh.width() - 2), pos.y());
648 path.lineTo(mr - rc, mt);
649 path.arcTo(QRect(mr - rc*2, mt, rc*2, rc*2), 90, -90);
650 path.lineTo(mr, mb - rc);
651 path.arcTo(QRect(mr - rc*2, mb - rc*2, rc*2, rc*2), 0, -90);
652 if (!arrowAtTop && !arrowAtLeft) {
654 path.lineTo(mr - ao, mb);
655 path.lineTo(mr - ao, mb + ah);
656 path.lineTo(mr - ao - aw, mb);
658 move(qMin(pos.x() - sh.width() + ao, screenRect.right() - sh.width() - 2),
659 pos.y() - sh.height());
660 }
else if (!arrowAtTop && arrowAtLeft) {
662 path.lineTo(ao + aw, mb);
663 path.lineTo(ao, mb + ah);
666 move(qMax(pos.x() - ao, screenRect.x() + 2), pos.y() - sh.height());
668 path.lineTo(ml + rc, mb);
669 path.arcTo(QRect(ml, mb - rc*2, rc*2, rc*2), -90, -90);
670 path.lineTo(ml, mt + rc);
671 path.arcTo(QRect(ml, mt, rc*2, rc*2), 180, -90);
674 QBitmap bitmap = QBitmap(sizeHint());
675 bitmap.fill(Qt::color0);
677 painter1.setPen(QPen(Qt::color1, border));
678 painter1.setBrush(QBrush(Qt::color1));
679 painter1.drawPath(path);
683 pixmap = QPixmap(sz);
685 painter2.setPen(QPen(palette().color(QPalette::Window).darker(160), border));
686 painter2.setBrush(palette().color(QPalette::Window));
687 painter2.drawPath(path);
690 timer.start(timeout,
this);
697 if (e->button() == Qt::LeftButton)
698 emit trayIcon->messageClicked();
703 if (e->id() == timer.id()) {
709 QWidget::timerEvent(e);
716 QObject::connect(qpa_sys, SIGNAL(activated(QPlatformSystemTrayIcon::ActivationReason)),
717 q_func(), SLOT(_q_emitActivated(QPlatformSystemTrayIcon::ActivationReason)));
718 QObject::connect(qpa_sys, &QPlatformSystemTrayIcon::messageClicked,
719 q_func(), &QSystemTrayIcon::messageClicked);
727 QObject::disconnect(qpa_sys, SIGNAL(activated(QPlatformSystemTrayIcon::ActivationReason)),
728 q_func(), SLOT(_q_emitActivated(QPlatformSystemTrayIcon::ActivationReason)));
729 QObject::disconnect(qpa_sys, &QPlatformSystemTrayIcon::messageClicked,
730 q_func(), &QSystemTrayIcon::messageClicked);
737 if (menu->platformMenu())
742 const auto actions = menu->actions();
743 for (QAction *action : actions) {
745 addPlatformMenu(action->menu());
750 QPlatformMenu *platformMenu = qpa_sys->createMenu();
752 menu->setPlatformMenu(platformMenu);
762#include "moc_qsystemtrayicon.cpp"
763#include "moc_qsystemtrayicon_p.cpp"
static void hideBalloon()
static bool isBalloonVisible()
void timerEvent(QTimerEvent *e) override
This event handler can be reimplemented in a subclass to receive timer events for the object.
void mousePressEvent(QMouseEvent *e) override
This event handler, for event event, can be reimplemented in a subclass to receive mouse press events...
static void updateBalloonPosition(QPoint pos)
QPainter(QPaintDevice *)
Constructs a painter that begins painting the paint device immediately.
\inmodule QtCore\reentrant
void _q_emitActivated(QPlatformSystemTrayIcon::ActivationReason reason)
QPlatformSystemTrayIcon * qpa_sys
static QBalloonTip * theSolitaryBalloonTip