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

Last change on this file since 296 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 14.1 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the examples of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** 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 are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
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::show&M&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#ifndef Q_WS_WIN
154#ifndef Q_OS_MAC
155 native->hide();
156#endif
157#endif
158 QGridLayout *layout = new QGridLayout;
159 layout->setColumnStretch(1, 1);
160 layout->setColumnMinimumWidth(1, 250);
161 layout->addWidget(integerButton, 0, 0);
162 layout->addWidget(integerLabel, 0, 1);
163 layout->addWidget(doubleButton, 1, 0);
164 layout->addWidget(doubleLabel, 1, 1);
165 layout->addWidget(itemButton, 2, 0);
166 layout->addWidget(itemLabel, 2, 1);
167 layout->addWidget(textButton, 3, 0);
168 layout->addWidget(textLabel, 3, 1);
169 layout->addWidget(colorButton, 4, 0);
170 layout->addWidget(colorLabel, 4, 1);
171 layout->addWidget(fontButton, 5, 0);
172 layout->addWidget(fontLabel, 5, 1);
173 layout->addWidget(directoryButton, 6, 0);
174 layout->addWidget(directoryLabel, 6, 1);
175 layout->addWidget(openFileNameButton, 7, 0);
176 layout->addWidget(openFileNameLabel, 7, 1);
177 layout->addWidget(openFileNamesButton, 8, 0);
178 layout->addWidget(openFileNamesLabel, 8, 1);
179 layout->addWidget(saveFileNameButton, 9, 0);
180 layout->addWidget(saveFileNameLabel, 9, 1);
181 layout->addWidget(criticalButton, 10, 0);
182 layout->addWidget(criticalLabel, 10, 1);
183 layout->addWidget(informationButton, 11, 0);
184 layout->addWidget(informationLabel, 11, 1);
185 layout->addWidget(questionButton, 12, 0);
186 layout->addWidget(questionLabel, 12, 1);
187 layout->addWidget(warningButton, 13, 0);
188 layout->addWidget(warningLabel, 13, 1);
189 layout->addWidget(errorButton, 14, 0);
190 layout->addWidget(errorLabel, 14, 1);
191 layout->addWidget(native, 15, 0);
192 setLayout(layout);
193
194 setWindowTitle(tr("Standard Dialogs"));
195}
196
197void Dialog::setInteger()
198{
199//! [0]
200 bool ok;
201 int i = QInputDialog::getInt(this, tr("QInputDialog::getInteger()"),
202 tr("Percentage:"), 25, 0, 100, 1, &ok);
203 if (ok)
204 integerLabel->setText(tr("%1%").arg(i));
205//! [0]
206}
207
208void Dialog::setDouble()
209{
210//! [1]
211 bool ok;
212 double d = QInputDialog::getDouble(this, tr("QInputDialog::getDouble()"),
213 tr("Amount:"), 37.56, -10000, 10000, 2, &ok);
214 if (ok)
215 doubleLabel->setText(QString("$%1").arg(d));
216//! [1]
217}
218
219void Dialog::setItem()
220{
221//! [2]
222 QStringList items;
223 items << tr("Spring") << tr("Summer") << tr("Fall") << tr("Winter");
224
225 bool ok;
226 QString item = QInputDialog::getItem(this, tr("QInputDialog::getItem()"),
227 tr("Season:"), items, 0, false, &ok);
228 if (ok && !item.isEmpty())
229 itemLabel->setText(item);
230//! [2]
231}
232
233void Dialog::setText()
234{
235//! [3]
236 bool ok;
237 QString text = QInputDialog::getText(this, tr("QInputDialog::getText()"),
238 tr("User name:"), QLineEdit::Normal,
239 QDir::home().dirName(), &ok);
240 if (ok && !text.isEmpty())
241 textLabel->setText(text);
242//! [3]
243}
244
245void Dialog::setColor()
246{
247 QColor color = QColorDialog::getColor(Qt::green, this);
248 if (color.isValid()) {
249 colorLabel->setText(color.name());
250 colorLabel->setPalette(QPalette(color));
251 colorLabel->setAutoFillBackground(true);
252 }
253}
254
255void Dialog::setFont()
256{
257 bool ok;
258 QFont font = QFontDialog::getFont(&ok, QFont(fontLabel->text()), this);
259 if (ok) {
260 fontLabel->setText(font.key());
261 fontLabel->setFont(font);
262 }
263}
264
265void Dialog::setExistingDirectory()
266{
267 QFileDialog::Options options = QFileDialog::DontResolveSymlinks | QFileDialog::ShowDirsOnly;
268 if (!native->isChecked())
269 options |= QFileDialog::DontUseNativeDialog;
270 QString directory = QFileDialog::getExistingDirectory(this,
271 tr("QFileDialog::getExistingDirectory()"),
272 directoryLabel->text(),
273 options);
274 if (!directory.isEmpty())
275 directoryLabel->setText(directory);
276}
277
278void Dialog::setOpenFileName()
279{
280 QFileDialog::Options options;
281 if (!native->isChecked())
282 options |= QFileDialog::DontUseNativeDialog;
283 QString selectedFilter;
284 QString fileName = QFileDialog::getOpenFileName(this,
285 tr("QFileDialog::getOpenFileName()"),
286 openFileNameLabel->text(),
287 tr("All Files (*);;Text Files (*.txt)"),
288 &selectedFilter,
289 options);
290 if (!fileName.isEmpty())
291 openFileNameLabel->setText(fileName);
292}
293
294void Dialog::setOpenFileNames()
295{
296 QFileDialog::Options options;
297 if (!native->isChecked())
298 options |= QFileDialog::DontUseNativeDialog;
299 QString selectedFilter;
300 QStringList files = QFileDialog::getOpenFileNames(
301 this, tr("QFileDialog::getOpenFileNames()"),
302 openFilesPath,
303 tr("All Files (*);;Text Files (*.txt)"),
304 &selectedFilter,
305 options);
306 if (files.count()) {
307 openFilesPath = files[0];
308 openFileNamesLabel->setText(QString("[%1]").arg(files.join(", ")));
309 }
310}
311
312void Dialog::setSaveFileName()
313{
314 QFileDialog::Options options;
315 if (!native->isChecked())
316 options |= QFileDialog::DontUseNativeDialog;
317 QString selectedFilter;
318 QString fileName = QFileDialog::getSaveFileName(this,
319 tr("QFileDialog::getSaveFileName()"),
320 saveFileNameLabel->text(),
321 tr("All Files (*);;Text Files (*.txt)"),
322 &selectedFilter,
323 options);
324 if (!fileName.isEmpty())
325 saveFileNameLabel->setText(fileName);
326}
327
328void Dialog::criticalMessage()
329{
330 QMessageBox::StandardButton reply;
331 reply = QMessageBox::critical(this, tr("QMessageBox::critical()"),
332 MESSAGE,
333 QMessageBox::Abort | QMessageBox::Retry | QMessageBox::Ignore);
334 if (reply == QMessageBox::Abort)
335 criticalLabel->setText(tr("Abort"));
336 else if (reply == QMessageBox::Retry)
337 criticalLabel->setText(tr("Retry"));
338 else
339 criticalLabel->setText(tr("Ignore"));
340}
341
342void Dialog::informationMessage()
343{
344 QMessageBox::StandardButton reply;
345 reply = QMessageBox::information(this, tr("QMessageBox::information()"), MESSAGE);
346 if (reply == QMessageBox::Ok)
347 informationLabel->setText(tr("OK"));
348 else
349 informationLabel->setText(tr("Escape"));
350}
351
352void Dialog::questionMessage()
353{
354 QMessageBox::StandardButton reply;
355 reply = QMessageBox::question(this, tr("QMessageBox::question()"),
356 MESSAGE,
357 QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);
358 if (reply == QMessageBox::Yes)
359 questionLabel->setText(tr("Yes"));
360 else if (reply == QMessageBox::No)
361 questionLabel->setText(tr("No"));
362 else
363 questionLabel->setText(tr("Cancel"));
364}
365
366void Dialog::warningMessage()
367{
368 QMessageBox msgBox(QMessageBox::Warning, tr("QMessageBox::warning()"),
369 MESSAGE, 0, this);
370 msgBox.addButton(tr("Save &Again"), QMessageBox::AcceptRole);
371 msgBox.addButton(tr("&Continue"), QMessageBox::RejectRole);
372 if (msgBox.exec() == QMessageBox::AcceptRole)
373 warningLabel->setText(tr("Save Again"));
374 else
375 warningLabel->setText(tr("Continue"));
376
377}
378
379void Dialog::errorMessage()
380{
381 errorMessageDialog->showMessage(
382 tr("This dialog shows and remembers error messages. "
383 "If the checkbox is checked (as it is by default), "
384 "the shown message will be shown again, "
385 "but if the user unchecks the box the message "
386 "will not appear again if QErrorMessage::showMessage() "
387 "is called with the same message."));
388 errorLabel->setText(tr("If the box is unchecked, the message "
389 "won't appear again."));
390}
Note: See TracBrowser for help on using the repository browser.