Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qsystemtrayicon_x11.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qtwidgetsglobal.h"
5#if QT_CONFIG(label)
6#include "qlabel.h"
7#endif
8#include "qpainter.h"
9#include "qpixmap.h"
10#include "qbitmap.h"
11#include "qevent.h"
12#include "qapplication.h"
13#include "qlist.h"
14#if QT_CONFIG(menu)
15#include "qmenu.h"
16#endif
17#include "qtimer.h"
18#include "qsystemtrayicon_p.h"
19#include "qpaintengine.h"
20#include <qwindow.h>
21#include <qguiapplication.h>
22#include <qscreen.h>
23#include <qbackingstore.h>
24#include <qpa/qplatformnativeinterface.h>
25#include <qpa/qplatformsystemtrayicon.h>
26#include <qpa/qplatformtheme.h>
27#include <private/qguiapplication_p.h>
28#include <qdebug.h>
29
30#ifndef QT_NO_SYSTEMTRAYICON
32
33using namespace Qt::StringLiterals;
34
35static inline unsigned long locateSystemTray()
36{
37 return (unsigned long)QGuiApplication::platformNativeInterface()->nativeResourceForScreen(QByteArrayLiteral("traywindow"), QGuiApplication::primaryScreen());
38}
39
40// System tray widget. Could be replaced by a QWindow with
41// a backing store if it did not need tooltip handling.
43{
45public:
47
48 inline void updateIcon() { update(); }
49 inline QSystemTrayIcon *systemTrayIcon() const { return q; }
50
51 QRect globalGeometry() const;
52
53protected:
54 virtual void mousePressEvent(QMouseEvent *ev) override;
55 virtual void mouseDoubleClickEvent(QMouseEvent *ev) override;
56 virtual bool event(QEvent *) override;
57 virtual void paintEvent(QPaintEvent *) override;
58 virtual void resizeEvent(QResizeEvent *) override;
59 virtual void moveEvent(QMoveEvent *) override;
60
61private:
63};
64
66 : QWidget(nullptr, Qt::Window | Qt::FramelessWindowHint | Qt::BypassWindowManagerHint)
67 , q(qIn)
68{
69 setObjectName(QStringLiteral("QSystemTrayIconSys"));
70#if QT_CONFIG(tooltip)
71 setToolTip(q->toolTip());
72#endif
75 const QSize size(22, 22); // Gnome, standard size
79 setMouseTracking(true);
80}
81
86
88{
89 QPoint globalPos = ev->globalPosition().toPoint();
90#ifndef QT_NO_CONTEXTMENU
91 if (ev->button() == Qt::RightButton && q->contextMenu())
92 q->contextMenu()->popup(globalPos);
93#else
94 Q_UNUSED(globalPos);
95#endif // QT_NO_CONTEXTMENU
96
98 emit q->messageClicked();
100 }
101
102 if (ev->button() == Qt::LeftButton)
103 emit q->activated(QSystemTrayIcon::Trigger);
104 else if (ev->button() == Qt::RightButton)
105 emit q->activated(QSystemTrayIcon::Context);
106 else if (ev->button() == Qt::MiddleButton)
108}
109
115
117{
118 switch (e->type()) {
119 case QEvent::ToolTip:
121 break;
122#if QT_CONFIG(wheelevent)
123 case QEvent::Wheel:
125#endif
126 default:
127 break;
128 }
129 return QWidget::event(e);
130}
131
133{
134 const QRect rect(QPoint(0, 0), geometry().size());
135 QPainter painter(this);
136
137 q->icon().paint(&painter, rect);
138}
139
146
155
157{
159public:
161 : QObject(trayIcon)
162 , mTrayIcon(trayIcon)
163 {
164 // This code uses string-based syntax because we want to connect to a signal
165 // which is defined in XCB plugin - QXcbNativeInterface::systemTrayWindowChanged().
166 connect(qGuiApp->platformNativeInterface(), SIGNAL(systemTrayWindowChanged(QScreen*)),
167 this, SLOT(systemTrayWindowChanged(QScreen*)));
168 }
169
170private slots:
171 void systemTrayWindowChanged(QScreen *)
172 {
173 auto icon = static_cast<QSystemTrayIconPrivate *>(QObjectPrivate::get(mTrayIcon));
174 icon->destroyIcon();
175 if (icon->visible && locateSystemTray()) {
176 icon->sys = new QSystemTrayIconSys(mTrayIcon);
177 icon->sys->show();
178 }
179 }
180
181private:
182 QSystemTrayIcon *mTrayIcon = nullptr;
183};
185
187 : sys(nullptr),
188 qpa_sys(QGuiApplicationPrivate::platformTheme()->createPlatformSystemTrayIcon()),
189 visible(false),
190 trayWatcher(nullptr)
191{
192}
193
195{
196 delete qpa_sys;
197}
198
200{
201 Q_Q(QSystemTrayIcon);
202
203 if (qpa_sys) {
204 install_sys_qpa();
205 return;
206 }
207
208 if (!sys) {
209 if (!trayWatcher)
211
212 if (locateSystemTray()) {
213 sys = new QSystemTrayIconSys(q);
214 sys->show();
215 }
216 }
217}
218
220{
221 if (qpa_sys)
222 return qpa_sys->geometry();
223 if (!sys)
224 return QRect();
225 return sys->globalGeometry();
226}
227
229{
230 if (qpa_sys) {
231 remove_sys_qpa();
232 return;
233 }
234
235 destroyIcon();
236}
237
239{
240 if (!sys)
241 return;
243 sys->hide();
244 delete sys;
245 sys = nullptr;
246}
247
248
250{
251 if (qpa_sys) {
253 return;
254 }
255 if (sys)
256 sys->updateIcon();
257}
258
260{
261#if QT_CONFIG(menu)
262 if (qpa_sys && menu) {
263 addPlatformMenu(menu);
265 }
266#endif
267}
268
270{
271 if (qpa_sys) {
273 return;
274 }
275 if (!sys)
276 return;
277#if QT_CONFIG(tooltip)
278 sys->setToolTip(toolTip);
279#endif
280}
281
283{
284 QScopedPointer<QPlatformSystemTrayIcon> sys(QGuiApplicationPrivate::platformTheme()->createPlatformSystemTrayIcon());
285 if (sys && sys->isSystemTrayAvailable())
286 return true;
287
288 // no QPlatformSystemTrayIcon so fall back to default xcb platform behavior
290 if (platform.compare("xcb"_L1, Qt::CaseInsensitive) == 0)
291 return locateSystemTray();
292 return false;
293}
294
296{
297 QScopedPointer<QPlatformSystemTrayIcon> sys(QGuiApplicationPrivate::platformTheme()->createPlatformSystemTrayIcon());
298 if (sys)
299 return sys->supportsMessages();
300
301 // no QPlatformSystemTrayIcon so fall back to default xcb platform behavior
302 return true;
303}
304
306 const QIcon &icon, QSystemTrayIcon::MessageIcon msgIcon, int msecs)
307{
308 if (qpa_sys) {
310 static_cast<QPlatformSystemTrayIcon::MessageIcon>(msgIcon), msecs);
311 return;
312 }
313 if (!sys)
314 return;
315 QBalloonTip::showBalloon(icon, title, message, sys->systemTrayIcon(),
316 sys->globalGeometry().center(),
317 msecs);
318}
319
321
322#include "qsystemtrayicon_x11.moc"
323
324#endif //QT_NO_SYSTEMTRAYICON
static void hideBalloon()
static bool isBalloonVisible()
static void updateBalloonPosition(const QPoint &pos)
static void showBalloon(const QIcon &icon, const QString &title, const QString &msg, QSystemTrayIcon *trayIcon, const QPoint &pos, int timeout, bool showArrow=true)
static bool sendEvent(QObject *receiver, QEvent *event)
Sends event event directly to receiver receiver, using the notify() function.
\inmodule QtCore
Definition qcoreevent.h:45
Type type() const
Returns the event type.
Definition qcoreevent.h:304
static QPlatformTheme * platformTheme()
static QPlatformNativeInterface * platformNativeInterface()
QScreen * primaryScreen
the primary (or default) screen of the application.
QString platformName
The name of the underlying platform plugin.
The QIcon class provides scalable icons in different modes and states.
Definition qicon.h:20
QPlatformMenu * platformMenu()
Definition qmenu.cpp:3710
\inmodule QtGui
Definition qevent.h:196
The QMoveEvent class contains event parameters for move events.
Definition qevent.h:502
static QObjectPrivate * get(QObject *o)
Definition qobject_p.h:150
\inmodule QtCore
Definition qobject.h:103
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
Q_WEAK_OVERLOAD void setObjectName(const QString &name)
Sets the object's name to name.
Definition qobject.h:127
The QPaintEvent class contains event parameters for paint events.
Definition qevent.h:486
The QPainter class performs low-level painting on widgets and other paint devices.
Definition qpainter.h:46
MessageIcon
This enum describes the icon that is shown when a balloon message is displayed.
virtual void updateIcon(const QIcon &icon)=0
This method is called when the icon did change.
virtual void updateToolTip(const QString &tooltip)=0
This method is called when the tooltip text did change.
virtual void showMessage(const QString &title, const QString &msg, const QIcon &icon, MessageIcon iconType, int msecs)=0
Shows a balloon message for the entry with the given title, message msg and icon for the time specifi...
virtual QRect geometry() const =0
This method returns the geometry of the platform dependent system tray icon on the screen.
virtual void updateMenu(QPlatformMenu *menu)=0
This method is called when the system tray menu did change.
\inmodule QtCore\reentrant
Definition qpoint.h:25
\inmodule QtCore\reentrant
Definition qrect.h:30
The QResizeEvent class contains event parameters for resize events.
Definition qevent.h:548
The QScreen class is used to query screen properties. \inmodule QtGui.
Definition qscreen.h:32
\inmodule QtCore
Definition qsize.h:25
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
QSystemTrayIconSys * sys
static bool isSystemTrayAvailable_sys()
QSystemTrayWatcher * trayWatcher
QPlatformSystemTrayIcon * qpa_sys
void showMessage_sys(const QString &title, const QString &msg, const QIcon &icon, QSystemTrayIcon::MessageIcon msgIcon, int msecs)
QSystemTrayIcon * systemTrayIcon() const
virtual void moveEvent(QMoveEvent *) override
This event handler can be reimplemented in a subclass to receive widget move events which are passed ...
virtual bool event(QEvent *) override
This virtual function receives events to an object and should return true if the event e was recogniz...
virtual void mouseDoubleClickEvent(QMouseEvent *ev) override
This event handler, for event event, can be reimplemented in a subclass to receive mouse double click...
virtual void paintEvent(QPaintEvent *) override
This event handler can be reimplemented in a subclass to receive paint events passed in event.
virtual void resizeEvent(QResizeEvent *) override
This event handler can be reimplemented in a subclass to receive widget resize events which are passe...
QSystemTrayIconSys(QSystemTrayIcon *q)
virtual void mousePressEvent(QMouseEvent *ev) override
This event handler, for event event, can be reimplemented in a subclass to receive mouse press events...
The QSystemTrayIcon class provides an icon for an application in the system tray.
MessageIcon
This enum describes the icon that is shown when a balloon message is displayed.
QSystemTrayWatcher(QSystemTrayIcon *trayIcon)
The QWidget class is the base class of all user interface objects.
Definition qwidget.h:99
void setAttribute(Qt::WidgetAttribute, bool on=true)
Sets the attribute attribute on this widget if on is true; otherwise clears the attribute.
void setGeometry(int x, int y, int w, int h)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qwidget.h:886
void setMinimumSize(const QSize &)
Definition qwidget.h:832
QSize size
the size of the widget excluding any window frame
Definition qwidget.h:113
QPointF mapToGlobal(const QPointF &) const
Translates the widget coordinate pos to global screen coordinates.
QRect geometry
the geometry of the widget relative to its parent and excluding the window frame
Definition qwidget.h:106
void setMouseTracking(bool enable)
Definition qwidget.h:853
virtual void moveEvent(QMoveEvent *event)
This event handler can be reimplemented in a subclass to receive widget move events which are passed ...
Definition qwidget.cpp:9801
QRect rect
the internal geometry of the widget excluding any window frame
Definition qwidget.h:116
void update()
Updates the widget unless updates are disabled or the widget is hidden.
bool event(QEvent *event) override
This is the main event handler; it handles event event.
Definition qwidget.cpp:8866
virtual void resizeEvent(QResizeEvent *event)
This event handler can be reimplemented in a subclass to receive widget resize events which are passe...
Definition qwidget.cpp:9822
Combined button and popup list for selecting options.
Definition qcompare.h:63
@ LeftButton
Definition qnamespace.h:58
@ RightButton
Definition qnamespace.h:59
@ MiddleButton
Definition qnamespace.h:60
@ WA_AlwaysShowToolTips
Definition qnamespace.h:354
@ WA_QuitOnClose
Definition qnamespace.h:342
@ WA_TranslucentBackground
Definition qnamespace.h:402
@ CaseInsensitive
#define QByteArrayLiteral(str)
Definition qbytearray.h:52
#define qGuiApp
#define SLOT(a)
Definition qobjectdefs.h:52
#define SIGNAL(a)
Definition qobjectdefs.h:53
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLuint GLsizei const GLchar * message
struct _cl_event * event
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
decorationRoleName setToolTip
#define QStringLiteral(str)
static unsigned long locateSystemTray()
#define Q_OBJECT
#define slots
#define emit
#define Q_UNUSED(x)
QT_BEGIN_NAMESPACE Platform platform()
QObject::connect nullptr
QString title
[35]
QPainter painter(this)
[7]