File indexing completed on 2025-01-19 03:56:07

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2004-07-03
0007  * Description : dialog-like popup that displays messages without interrupting the user
0008  *
0009  * SPDX-FileCopyrightText: 2009-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0010  * SPDX-FileCopyrightText: 2001-2006 by Richard Moore <rich at kde dot org>
0011  * SPDX-FileCopyrightText: 2004-2005 by Sascha Cunz <sascha.cunz at tiscali dot de>
0012  *
0013  * SPDX-License-Identifier: GPL-2.0-or-later
0014  *
0015  * ============================================================ */
0016 
0017 #ifndef DIGIKAM_DNOTIFICATION_POPUP_H
0018 #define DIGIKAM_DNOTIFICATION_POPUP_H
0019 
0020 // Qt includes
0021 
0022 #include <QFrame>
0023 
0024 // Local includes
0025 
0026 #include "digikam_export.h"
0027 
0028 class QSystemTrayIcon;
0029 
0030 namespace Digikam
0031 {
0032 
0033 /**
0034  * @short A dialog-like popup that displays messages without interrupting the user.
0035  *
0036  * The simplest uses of DNotificationPopup are by using the various message() static
0037  * methods. The position the popup appears at depends on the type of the parent window:
0038  *
0039  */
0040 class DIGIKAM_EXPORT DNotificationPopup : public QFrame
0041 {
0042     Q_OBJECT
0043     Q_PROPERTY(bool autoDelete READ autoDelete WRITE setAutoDelete)
0044     Q_PROPERTY(int timeout     READ timeout    WRITE setTimeout)
0045 
0046 public:
0047 
0048     /**
0049      * Styles that a DNotificationPopup can have.
0050      */
0051     enum PopupStyle
0052     {
0053         Boxed,             ///< Information will appear in a framed box (default)
0054         Balloon,           ///< Information will appear in a comic-alike balloon
0055     };
0056 
0057 public:
0058 
0059     /**
0060      * Creates a popup for the specified widget.
0061      */
0062     explicit DNotificationPopup(QWidget* const parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags());
0063 
0064     /**
0065      * Creates a popup for the specified window.
0066      */
0067     explicit DNotificationPopup(WId parent);
0068 
0069     /**
0070      * Cleans up.
0071      */
0072     ~DNotificationPopup() override;
0073 
0074     /**
0075      * Sets the main view to be the specified widget (which must be a child of the popup).
0076      */
0077     void setView(QWidget* child);
0078 
0079     /**
0080      * Creates a standard view then calls setView(QWidget*) .
0081      */
0082     void setView(const QString& caption, const QString& text = QString());
0083 
0084     /**
0085      * Creates a standard view then calls setView(QWidget*) .
0086      */
0087     virtual void setView(const QString& caption, const QString& text, const QPixmap& icon);
0088 
0089     /**
0090      * Returns a widget that is used as standard view if one of the
0091      * setView() methods taking the QString arguments is used.
0092      * You can use the returned widget to customize the passivepopup while
0093      * keeping the look similar to the "standard" passivepopups.
0094      *
0095      * After customizing the widget, pass it to setView( QWidget* )
0096      *
0097      * @param caption The window caption (title) on the popup
0098      * @param text The text for the popup
0099      * @param icon The icon to use for the popup
0100      * @param parent The parent widget used for the returned widget. If left 0,
0101      * then "this", i.e. the passive popup object will be used.
0102      *
0103      * @return a QWidget containing the given arguments, looking like the
0104      * standard passivepopups. The returned widget contains a QVBoxLayout,
0105      * which is accessible through layout().
0106      * @see setView( QWidget * )
0107      * @see setView( const QString&, const QString& )
0108      * @see setView( const QString&, const QString&, const QPixmap& )
0109      */
0110     QWidget* standardView(const QString& caption, const QString& text,
0111                           const QPixmap& icon, QWidget* parent = nullptr);
0112 
0113     /**
0114      * Returns the main view.
0115      */
0116     QWidget* view() const;
0117 
0118     /**
0119      * Returns the delay before the popup is removed automatically.
0120      */
0121     int timeout() const;
0122 
0123     /**
0124      * Sets whether the popup will be deleted when it is hidden.
0125      *
0126      * The default is false (unless created by one of the static
0127      * message() overloads).
0128      */
0129     virtual void setAutoDelete(bool autoDelete);
0130 
0131     /**
0132      * Returns whether the popup will be deleted when it is hidden.
0133      *
0134      * @see setAutoDelete
0135      */
0136     bool autoDelete() const;
0137 
0138     /**
0139      * Returns the position to which this popup is anchored.
0140      */
0141     QPoint anchor() const;
0142 
0143     /**
0144      * Sets the anchor of this popup.
0145      *
0146      * The popup is placed near to the anchor.
0147      */
0148     void setAnchor(const QPoint& anchor);
0149 
0150     /**
0151      * Convenience method that displays popup with the specified  message  beside the
0152      * icon of the specified widget.
0153      * Note that the returned object is destroyed when it is hidden.
0154      * @see setAutoDelete
0155      */
0156     static DNotificationPopup* message(const QString& text, QWidget* parent,
0157                                        const QPoint& p = QPoint());
0158 
0159     /**
0160      * Convenience method that displays popup with the specified  message  beside the
0161      * icon of the specified QSystemTrayIcon.
0162      * Note that the returned object is destroyed when it is hidden.
0163      * @see setAutoDelete
0164      */
0165     static DNotificationPopup* message(const QString& text, QSystemTrayIcon* parent);
0166 
0167     /**
0168      * Convenience method that displays popup with the specified caption and message
0169      * beside the icon of the specified widget.
0170      * Note that the returned object is destroyed when it is hidden.
0171      * @see setAutoDelete
0172      */
0173     static DNotificationPopup* message(const QString& caption, const QString& text,
0174                                        QWidget* parent, const QPoint& p = QPoint());
0175 
0176     /**
0177      * Convenience method that displays popup with the specified caption and message
0178      * beside the icon of the specified QSystemTrayIcon.
0179      * Note that the returned object is destroyed when it is hidden.
0180      * @see setAutoDelete
0181      */
0182     static DNotificationPopup* message(const QString& caption, const QString& text,
0183                                        QSystemTrayIcon* parent);
0184 
0185     /**
0186      * Convenience method that displays popup with the specified icon, caption and
0187      * message beside the icon of the specified widget.
0188      * Note that the returned object is destroyed when it is hidden.
0189      * @see setAutoDelete
0190      */
0191     static DNotificationPopup* message(const QString& caption, const QString& text,
0192                                        const QPixmap& icon, QWidget* parent, int timeout = -1,
0193                                        const QPoint& p = QPoint());
0194 
0195     /**
0196      * Convenience method that displays popup with the specified icon, caption and
0197      * message beside the icon of the specified QSystemTrayIcon.
0198      * Note that the returned object is destroyed when it is hidden.
0199      * @see setAutoDelete
0200      */
0201     static DNotificationPopup* message(const QString& caption, const QString& text,
0202                                        const QPixmap& icon, QSystemTrayIcon* parent, int timeout = -1);
0203 
0204     /**
0205      * Convenience method that displays popup with the specified icon, caption and
0206      * message beside the icon of the specified window.
0207      * Note that the returned object is destroyed when it is hidden.
0208      * @see setAutoDelete
0209      */
0210     static DNotificationPopup* message(const QString& caption, const QString& text,
0211                                        const QPixmap& icon, WId parent,
0212                                        int timeout = -1, const QPoint& p = QPoint());
0213 
0214     /**
0215      * Convenience method that displays popup with the specified popup-style and message beside the
0216      * icon of the specified widget.
0217      * Note that the returned object is destroyed when it is hidden.
0218      * @see setAutoDelete
0219      */
0220     static DNotificationPopup* message(int popupStyle, const QString& text, QWidget* parent, const QPoint& p = QPoint());
0221 
0222     /**
0223      * Convenience method that displays popup with the specified popup-style and message beside the
0224      * icon of the specified QSystemTrayIcon.
0225      * Note that the returned object is destroyed when it is hidden.
0226      * @see setAutoDelete
0227      */
0228     static DNotificationPopup* message(int popupStyle, const QString& text, QSystemTrayIcon* parent);
0229 
0230     /**
0231      * Convenience method that displays popup with the specified popup-style, caption and message
0232      * beside the icon of the specified QSystemTrayIcon.
0233      * Note that the returned object is destroyed when it is hidden.
0234      * @see setAutoDelete
0235      */
0236     static DNotificationPopup* message(int popupStyle, const QString& caption, const QString& text,
0237                                        QSystemTrayIcon* parent);
0238 
0239     /**
0240      * Convenience method that displays popup with the specified popup-style, caption and message
0241      * beside the icon of the specified widget.
0242      * Note that the returned object is destroyed when it is hidden.
0243      * @see setAutoDelete
0244      */
0245     static DNotificationPopup* message(int popupStyle, const QString& caption, const QString& text,
0246                                        QWidget* parent, const QPoint& p = QPoint());
0247 
0248     /**
0249      * Convenience method that displays popup with the specified popup-style, icon, caption and
0250      * message beside the icon of the specified widget.
0251      * Note that the returned object is destroyed when it is hidden.
0252      * @see setAutoDelete
0253      */
0254     static DNotificationPopup* message(int popupStyle, const QString& caption, const QString& text,
0255                                        const QPixmap& icon, QWidget* parent, int timeout = -1,
0256                                        const QPoint& p = QPoint());
0257 
0258     /**
0259      * Convenience method that displays popup with the specified popup-style, icon, caption and
0260      * message beside the icon of the specified QSystemTrayIcon.
0261      * Note that the returned object is destroyed when it is hidden.
0262      * @see setAutoDelete
0263      */
0264     static DNotificationPopup* message(int popupStyle, const QString& caption, const QString& text,
0265                                        const QPixmap& icon, QSystemTrayIcon* parent, int timeout = -1);
0266 
0267     /**
0268      * Convenience method that displays popup with the specified popup-style, icon, caption and
0269      * message beside the icon of the specified window.
0270      * Note that the returned object is destroyed when it is hidden.
0271      * @see setAutoDelete
0272      */
0273     static DNotificationPopup* message(int popupStyle, const QString& caption, const QString& text,
0274                                        const QPixmap& icon, WId parent, int timeout = -1,
0275                                        const QPoint& p = QPoint());
0276 
0277     // we create an overloaded version of show()
0278     using QFrame::show;
0279 
0280 public Q_SLOTS:
0281 
0282     /**
0283      * Sets the delay for the popup is removed automatically. Setting the delay to 0
0284      * disables the timeout, if you're doing this, you may want to connect the
0285      * clicked() signal to the hide() slot.
0286      * Setting the delay to -1 makes it use the default value.
0287      *
0288      * @see timeout
0289      */
0290     void setTimeout(int delay);
0291 
0292     /**
0293      * Sets the visual appearance of the popup.
0294      * @see PopupStyle
0295      */
0296     void setPopupStyle(int popupstyle);
0297 
0298     /**
0299      * Shows the popup in the given point
0300      */
0301     void show(const QPoint& p);
0302 
0303     /**
0304      * @reimp
0305      */
0306     void setVisible(bool visible) override;
0307 
0308 Q_SIGNALS:
0309 
0310     /**
0311      * Emitted when the popup is clicked.
0312      */
0313     void clicked();
0314 
0315     /**
0316      * Emitted when the popup is clicked.
0317      */
0318     void clicked(const QPoint& pos);
0319 
0320 protected:
0321 
0322     /**
0323      * Positions the popup.
0324      *
0325      * The default implementation attempts to place it by the taskbar
0326      * entry; failing that it places it by the window of the associated
0327      * widget; failing that it places it at the location given by
0328      * defaultLocation().
0329      *
0330      * @see moveNear()
0331      */
0332     virtual void positionSelf();
0333 
0334     /**
0335      * Returns a default location for popups when a better placement
0336      * cannot be found.
0337      *
0338      * The default implementation returns the top-left corner of the
0339      * available work area of the desktop (ie: minus panels, etc).
0340      */
0341     virtual QPoint defaultLocation() const;
0342 
0343     /**
0344      * Moves the popup to be adjacent to @p target.
0345      *
0346      * The popup will be placed adjacent to, but outside of, @p target,
0347      * without going off the current desktop.
0348      *
0349      * Reimplementations of positionSelf() can use this to actually
0350      * position the popup.
0351      */
0352     void moveNear(const QRect& target);
0353 
0354     /**
0355      * @reimp
0356      */
0357     void hideEvent(QHideEvent*) override;
0358 
0359     /**
0360      * @reimp
0361      */
0362     void mouseReleaseEvent(QMouseEvent* e) override;
0363 
0364     /**
0365      * @reimp
0366      */
0367     void paintEvent(QPaintEvent* pe) override;
0368 
0369 private:
0370 
0371     class Private;
0372     Private* const d;
0373 };
0374 
0375 } // namespace Digikam
0376 
0377 #endif // DIGIKAM_DNOTIFICATION_POPUP_H