KNotifications

kstatusnotifieritem.h
1 /*
2  This file is part of the KDE libraries
3  SPDX-FileCopyrightText: 2009 Marco Martin <[email protected]>
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 
15 #include <knotifications_export.h>
16 
17 #include <memory>
18 
19 class QAction;
20 class QMenu;
21 
22 class KStatusNotifierItemPrivate;
23 
24 /**
25  * @class KStatusNotifierItem kstatusnotifieritem.h KStatusNotifierItem
26  *
27  * \brief %KDE Status notifier Item protocol implementation
28  *
29  * This class implements the Status notifier Item D-Bus specification.
30  * It provides an icon similar to the classical systemtray icons,
31  * with some key differences:
32  *
33  * - the actual representation is done by the systemtray (or the app behaving
34  * like it) itself, not by this app. Since 4.5 this also includes the menu,
35  * which means you cannot use embed widgets in the menu.
36  *
37  * - there is communication between the systemtray and the icon owner, so the
38  * system tray can know if the application is in a normal or in a requesting
39  * attention state.
40  *
41  * - icons are divided in categories, so the systemtray can represent in a
42  * different way the icons from normal applications and for instance the ones
43  * about hardware status.
44  *
45  * Whenever possible you should prefer passing icon by name rather than by
46  * pixmap because:
47  *
48  * - it is much lighter on D-Bus (no need to pass all image pixels).
49  *
50  * - it makes it possible for the systemtray to load an icon of the appropriate
51  * size or to replace your icon with a systemtray specific icon which matches
52  * with the desktop theme.
53  *
54  * - some implementations of the system tray do not support passing icons by
55  * pixmap and will show a blank icon instead.
56  *
57  * @author Marco Martin <[email protected]>
58  * @since 4.4
59  */
60 class KNOTIFICATIONS_EXPORT KStatusNotifierItem : public QObject
61 {
62  Q_OBJECT
63 
64  Q_PROPERTY(ItemCategory category READ category WRITE setCategory)
65  Q_PROPERTY(QString title READ title WRITE setTitle)
66  Q_PROPERTY(ItemStatus status READ status WRITE setStatus)
67  Q_PROPERTY(QString iconName READ iconName WRITE setIconByName)
68  Q_PROPERTY(QString overlayIconName READ overlayIconName WRITE setOverlayIconByName)
69  Q_PROPERTY(QString attentionIconName READ attentionIconName WRITE setAttentionIconByName)
70  Q_PROPERTY(QString toolTipIconName READ toolTipIconName WRITE setToolTipIconByName)
71  Q_PROPERTY(QString toolTipTitle READ toolTipTitle WRITE setToolTipTitle)
72  Q_PROPERTY(QString toolTipSubTitle READ toolTipSubTitle WRITE setToolTipSubTitle)
73 
74  friend class KStatusNotifierItemDBus;
75  friend class KStatusNotifierItemPrivate;
76 
77 public:
78  /**
79  * All the possible status this icon can have, depending on the
80  * importance of the events that happens in the parent application
81  */
82  enum ItemStatus {
83  /// Nothing is happening in the application, so showing this icon is not required. This is the default value
84  Passive = 1,
85  /// The application is doing something, or it is important that the
86  /// icon is always reachable from the user
87  Active = 2,
88  /// The application requests the attention of the user, for instance
89  /// battery running out or a new IM message was received
90  NeedsAttention = 3,
91  };
92  Q_ENUM(ItemStatus)
93 
94  /**
95  * Different kinds of applications announce their type to the systemtray,
96  * so can be drawn in a different way or in a different place
97  */
98  enum ItemCategory {
99  /// An icon for a normal application, can be seen as its taskbar entry. This is the default value
100  ApplicationStatus = 1,
101  /// This is a communication oriented application; this icon will be used
102  /// for things such as the notification of a new message
103  Communications = 2,
104  /// This is a system service, it can show itself in the system tray if
105  /// it requires interaction from the user or wants to inform him about
106  /// something
107  SystemServices = 3,
108  /// This application shows hardware status or a means to control it
109  Hardware = 4,
110  Reserved = 129,
111  };
112  Q_ENUM(ItemCategory)
113 
114  /**
115  * Construct a new status notifier item
116  *
117  * @param parent the parent object for this object. If the object passed in as
118  * a parent is also a QWidget, it will be used as the main application window
119  * represented by this icon and will be shown/hidden when an activation is requested.
120  * @see associatedWidget
121  **/
122  explicit KStatusNotifierItem(QObject *parent = nullptr);
123 
124  /**
125  * Construct a new status notifier item with a unique identifier.
126  * If your application has more than one status notifier item and the user
127  * should be able to manipulate them separately (e.g. mark them for hiding
128  * in a user interface), the id can be used to differentiate between them.
129  *
130  * The id should remain consistent even between application restarts.
131  * Status notifier items without ids default to the application's name for the id.
132  * This id may be used, for instance, by hosts displaying status notifier items to
133  * associate configuration information with this item in a way that can persist
134  * between sessions or application restarts.
135  *
136  * @param id the unique id for this icon
137  * @param parent the parent object for this object. If the object passed in as
138  * a parent is also a QWidget, it will be used as the main application window
139  * represented by this icon and will be shown/hidden when an activation is requested.
140  * @see associatedWidget
141  **/
142  explicit KStatusNotifierItem(const QString &id, QObject *parent = nullptr);
143 
144  ~KStatusNotifierItem() override;
145 
146  /**
147  * @return The id which was specified in the constructor. This should be
148  * guaranteed to be consistent between application starts and
149  * untranslated, as host applications displaying items may use it for
150  * storing configuration related to this item.
151  */
152  QString id() const;
153 
154  /**
155  * Sets the category for this icon, usually it's needed to call this function only once
156  *
157  * @param category the new category for this icon
158  */
159  void setCategory(const ItemCategory category);
160 
161  /**
162  * @return the application category
163  */
164  ItemCategory category() const;
165 
166  /**
167  * Sets a title for this icon
168  */
169  void setTitle(const QString &title);
170 
171  /**
172  * @return the title of this icon
173  */
174  QString title() const;
175 
176  /**
177  * Sets a new status for this icon.
178  */
179  void setStatus(const ItemStatus status);
180 
181  /**
182  * @return the current application status
183  */
184  ItemStatus status() const;
185 
186  // Main icon related functions
187  /**
188  * Sets a new main icon for the system tray
189  *
190  * @param name it must be a QIcon::fromTheme compatible name, this is
191  * the preferred way to set an icon
192  */
193  void setIconByName(const QString &name);
194 
195  /**
196  * @return the name of the main icon to be displayed
197  * if image() is not empty this will always return an empty string
198  */
199  QString iconName() const;
200 
201  /**
202  * Sets a new main icon for the system tray
203  *
204  * @param pixmap our icon, use setIcon(const QString) when possible
205  */
206  void setIconByPixmap(const QIcon &icon);
207 
208  /**
209  * @return a pixmap of the icon
210  */
211  QIcon iconPixmap() const;
212 
213  /**
214  * Sets an icon to be used as overlay for the main one
215  * @param icon name, if name is and empty QString()
216  * (and overlayIconPixmap() is empty too) the icon will be removed
217  */
218  void setOverlayIconByName(const QString &name);
219 
220  /**
221  * @return the name of the icon to be used as overlay fr the main one
222  */
223  QString overlayIconName() const;
224 
225  /**
226  * Sets an icon to be used as overlay for the main one
227  * setOverlayIconByPixmap(QIcon()) will remove the overlay when
228  * overlayIconName() is empty too.
229  *
230  * @param pixmap our overlay icon, use setOverlayIcon(const QString) when possible.
231  */
232  void setOverlayIconByPixmap(const QIcon &icon);
233 
234  /**
235  * @return a pixmap of the icon
236  */
237  QIcon overlayIconPixmap() const;
238 
239  // Requesting attention icon
240 
241  /**
242  * Sets a new icon that should be used when the application
243  * wants to request attention (usually the systemtray
244  * will blink between this icon and the main one)
245  *
246  * @param name QIcon::fromTheme compatible name of icon to use
247  */
248  void setAttentionIconByName(const QString &name);
249 
250  /**
251  * @return the name of the icon to be displayed when the application
252  * is requesting the user's attention
253  * if attentionImage() is not empty this will always return an empty string
254  */
255  QString attentionIconName() const;
256 
257  /**
258  * Sets the pixmap of the requesting attention icon.
259  * Use setAttentionIcon(const QString) instead when possible.
260  *
261  * @param icon QIcon to use for requesting attention.
262  */
263  void setAttentionIconByPixmap(const QIcon &icon);
264 
265  /**
266  * @return a pixmap of the requesting attention icon
267  */
268  QIcon attentionIconPixmap() const;
269 
270  /**
271  * Sets a movie as the requesting attention icon.
272  * This overrides anything set in setAttentionIcon()
273  */
274  void setAttentionMovieByName(const QString &name);
275 
276  /**
277  * @return the name of the movie to be displayed when the application is
278  * requesting the user attention
279  */
280  QString attentionMovieName() const;
281 
282  // ToolTip handling
283  /**
284  * Sets a new toolTip or this icon, a toolTip is composed of an icon,
285  * a title and a text, all fields are optional.
286  *
287  * @param iconName a QIcon::fromTheme compatible name for the tootip icon
288  * @param title tootip title
289  * @param subTitle subtitle for the toolTip
290  */
291  void setToolTip(const QString &iconName, const QString &title, const QString &subTitle);
292 
293  /**
294  * Sets a new toolTip or this status notifier item.
295  * This is an overloaded member provided for convenience
296  */
297  void setToolTip(const QIcon &icon, const QString &title, const QString &subTitle);
298 
299  /**
300  * Set a new icon for the toolTip
301  *
302  * @param name the name for the icon
303  */
304  void setToolTipIconByName(const QString &name);
305 
306  /**
307  * @return the name of the toolTip icon
308  * if toolTipImage() is not empty this will always return an empty string
309  */
310  QString toolTipIconName() const;
311 
312  /**
313  * Set a new icon for the toolTip.
314  *
315  * Use setToolTipIconByName(QString) if possible.
316  * @param pixmap representing the icon
317  */
318  void setToolTipIconByPixmap(const QIcon &icon);
319 
320  /**
321  * @return a serialization of the toolTip icon data
322  */
323  QIcon toolTipIconPixmap() const;
324 
325  /**
326  * Sets a new title for the toolTip
327  */
328  void setToolTipTitle(const QString &title);
329 
330  /**
331  * @return the title of the main icon toolTip
332  */
333  QString toolTipTitle() const;
334 
335  /**
336  * Sets a new subtitle for the toolTip
337  */
338  void setToolTipSubTitle(const QString &subTitle);
339 
340  /**
341  * @return the subtitle of the main icon toolTip
342  */
343  QString toolTipSubTitle() const;
344 
345  /**
346  * Sets a new context menu for this StatusNotifierItem.
347  * the menu will be shown with a contextMenu(int,int)
348  * call by the systemtray over D-Bus
349  * usually you don't need to call this unless you want to use
350  * a custom QMenu subclass as context menu.
351  *
352  * The KStatusNotifierItem instance takes ownership of the menu,
353  * and will delete it upon its destruction.
354  */
355  void setContextMenu(QMenu *menu);
356 
357  /**
358  * Access the context menu associated to this status notifier item
359  */
360  QMenu *contextMenu() const;
361 
362  /**
363  * Sets the main widget associated with this StatusNotifierItem
364  *
365  * If you pass contextMenu() as a parent then the menu will be displayed
366  * when the user activate the icon. In this case the activate() method will
367  * not be called and the activateRequested() signal will not be emitted
368  *
369  * @param parent the new main widget: must be a top level window,
370  * if it's not parent->window() will be used instead.
371  */
372  void setAssociatedWidget(QWidget *parent);
373 
374  /**
375  * Access the main widget associated with this StatusNotifierItem
376  */
377  QWidget *associatedWidget() 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 
433 public Q_SLOTS:
434 
435  /**
436  * Shows the main widget and try to position it on top
437  * of the other windows, if the widget 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 widget, 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 widget via activate(const QPoint &)
448  * is not wanted, but instead the hidden state should be reached in any case.
449  *
450  * @since 5.91
451  */
452  void hideAssociatedWidget();
453 
454 Q_SIGNALS:
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  */
483  void secondaryActivateRequested(const QPoint &pos);
484 
485 protected:
486  bool eventFilter(QObject *watched, QEvent *event) override;
487 
488 private:
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
Q_PROPERTY(...)
Q_ENUM(...)
Q_SLOTSQ_SLOTS
ItemStatus
All the possible status this icon can have, depending on the importance of the events that happens in...
virtual bool eventFilter(QObject *watched, QEvent *event)
Orientation
Q_SCRIPTABLE CaptureState status()
Q_SIGNALSQ_SIGNALS
KDE Status notifier Item protocol implementation
ItemCategory
Different kinds of applications announce their type to the systemtray, so can be drawn in a different...
QString message
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon May 8 2023 03:49:15 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.