source: trunk/doc/src/snippets/dialogs/dialogs.cpp@ 973

Last change on this file since 973 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

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