1 /*
2     SPDX-License-Identifier: GPL-2.0-or-later
3     SPDX-FileCopyrightText: 2018-2020 Umbrello UML Modeller Authors <umbrello-devel@kde.org>
4 */
5 
6 #ifndef DONTASKAGAIN_H
7 #define DONTASKAGAIN_H
8 
9 // Qt includes
10 #include <QList>
11 #include <QString>
12 #include <QWidget>
13 
14 // KDE includes
15 #include <KLocalizedString>
16 
17 class QVBoxLayout;
18 
19 /**
20  * The DontAskAgainItem class holds a 'dont ask again' item,
21  * see @ref DontAskAgainHandler for details.
22  *
23  * @author Ralf Habacker <ralf.habacker@freenet.de>
24  */
25 class DontAskAgainItem {
26 public:
27     DontAskAgainItem(const QString &name);
28     virtual ~DontAskAgainItem();
29     QString &name();
30     virtual QString text() const = 0;
31     bool isAll();
32     bool isEnabled();
33     void setEnabled(bool state = true);
34 
35 protected:
36     QString m_name;
37 };
38 
39 #define DefineDontAskAgainItem(name,key,_text) \
40 class DontAskAgainItem##name : public DontAskAgainItem \
41 { \
42 public: \
43     DontAskAgainItem##name() : DontAskAgainItem(QLatin1String(key)) {} \
44     virtual QString text() const { return _text; } \
45 }; \
46 static DontAskAgainItem##name name;
47 
48 /**
49  * The DontAskAgainWidget provides a graphical user interface
50  * to change 'dont ask again' settings and could be embedded
51  * in dialogs or dialog pages.
52  *
53  * After adding the widget to a dialog call @ref apply() to apply
54  * changed values and call @ref setDefaults() to reset changes to
55  * default values, which is all messages enabled.
56  *
57  * The widget is designed as group box with embedded check boxes.
58  *
59  * @author Ralf Habacker <ralf.habacker@freenet.de>
60  */
61 class DontAskAgainWidget : public QWidget
62 {
63     Q_OBJECT
64 public:
65     DontAskAgainWidget(QList<DontAskAgainItem *> &items, QWidget *parent = 0);
66     bool apply();
67     void setDefaults();
68 
69 protected:
70     void addItem(DontAskAgainItem *item);
71     QVBoxLayout *m_layout;
72     QList<DontAskAgainItem *> &m_items;
73 
74 protected slots:
75     void slotToggled(bool state);
76 };
77 
78 /**
79  * Dialogs provided by namespace KMessageBox support a feature to hide dialogs on user request
80  * by specifying the parameter dontAskAgainName, which adds a checkbox named "Don't ask again"
81  * to the related dialog.
82  *
83  * What is currently missing in KMessageBox namespace and therefore provided by class
84  * DontAskAgainHandler, is a widget to reenable or disable dialogs using the "Don't ask again"
85  * support in an application setting dialog.
86  *
87  * To use this support call macro @ref DefineDontAskAgainItem and add a call to method
88  * DontAskAgainItem::name as parameter @p dontAskAgainName to related class KMessageBox
89  * methods. See the following example:
90  *
91  * \code{.cpp}
92  * DefineDontAskAgainItem(aDontAskAgainItem, "delete-diagram", i18n("Enable 'delete diagram' related messages"));
93  * ...
94  * return KMessageBox::warningContinueCancel(..., aDontAskAgainItem.name())
95  * \endcode
96  *
97  * To add the mentioned widget to a settings dialog call @ref createWidget() and
98  * add the result to a dialog layout.
99  *
100  * @author Ralf Habacker <ralf.habacker@freenet.de>
101  */
102 class DontAskAgainHandler
103 {
104 public:
105     void addItem(DontAskAgainItem *item);
106     static DontAskAgainHandler &instance();
107     DontAskAgainWidget *createWidget();
108 protected:
109     QList<DontAskAgainItem*> m_items;
110 };
111 
112 #endif // DONTASKAGAIN_H
113