1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtWidgets module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #ifndef QMESSAGEBOX_H
41 #define QMESSAGEBOX_H
42 
43 #include <QtWidgets/qtwidgetsglobal.h>
44 
45 #include <QtWidgets/qdialog.h>
46 
47 QT_REQUIRE_CONFIG(messagebox);
48 
49 QT_BEGIN_NAMESPACE
50 
51 class QLabel;
52 class QMessageBoxPrivate;
53 class QAbstractButton;
54 class QCheckBox;
55 
56 class Q_WIDGETS_EXPORT QMessageBox : public QDialog
57 {
58     Q_OBJECT
59     Q_PROPERTY(QString text READ text WRITE setText)
60     Q_PROPERTY(Icon icon READ icon WRITE setIcon)
61     Q_PROPERTY(QPixmap iconPixmap READ iconPixmap WRITE setIconPixmap)
62     Q_PROPERTY(Qt::TextFormat textFormat READ textFormat WRITE setTextFormat)
63     Q_PROPERTY(StandardButtons standardButtons READ standardButtons WRITE setStandardButtons)
64 #if QT_CONFIG(textedit)
65     Q_PROPERTY(QString detailedText READ detailedText WRITE setDetailedText)
66 #endif
67     Q_PROPERTY(QString informativeText READ informativeText WRITE setInformativeText)
68     Q_PROPERTY(Qt::TextInteractionFlags textInteractionFlags READ textInteractionFlags WRITE setTextInteractionFlags)
69 
70 public:
71     enum Icon {
72         // keep this in sync with QMessageDialogOptions::Icon
73         NoIcon = 0,
74         Information = 1,
75         Warning = 2,
76         Critical = 3,
77         Question = 4
78     };
79     Q_ENUM(Icon)
80 
81     enum ButtonRole {
82         // keep this in sync with QDialogButtonBox::ButtonRole and QPlatformDialogHelper::ButtonRole
83         InvalidRole = -1,
84         AcceptRole,
85         RejectRole,
86         DestructiveRole,
87         ActionRole,
88         HelpRole,
89         YesRole,
90         NoRole,
91         ResetRole,
92         ApplyRole,
93 
94         NRoles
95     };
96 
97     enum StandardButton {
98         // keep this in sync with QDialogButtonBox::StandardButton and QPlatformDialogHelper::StandardButton
99         NoButton           = 0x00000000,
100         Ok                 = 0x00000400,
101         Save               = 0x00000800,
102         SaveAll            = 0x00001000,
103         Open               = 0x00002000,
104         Yes                = 0x00004000,
105         YesToAll           = 0x00008000,
106         No                 = 0x00010000,
107         NoToAll            = 0x00020000,
108         Abort              = 0x00040000,
109         Retry              = 0x00080000,
110         Ignore             = 0x00100000,
111         Close              = 0x00200000,
112         Cancel             = 0x00400000,
113         Discard            = 0x00800000,
114         Help               = 0x01000000,
115         Apply              = 0x02000000,
116         Reset              = 0x04000000,
117         RestoreDefaults    = 0x08000000,
118 
119         FirstButton        = Ok,                // internal
120         LastButton         = RestoreDefaults,   // internal
121 
122         YesAll             = YesToAll,          // obsolete
123         NoAll              = NoToAll,           // obsolete
124 
125         Default            = 0x00000100,        // obsolete
126         Escape             = 0x00000200,        // obsolete
127         FlagMask           = 0x00000300,        // obsolete
128         ButtonMask         = ~FlagMask          // obsolete
129     };
130     typedef StandardButton Button;  // obsolete
131 
132     Q_DECLARE_FLAGS(StandardButtons, StandardButton)
133     Q_FLAG(StandardButtons)
134 
135     explicit QMessageBox(QWidget *parent = nullptr);
136     QMessageBox(Icon icon, const QString &title, const QString &text,
137                 StandardButtons buttons = NoButton, QWidget *parent = nullptr,
138                 Qt::WindowFlags flags = Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint);
139     ~QMessageBox();
140 
141     void addButton(QAbstractButton *button, ButtonRole role);
142     QPushButton *addButton(const QString &text, ButtonRole role);
143     QPushButton *addButton(StandardButton button);
144     void removeButton(QAbstractButton *button);
145 
146     using QDialog::open;
147     void open(QObject *receiver, const char *member);
148 
149     QList<QAbstractButton *> buttons() const;
150     ButtonRole buttonRole(QAbstractButton *button) const;
151 
152     void setStandardButtons(StandardButtons buttons);
153     StandardButtons standardButtons() const;
154     StandardButton standardButton(QAbstractButton *button) const;
155     QAbstractButton *button(StandardButton which) const;
156 
157     QPushButton *defaultButton() const;
158     void setDefaultButton(QPushButton *button);
159     void setDefaultButton(StandardButton button);
160 
161     QAbstractButton *escapeButton() const;
162     void setEscapeButton(QAbstractButton *button);
163     void setEscapeButton(StandardButton button);
164 
165     QAbstractButton *clickedButton() const;
166 
167     QString text() const;
168     void setText(const QString &text);
169 
170     Icon icon() const;
171     void setIcon(Icon);
172 
173     QPixmap iconPixmap() const;
174     void setIconPixmap(const QPixmap &pixmap);
175 
176     Qt::TextFormat textFormat() const;
177     void setTextFormat(Qt::TextFormat format);
178 
179     void setTextInteractionFlags(Qt::TextInteractionFlags flags);
180     Qt::TextInteractionFlags textInteractionFlags() const;
181 
182     void setCheckBox(QCheckBox *cb);
183     QCheckBox* checkBox() const;
184 
185     static StandardButton information(QWidget *parent, const QString &title,
186          const QString &text, StandardButtons buttons = Ok,
187          StandardButton defaultButton = NoButton);
188     static StandardButton question(QWidget *parent, const QString &title,
189          const QString &text, StandardButtons buttons = StandardButtons(Yes | No),
190          StandardButton defaultButton = NoButton);
191     static StandardButton warning(QWidget *parent, const QString &title,
192          const QString &text, StandardButtons buttons = Ok,
193          StandardButton defaultButton = NoButton);
194     static StandardButton critical(QWidget *parent, const QString &title,
195          const QString &text, StandardButtons buttons = Ok,
196          StandardButton defaultButton = NoButton);
197     static void about(QWidget *parent, const QString &title, const QString &text);
198     static void aboutQt(QWidget *parent, const QString &title = QString());
199 
200     // the following functions are obsolete:
201 
202     QMessageBox(const QString &title, const QString &text, Icon icon,
203                   int button0, int button1, int button2,
204                   QWidget *parent = nullptr,
205                   Qt::WindowFlags f = Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint);
206 
207     static int information(QWidget *parent, const QString &title,
208                            const QString& text,
209                            int button0, int button1 = 0, int button2 = 0);
210     static int information(QWidget *parent, const QString &title,
211                            const QString& text,
212                            const QString& button0Text,
213                            const QString& button1Text = QString(),
214                            const QString& button2Text = QString(),
215                            int defaultButtonNumber = 0,
216                            int escapeButtonNumber = -1);
217     inline static StandardButton information(QWidget *parent, const QString &title,
218                                   const QString& text,
219                                   StandardButton button0, StandardButton button1 = NoButton)
220     { return information(parent, title, text, StandardButtons(button0), button1); }
221 
222     static int question(QWidget *parent, const QString &title,
223                         const QString& text,
224                         int button0, int button1 = 0, int button2 = 0);
225     static int question(QWidget *parent, const QString &title,
226                         const QString& text,
227                         const QString& button0Text,
228                         const QString& button1Text = QString(),
229                         const QString& button2Text = QString(),
230                         int defaultButtonNumber = 0,
231                         int escapeButtonNumber = -1);
question(QWidget * parent,const QString & title,const QString & text,StandardButton button0,StandardButton button1)232     inline static int question(QWidget *parent, const QString &title,
233                                const QString& text,
234                                StandardButton button0, StandardButton button1)
235     { return question(parent, title, text, StandardButtons(button0), button1); }
236 
237     static int warning(QWidget *parent, const QString &title,
238                        const QString& text,
239                        int button0, int button1, int button2 = 0);
240     static int warning(QWidget *parent, const QString &title,
241                        const QString& text,
242                        const QString& button0Text,
243                        const QString& button1Text = QString(),
244                        const QString& button2Text = QString(),
245                        int defaultButtonNumber = 0,
246                        int escapeButtonNumber = -1);
warning(QWidget * parent,const QString & title,const QString & text,StandardButton button0,StandardButton button1)247     inline static int warning(QWidget *parent, const QString &title,
248                               const QString& text,
249                               StandardButton button0, StandardButton button1)
250     { return warning(parent, title, text, StandardButtons(button0), button1); }
251 
252     static int critical(QWidget *parent, const QString &title,
253                         const QString& text,
254                         int button0, int button1, int button2 = 0);
255     static int critical(QWidget *parent, const QString &title,
256                         const QString& text,
257                         const QString& button0Text,
258                         const QString& button1Text = QString(),
259                         const QString& button2Text = QString(),
260                         int defaultButtonNumber = 0,
261                         int escapeButtonNumber = -1);
critical(QWidget * parent,const QString & title,const QString & text,StandardButton button0,StandardButton button1)262     inline static int critical(QWidget *parent, const QString &title,
263                                const QString& text,
264                                StandardButton button0, StandardButton button1)
265     { return critical(parent, title, text, StandardButtons(button0), button1); }
266 
267     QString buttonText(int button) const;
268     void setButtonText(int button, const QString &text);
269 
270     QString informativeText() const;
271     void setInformativeText(const QString &text);
272 
273 #if QT_CONFIG(textedit)
274     QString detailedText() const;
275     void setDetailedText(const QString &text);
276 #endif
277 
278     void setWindowTitle(const QString &title);
279     void setWindowModality(Qt::WindowModality windowModality);
280 
281 
282     static QPixmap standardIcon(Icon icon);
283 
284 Q_SIGNALS:
285     void buttonClicked(QAbstractButton *button);
286 
287 #ifdef Q_CLANG_QDOC
288 public Q_SLOTS:
289     int exec() override;
290 #endif
291 
292 protected:
293     bool event(QEvent *e) override;
294     void resizeEvent(QResizeEvent *event) override;
295     void showEvent(QShowEvent *event) override;
296     void closeEvent(QCloseEvent *event) override;
297     void keyPressEvent(QKeyEvent *event) override;
298     void changeEvent(QEvent *event) override;
299 
300 private:
301     Q_PRIVATE_SLOT(d_func(), void _q_buttonClicked(QAbstractButton *))
302     Q_PRIVATE_SLOT(d_func(), void _q_clicked(QPlatformDialogHelper::StandardButton, QPlatformDialogHelper::ButtonRole))
303 
304     Q_DISABLE_COPY(QMessageBox)
305     Q_DECLARE_PRIVATE(QMessageBox)
306 };
307 
308 Q_DECLARE_OPERATORS_FOR_FLAGS(QMessageBox::StandardButtons)
309 
310 #define QT_REQUIRE_VERSION(argc, argv, str) { QString s = QString::fromLatin1(str);\
311 QString sq = QString::fromLatin1(qVersion()); \
312 if ((sq.section(QChar::fromLatin1('.'),0,0).toInt()<<16)+\
313 (sq.section(QChar::fromLatin1('.'),1,1).toInt()<<8)+\
314 sq.section(QChar::fromLatin1('.'),2,2).toInt()<(s.section(QChar::fromLatin1('.'),0,0).toInt()<<16)+\
315 (s.section(QChar::fromLatin1('.'),1,1).toInt()<<8)+\
316 s.section(QChar::fromLatin1('.'),2,2).toInt()) { \
317 if (!qApp){ \
318     new QApplication(argc,argv); \
319 } \
320 QString s = QApplication::tr("Executable '%1' requires Qt "\
321  "%2, found Qt %3.").arg(qAppName()).arg(QString::fromLatin1(\
322 str)).arg(QString::fromLatin1(qVersion())); QMessageBox::critical(0, QApplication::tr(\
323 "Incompatible Qt Library Error"), s, QMessageBox::Abort, 0); qFatal("%s", s.toLatin1().data()); }}
324 
325 QT_END_NAMESPACE
326 
327 #endif // QMESSAGEBOX_H
328