File indexing completed on 2024-04-28 03:59:08

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2020 Ahmad Samir <a.samirh78@gmail.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0006 */
0007 
0008 #ifndef KMESSAGEDIALOG_H
0009 #define KMESSAGEDIALOG_H
0010 
0011 #include <kwidgetsaddons_export.h>
0012 
0013 #include <KStandardGuiItem>
0014 
0015 #include <QDialog>
0016 #include <QMessageBox>
0017 
0018 #include <memory>
0019 
0020 class KMessageDialogPrivate;
0021 class KGuiItem;
0022 class QWidget;
0023 
0024 /**
0025  * @class KMessageDialog kmessagedialog.h <KMessageDialog>
0026  *
0027  *
0028  * @brief KMessageDialog creates a message box similar to the ones you get from KMessageBox,
0029  * but that can be used asynchronously, i.e. you can show the dialog by using @c show()
0030  * or @c open().
0031  *
0032  * This class contructs a dialog similar to the dialogs the KMessageBox convenience functions
0033  * create. The main difference is that the KMessageBox methods typically use @c exec() to show
0034  * the dialogs; one of the main disadvantages of using @c exec(), is that it starts a nested
0035  * eventloop, which could lead to nasty crashes.
0036  *
0037  * Another difference is that the API is supposed to be slightly easier to use as it has
0038  * various methods to set up the dialog, e.g. @ref setCaption(), @ref setDetails() ...etc.
0039  *
0040  * By default, appropriate buttons based on the dialog type are added (since 5.85) (e.g. an
0041  * "OK" button is added to an Information dialog), you can set custom buttons by using the
0042  * @ref setButtons() method.
0043  *
0044  * The @ref QDialog::done() slot is called to set the result of the dialog, which will emit the
0045  * @ref QDialog::finished() signal with that result. The result is one of the
0046  * KMessageDialog::ButtonType enum. This is useful as you can tell exactly which button
0047  * was clicked by the user. E.g.:
0048  * - the secondary action button having been clicked, in which case you may still want to save the status
0049  *   of the "Do not ask again" CheckBox
0050  * - the "Cancel" button having been clicked, in which case you ideally will ignore the status
0051  *   of the "Do not ask again" CheckBox
0052  *
0053  * For "warning" dialogs, i.e. dialogs with a potentially destructive action, the default
0054  * button is set to a button with the QDialogButtonBox::RejectRole. If the "Cancel" button
0055 + * is used, it will be the default, otherwise the secondary action button.
0056  *
0057  * This class intends to be very flexible with the buttons that can be used, since you can
0058  * call the @ref setButtons() method with a KGuiItem that has custom text/icon.
0059  *
0060  * Since Frameworks 5.97 a notification sound is played when the dialog opens like KMessageBox
0061  * does, this can be controlled using the @ref setNotifyEnabled() method.
0062  *
0063  * Example:
0064  * @code
0065  * auto *dlg = new KMessageDialog(KMessageDialog::QuestionTwoActionsCancel,
0066  *                                QStringLiteral("Back or forward?"),
0067  *                                nullptr);
0068  *
0069  * dlg->setCaption(QStringLiteral("Window Title"));
0070  * dlg->setButtons(KStandardGuiItem::back(), KStandardGuiItem::forward(), KStandardGuiItem::cancel());
0071  * dlg->setListWidgetItems(QStringList{QStringLiteral("file1"), QStringLiteral("file2")});
0072  * dlg->setDetails(QStringLiteral("Some more details."));
0073  * dlg->setDontAskAgainText(QStringLiteral("Do not ask again"));
0074  * dlg->setDontAskAgainChecked(false);
0075  *
0076  *  // Delete the dialog when it's closed
0077  *  dlg->setAttribute(Qt::WA_DeleteOnClose);
0078  *  // Make the dialog window modal
0079  *  dlg->setWindowModality(Qt::WindowModal);
0080  *
0081  *  QObject::connect(dlg, &QDialog::finished, &app, [dlg](int result) {
0082  *      auto button = static_cast<KMessageDialog::ButtonType>(result);
0083  *      switch(button) {
0084  *      case KMessageDialog::PrimaryAction:
0085  *          // The user clicked the primary action, handle the result...
0086  *          // save the "do not ask again" box status...
0087  *          break;
0088  *      case KMessageDialog::SecondaryAction:
0089  *          // The user clicked the secondary action, handle the result...
0090  *          // save the "do not ask again" box status...
0091  *          break;
0092  *      case KMessageDialog::Cancel:
0093  *          // The user clicked cancel, reject the changes...
0094  *          break;
0095  *      default:
0096  *          break;
0097  *      }
0098  *  });
0099  *
0100  *  dlg->show();
0101  * @endcode
0102  *
0103  * @since 5.77
0104  */
0105 
0106 class KWIDGETSADDONS_EXPORT KMessageDialog : public QDialog
0107 {
0108     Q_OBJECT
0109 
0110 public:
0111     /**
0112      * Button types
0113      * @since 5.100
0114      */
0115     enum ButtonType {
0116         Ok = 1, ///< Ok button
0117         Cancel = 2, ///< Cancel button
0118         PrimaryAction = 3, ///< Primary action button
0119         SecondaryAction = 4, ///< Secondary action button
0120     };
0121 
0122     enum Type {
0123         QuestionTwoActions = 1, ///< Question dialog with two buttons; @since 5.100
0124         QuestionTwoActionsCancel = 2, ///< Question dialog with two buttons and Cancel; @since 5.100
0125         WarningTwoActions = 3, ///< Warning dialog with two buttons; @since 5.100
0126         WarningTwoActionsCancel = 4, ///< Warning dialog with two buttons and Cancel; @since 5.100
0127         WarningContinueCancel = 5, ///< Warning dialog with Continue and Cancel
0128         Information = 6, ///< Information dialog
0129         Error = 8, ///< Error dialog
0130     };
0131 
0132     /**
0133      * Constructs a KMessageDialog.
0134      *
0135      * Buttons based on the dialog type are set by default in some cases,
0136      * using KStandardGuiItem instances. For the dialog types Information and Error
0137      * the button is set to KStandardGuiItem::ok(). For the type WarningContinueCancel
0138      * the buttons are set to  KStandardGuiItem::cont() & KStandardGuiItem::cancel().
0139      *
0140      * For the other Quesion* and Warning* types the buttons are to be set explicitly.
0141      *
0142      * @param type the dialog Type, one of KMessageDialog::Type enum
0143      * @param text the text message that is going to be displayed in the dialog
0144      * @param parent a QWidget* that will be set as the dialog parent
0145      */
0146     explicit KMessageDialog(KMessageDialog::Type type, const QString &text, QWidget *parent = nullptr);
0147 
0148     /**
0149      * This constructor takes the window Id of the parent window, instead of a QWidget*.
0150      *
0151      * @param type the dialog Type, one of KMessageDialog::Type enum
0152      * @param text the text message that is going to be displayed in the dialog
0153      * @param parent_id the native parent's window system identifier
0154      */
0155     explicit KMessageDialog(KMessageDialog::Type type, const QString &text, WId parent_id);
0156     /**
0157      * Destructor
0158      */
0159     ~KMessageDialog() override;
0160 
0161     /**
0162      * This can be used to set the title of the dialog window. If you pass an
0163      * empty QString(), a generic title will be used depending on the dialog
0164      * Type. E.g. for KMessageDialog::WarningTwoActions, "Warning" will be used.
0165      */
0166     void setCaption(const QString &caption);
0167 
0168     /**
0169      * This can be used to set an icon that will be shown next to the main
0170      * text message. If you pass a null QIcon() a generic icon based on the dialog
0171      * Type will be used. E.g. for KMessageDialog::QuestionTwoActions, QMessageBox::Question
0172      * will be used.
0173      */
0174     void setIcon(const QIcon &icon);
0175 
0176     /**
0177      * This will add a QListWidget to the dialog and populate it with @p strlist.
0178      * If @p strlist is empty, the list widget will not be shown.
0179      */
0180     void setListWidgetItems(const QStringList &strlist);
0181 
0182     /**
0183      * This will add a KCollapsibleGroupBox with a title "Details", as the class name
0184      * implies it is collapsible (and collapsed by default); you can use it to add a
0185      * more detailed explanation of what the dialog is trying to tell the user.
0186      *
0187      * If @p details is empty, the details widget will not be shown.
0188      */
0189     void setDetails(const QString &details);
0190 
0191     /**
0192      * This will add a "Do not ask again" checkbox to the dialog with the text
0193      * from @p dontAskAgainText. You can set the initial status of the checkbox
0194      * by using setDontAskAgainChecked().
0195      *
0196      * If @p dontAskAgainText is empty, no checkbox will be shown.
0197      *
0198      * Typical usage of this checkbox is for recurring questions, e.g. showing
0199      * a dialog to confirm moving files/directories to trash, the user can then
0200      * set the checkbox so as not to be asked about that again.
0201      *
0202      * You can get the state of the checkbox by using isDontAskAgainChecked().
0203      */
0204     void setDontAskAgainText(const QString &dontAskAgainText);
0205 
0206     /**
0207      * This can be used to set the initial status of the "Do not ask again" checkbox,
0208      * checked or unchecked, by setting @p isChecked to @c true or @c false
0209      * respectively.
0210      *
0211      * You need to call setDontAskAgainText() first to actually show a checkbox in
0212      * the dialog, otherwise calling this function will have no effect.
0213      */
0214     void setDontAskAgainChecked(bool isChecked);
0215 
0216     /**
0217      * This can be used to query the status of the "Do not ask again" checkbox;
0218      * returns @c true if the box is checked and @c false otherwise.
0219      *
0220      * @note This method will return @c false if a checkbox widget isn't shown in
0221      * the dialog. The dialog will not show a checkbox if setDontAskAgainText() was
0222      * not used previously to add a checkbox to begin with.
0223      */
0224     bool isDontAskAgainChecked() const;
0225 
0226     /**
0227      * Sets the text labels in the dialog to either allow opening external links or not.
0228      */
0229     void setOpenExternalLinks(bool isAllowed);
0230 
0231     /**
0232      * Whether a KNotification is emitted when the dialog is shown.
0233      *
0234      * This typically plays a notification sound. Default is true.
0235      *
0236      * @since 5.97
0237      * @sa KMessageBox::Notify
0238      */
0239     bool isNotifyEnabled() const;
0240 
0241     /**
0242      * Whether to emit a KNotification when the dialog is shown.
0243      *
0244      * This typically plays a notification sound.
0245      *
0246      * @since 5.97
0247      * @sa KMessageBox::Notify
0248      */
0249     void setNotifyEnabled(bool enable);
0250 
0251     /**
0252      * Sets the buttons in the buttom box.
0253      *
0254      * Using this method, you can customize the behavior based on your use-case, by
0255      * using a KGuiItem to get a button with custom text and icon.
0256      *
0257      * Since 5.85 buttons based on the dialog type are added by default (see
0258      * KMessageDialog(KMessageDialog::Type, const QString &, QWidget *) for details).
0259      * Before, this method had to be called explicitly to have any buttons added to the dialog.
0260      *
0261      * @note
0262      * - For dialog types Information and Error only one button
0263      *   (KStandardGuiItem::ok()) is added to the dialog.
0264      *
0265      * @param primaryAction the action for the primary button.
0266      *                      Reported in the result for dialog types Information and Error
0267      *                      as KMessageDialog::Ok enum value, otherwise as KMessageDialog::PrimaryAction.
0268      * @param secondaryAction the action for the secondary button.
0269      *                        Reported in the result as KMessageDialog::SecondaryAction enum value.
0270      *                        Ignored with all dialog types without a "secondary" action.
0271      * @param cancelAction the action for the cancel button.
0272      *                     Reported in the result as KMessageDialog::Cancel enum value.
0273      *                     Ignored with all dialog types without a Cancel button.
0274      */
0275     void setButtons(const KGuiItem &primaryAction = KGuiItem(), const KGuiItem &secondaryAction = KGuiItem(), const KGuiItem &cancelAction = KGuiItem());
0276 
0277     /**
0278      * Manually play the notification sound
0279      *
0280      * When implementing your entirely own message box, not using KMessageDialog,
0281      * you can call this function to play the relevant notification sound (if enabled).
0282      *
0283      * @note You don't need to call this when using KMessageDialog, it plays the sound automatically.
0284      *
0285      * @param type The message box type
0286      * @param text The message box contents, for accessibility purposes.
0287      * @param dialog The dialog that was displayed
0288      * @since 6.0
0289      * @sa setNotifyEnabled
0290      */
0291     static void beep(KMessageDialog::Type type, const QString &text = QString(), QWidget *dialog = nullptr);
0292 
0293 protected:
0294     void showEvent(QShowEvent *event) override;
0295 
0296 private:
0297     std::unique_ptr<KMessageDialogPrivate> const d;
0298 };
0299 
0300 #endif // KMESSAGEDIALOG_H