File indexing completed on 2024-04-28 04:37:33
0001 /* 0002 SPDX-FileCopyrightText: 2012-2013 Dominik Haumann <dhaumann@kde.org> 0003 SPDX-FileCopyrightText: 2020 Friedrich W. H. Kossebau <kossebau@kde.org> 0004 0005 SPDX-License-Identifier: LGPL-2.0-or-later 0006 */ 0007 0008 // Forked from KTextEditor::Message at v5.66.0 0009 // Dropped Document/View properties, made wordWrap true by default, dropped position & autoHideMode 0010 // Should be investigated later to turn this and the messagewidget class 0011 // into some reusable generic in-shell-message code, e.g. as KF module 0012 // TODO: re-add autoHideMode once there is an idea how to determine user interaction with the shell 0013 0014 #ifndef KDEVPLATFORM_SUBLIME_MESSAGE_H 0015 #define KDEVPLATFORM_SUBLIME_MESSAGE_H 0016 0017 #include "sublimeexport.h" 0018 // Qt 0019 #include <QAction> 0020 #include <QIcon> 0021 #include <QVector> 0022 #include <QObject> 0023 0024 namespace Sublime 0025 { 0026 0027 /** 0028 * @brief This class holds a Message to display in a message area. 0029 * 0030 * @section message_intro Introduction 0031 * 0032 * The Message class holds the data used to display interactive message widgets 0033 * in the shell. Use the MainWindow::postMessage() to post a message as follows: 0034 * 0035 * @code 0036 * // if you keep a pointer after calling postMessage(), 0037 * // always use a QPointer go guard your Message, 0038 * QPointer<Sublime::Message> message = 0039 * new Sublime::Message("text", Sublime::Message::Information); 0040 * message->addAction(...); // add your actions, if any... 0041 * window->postMessage(message); 0042 * @endcode 0043 * 0044 * A Message is deleted automatically if the Message gets closed, meaning that 0045 * you usually can forget the pointer. If you really need to delete a message 0046 * before the user processed it, always guard it with a QPointer! 0047 * 0048 * @section message_creation Message Creation and Deletion 0049 * 0050 * To create a new Message, use code like this: 0051 * @code 0052 * QPointer<Sublime::Message> message = 0053 * new Sublime::Message("My information text", Message::Information); 0054 * // ... 0055 * @endcode 0056 * 0057 * Although discouraged in general, the text of the Message can be changed 0058 * on the fly when it is already visible with setText(). 0059 * 0060 * Once you posted the Message through MainWindow::postMessage(), the 0061 * lifetime depends on the user interaction. The Message gets automatically 0062 * deleted if the user clicks a closing action in the message, or the set 0063 * timeout is reached. 0064 * 0065 * If you posted a message but want to remove it yourself again, just delete 0066 * the message. But beware of the following warning! 0067 * 0068 * @warning Always use QPointer\<Message\> to guard the message pointer from 0069 * getting invalid, if you need to access the Message after you posted 0070 * it. 0071 * 0072 * @section message_hiding Autohiding Messages 0073 * 0074 * Message%s can be shown for only a short amount of time by using the autohide 0075 * feature. With setAutoHide() a timeout in milliseconds can be set after which 0076 * the Message is automatically hidden. 0077 */ 0078 class KDEVPLATFORMSUBLIME_EXPORT Message : public QObject 0079 { 0080 Q_OBJECT 0081 0082 // 0083 // public data types 0084 // 0085 public: 0086 /** 0087 * Message types used as visual indicator. 0088 * The message types match exactly the behavior of KMessageWidget::MessageType. 0089 * For simple notifications either use Positive or Information. 0090 */ 0091 enum MessageType { 0092 Positive = 0, ///< positive information message 0093 Information, ///< information message type 0094 Warning, ///< warning message type 0095 Error ///< error message type 0096 }; 0097 0098 public: 0099 /** 0100 * Constructor for new messages. 0101 * @param type the message type, e.g. MessageType::Information 0102 * @param richtext text to be displayed 0103 */ 0104 explicit Message(const QString& richtext, MessageType type = Message::Information); 0105 0106 /** 0107 * Destructor. 0108 */ 0109 ~Message() override; 0110 0111 public: 0112 /** 0113 * Returns the text set in the constructor. 0114 */ 0115 QString text() const; 0116 0117 /** 0118 * Returns the icon of this message. 0119 * If the message has no icon set, a null icon is returned. 0120 * @see setIcon() 0121 */ 0122 QIcon icon() const; 0123 0124 /** 0125 * Returns the message type set in the constructor. 0126 */ 0127 MessageType messageType() const; 0128 0129 /** 0130 * Adds an action to the message. 0131 * 0132 * By default (@p closeOnTrigger = true), the action closes the message 0133 * displayed. If @p closeOnTrigger is @e false, the message is stays open. 0134 * 0135 * The actions will be displayed in the order you added the actions. 0136 * 0137 * To connect to an action, use the following code: 0138 * @code 0139 * connect(action, &QAction::triggered, receiver, &ReceiverType::slotActionTriggered); 0140 * @endcode 0141 * 0142 * @param action action to be added 0143 * @param closeOnTrigger when triggered, the message widget is closed 0144 * 0145 * @warning The added actions are deleted automatically. 0146 * So do @em not delete the added actions yourself. 0147 */ 0148 void addAction(QAction* action, bool closeOnTrigger = true); 0149 0150 /** 0151 * Accessor to all actions, mainly used in the internal implementation 0152 * to add the actions into the gui. 0153 * @see addAction() 0154 */ 0155 QVector<QAction*> actions() const; 0156 0157 /** 0158 * Set the auto hide time to @p delay milliseconds. 0159 * If @p delay < 0, auto hide is disabled. 0160 * If @p delay = 0, auto hide is enabled and set to a sane default 0161 * value of several seconds. 0162 * 0163 * By default, auto hide is disabled. 0164 * 0165 * @see autoHide() 0166 */ 0167 void setAutoHide(int delay = 0); 0168 0169 /** 0170 * Returns the auto hide time in milliseconds. 0171 * Please refer to setAutoHide() for an explanation of the return value. 0172 * 0173 * @see setAutoHide() 0174 */ 0175 int autoHide() const; 0176 0177 /** 0178 * Enabled word wrap according to @p wordWrap. 0179 * By default, auto wrap is enabled. 0180 * 0181 * Word wrap is enabled automatically, if the Message's width is larger than 0182 * the parent widget's width to avoid breaking the gui layout. 0183 * 0184 * @see wordWrap() 0185 */ 0186 void setWordWrap(bool wordWrap); 0187 0188 /** 0189 * Check, whether word wrap is enabled or not. 0190 * 0191 * @see setWordWrap() 0192 */ 0193 bool wordWrap() const; 0194 0195 /** 0196 * Set the priority of this message to @p priority. 0197 * Messages with higher priority are shown first. 0198 * The default priority is 0. 0199 * 0200 * @see priority() 0201 */ 0202 void setPriority(int priority); 0203 0204 /** 0205 * Returns the priority of the message. 0206 * 0207 * @see setPriority() 0208 */ 0209 int priority() const; 0210 0211 public Q_SLOTS: 0212 /** 0213 * Sets the notification contents to @p richtext. 0214 * If the message was already sent through MainWindow::postMessage(), 0215 * the displayed text changes on the fly. 0216 * @note Change text on the fly with care, since changing the text may 0217 * resize the notification widget, which may result in a distracting 0218 * user experience. 0219 * @param richtext new notification text (rich text supported) 0220 * @see textChanged() 0221 */ 0222 void setText(const QString& richtext); 0223 0224 /** 0225 * Add an optional @p icon for this notification which will be shown next to 0226 * the message text. If the message was already sent through MainWindow::postMessage(), 0227 * the displayed icon changes on the fly. 0228 * @note Change the icon on the fly with care, since changing the text may 0229 * resize the notification widget, which may result in a distracting 0230 * user experience. 0231 * @param icon the icon to be displayed 0232 * @see iconChanged() 0233 */ 0234 void setIcon(const QIcon& icon); 0235 0236 Q_SIGNALS: 0237 /** 0238 * This signal is emitted before the @p message is deleted. Afterwards, this 0239 * pointer is invalid. 0240 * 0241 * @param message the closed/processed message 0242 */ 0243 void closed(Message* message); 0244 0245 /** 0246 * This signal is emitted whenever setText() was called. 0247 * 0248 * @param text the new notification text (rich text supported) 0249 * @see setText() 0250 */ 0251 void textChanged(const QString& text); 0252 0253 /** 0254 * This signal is emitted whenever setIcon() was called. 0255 * @param icon the new notification icon 0256 * @see setIcon() 0257 */ 0258 void iconChanged(const QIcon& icon); 0259 0260 private: 0261 const QScopedPointer<class MessagePrivate> d; 0262 }; 0263 0264 } 0265 0266 #endif