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 : 2011-07-03 0007 * Description : A widget to provide feedback or propose opportunistic interactions 0008 * 0009 * SPDX-FileCopyrightText: 2009-2024 by Gilles Caulier <caulier dot gilles at gmail dot com> 0010 * SPDX-FileCopyrightText: 2011 by Aurelien Gateau <agateau at kde dot org> 0011 * SPDX-FileCopyrightText: 2014 by Dominik Haumann <dhaumann at kde dot org> 0012 * 0013 * SPDX-License-Identifier: GPL-2.0-or-later 0014 * 0015 * ============================================================ */ 0016 0017 #ifndef DIGIKAM_DNOTIFICATION_WIDGET_H 0018 #define DIGIKAM_DNOTIFICATION_WIDGET_H 0019 0020 // Qt includes 0021 0022 #include <QFrame> 0023 #include <QIcon> 0024 #include <QString> 0025 #include <QAction> 0026 0027 // Local includes 0028 0029 #include "digikam_export.h" 0030 0031 namespace Digikam 0032 { 0033 0034 /** 0035 * This widget can be used to provide inline positive or negative 0036 * feedback, or to implement opportunistic interactions. 0037 */ 0038 class DIGIKAM_EXPORT DNotificationWidget : public QFrame 0039 { 0040 Q_OBJECT 0041 Q_PROPERTY(QString text READ text WRITE setText) 0042 Q_PROPERTY(bool wordWrap READ wordWrap WRITE setWordWrap) 0043 Q_PROPERTY(bool closeButtonVisible READ isCloseButtonVisible WRITE setCloseButtonVisible) 0044 Q_PROPERTY(MessageType messageType READ messageType WRITE setMessageType) 0045 Q_PROPERTY(QIcon icon READ icon WRITE setIcon) 0046 0047 public: 0048 0049 /** 0050 * Available message types. 0051 * The background colors are chosen depending on the message type. 0052 */ 0053 enum MessageType 0054 { 0055 Positive, 0056 Information, 0057 Warning, 0058 Error 0059 }; 0060 Q_ENUM(MessageType) 0061 0062 public: 0063 0064 /** 0065 * Constructs a DNotificationWidget with the specified @p parent. 0066 */ 0067 explicit DNotificationWidget(QWidget* const parent = nullptr); 0068 0069 /** 0070 * Constructs a DNotificationWidget with the specified @p parent and 0071 * contents @p text. 0072 */ 0073 explicit DNotificationWidget(const QString& text, QWidget* const parent = nullptr); 0074 0075 /** 0076 * Destructor. 0077 */ 0078 ~DNotificationWidget() override; 0079 0080 /** 0081 * Get the text of this message widget. 0082 * @see setText() 0083 */ 0084 QString text() const; 0085 0086 /** 0087 * Check whether word wrap is enabled. 0088 * 0089 * If word wrap is enabled, the message widget wraps the displayed text 0090 * as required to the available width of the widget. This is useful to 0091 * avoid breaking widget layouts. 0092 * 0093 * @see setWordWrap() 0094 */ 0095 bool wordWrap() const; 0096 0097 /** 0098 * Check whether the close button is visible. 0099 * 0100 * @see setCloseButtonVisible() 0101 */ 0102 bool isCloseButtonVisible() const; 0103 0104 /** 0105 * Get the type of this message. 0106 * By default, the type is set to DNotificationWidget::Information. 0107 * 0108 * @see DNotificationWidget::MessageType, setMessageType() 0109 */ 0110 MessageType messageType() const; 0111 0112 /** 0113 * Add @p action to the message widget. 0114 * For each action a button is added to the message widget in the 0115 * order the actions were added. 0116 * 0117 * @param action the action to add 0118 * @see removeAction(), QWidget::actions() 0119 */ 0120 void addAction(QAction* action); 0121 0122 /** 0123 * Remove @p action from the message widget. 0124 * 0125 * @param action the action to remove 0126 * @see DNotificationWidget::MessageType, addAction(), setMessageType() 0127 */ 0128 void removeAction(QAction* action); 0129 0130 /** 0131 * clear all actions from the message widget. 0132 * 0133 * @see DNotificationWidget::MessageType, addAction(), setMessageType() 0134 */ 0135 void clearAllActions(); 0136 0137 /** 0138 * Returns the preferred size of the message widget. 0139 */ 0140 QSize sizeHint() const override; 0141 0142 /** 0143 * Returns the minimum size of the message widget. 0144 */ 0145 QSize minimumSizeHint() const override; 0146 0147 /** 0148 * Returns the required height for @p width. 0149 * @param width the width in pixels 0150 */ 0151 int heightForWidth(int width) const override; 0152 0153 /** 0154 * The icon shown on the left of the text. By default, no icon is shown. 0155 */ 0156 QIcon icon() const; 0157 0158 /** 0159 * Check whether the hide animation started by calling animatedHide() 0160 * is still running. If animations are disabled, this function always 0161 * returns @e false. 0162 * 0163 * @see animatedHide(), hideAnimationFinished() 0164 */ 0165 bool isHideAnimationRunning() const; 0166 0167 /** 0168 * Check whether the show animation started by calling animatedShow() 0169 * is still running. If animations are disabled, this function always 0170 * returns @e false. 0171 * 0172 * @see animatedShow(), showAnimationFinished() 0173 */ 0174 bool isShowAnimationRunning() const; 0175 0176 /** 0177 * Show the widget using an animation. The widget is automatically hidden after the delay (in ms). 0178 */ 0179 void animatedShowTemporized(int delay); 0180 0181 public Q_SLOTS: 0182 0183 /** 0184 * Set the text of the message widget to @p text. 0185 * If the message widget is already visible, the text changes on the fly. 0186 * 0187 * @param text the text to display, rich text is allowed 0188 * @see text() 0189 */ 0190 void setText(const QString& text); 0191 0192 /** 0193 * Set word wrap to @p wordWrap. If word wrap is enabled, the text() 0194 * of the message widget is wrapped to fit the available width. 0195 * If word wrap is disabled, the message widget's minimum size is 0196 * such that the entire text fits. 0197 * 0198 * @param wordWrap disable/enable word wrap 0199 * @see wordWrap() 0200 */ 0201 void setWordWrap(bool wordWrap); 0202 0203 /** 0204 * Set the visibility of the close button. If @p visible is @e true, 0205 * a close button is shown that calls animatedHide() if clicked. 0206 * 0207 * @see closeButtonVisible(), animatedHide() 0208 */ 0209 void setCloseButtonVisible(bool visible); 0210 0211 /** 0212 * Set the message type to @p type. 0213 * By default, the message type is set to DNotificationWidget::Information. 0214 * 0215 * @see messageType(), DNotificationWidget::MessageType 0216 */ 0217 void setMessageType(DNotificationWidget::MessageType type); 0218 0219 /** 0220 * Show the widget using an animation. 0221 */ 0222 void animatedShow(); 0223 0224 /** 0225 * Hide the widget using an animation. 0226 */ 0227 void animatedHide(); 0228 0229 /** 0230 * Define an icon to be shown on the left of the text 0231 */ 0232 void setIcon(const QIcon& icon); 0233 0234 Q_SIGNALS: 0235 0236 /** 0237 * This signal is emitted when the user clicks a link in the text label. 0238 * The URL referred to by the href anchor is passed in contents. 0239 * @param contents text of the href anchor 0240 * @see QLabel::linkActivated() 0241 */ 0242 void linkActivated(const QString& contents); 0243 0244 /** 0245 * This signal is emitted when the user hovers over a link in the text label. 0246 * The URL referred to by the href anchor is passed in contents. 0247 * @param contents text of the href anchor 0248 * @see QLabel::linkHovered() 0249 */ 0250 void linkHovered(const QString& contents); 0251 0252 /** 0253 * This signal is emitted when the hide animation is finished, started by 0254 * calling animatedHide(). If animations are disabled, this signal is 0255 * emitted immediately after the message widget got hidden. 0256 * 0257 * @note This signal is @e not emitted if the widget was hidden by 0258 * calling hide(), so this signal is only useful in conjunction 0259 * with animatedHide(). 0260 * 0261 * @see animatedHide() 0262 */ 0263 void hideAnimationFinished(); 0264 0265 /** 0266 * This signal is emitted when the show animation is finished, started by 0267 * calling animatedShow(). If animations are disabled, this signal is 0268 * emitted immediately after the message widget got shown. 0269 * 0270 * @note This signal is @e not emitted if the widget was shown by 0271 * calling show(), so this signal is only useful in conjunction 0272 * with animatedShow(). 0273 * 0274 * @see animatedShow() 0275 */ 0276 void showAnimationFinished(); 0277 0278 private Q_SLOTS: 0279 0280 void slotTimerTimeout(); 0281 0282 protected: 0283 0284 void paintEvent(QPaintEvent* event) override; 0285 bool event(QEvent* event) override; 0286 void resizeEvent(QResizeEvent* event) override; 0287 0288 private: 0289 0290 class Private; 0291 Private* const d; 0292 0293 friend class Private; 0294 }; 0295 0296 } // namespace Digikam 0297 0298 #endif // DIGIKAM_DNOTIFICATION_WIDGET_H