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

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

trunk: Merged in qt 4.6.1 sources.

File size: 14.1 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 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: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 [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::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())