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 * @author Marco Martin <notmart@gmail.com>
59 * @since 4.4
60 */
61class KSTATUSNOTIFIERITEM_EXPORT KStatusNotifierItem : public QObject
62{
63 Q_OBJECT
64
65 Q_PROPERTY(ItemCategory category READ category WRITE setCategory)
66 Q_PROPERTY(QString title READ title WRITE setTitle)
67 Q_PROPERTY(ItemStatus status READ status WRITE setStatus)
68 Q_PROPERTY(QString iconName READ iconName WRITE setIconByName)
69 Q_PROPERTY(QString overlayIconName READ overlayIconName WRITE setOverlayIconByName)
70 Q_PROPERTY(QString attentionIconName READ attentionIconName WRITE setAttentionIconByName)
71 Q_PROPERTY(QString toolTipIconName READ toolTipIconName WRITE setToolTipIconByName)
72 Q_PROPERTY(QString toolTipTitle READ toolTipTitle WRITE setToolTipTitle)
73 Q_PROPERTY(QString toolTipSubTitle READ toolTipSubTitle WRITE setToolTipSubTitle)
74
75 friend class KStatusNotifierItemDBus;
76 friend class KStatusNotifierItemPrivate;
77
78public:
79 /**
80 * All the possible status this icon can have, depending on the
81 * importance of the events that happens in the parent application
82 */
84 /// Nothing is happening in the application, so showing this icon is not required. This is the default value
85 Passive = 1,
86 /// The application is doing something, or it is important that the
87 /// icon is always reachable from the user
88 Active = 2,
89 /// The application requests the attention of the user, for instance
90 /// battery running out or a new IM message was received
91 NeedsAttention = 3,
92 };
93 Q_ENUM(ItemStatus)
94
95 /**
96 * Different kinds of applications announce their type to the systemtray,
97 * so can be drawn in a different way or in a different place
98 */
100 /// An icon for a normal application, can be seen as its taskbar entry. This is the default value
101 ApplicationStatus = 1,
102 /// This is a communication oriented application; this icon will be used
103 /// for things such as the notification of a new message
104 Communications = 2,
105 /// This is a system service, it can show itself in the system tray if
106 /// it requires interaction from the user or wants to inform him about
107 /// something
108 SystemServices = 3,
109 /// This application shows hardware status or a means to control it
110 Hardware = 4,
111 Reserved = 129,
112 };
113 Q_ENUM(ItemCategory)
114
115 /**
116 * Construct a new status notifier item
117 *
118 * @param parent the parent object for this object. If the object passed in as
119 * a parent is also a QWidget, it will be used as the main application window
120 * represented by this icon and will be shown/hidden when an activation is requested.
121 * @see associatedWindow
122 **/
123 explicit KStatusNotifierItem(QObject *parent = nullptr);
124
125 /**
126 * Construct a new status notifier item with a unique identifier.
127 * If your application has more than one status notifier item and the user
128 * should be able to manipulate them separately (e.g. mark them for hiding
129 * in a user interface), the id can be used to differentiate between them.
130 *
131 * The id should remain consistent even between application restarts.
132 * Status notifier items without ids default to the application's name for the id.
133 * This id may be used, for instance, by hosts displaying status notifier items to
134 * associate configuration information with this item in a way that can persist
135 * between sessions or application restarts.
136 *
137 * @param id the unique id for this icon
138 * @param parent the parent object for this object. If the object passed in as
139 * a parent is also a QWidget, it will be used as the main application window
140 * represented by this icon and will be shown/hidden when an activation is requested.
141 * @see associatedWindow
142 **/
143 explicit KStatusNotifierItem(const QString &id, QObject *parent = nullptr);
144
145 ~KStatusNotifierItem() override;
146
147 /**
148 * @return The id which was specified in the constructor. This should be
149 * guaranteed to be consistent between application starts and
150 * untranslated, as host applications displaying items may use it for
151 * storing configuration related to this item.
152 */
153 QString id() const;
154
155 /**
156 * Sets the category for this icon, usually it's needed to call this function only once
157 *
158 * @param category the new category for this icon
159 */
160 void setCategory(const ItemCategory category);
161
162 /**
163 * @return the application category
164 */
165 ItemCategory category() const;
166
167 /**
168 * Sets a title for this icon
169 */
170 void setTitle(const QString &title);
171
172 /**
173 * @return the title of this icon
174 */
175 QString title() const;
176
177 /**
178 * Sets a new status for this icon.
179 */
180 void setStatus(const ItemStatus status);
181
182 /**
183 * @return the current application status
184 */
185 ItemStatus status() const;
186
187 // Main icon related functions
188 /**
189 * Sets a new main icon for the system tray
190 *
191 * @param name it must be a QIcon::fromTheme compatible name, this is
192 * the preferred way to set an icon
193 */
194 void setIconByName(const QString &name);
195
196 /**
197 * @return the name of the main icon to be displayed
198 * if image() is not empty this will always return an empty string
199 */
200 QString iconName() const;
201
202 /**
203 * Sets a new main icon for the system tray
204 *
205 * @param pixmap our icon, use setIcon(const QString) when possible
206 */
207 void setIconByPixmap(const QIcon &icon);
208
209 /**
210 * @return a pixmap of the icon
211 */
212 QIcon iconPixmap() const;
213
214 /**
215 * Sets an icon to be used as overlay for the main one
216 * @param icon name, if name is and empty QString()
217 * (and overlayIconPixmap() is empty too) the icon will be removed
218 */
219 void setOverlayIconByName(const QString &name);
220
221 /**
222 * @return the name of the icon to be used as overlay fr the main one
223 */
224 QString overlayIconName() const;
225
226 /**
227 * Sets an icon to be used as overlay for the main one
228 * setOverlayIconByPixmap(QIcon()) will remove the overlay when
229 * overlayIconName() is empty too.
230 *
231 * @param pixmap our overlay icon, use setOverlayIcon(const QString) when possible.
232 */
233 void setOverlayIconByPixmap(const QIcon &icon);
234
235 /**
236 * @return a pixmap of the icon
237 */
238 QIcon overlayIconPixmap() const;
239
240 // Requesting attention icon
241
242 /**
243 * Sets a new icon that should be used when the application
244 * wants to request attention (usually the systemtray
245 * will blink between this icon and the main one)
246 *
247 * @param name QIcon::fromTheme compatible name of icon to use
248 */
249 void setAttentionIconByName(const QString &name);
250
251 /**
252 * @return the name of the icon to be displayed when the application
253 * is requesting the user's attention
254 * if attentionImage() is not empty this will always return an empty string
255 */
256 QString attentionIconName() const;
257
258 /**
259 * Sets the pixmap of the requesting attention icon.
260 * Use setAttentionIcon(const QString) instead when possible.
261 *
262 * @param icon QIcon to use for requesting attention.
263 */
264 void setAttentionIconByPixmap(const QIcon &icon);
265
266 /**
267 * @return a pixmap of the requesting attention icon
268 */
269 QIcon attentionIconPixmap() const;
270
271 /**
272 * Sets a movie as the requesting attention icon.
273 * This overrides anything set in setAttentionIcon()
274 */
275 void setAttentionMovieByName(const QString &name);
276
277 /**
278 * @return the name of the movie to be displayed when the application is
279 * requesting the user attention
280 */
281 QString attentionMovieName() const;
282
283 // ToolTip handling
284 /**
285 * Sets a new toolTip or this icon, a toolTip is composed of an icon,
286 * a title and a text, all fields are optional.
287 *
288 * @param iconName a QIcon::fromTheme compatible name for the tootip icon
289 * @param title tootip title
290 * @param subTitle subtitle for the toolTip
291 */
292 void setToolTip(const QString &iconName, const QString &title, const QString &subTitle);
293
294 /**
295 * Sets a new toolTip or this status notifier item.
296 * This is an overloaded member provided for convenience
297 */
298 void setToolTip(const QIcon &icon, const QString &title, const QString &subTitle);
299
300 /**
301 * Set a new icon for the toolTip
302 *
303 * @param name the name for the icon
304 */
305 void setToolTipIconByName(const QString &name);
306
307 /**
308 * @return the name of the toolTip icon
309 * if toolTipImage() is not empty this will always return an empty string
310 */
311 QString toolTipIconName() const;
312
313 /**
314 * Set a new icon for the toolTip.
315 *
316 * Use setToolTipIconByName(QString) if possible.
317 * @param pixmap representing the icon
318 */
319 void setToolTipIconByPixmap(const QIcon &icon);
320
321 /**
322 * @return a serialization of the toolTip icon data
323 */
324 QIcon toolTipIconPixmap() const;
325
326 /**
327 * Sets a new title for the toolTip
328 */
329 void setToolTipTitle(const QString &title);
330
331 /**
332 * @return the title of the main icon toolTip
333 */
334 QString toolTipTitle() const;
335
336 /**
337 * Sets a new subtitle for the toolTip
338 */
339 void setToolTipSubTitle(const QString &subTitle);
340
341 /**
342 * @return the subtitle of the main icon toolTip
343 */
344 QString toolTipSubTitle() const;
345
346 /**
347 * Sets a new context menu for this StatusNotifierItem.
348 * the menu will be shown with a contextMenu(int,int)
349 * call by the systemtray over D-Bus
350 * usually you don't need to call this unless you want to use
351 * a custom QMenu subclass as context menu.
352 *
353 * The KStatusNotifierItem instance takes ownership of the menu,
354 * and will delete it upon its destruction.
355 */
356 void setContextMenu(QMenu *menu);
357
358 /**
359 * Access the context menu associated to this status notifier item
360 */
361 QMenu *contextMenu() const;
362
363 /**
364 * Sets the main window associated with this StatusNotifierItem
365 *
366 * @param window The window to be used.
367 *
368 * @since 6.0
369 */
370 void setAssociatedWindow(QWindow *window);
371
372 /**
373 * Access the main window associated with this StatusNotifierItem
374 *
375 * @since 6.0
376 */
377 QWindow *associatedWindow() const;
378
379 /**
380 * All the actions present in the menu
381 */
382 QList<QAction *> actionCollection() const;
383
384 /**
385 * Adds an action to the actionCollection()
386 *
387 * @param name the name of the action
388 * @param action the action we want to add
389 */
390 void addAction(const QString &name, QAction *action);
391
392 /**
393 * Removes an action from the collection
394 *
395 * @param name the name of the action
396 */
397 void removeAction(const QString &name);
398
399 /**
400 * Retrieves an action from the action collection
401 * by the action name
402 *
403 * @param name the name of the action to retrieve
404 * @since 5.12
405 */
406 QAction *action(const QString &name) const;
407
408 /**
409 * Sets whether to show the standard items in the menu, such as Quit
410 */
411 void setStandardActionsEnabled(bool enabled);
412
413 /**
414 * @return if the standard items in the menu, such as Quit
415 */
416 bool standardActionsEnabled() const;
417
418 /**
419 * Shows the user a notification. If possible use KNotify instead
420 *
421 * @param title message title
422 * @param message the actual text shown to the user
423 * @param icon icon to be shown to the user
424 * @param timeout how much time will elaps before hiding the message
425 */
426 void showMessage(const QString &title, const QString &message, const QString &icon, int timeout = 10000);
427
428 /**
429 * @return the last provided token to be used with Wayland's xdg_activation_v1
430 */
431 QString providedToken() const;
432
433public Q_SLOTS:
434
435 /**
436 * Shows the main window and try to position it on top
437 * of the other windows, if the window is already visible, hide it.
438 *
439 * @param pos if it's a valid position it represents the mouse coordinates when the event was triggered
440 */
441 virtual void activate(const QPoint &pos = QPoint());
442
443 /**
444 * Hides the main window, if not already hidden.
445 *
446 * Stores some information about the window which otherwise would be lost due to unmapping
447 * from the window system. Use when toggling the main window via activate(const QPoint &)
448 * is not wanted, but instead the hidden state should be reached in any case.
449 *
450 * @since 6.0
451 */
452 void hideAssociatedWindow();
453
455 /**
456 * Inform the host application that the mouse wheel
457 * (or another mean of scrolling that the visualization provides) has been used
458 *
459 * @param delta the amount of scrolling, can be either positive or negative
460 * @param orientation direction of the scrolling, can be either horizontal or vertical
461 */
462 void scrollRequested(int delta, Qt::Orientation orientation);
463
464 /**
465 * Inform the host application that an activation has been requested,
466 * for instance left mouse click, but this is not guaranteed since
467 * it's dependent from the visualization
468 * @param active if it's true the application asked for the activation
469 * of the main window, if it's false it asked for hiding
470 * @param pos the position in the screen where the user clicked to
471 * trigger this signal, QPoint() if it's not the consequence of a mouse click.
472 */
473 void activateRequested(bool active, const QPoint &pos);
474
475 /**
476 * Alternate activate action,
477 * for instance right mouse click, but this is not guaranteed since
478 * it's dependent from the visualization
479 *
480 * @param pos the position in the screen where the user clicked to
481 * trigger this signal, QPoint() if it's not the consequence of a mouse click.
482 */
484
485protected:
486 bool eventFilter(QObject *watched, QEvent *event) override;
487
488private:
489 std::unique_ptr<KStatusNotifierItemPrivate> const d;
490
491 Q_PRIVATE_SLOT(d, void serviceChange(const QString &name, const QString &oldOwner, const QString &newOwner))
492 Q_PRIVATE_SLOT(d, void contextMenuAboutToShow())
493 Q_PRIVATE_SLOT(d, void maybeQuit())
494 Q_PRIVATE_SLOT(d, void minimizeRestore())
495 Q_PRIVATE_SLOT(d, void legacyWheelEvent(int))
496 Q_PRIVATE_SLOT(d, void legacyActivated(QSystemTrayIcon::ActivationReason))
497};
498
499#endif
KDE Status notifier Item protocol implementation
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 Fri Jul 26 2024 11:51:11 by doxygen 1.11.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.