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