source: trunk/examples/dialogs/standarddialogs/dialog.cpp@ 846

Last change on this file since 846 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: 14.3 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 examples 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
43#include "dialog.h"
44
45#define MESSAGE \
46 Dialog::tr("<p>Message boxes have a caption, a text, " \
47 "and any number of buttons, each with standard or custom texts." \
48 "<p>Click a button to close the message box. Pressing the Esc button " \
49 "will activate the detected escape button (if any).")
50
51Dialog::Dialog(QWidget *parent)
52 : QDialog(parent)
53{
54 errorMessageDialog = new QErrorMessage(this);
55
56 int frameStyle = QFrame::Sunken | QFrame::Panel;
57
58 integerLabel = new QLabel;
59 integerLabel->setFrameStyle(frameStyle);
60 QPushButton *integerButton =
61 new QPushButton(tr("QInputDialog::get&Int()"));
62
63 doubleLabel = new QLabel;
64 doubleLabel->setFrameStyle(frameStyle);
65 QPushButton *doubleButton =
66 new QPushButton(tr("QInputDialog::get&Double()"));
67
68 itemLabel = new QLabel;
69 itemLabel->setFrameStyle(frameStyle);
70 QPushButton *itemButton = new QPushButton(tr("QInputDialog::getIte&m()"));
71
72 textLabel = new QLabel;
73 textLabel->setFrameStyle(frameStyle);
74 QPushButton *textButton = new QPushButton(tr("QInputDialog::get&Text()"));
75
76 colorLabel = new QLabel;
77 colorLabel->setFrameStyle(frameStyle);
78 QPushButton *colorButton = new QPushButton(tr("QColorDialog::get&Color()"));
79
80 fontLabel = new QLabel;
81 fontLabel->setFrameStyle(frameStyle);
82 QPushButton *fontButton = new QPushButton(tr("QFontDialog::get&Font()"));
83
84 directoryLabel = new QLabel;
85 directoryLabel->setFrameStyle(frameStyle);
86 QPushButton *directoryButton =
87 new QPushButton(tr("QFileDialog::getE&xistingDirectory()"));
88
89 openFileNameLabel = new QLabel;
90 openFileNameLabel->setFrameStyle(frameStyle);
91 QPushButton *openFileNameButton =
92 new QPushButton(tr("QFileDialog::get&OpenFileName()"));
93
94 openFileNamesLabel = new QLabel;
95 openFileNamesLabel->setFrameStyle(frameStyle);
96 QPushButton *openFileNamesButton =
97 new QPushButton(tr("QFileDialog::&getOpenFileNames()"));
98
99 saveFileNameLabel = new QLabel;
100 saveFileNameLabel->setFrameStyle(frameStyle);
101 QPushButton *saveFileNameButton =
102 new QPushButton(tr("QFileDialog::get&SaveFileName()"));
103
104 criticalLabel = new QLabel;
105 criticalLabel->setFrameStyle(frameStyle);
106 QPushButton *criticalButton =
107 new QPushButton(tr("QMessageBox::critica&l()"));
108
109 informationLabel = new QLabel;
110 informationLabel->setFrameStyle(frameStyle);
111 QPushButton *informationButton =
112 new QPushButton(tr("QMessageBox::i&nformation()"));
113
114 questionLabel = new QLabel;
115 questionLabel->setFrameStyle(frameStyle);
116 QPushButton *questionButton =
117 new QPushButton(tr("QMessageBox::&question()"));
118
119 warningLabel = new QLabel;
120 warningLabel->setFrameStyle(frameStyle);
121 QPushButton *warningButton = new QPushButton(tr("QMessageBox::&warning()"));
122
123 errorLabel = new QLabel;
124 errorLabel->setFrameStyle(frameStyle);
125 QPushButton *errorButton =
126 new QPushButton(tr("QErrorMessage::showM&essage()"));
127
128 connect(integerButton, SIGNAL(clicked()), this, SLOT(setInteger()));
129 connect(doubleButton, SIGNAL(clicked()), this, SLOT(setDouble()));
130 connect(itemButton, SIGNAL(clicked()), this, SLOT(setItem()));
131 connect(textButton, SIGNAL(clicked()), this, SLOT(setText()));
132 connect(colorButton, SIGNAL(clicked()), this, SLOT(setColor()));
133 connect(fontButton, SIGNAL(clicked()), this, SLOT(setFont()));
134 connect(directoryButton, SIGNAL(clicked()),
135 this, SLOT(setExistingDirectory()));
136 connect(openFileNameButton, SIGNAL(clicked()),
137 this, SLOT(setOpenFileName()));
138 connect(openFileNamesButton, SIGNAL(clicked()),
139 this, SLOT(setOpenFileNames()));
140 connect(saveFileNameButton, SIGNAL(clicked()),
141 this, SLOT(setSaveFileName()));
142 connect(criticalButton, SIGNAL(clicked()), this, SLOT(criticalMessage()));
143 connect(informationButton, SIGNAL(clicked()),
144 this, SLOT(informationMessage()));
145 connect(questionButton, SIGNAL(clicked()), this, SLOT(questionMessage()));
146 connect(warningButton, SIGNAL(clicked()), this, SLOT(warningMessage()));
147 connect(errorButton, SIGNAL(clicked()), this, SLOT(errorMessage()));
148
149 native = new QCheckBox(this);
150 native->setText("Use native file dialog.");
151 native->setChecked(true);
152 QGridLayout *layout = new QGridLayout;
153 layout->setColumnStretch(1, 1);
154 layout->setColumnMinimumWidth(1, 250);
155 layout->addWidget(integerButton, 0, 0);
156 layout->addWidget(integerLabel, 0, 1);
157 layout->addWidget(doubleButton, 1, 0);
158 layout->addWidget(doubleLabel, 1, 1);
159 layout->addWidget(itemButton, 2, 0);
160 layout->addWidget(itemLabel, 2, 1);
161 layout->addWidget(textButton, 3, 0);
162 layout->addWidget(textLabel, 3, 1);
163 layout->addWidget(colorButton, 4, 0);
164 layout->addWidget(colorLabel, 4, 1);
165 layout->addWidget(fontButton, 5, 0);
166 layout->addWidget(fontLabel, 5, 1);
167 layout->addWidget(directoryButton, 6, 0);
168 layout->addWidget(directoryLabel, 6, 1);
169 layout->addWidget(openFileNameButton, 7, 0);
170 layout->addWidget(openFileNameLabel, 7, 1);
171 layout->addWidget(openFileNamesButton, 8, 0);
172 layout->addWidget(openFileNamesLabel, 8, 1);
173 layout->addWidget(saveFileNameButton, 9, 0);
174 layout->addWidget(saveFileNameLabel, 9, 1);
175 layout->addWidget(criticalButton, 10, 0);
176 layout->addWidget(criticalLabel, 10, 1);
177 layout->addWidget(informationButton, 11, 0);
178 layout->addWidget(informationLabel, 11, 1);
179 layout->addWidget(questionButton, 12, 0);
180 layout->addWidget(questionLabel, 12, 1);
181 layout->addWidget(warningButton, 13, 0);
182 layout->addWidget(warningLabel, 13, 1);
183 layout->addWidget(errorButton, 14, 0);
184 layout->addWidget(errorLabel, 14, 1);
185 layout->addWidget(native, 15, 0);
186 setLayout(layout);
187
188 setWindowTitle(tr("Standard Dialogs"));
189}
190
191void Dialog::setInteger()
192{
193//! [0]
194 bool ok;
195 int i = QInputDialog::getInt(this, tr("QInputDialog::getInteger()"),
196 tr("Percentage:"), 25, 0, 100, 1, &ok);
197 if (ok)
198 integerLabel->setText(tr("%1%").arg(i));
199//! [0]
200}
201
202void Dialog::setDouble()
203{
204//! [1]
205 bool ok;
206 double d = QInputDialog::getDouble(this, tr("QInputDialog::getDouble()"),
207 tr("Amount:"), 37.56, -10000, 10000, 2, &ok);
208 if (ok)
209 doubleLabel->setText(QString("$%1").arg(d));
210//! [1]
211}
212
213void Dialog::setItem()
214{
215//! [2]
216 QStringList items;
217 items << tr("Spring") << tr("Summer") << tr("Fall") << tr("Winter");
218
219 bool ok;
220 QString item = QInputDialog::getItem(this, tr("QInputDialog::getItem()"),
221 tr("Season:"), items, 0, false, &ok);
222 if (ok && !item.isEmpty())
223 itemLabel->setText(item);
224//! [2]
225}
226
227void Dialog::setText()
228{
229//! [3]
230 bool ok;
231 QString text = QInputDialog::getText(this, tr("QInputDialog::getText()"),
232 tr("User name:"), QLineEdit::Normal,
233 QDir::home().dirName(), &ok);
234 if (ok && !text.isEmpty())
235 textLabel->setText(text);
236//! [3]
237}
238
239void Dialog::setColor()
240{
241 QColor color;
242 if (native->isChecked())
243 color = QColorDialog::getColor(Qt::green, this);
244 else
245 color = QColorDialog::getColor(Qt::green, this, "Select Color", QColorDialog::DontUseNativeDialog);
246
247 if (color.isValid()) {
248 colorLabel->setText(color.name());
249 colorLabel->setPalette(QPalette(color));
250 colorLabel->setAutoFillBackground(true);
251 }
252}
253
254void Dialog::setFont()
255{
256 bool ok;
257 QFont font = QFontDialog::getFont(&ok, QFont(fontLabel->text()), this);
258 if (ok) {
259 fontLabel->setText(font.key());
260 fontLabel->setFont(font);
261 }
262}
263
264void Dialog::setExistingDirectory()
265{
266 QFileDialog::Options options = QFileDialog::DontResolveSymlinks | QFileDialog::ShowDirsOnly;
267 if (!native->isChecked())
268 options |= QFileDialog::DontUseNativeDialog;
269 QString directory = QFileDialog::getExistingDirectory(this,
270 tr("QFileDialog::getExistingDirectory()"),
271 directoryLabel->text(),
272 options);
273 if (!directory.isEmpty())
274 directoryLabel->setText(directory);
275}
276
277void Dialog::setOpenFileName()
278{
279 QFileDialog::Options options;
280 if (!native->isChecked())
281 options |= QFileDialog::DontUseNativeDialog;
282 QString selectedFilter;
283 QString fileName = QFileDialog::getOpenFileName(this,
284 tr("QFileDialog::getOpenFileName()"),
285 openFileNameLabel->text(),
286 tr("All Files (*);;Text Files (*.txt)"),
287 &selectedFilter,
288 options);
289 if (!fileName.isEmpty())
290 openFileNameLabel->setText(fileName);
291}
292
293void Dialog::setOpenFileNames()
294{
295 QFileDialog::Options options;
296 if (!native->isChecked())
297 options |= QFileDialog::DontUseNativeDialog;
298 QString selectedFilter;
299 QStringList files = QFileDialog::getOpenFileNames(
300 this, tr("QFileDialog::getOpenFileNames()"),
301 openFilesPath,
302 tr("All Files (*);;Text Files (*.txt)"),
303 &selectedFilter,
304 options);
305 if (files.count()) {
306 openFilesPath = files[0];
307 openFileNamesLabel->setText(QString("[%1]").arg(files.join(", ")));
308 }
309}
310
311void Dialog::setSaveFileName()
312{
313 QFileDialog::Options options;
314 if (!native->isChecked())
315 options |= QFileDialog::DontUseNativeDialog;
316 QString selectedFilter;
317 QString fileName = QFileDialog::getSaveFileName(this,
318 tr("QFileDialog::getSaveFileName()"),
319 saveFileNameLabel->text(),
320 tr("All Files (*);;Text Files (*.txt)"),
321 &selectedFilter,
322 options);
323 if (!fileName.isEmpty())
324 saveFileNameLabel->setText(fileName);
325}
326
327void Dialog::criticalMessage()
328{
329 QMessageBox::StandardButton reply;
330 reply = QMessageBox::critical(this, tr("QMessageBox::critical()"),
331 MESSAGE,
332 QMessageBox::Abort | QMessageBox::Retry | QMessageBox::Ignore);
333 if (reply == QMessageBox::Abort)
334 criticalLabel->setText(tr("Abort"));
335 else if (reply == QMessageBox::Retry)
336 criticalLabel->setText(tr("Retry"));
337 else
338 criticalLabel->setText(tr("Ignore"));
339}
340
341void Dialog::informationMessage()
342{
343 QMessageBox::StandardButton reply;
344 reply = QMessageBox::information(this, tr("QMessageBox::information()"), MESSAGE);
345 if (reply == QMessageBox::Ok)
346 informationLabel->setText(tr("OK"));
347 else
348 informationLabel->setText(tr("Escape"));
349}
350
351void Dialog::questionMessage()
352{
353 QMessageBox::StandardButton reply;
354 reply = QMessageBox::question(this, tr("QMessageBox::question()"),
355 MESSAGE,
356 QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);
357 if (reply == QMessageBox::Yes)
358 questionLabel->setText(tr("Yes"));
359 else if (reply == QMessageBox::No)
360 questionLabel->setText(tr("No"));
361 else
362 questionLabel->setText(tr("Cancel"));
363}
364
365void Dialog::warningMessage()
366{
367 QMessageBox msgBox(QMessageBox::Warning, tr("QMessageBox::warning()"),
368 MESSAGE, 0, this);
369 msgBox.addButton(tr("Save &Again"), QMessageBox::AcceptRole);
370 msgBox.addButton(tr("&Continue"), QMessageBox::RejectRole);
371 if (msgBox.exec() == QMessageBox::AcceptRole)
372 warningLabel->setText(tr("Save Again"));
373 else
374 warningLabel->setText(tr("Continue"));
375
376}
377
378void Dialog::errorMessage()
379{
380 errorMessageDialog->showMessage(
381 tr("This dialog shows and remembers error messages. "
382 "If the checkbox is checked (as it is by default), "
383 "the shown message will be shown again, "
384 "but if the user unchecks the box the message "
385 "will not appear again if QErrorMessage::showMessage() "
386 "is called with the same message."));
387 errorLabel->setText(tr("If the box is unchecked, the message "
388 "won't appear again."));
389}
Note: See TracBrowser for help on using the repository browser.