KStatusNotifierItem

kstatusnotifieritem.h
1/*
2 This file is part of the KDE libraries
3 SPDX-FileCopyrightText: 2009 Marco Martin <notmart@gmail.com>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#ifndef KSTATUSNOTIFIERITEM_H
9#define KSTATUSNOTIFIERITEM_H
10
11#include <QObject>
12#include <QPoint>
13#include <QString>
14#include <QWindow>
15
16#include <kstatusnotifieritem_export.h>
17
18#include <memory>
19
20class QAction;
21class QMenu;
22
23class KStatusNotifierItemPrivate;
24
25/**
26 * @class KStatusNotifierItem kstatusnotifieritem.h KStatusNotifierItem
27 *
28 * \brief %KDE Status notifier Item protocol implementation
29 *
30 * This class implements the Status notifier Item D-Bus specification.
31 * It provides an icon similar to the classical systemtray icons,
32 * with some key differences:
33 *
34 * - the actual representation is done by the systemtray (or the app behaving
35 * like it) itself, not by this app. Since 4.5 this also includes the menu,
36 * which means you cannot use embed widgets in the menu.
37 *
38 * - there is communication between the systemtray and the icon owner, so the
39 * system tray can know if the application is in a normal or in a requesting
40 * attention state.
41 *
42 * - icons are divided in categories, so the systemtray can represent in a
43 * different way the icons from normal applications and for instance the ones
44 * about hardware status.
45 *
46 * Whenever possible you should prefer passing icon by name rather than by
47 * pixmap because:
48 *
49 * - it is much lighter on D-Bus (no need to pass all image pixels).
50 *
51 * - it makes it possible for the systemtray to load an icon of the appropriate
52 * size or to replace your icon with a systemtray specific icon which matches
53 * with the desktop theme.
54 *
55 * - some implementations of the system tray do not support passing icons by
56 * pixmap and will show a blank icon instead.
57 *
58 * @note When used inside a Flatpak it is important to request explicit support
59 * in the Flatpak manifest with the following line.
60 * --talk-name=org.kde.StatusNotifierWatcher
61 *
62 * @author Marco Martin <notmart@gmail.com>
63 * @since 4.4
64 */
65class KSTATUSNOTIFIERITEM_EXPORT KStatusNotifierItem : public QObject
66{
67 Q_OBJECT
68
69 Q_PROPERTY(ItemCategory category READ category WRITE setCategory)
70 Q_PROPERTY(QString title READ title WRITE setTitle)
71 Q_PROPERTY(ItemStatus status READ status WRITE setStatus)
72 Q_PROPERTY(QString iconName READ iconName WRITE setIconByName)
73 Q_PROPERTY(QString overlayIconName READ overlayIconName WRITE setOverlayIconByName)
74 Q_PROPERTY(QString attentionIconName READ attentionIconName WRITE setAttentionIconByName)
75 Q_PROPERTY(QString toolTipIconName READ toolTipIconName WRITE setToolTipIconByName)
76 Q_PROPERTY(QString toolTipTitle READ toolTipTitle WRITE setToolTipTitle)
77 Q_PROPERTY(QString toolTipSubTitle READ toolTipSubTitle WRITE setToolTipSubTitle)
78
79 friend class KStatusNotifierItemDBus;
80 friend class KStatusNotifierItemPrivate;
81
82public:
83 /**
84 * All the possible status this icon can have, depending on the
85 * importance of the events that happens in the parent application
86 */
88 /// Nothing is happening in the application, so showing this icon is not required. This is the default value
89 Passive = 1,
90 /// The application is doing something, or it is important that the
91 /// icon is always reachable from the user
92 Active = 2,
93 /// The application requests the attention of the user, for instance
94 /// battery running out or a new IM message was received
95 NeedsAttention = 3,
96 };
97 Q_ENUM(ItemStatus)
98
99 /**
100 * Different kinds of applications announce their type to the systemtray,
101 * so can be drawn in a different way or in a different place
102 */
104 /// An icon for a normal application, can be seen as its taskbar entry. This is the default value
105 ApplicationStatus = 1,
106 /// This is a communication oriented application; this icon will be used
107 /// for things such as the notification of a new message
108 Communications = 2,
109 /// This is a system service, it can show itself in the system tray if
110 /// it requires interaction from the user or wants to inform him about
111 /// something
112 SystemServices = 3,
113 /// This application shows hardware status or a means to control it
114 Hardware = 4,
115 Reserved = 129,
116 };
117 Q_ENUM(ItemCategory)
118
119 /**
120 * Construct a new status notifier item
121 *
122 * @param parent the parent object for this object. If the object passed in as
123 * a parent is also a QWidget, it will be used as the main application window
124 * represented by this icon and will be shown/hidden when an activation is requested.
125 * @see associatedWindow
126 **/
127 explicit KStatusNotifierItem(QObject *parent = nullptr);
128
129 /**
130 * Construct a new status notifier item with a unique identifier.
131 * If your application has more than one status notifier item and the user
132 * should be able to manipulate them separately (e.g. mark them for hiding
133 * in a user interface), the id can be used to differentiate between them.
134 *
135 * The id should remain consistent even between application restarts.
136 * Status notifier items without ids default to the application's name for the id.
137 * This id may be used, for instance, by hosts displaying status notifier items to
138 * associate configuration information with this item in a way that can persist
139 * between sessions or application restarts.
140 *
141 * @param id the unique id for this icon
142 * @param parent the parent object for this object. If the object passed in as
143 * a parent is also a QWidget, it will be used as the main application window
144 * represented by this icon and will be shown/hidden when an activation is requested.
145 * @see associatedWindow
146 **/
147 explicit KStatusNotifierItem(const QString &id, QObject *parent = nullptr);
148
149 ~KStatusNotifierItem() override;
150
151 /**
152 * @return The id which was specified in the constructor. This should be
153 * guaranteed to be consistent between application starts and
154 * untranslated, as host applications displaying items may use it for
155 * storing configuration related to this item.
156 */
157 QString id() const;
158
159 /**
160 * Sets the category for this icon, usually it's needed to call this function only once
161 *
162 * @param category the new category for this icon
163 */
164 void setCategory(const ItemCategory category);
165
166 /**
167 * @return the application category
168 */
169 ItemCategory category() const;
170
171 /**
172 * Sets a title for this icon
173 */
174 void setTitle(const QString &title);
175
176 /**
177 * @return the title of this icon
178 */
179 QString title() const;
180
181 /**
182 * Sets a new status for this icon.
183 */
184 void setStatus(const ItemStatus status);
185
186 /**
187 * @return the current application status
188 */
189 ItemStatus status() const;
190
191 // Main icon related functions
192 /**
193 * Sets a new main icon for the system tray
194 *
195 * @param name it must be a QIcon::fromTheme compatible name, this is
196 * the preferred way to set an icon
197 */
198 void setIconByName(const QString &name);
199
200 /**
201 * @return the name of the main icon to be displayed
202 * if image() is not empty this will always return an empty string
203 */
204 QString iconName() const;
205
206 /**
207 * Sets a new main icon for the system tray
208 *
209 * @param pixmap our icon, use setIcon(const QString) when possible
210 */
211 void setIconByPixmap(const QIcon &icon);
212
213 /**
214 * @return a pixmap of the icon
215 */
216 QIcon iconPixmap() const;
217
218 /**
219 * Sets an icon to be used as overlay for the main one
220 * @param icon name, if name is and empty QString()
221 * (and overlayIconPixmap() is empty too) the icon will be removed
222 */
223 void setOverlayIconByName(const QString &name);
224
225 /**
226 * @return the name of the icon to be used as overlay fr the main one
227 */
228 QString overlayIconName() const;
229
230 /**
231 * Sets an icon to be used as overlay for the main one
232 * setOverlayIconByPixmap(QIcon()) will remove the overlay when
233 * overlayIconName() is empty too.
234 *
235 * @param pixmap our overlay icon, use setOverlayIcon(const QString) when possible.
236 */
237 void setOverlayIconByPixmap(const QIcon &icon);
238
239 /**
240 * @return a pixmap of the icon
241 */
242 QIcon overlayIconPixmap() const;
243
244 // Requesting attention icon
245
246 /**
247 * Sets a new icon that should be used when the application
248 * wants to request attention (usually the systemtray
249 * will blink between this icon and the main one)
250 *
251 * @param name QIcon::fromTheme compatible name of icon to use
252 */
253 void setAttentionIconByName(const QString &name);
254
255 /**
256 * @return the name of the icon to be displayed when the application
257 * is requesting the user's attention
258 * if attentionImage() is not empty this will always return an empty string
259 */
260 QString attentionIconName() const;
261
262 /**
263 * Sets the pixmap of the requesting attention icon.
264 * Use setAttentionIcon(const QString) instead when possible.
265 *
266 * @param icon QIcon to use for requesting attention.
267 */
268 void setAttentionIconByPixmap(const QIcon &icon);
269
270 /**
271 * @return a pixmap of the requesting attention icon
272 */
273 QIcon attentionIconPixmap() const;
274
275 /**
276 * Sets a movie as the requesting attention icon.
277 * This overrides anything set in setAttentionIcon()
278 */
279 void setAttentionMovieByName(const QString &name);
280
281 /**
282 * @return the name of the movie to be displayed when the application is
283 * requesting the user attention
284 */
285 QString attentionMovieName() const;
286
287 // ToolTip handling
288 /**
289 * Sets a new toolTip or this icon, a toolTip is composed of an icon,
290 * a title and a text, all fields are optional.
291 *
292 * @param iconName a QIcon::fromTheme compatible name for the tootip icon
293 * @param title tootip title
294 * @param subTitle subtitle for the toolTip
295 */
296 void setToolTip(const QString &iconName, const QString &title, const QString &subTitle);
297
298 /**
299 * Sets a new toolTip or this status notifier item.
300 * This is an overloaded member provided for convenience
301 */
302 void setToolTip(const QIcon &icon, const QString &title, const QString &subTitle);
303
304 /**
305 * Set a new icon for the toolTip
306 *
307 * @param name the name for the icon
308 */
309 void setToolTipIconByName(const QString &name);
310
311 /**
312 * @return the name of the toolTip icon
313 * if toolTipImage() is not empty this will always return an empty string
314 */
315 QString toolTipIconName() const;
316
317 /**
318 * Set a new icon for the toolTip.
319 *
320 * Use setToolTipIconByName(QString) if possible.
321 * @param pixmap representing the icon
322 */
323 void setToolTipIconByPixmap(const QIcon &icon);
324
325 /**
326 * @return a serialization of the toolTip icon data
327 */
328 QIcon toolTipIconPixmap() const;
329
330 /**
331 * Sets a new title for the toolTip
332 */
333 void setToolTipTitle(const QString &title);
334
335 /**
336 * @return the title of the main icon toolTip
337 */
338 QString toolTipTitle() const;
339
340 /**
341 * Sets a new subtitle for the toolTip
342 */
343 void setToolTipSubTitle(const QString &subTitle);
344
345 /**
346 * @return the subtitle of the main icon toolTip
347 */
348 QString toolTipSubTitle() const;
349
350 /**
351 * Sets a new context menu for this StatusNotifierItem.
352 * the menu will be shown with a contextMenu(int,int)
353 * call by the systemtray over D-Bus
354 * usually you don't need to call this unless you want to use
355 * a custom QMenu subclass as context menu.
356 *
357 * The KStatusNotifierItem instance takes ownership of the menu,
358 * and will delete it upon its destruction.
359 */
360 void setContextMenu(QMenu *menu);
361
362 /**
363 * Access the context menu associated to this status notifier item
364 */
365 QMenu *contextMenu() const;
366
367 /**
368 * Sets the main window associated with this StatusNotifierItem
369 *
370 * @param window The window to be used.
371 *
372 * @since 6.0
373 */
374 void setAssociatedWindow(QWindow *window);
375
376 /**
377 * Access the main window associated with this StatusNotifierItem
378 *
379 * @since 6.0
380 */
381 QWindow *associatedWindow() const;
382
383#if KSTATUSNOTIFIERITEM_ENABLE_DEPRECATED_SINCE(6, 6)
384 /**
385 * All the actions present in the menu
386 *
387 * @deprecated since 6.6, read actions from contextMenu()
388 */
389 KSTATUSNOTIFIERITEM_DEPRECATED_VERSION(6, 6, "Read actions from contextMenu()")
390 QList<QAction *> actionCollection() const;
391#endif
392
393#if KSTATUSNOTIFIERITEM_ENABLE_DEPRECATED_SINCE(6, 6)
394 /**
395 * Adds an action to the actionCollection()
396 *
397 * @param name the name of the action
398 * @param action the action we want to add
399 *
400 * @deprecated since 6.6, add actions to contextMenu()
401 */
402 KSTATUSNOTIFIERITEM_DEPRECATED_VERSION(6, 6, "Add actions to contextMenu()")
403 void addAction(const QString &name, QAction *action);
404#endif
405
406#if KSTATUSNOTIFIERITEM_ENABLE_DEPRECATED_SINCE(6, 6)
407 /**
408 * Removes an action from the collection
409 *
410 * @param name the name of the action
411 *
412 * @deprecated since 6.6, remove actions from contextMenu()
413 */
414 KSTATUSNOTIFIERITEM_DEPRECATED_VERSION(6, 6, "Remove actions from contextMenu()")
415 void removeAction(const QString &name);
416#endif
417
418#if KSTATUSNOTIFIERITEM_ENABLE_DEPRECATED_SINCE(6, 6)
419 /**
420 * Retrieves an action from the action collection
421 * by the action name
422 *
423 * @param name the name of the action to retrieve
424 * @since 5.12
425 *
426 * @deprecated since 6.6. Read actions from contextMenu().
427 * For controlling the behavior of the Quit action use quitRequested()
428 * and abortQuit()
429 */
430 KSTATUSNOTIFIERITEM_DEPRECATED_VERSION(6, 6, "See API docs")
431 QAction *action(const QString &name) const;
432#endif
433
434 /**
435 * Sets whether to show the standard items in the menu, such as Quit
436 */
437 void setStandardActionsEnabled(bool enabled);
438
439 /**
440 * @return if the standard items in the menu, such as Quit
441 */
442 bool standardActionsEnabled() const;
443
444 /**
445 * Shows the user a notification. If possible use KNotify instead
446 *
447 * @param title message title
448 * @param message the actual text shown to the user
449 * @param icon icon to be shown to the user
450 * @param timeout how much time will elapse before hiding the message
451 */
452 void showMessage(const QString &title, const QString &message, const QString &icon, int timeout = 10000);
453
454 /**
455 * @return the last provided token to be used with Wayland's xdg_activation_v1
456 */
457 QString providedToken() const;
458
459 /**
460 * Cancelles an ongoing quit operation.
461 *
462 * Call this in a slot connected to quitRequested().
463 *
464 * @see quitRequested()
465 *
466 * @since 6.5
467 */
468 void abortQuit();
469
470public Q_SLOTS:
471
472 /**
473 * Shows the main window and try to position it on top
474 * of the other windows, if the window is already visible, hide it.
475 *
476 * @param pos if it's a valid position it represents the mouse coordinates when the event was triggered
477 */
478 virtual void activate(const QPoint &pos = QPoint());
479
480 /**
481 * Hides the main window, if not already hidden.
482 *
483 * Stores some information about the window which otherwise would be lost due to unmapping
484 * from the window system. Use when toggling the main window via activate(const QPoint &)
485 * is not wanted, but instead the hidden state should be reached in any case.
486 *
487 * @since 6.0
488 */
489 void hideAssociatedWindow();
490
492 /**
493 * Inform the host application that the mouse wheel
494 * (or another mean of scrolling that the visualization provides) has been used
495 *
496 * @param delta the amount of scrolling, can be either positive or negative
497 * @param orientation direction of the scrolling, can be either horizontal or vertical
498 */
499 void scrollRequested(int delta, Qt::Orientation orientation);
500
501 /**
502 * Inform the host application that an activation has been requested,
503 * for instance left mouse click, but this is not guaranteed since
504 * it's dependent from the visualization
505 * @param active if it's true the application asked for the activation
506 * of the main window, if it's false it asked for hiding
507 * @param pos the position in the screen where the user clicked to
508 * trigger this signal, QPoint() if it's not the consequence of a mouse click.
509 */
510 void activateRequested(bool active, const QPoint &pos);
511
512 /**
513 * Alternate activate action,
514 * for instance right mouse click, but this is not guaranteed since
515 * it's dependent from the visualization
516 *
517 * @param pos the position in the screen where the user clicked to
518 * trigger this signal, QPoint() if it's not the consequence of a mouse click.
519 */
521
522 /**
523 * Emitted when the Quit action is triggered.
524 *
525 * If abortQuit() is called from the slot the quit is cancelled.
526 * This allows to e.g. display a custom confirmation prompt.
527 *
528 * @since 6.5
529 */
531
532protected:
533 bool eventFilter(QObject *watched, QEvent *event) override;
534
535private:
536 std::unique_ptr<KStatusNotifierItemPrivate> const d;
537
538 Q_PRIVATE_SLOT(d, void serviceChange(const QString &name, const QString &oldOwner, const QString &newOwner))
539 Q_PRIVATE_SLOT(d, void contextMenuAboutToShow())
540 Q_PRIVATE_SLOT(d, void maybeQuit())
541 Q_PRIVATE_SLOT(d, void minimizeRestore())
542 Q_PRIVATE_SLOT(d, void legacyWheelEvent(int))
543 Q_PRIVATE_SLOT(d, void legacyActivated(QSystemTrayIcon::ActivationReason))
544};
545
546#endif
KDE Status notifier Item protocol implementation
void quitRequested()
Emitted when the Quit action is triggered.
ItemCategory
Different kinds of applications announce their type to the systemtray, so can be drawn in a different...
void activateRequested(bool active, const QPoint &pos)
Inform the host application that an activation has been requested, for instance left mouse click,...
ItemStatus
All the possible status this icon can have, depending on the importance of the events that happens in...
void scrollRequested(int delta, Qt::Orientation orientation)
Inform the host application that the mouse wheel (or another mean of scrolling that the visualization...
void secondaryActivateRequested(const QPoint &pos)
Alternate activate action, for instance right mouse click, but this is not guaranteed since it's depe...
Q_SCRIPTABLE CaptureState status()
Q_ENUM(...)
Q_PROPERTY(...)
Q_SIGNALSQ_SIGNALS
Q_SLOTSQ_SLOTS
virtual bool eventFilter(QObject *watched, QEvent *event)
Orientation
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:05:22 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.