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

Last change on this file since 651 was 651, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.2 sources.

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