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
qquicklabsplatformsystemtrayicon.cpp
Go to the documentation of this file.
1// Copyright (C) 2017 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
7
8#include <QtCore/qloggingcategory.h>
9#include <QtGui/qpa/qplatformtheme.h>
10#include <QtGui/private/qguiapplication_p.h>
11
13
15
20
133Q_LOGGING_CATEGORY(qtLabsPlatformTray, "qt.labs.platform.tray")
134
136 : QObject(parent),
137 m_complete(false),
138 m_visible(false),
139 m_menu(nullptr),
140 m_iconLoader(nullptr),
141 m_handle(nullptr)
142{
143 m_handle = QGuiApplicationPrivate::platformTheme()->createPlatformSystemTrayIcon();
144 if (!m_handle)
146 qCDebug(qtLabsPlatformTray) << "SystemTrayIcon ->" << m_handle;
147
148 if (m_handle) {
151 }
152}
153
155{
156 if (m_menu)
157 m_menu->setSystemTrayIcon(nullptr);
158 cleanup();
159 delete m_iconLoader;
160 m_iconLoader = nullptr;
161 delete m_handle;
162 m_handle = nullptr;
163}
164
169
177{
178 return m_handle && m_handle->isSystemTrayAvailable();
179}
180
190{
191 return m_handle && m_handle->supportsMessages();
192}
193
202{
203 return m_visible;
204}
205
207{
208 if (m_visible == visible)
209 return;
210
211 if (m_handle && m_complete) {
212 if (visible)
213 init();
214 else
215 cleanup();
216 }
217
218 m_visible = visible;
220}
221
228{
229 return m_tooltip;
230}
231
233{
234 if (m_tooltip == tooltip)
235 return;
236
237 if (m_handle && m_complete)
238 m_handle->updateToolTip(tooltip);
239
240 m_tooltip = tooltip;
242}
243
253
255{
256 if (m_menu == menu)
257 return;
258
259 if (m_menu)
260 m_menu->setSystemTrayIcon(nullptr);
261
262 if (menu)
263 menu->setSystemTrayIcon(this);
264
265 if (m_handle && m_complete) {
266 if (menu && menu->create())
267 m_handle->updateMenu(menu->handle());
268 else
269 m_handle->updateMenu(nullptr);
270 }
271
272 m_menu = menu;
274}
275
283{
284 return m_handle ? m_handle->geometry() : QRect();
285}
286
303{
304 if (!m_iconLoader)
305 return QQuickLabsPlatformIcon();
306
307 return m_iconLoader->icon();
308}
309
311{
312 if (iconLoader()->icon() == icon)
313 return;
314
316 emit iconChanged();
317}
318
328
338
352{
353 if (m_handle)
354 m_handle->showMessage(title, msg, QIcon(), icon, msecs);
355}
356
358{
359 if (!m_handle)
360 return;
361
362 m_handle->init();
363 if (m_menu && m_menu->create())
364 m_handle->updateMenu(m_menu->handle());
365 m_handle->updateToolTip(m_tooltip);
366 if (m_iconLoader)
367 m_iconLoader->setEnabled(true);
368}
369
371{
372 if (m_handle)
373 m_handle->cleanup();
374 if (m_iconLoader)
375 m_iconLoader->setEnabled(false);
376}
377
381
383{
384 m_complete = true;
385 if (m_visible)
386 init();
387}
388
390{
391 if (!m_iconLoader) {
393 static int slot = staticMetaObject.indexOfSlot("updateIcon()");
394 m_iconLoader = new QQuickLabsPlatformIconLoader(slot, that);
395 m_iconLoader->setEnabled(m_complete);
396 }
397 return m_iconLoader;
398}
399
400void QQuickLabsPlatformSystemTrayIcon::updateIcon()
401{
402 if (!m_handle || !m_iconLoader)
403 return;
404
405 const QRect oldGeometry = m_handle->geometry();
406
407 m_handle->updateIcon(m_iconLoader->toQIcon());
408
409 if (oldGeometry != m_handle->geometry())
410 emit geometryChanged();
411}
412
414
415#include "moc_qquicklabsplatformsystemtrayicon_p.cpp"
static QPlatformTheme * platformTheme()
The QIcon class provides scalable icons in different modes and states.
Definition qicon.h:20
\inmodule QtCore
Definition qobject.h:103
MessageIcon
This enum describes the icon that is shown when a balloon message is displayed.
virtual void cleanup()=0
This method is called to cleanup the platform dependent implementation.
virtual void updateIcon(const QIcon &icon)=0
This method is called when the icon did change.
virtual bool isSystemTrayAvailable() const =0
Returns true if the system tray is available on the platform.
void activated(QPlatformSystemTrayIcon::ActivationReason reason)
This signal is emitted when the user activates the system tray icon.
virtual void updateToolTip(const QString &tooltip)=0
This method is called when the tooltip text did change.
void messageClicked()
This signal is emitted when the message displayed using showMessage() was clicked by the user.
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 bool supportsMessages() const =0
Returns true if the system tray supports messages on the platform.
virtual void updateMenu(QPlatformMenu *menu)=0
This method is called when the system tray menu did change.
virtual void init()=0
This method is called to initialize the platform dependent implementation.
void setIcon(const QQuickLabsPlatformIcon &icon)
QQuickLabsPlatformIcon icon() const
QPlatformMenu * handle() const
void hide()
\qmlmethod void Qt.labs.platform::SystemTrayIcon::hide()
bool isVisible() const
\qmlproperty bool Qt.labs.platform::SystemTrayIcon::visible
void showMessage(const QString &title, const QString &message, QPlatformSystemTrayIcon::MessageIcon iconType=QPlatformSystemTrayIcon::Information, int msecs=10000)
\qmlmethod void Qt.labs.platform::SystemTrayIcon::showMessage(string title, string message,...
void activated(QPlatformSystemTrayIcon::ActivationReason reason)
void classBegin() override
Invoked after class creation, but before any properties have been set.
QQuickLabsPlatformIconLoader * iconLoader() const
void show()
\qmlmethod void Qt.labs.platform::SystemTrayIcon::show()
bool isAvailable() const
\readonly \qmlproperty bool Qt.labs.platform::SystemTrayIcon::available
void componentComplete() override
Invoked after the root component that caused this instantiation has completed construction.
void setIcon(const QQuickLabsPlatformIcon &icon)
\inmodule QtCore\reentrant
Definition qrect.h:30
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
Combined button and popup list for selecting options.
static QPlatformSystemTrayIcon * createSystemTrayIcon(QObject *parent=nullptr)
#define Q_LOGGING_CATEGORY(name,...)
#define qCDebug(category,...)
#define emit
connect(quitButton, &QPushButton::clicked, &app, &QCoreApplication::quit, Qt::QueuedConnection)
QString title
[35]
QMenu menu
[5]