KNotifications

kpassivepopup.h
1 // -*- c++ -*-
2 /*
3  SPDX-FileCopyrightText: 2001-2006 Richard Moore <[email protected]>
4  SPDX-FileCopyrightText: 2004-2005 Sascha Cunz <[email protected]>
5 
6  SPDX-License-Identifier: LGPL-2.0-or-later
7 */
8 
9 #ifndef KPASSIVEPOPUP_H
10 #define KPASSIVEPOPUP_H
11 
12 #include <knotifications_export.h>
13 
14 #ifdef QT_WIDGETS_LIB
15 #include <QFrame>
16 #endif
17 
18 #include <memory>
19 
20 class QSystemTrayIcon;
21 
22 #if KNOTIFICATIONS_ENABLE_DEPRECATED_SINCE(5, 79)
23 /**
24  * @class KPassivePopup kpassivepopup.h KPassivePopup
25  *
26  * @short A dialog-like popup that displays messages without interrupting the user.
27  *
28  * The simplest uses of KPassivePopup are by using the various message() static
29  * methods. The position the popup appears at depends on the type of the parent window:
30  *
31  * @li Normal Windows: The popup is placed adjacent to the icon of the window.
32  * @li System Tray Windows: The popup is placed adjacent to the system tray window itself.
33  * @li Skip Taskbar Windows: The popup is placed adjacent to the window
34  * itself if it is visible, and at the edge of the desktop otherwise.
35  *
36  * You also have the option of calling show with a QPoint as a parameter that
37  * removes the automatic placing of KPassivePopup and shows it in the point you want.
38  *
39  * The most basic use of KPassivePopup displays a popup containing a piece of text:
40  * \code
41  * KPassivePopup::message( "This is the message", this );
42  * \endcode
43  * We can create popups with titles and icons too, as this example shows:
44  * \code
45  * QPixmap px;
46  * px.load( "hi32-app-logtracker.png" );
47  * KPassivePopup::message( "Some title", "This is the main text", px, this );
48  * \endcode
49  * This screenshot shows a popup with both a caption and a main text which is
50  * being displayed next to the toolbar icon of the window that triggered it:
51  * \image html kpassivepopup.png "A passive popup"
52  *
53  * For more control over the popup, you can use the setView(QWidget *) method
54  * to create a custom popup.
55  * \code
56  * KPassivePopup *pop = new KPassivePopup( parent );
57  *
58  * QWidget* content = new QWidget( pop );
59  * QVBoxLayout* vBox = new QVBoxLayout( content );
60  * QLabel* label = new QLabel( "<b>Isn't this great?</b>", content );
61  * vBox->addWidget( label );
62  *
63  * QPushButton* btnYes = new QPushButton( "Yes", content );
64  * QPushButton* btnNo = new QPushButton( "No", content );
65  *
66  * QHBoxLayout* hBox = new QHBoxLayout( content );
67  * hBox->addWidget( btnYes );
68  * hBox->addWidget( btnNo );
69  *
70  * vBox->addLayout( vBox );
71  *
72  * pop->setView( content );
73  * pop->show();
74  * \endcode
75  *
76  * @deprecated since 5.79, use KNotification
77  *
78  * @author Richard Moore, [email protected]
79  * @author Sascha Cunz, [email protected]
80  */
81 class KNOTIFICATIONS_EXPORT KPassivePopup : public QFrame
82 {
83  Q_OBJECT
84  Q_PROPERTY(bool autoDelete READ autoDelete WRITE setAutoDelete)
85  Q_PROPERTY(int timeout READ timeout WRITE setTimeout)
86 
87 public:
88  /**
89  * Styles that a KPassivePopup can have.
90  */
91  enum PopupStyle {
92  Boxed, ///< Information will appear in a framed box (default)
93  Balloon, ///< Information will appear in a comic-alike balloon
94  };
95 
96  /**
97  * Creates a popup for the specified widget.
98  * @deprecated since 5.79, use KNotification
99  */
100  KNOTIFICATIONS_DEPRECATED_VERSION(5, 79, "Use KNotification")
101  explicit KPassivePopup(QWidget *parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags());
102 
103  /**
104  * Creates a popup for the specified window.
105  */
106  KNOTIFICATIONS_DEPRECATED_VERSION(5, 79, "Use KNotification")
107  explicit KPassivePopup(WId parent);
108 
109  /**
110  * Cleans up.
111  */
112  ~KPassivePopup() override;
113 
114  /**
115  * Sets the main view to be the specified widget (which must be a child of the popup).
116  */
117  void setView(QWidget *child);
118 
119  /**
120  * Creates a standard view then calls setView(QWidget*) .
121  */
122  void setView(const QString &caption, const QString &text = QString());
123 
124  /**
125  * Creates a standard view then calls setView(QWidget*) .
126  */
127  virtual void setView(const QString &caption, const QString &text, const QPixmap &icon);
128 
129  /**
130  * Returns a widget that is used as standard view if one of the
131  * setView() methods taking the QString arguments is used.
132  * You can use the returned widget to customize the passivepopup while
133  * keeping the look similar to the "standard" passivepopups.
134  *
135  * After customizing the widget, pass it to setView( QWidget* )
136  *
137  * @param caption The window caption (title) on the popup
138  * @param text The text for the popup
139  * @param icon The icon to use for the popup
140  * @param parent The parent widget used for the returned widget. If left 0,
141  * then "this", i.e. the passive popup object will be used.
142  *
143  * @return a QWidget containing the given arguments, looking like the
144  * standard passivepopups. The returned widget contains a QVBoxLayout,
145  * which is accessible through layout().
146  * @see setView( QWidget * )
147  * @see setView( const QString&, const QString& )
148  * @see setView( const QString&, const QString&, const QPixmap& )
149  */
150  QWidget *standardView(const QString &caption, const QString &text, const QPixmap &icon, QWidget *parent = nullptr);
151 
152  /**
153  * Returns the main view.
154  */
155  QWidget *view() const;
156 
157  /**
158  * Returns the delay before the popup is removed automatically.
159  */
160  int timeout() const;
161 
162  /**
163  * Sets whether the popup will be deleted when it is hidden.
164  *
165  * The default is false (unless created by one of the static
166  * message() overloads).
167  */
168  virtual void setAutoDelete(bool autoDelete);
169 
170  /**
171  * Returns whether the popup will be deleted when it is hidden.
172  *
173  * @see setAutoDelete
174  */
175  bool autoDelete() const;
176 
177  /**
178  * Returns the position to which this popup is anchored.
179  */
180  QPoint anchor() const;
181 
182  /**
183  * Sets the anchor of this popup.
184  *
185  * The popup is placed near to the anchor.
186  */
187  void setAnchor(const QPoint &anchor);
188 
189  /**
190  * Convenience method that displays popup with the specified message beside the
191  * icon of the specified widget.
192  * Note that the returned object is destroyed when it is hidden.
193  * @see setAutoDelete
194  * @deprecated since 5.79, use KNotification
195  */
196  KNOTIFICATIONS_DEPRECATED_VERSION(5, 79, "Use KNotification")
197  static KPassivePopup *message(const QString &text, QWidget *parent, const QPoint &p = QPoint());
198 
199  /**
200  * Convenience method that displays popup with the specified message beside the
201  * icon of the specified QSystemTrayIcon.
202  * Note that the returned object is destroyed when it is hidden.
203  * @see setAutoDelete
204  * @deprecated since 5.79, use KNotification
205  */
206  KNOTIFICATIONS_DEPRECATED_VERSION(5, 79, "Use KNotification")
207  static KPassivePopup *message(const QString &text, QSystemTrayIcon *parent);
208 
209  /**
210  * Convenience method that displays popup with the specified caption and message
211  * beside the icon of the specified widget.
212  * Note that the returned object is destroyed when it is hidden.
213  * @see setAutoDelete
214  * @deprecated since 5.79, use KNotification
215  */
216  KNOTIFICATIONS_DEPRECATED_VERSION(5, 79, "Use KNotification")
217  static KPassivePopup *message(const QString &caption, const QString &text, QWidget *parent, const QPoint &p = QPoint());
218 
219  /**
220  * Convenience method that displays popup with the specified caption and message
221  * beside the icon of the specified QSystemTrayIcon.
222  * Note that the returned object is destroyed when it is hidden.
223  * @see setAutoDelete
224  * @deprecated since 5.79, use KNotification
225  */
226  KNOTIFICATIONS_DEPRECATED_VERSION(5, 79, "Use KNotification")
227  static KPassivePopup *message(const QString &caption, const QString &text, QSystemTrayIcon *parent);
228 
229  /**
230  * Convenience method that displays popup with the specified icon, caption and
231  * message beside the icon of the specified widget.
232  * Note that the returned object is destroyed when it is hidden.
233  * @see setAutoDelete
234  * @deprecated since 5.79, use KNotification
235  */
236  KNOTIFICATIONS_DEPRECATED_VERSION(5, 79, "Use KNotification")
237  static KPassivePopup *
238  message(const QString &caption, const QString &text, const QPixmap &icon, QWidget *parent, int timeout = -1, const QPoint &p = QPoint());
239 
240  /**
241  * Convenience method that displays popup with the specified icon, caption and
242  * message beside the icon of the specified QSystemTrayIcon.
243  * Note that the returned object is destroyed when it is hidden.
244  * @see setAutoDelete
245  * @deprecated since 5.79, use KNotification
246  */
247  KNOTIFICATIONS_DEPRECATED_VERSION(5, 79, "Use KNotification")
248  static KPassivePopup *message(const QString &caption, const QString &text, const QPixmap &icon, QSystemTrayIcon *parent, int timeout = -1);
249 
250  /**
251  * Convenience method that displays popup with the specified icon, caption and
252  * message beside the icon of the specified window.
253  * Note that the returned object is destroyed when it is hidden.
254  * @see setAutoDelete
255  * @deprecated since 5.79, use KNotification
256  */
257  KNOTIFICATIONS_DEPRECATED_VERSION(5, 79, "Use KNotification")
258  static KPassivePopup *message(const QString &caption, const QString &text, const QPixmap &icon, WId parent, int timeout = -1, const QPoint &p = QPoint());
259 
260  /**
261  * Convenience method that displays popup with the specified popup-style and message beside the
262  * icon of the specified widget.
263  * Note that the returned object is destroyed when it is hidden.
264  * @see setAutoDelete
265  * @deprecated since 5.79, use KNotification
266  */
267  KNOTIFICATIONS_DEPRECATED_VERSION(5, 79, "Use KNotification")
268  static KPassivePopup *message(int popupStyle, const QString &text, QWidget *parent, const QPoint &p = QPoint());
269 
270  /**
271  * Convenience method that displays popup with the specified popup-style and message beside the
272  * icon of the specified QSystemTrayIcon.
273  * Note that the returned object is destroyed when it is hidden.
274  * @see setAutoDelete
275  * @deprecated since 5.79, use KNotification
276  */
277  KNOTIFICATIONS_DEPRECATED_VERSION(5, 79, "Use KNotification")
278  static KPassivePopup *message(int popupStyle, const QString &text, QSystemTrayIcon *parent);
279 
280  /**
281  * Convenience method that displays popup with the specified popup-style, caption and message
282  * beside the icon of the specified QSystemTrayIcon.
283  * Note that the returned object is destroyed when it is hidden.
284  * @see setAutoDelete
285  * @deprecated since 5.79, use KNotification
286  */
287  KNOTIFICATIONS_DEPRECATED_VERSION(5, 79, "Use KNotification")
288  static KPassivePopup *message(int popupStyle, const QString &caption, const QString &text, QSystemTrayIcon *parent);
289 
290  /**
291  * Convenience method that displays popup with the specified popup-style, caption and message
292  * beside the icon of the specified widget.
293  * Note that the returned object is destroyed when it is hidden.
294  * @see setAutoDelete
295  * @deprecated since 5.79, use KNotification
296  */
297  KNOTIFICATIONS_DEPRECATED_VERSION(5, 79, "Use KNotification")
298  static KPassivePopup *message(int popupStyle, const QString &caption, const QString &text, QWidget *parent, const QPoint &p = QPoint());
299 
300  /**
301  * Convenience method that displays popup with the specified popup-style, icon, caption and
302  * message beside the icon of the specified widget.
303  * Note that the returned object is destroyed when it is hidden.
304  * @see setAutoDelete
305  * @deprecated since 5.79, use KNotification
306  */
307  KNOTIFICATIONS_DEPRECATED_VERSION(5, 79, "Use KNotification")
308  static KPassivePopup *
309  message(int popupStyle, const QString &caption, const QString &text, const QPixmap &icon, QWidget *parent, int timeout = -1, const QPoint &p = QPoint());
310 
311  /**
312  * Convenience method that displays popup with the specified popup-style, icon, caption and
313  * message beside the icon of the specified QSystemTrayIcon.
314  * Note that the returned object is destroyed when it is hidden.
315  * @see setAutoDelete
316  * @deprecated since 5.79, use KNotification
317  */
318  KNOTIFICATIONS_DEPRECATED_VERSION(5, 79, "Use KNotification")
319  static KPassivePopup *message(int popupStyle, const QString &caption, const QString &text, const QPixmap &icon, QSystemTrayIcon *parent, int timeout = -1);
320 
321  /**
322  * Convenience method that displays popup with the specified popup-style, icon, caption and
323  * message beside the icon of the specified window.
324  * Note that the returned object is destroyed when it is hidden.
325  * @see setAutoDelete
326  * @deprecated since 5.79, use KNotification
327  */
328  KNOTIFICATIONS_DEPRECATED_VERSION(5, 79, "Use KNotification")
329  static KPassivePopup *
330  message(int popupStyle, const QString &caption, const QString &text, const QPixmap &icon, WId parent, int timeout = -1, const QPoint &p = QPoint());
331 
332  // we create an overloaded version of show()
333  using QFrame::show;
334 
335 public Q_SLOTS:
336  /**
337  * Sets the delay for the popup is removed automatically. Setting the delay to 0
338  * disables the timeout, if you're doing this, you may want to connect the
339  * clicked() signal to the hide() slot.
340  * Setting the delay to -1 makes it use the default value.
341  *
342  * @see timeout
343  */
344  void setTimeout(int delay);
345 
346  /**
347  * Sets the visual appearance of the popup.
348  * @see PopupStyle
349  */
350  void setPopupStyle(int popupstyle);
351 
352  /**
353  * Shows the popup in the given point
354  */
355  void show(const QPoint &p);
356 
357  /** @reimp */
358  void setVisible(bool visible) override;
359 
360 Q_SIGNALS:
361  /**
362  * Emitted when the popup is clicked.
363  */
364  void clicked(); // clazy:exclude=overloaded-signal
365 
366  /**
367  * Emitted when the popup is clicked.
368  */
369  void clicked(const QPoint &pos); // clazy:exclude=overloaded-signal
370 
371 protected:
372  /**
373  * Positions the popup.
374  *
375  * The default implementation attempts to place it by the taskbar
376  * entry; failing that it places it by the window of the associated
377  * widget; failing that it places it at the location given by
378  * defaultLocation().
379  *
380  * @see moveNear()
381  */
382  virtual void positionSelf();
383 
384  /**
385  * Returns a default location for popups when a better placement
386  * cannot be found.
387  *
388  * The default implementation returns the top-left corner of the
389  * available work area of the desktop (ie: minus panels, etc).
390  */
391  virtual QPoint defaultLocation() const;
392 
393  /**
394  * Moves the popup to be adjacent to @p target.
395  *
396  * The popup will be placed adjacent to, but outside of, @p target,
397  * without going off the current desktop.
398  *
399  * Reimplementations of positionSelf() can use this to actually
400  * position the popup.
401  */
402  void moveNear(const QRect &target);
403 
404  /** @reimp */
405  void hideEvent(QHideEvent *) override;
406 
407  /** @reimp */
408  void mouseReleaseEvent(QMouseEvent *e) override;
409 
410  /** @reimp */
411  void paintEvent(QPaintEvent *pe) override;
412 
413 private:
414  /* @internal */
415  class Private;
416  std::unique_ptr<Private> const d;
417 };
418 
419 #endif
420 
421 #endif // KPASSIVEPOPUP_H
422 
423 // Local Variables:
424 // End:
Q_PROPERTY(...)
@ Boxed
Information will appear in a framed box (default)
Definition: kpassivepopup.h:92
@ Balloon
Information will appear in a comic-alike balloon.
Definition: kpassivepopup.h:93
A dialog-like popup that displays messages without interrupting the user.
Definition: kpassivepopup.h:81
PopupStyle
Styles that a KPassivePopup can have.
Definition: kpassivepopup.h:91
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.