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 documentation of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
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 ** BSD License Usage
18 ** Alternatively, you may use this file under the terms of the BSD license
19 ** as follows:
20 **
21 ** "Redistribution and use in source and binary forms, with or without
22 ** modification, are permitted provided that the following conditions are
23 ** met:
24 **   * Redistributions of source code must retain the above copyright
25 **     notice, this list of conditions and the following disclaimer.
26 **   * Redistributions in binary form must reproduce the above copyright
27 **     notice, this list of conditions and the following disclaimer in
28 **     the documentation and/or other materials provided with the
29 **     distribution.
30 **   * Neither the name of The Qt Company Ltd nor the names of its
31 **     contributors may be used to endorse or promote products derived
32 **     from this software without specific prior written permission.
33 **
34 **
35 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46 **
47 ** $QT_END_LICENSE$
48 **
49 ****************************************************************************/
50 
51 #include <QtWidgets>
52 
53 typedef QDialog WordCountDialog;
54 typedef QDialog FindDialog;
55 
56 #define this 0
57 #define setWordCount(x) isVisible()
58 
tr(const char * text)59 QString tr(const char *text)
60 {
61     return QApplication::translate(text, text);
62 }
63 
64 class EditorWindow : public QMainWindow
65 {
66 public:
67     void find();
68     void countWords();
69 
70 private:
71     FindDialog *findDialog;
72 };
73 
74 //! [0]
find()75 void EditorWindow::find()
76 {
77     if (!findDialog) {
78         findDialog = new FindDialog(this);
79         connect(findDialog, &FindDialog::findNext,
80                 this, &EditorWindow::findNext);
81     }
82 
83     findDialog->show();
84     findDialog->raise();
85     findDialog->activateWindow();
86 }
87 //! [0]
88 
89 //! [1]
countWords()90 void EditorWindow::countWords()
91 {
92     WordCountDialog dialog(this);
93     dialog.setWordCount(document().wordCount());
94     dialog.exec();
95 }
96 //! [1]
97 
boo()98 inline bool boo()
99 {
100     QMessageBox::information(this, "Application name",
101                              "Unable to find the user preferences file.\n"
102                              "The factory default will be used instead.");
103 
104     QString filename;
105     if (QFile::exists(filename) &&
106         QMessageBox::question(
107             this,
108             tr("Overwrite File? -- Application Name"),
109             tr("A file called %1 already exists."
110                "Do you want to overwrite it?")
111             .arg(filename),
112             tr("&Yes"), tr("&No"),
113             QString(), 0, 1))
114         return false;
115 
116     switch(QMessageBox::warning(this, "Application name",
117                                 "Could not connect to the <mumble> server.\n"
118                                 "This program can't function correctly "
119                                 "without the server.\n\n",
120                                 "Retry",
121                                 "Quit", 0, 0, 1)) {
122     case 0: // The user clicked the Retry again button or pressed Enter
123         // try again
124         break;
125     case 1: // The user clicked the Quit or pressed Escape
126         // exit
127         break;
128     }
129 
130     switch(QMessageBox::information(this, "Application name here",
131                                     "The document contains unsaved changes\n"
132                                     "Do you want to save the changes before exiting?",
133                                     "&Save", "&Discard", "Cancel",
134                                     0,      // Enter == button 0
135                                     2)) { // Escape == button 2
136     case 0: // Save clicked or Alt+S pressed or Enter pressed.
137         // save
138         break;
139     case 1: // Discard clicked or Alt+D pressed
140         // don't save but exit
141         break;
142     case 2: // Cancel clicked or Escape pressed
143         // don't exit
144         break;
145     }
146 
147     switch(QMessageBox::warning(this, "Application name here",
148                                 "Could not save the user preferences,\n"
149                                 "because the disk is full. You can delete\n"
150                                 "some files and press Retry, or you can\n"
151                                 "abort the Save Preferences operation.",
152                                 QMessageBox::Retry | QMessageBox::Default,
153                                 QMessageBox::Abort | QMessageBox::Escape)) {
154     case QMessageBox::Retry: // Retry clicked or Enter pressed
155         // try again
156         break;
157     case QMessageBox::Abort: // Abort clicked or Escape pressed
158         // abort
159         break;
160     }
161 
162     QString errorDetails;
163     QMessageBox::critical(0, "Application name here",
164                           QString("An internal error occurred. Please ") +
165                           "call technical support at 1234-56789 and report\n"+
166                           "these numbers:\n\n" + errorDetails +
167                           "\n\nApplication will now exit.");
168 
169     QMessageBox::about(this, "About <Application>",
170                        "<Application> is a <one-paragraph blurb>\n\n"
171                        "Copyright 1991-2003 Such-and-such. "
172                        "<License words here.>\n\n"
173                        "For technical support, call 1234-56789 or see\n"
174                        "http://www.such-and-such.com/Application/\n");
175 
176     {
177         // saving the file
178         QMessageBox mb("Application name here",
179                        "Saving the file will overwrite the original file on the disk.\n"
180                        "Do you really want to save?",
181                        QMessageBox::Information,
182                        QMessageBox::Yes | QMessageBox::Default,
183                        QMessageBox::No,
184                        QMessageBox::Cancel | QMessageBox::Escape);
185         mb.setButtonText(QMessageBox::Yes, "Save");
186         mb.setButtonText(QMessageBox::No, "Discard");
187         switch(mb.exec()) {
188         case QMessageBox::Yes:
189             // save and exit
190             break;
191         case QMessageBox::No:
192             // exit without saving
193             break;
194         case QMessageBox::Cancel:
195             // don't save and don't exit
196             break;
197         }
198     }
199 
200     {
201         // hardware failure
202 //! [2]
203         QMessageBox mb("Application Name",
204                        "Hardware failure.\n\nDisk error detected\nDo you want to stop?",
205                        QMessageBox::Question,
206                        QMessageBox::Yes | QMessageBox::Default,
207                        QMessageBox::No | QMessageBox::Escape,
208                        QMessageBox::NoButton);
209         if (mb.exec() == QMessageBox::No) {
210             // try again
211 //! [2]
212         }
213     }
214 }
215 
moo()216 inline void moo()
217 {
218     int numFiles;
219 //! [3]
220     QProgressDialog progress("Copying files...", "Abort Copy", 0, numFiles, this);
221     progress.setWindowModality(Qt::WindowModal);
222 
223     for (int i = 0; i < numFiles; i++) {
224         progress.setValue(i);
225 
226         if (progress.wasCanceled())
227             break;
228         //... copy one file
229     }
230     progress.setValue(numFiles);
231 //! [3]
232 }
233 
234 class Operation : public QObject
235 {
236 public:
237     Operation(QObject *parent);
238     void perform();
239     void cancel();
240 
241 private:
242     int steps;
243     QProgressDialog *pd;
244     QTimer *t;
245 };
246 
247 //! [4]
248 // Operation constructor
Operation(QObject * parent)249 Operation::Operation(QObject *parent)
250     : QObject(parent), steps(0)
251 {
252     pd = new QProgressDialog("Operation in progress.", "Cancel", 0, 100);
253     connect(pd, &QProgressDialog::canceled, this, &Operation::cancel);
254     t = new QTimer(this);
255     connect(t, &QTimer::timeout, this, &Operation::perform);
256     t->start(0);
257 }
258 //! [4] //! [5]
259 
perform()260 void Operation::perform()
261 {
262     pd->setValue(steps);
263     //... perform one percent of the operation
264     steps++;
265     if (steps > pd->maximum())
266         t->stop();
267 }
268 //! [5] //! [6]
269 
cancel()270 void Operation::cancel()
271 {
272     t->stop();
273     //... cleanup
274 }
275 //! [6]
276 
main()277 int main()
278 {
279 }
280