source: vendor/trolltech/current/src/dialogs/qinputdialog.cpp

Last change on this file was 2, checked in by dmik, 20 years ago

Imported xplatform parts of the official release 3.3.1 from Trolltech

  • Property svn:keywords set to Id
File size: 15.6 KB
Line 
1/****************************************************************************
2** $Id: qinputdialog.cpp 2 2005-11-16 15:49:26Z dmik $
3**
4** Implementation of QInputDialog class
5**
6** Created : 991212
7**
8** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
9**
10** This file is part of the dialogs module of the Qt GUI Toolkit.
11**
12** This file may be distributed under the terms of the Q Public License
13** as defined by Trolltech AS of Norway and appearing in the file
14** LICENSE.QPL included in the packaging of this file.
15**
16** This file may be distributed and/or modified under the terms of the
17** GNU General Public License version 2 as published by the Free Software
18** Foundation and appearing in the file LICENSE.GPL included in the
19** packaging of this file.
20**
21** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
22** licenses may use this file in accordance with the Qt Commercial License
23** Agreement provided with the Software.
24**
25** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
26** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27**
28** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
29** information about Qt Commercial License Agreements.
30** See http://www.trolltech.com/qpl/ for QPL licensing information.
31** See http://www.trolltech.com/gpl/ for GPL licensing information.
32**
33** Contact info@trolltech.com if any conditions of this licensing are
34** not clear to you.
35**
36**********************************************************************/
37
38#include "qinputdialog.h"
39
40#ifndef QT_NO_INPUTDIALOG
41
42#include "qlayout.h"
43#include "qlabel.h"
44#include "qlineedit.h"
45#include "qpushbutton.h"
46#include "qspinbox.h"
47#include "qcombobox.h"
48#include "qwidgetstack.h"
49#include "qvalidator.h"
50#include "qapplication.h"
51
52class QInputDialogPrivate
53{
54public:
55 friend class QInputDialog;
56 QLabel *label;
57 QLineEdit *lineEdit;
58 QSpinBox *spinBox;
59 QComboBox *comboBox, *editComboBox;
60 QPushButton *ok;
61 QWidgetStack *stack;
62 QInputDialog::Type type;
63};
64
65/*!
66 \class QInputDialog
67 \brief The QInputDialog class provides a simple convenience dialog to get a single value from the user.
68 \ingroup dialogs
69 \mainclass
70
71 The input value can be a string, a number or an item from a list. A
72 label must be set to tell the user what they should enter.
73
74 Four static convenience functions are provided:
75 getText(), getInteger(), getDouble() and getItem(). All the
76 functions can be used in a similar way, for example:
77 \code
78 bool ok;
79 QString text = QInputDialog::getText(
80 "MyApp 3000", "Enter your name:", QLineEdit::Normal,
81 QString::null, &ok, this );
82 if ( ok && !text.isEmpty() ) {
83 // user entered something and pressed OK
84 } else {
85 // user entered nothing or pressed Cancel
86 }
87 \endcode
88
89 \img inputdialogs.png Input Dialogs
90*/
91
92/*!
93 \enum QInputDialog::Type
94
95 This enum specifies the type of the dialog, i.e. what kind of data you
96 want the user to input:
97
98 \value LineEdit A QLineEdit is used for obtaining string or numeric
99 input. The QLineEdit can be accessed using lineEdit().
100
101 \value SpinBox A QSpinBox is used for obtaining integer input.
102 Use spinBox() to access the QSpinBox.
103
104 \value ComboBox A read-only QComboBox is used to provide a fixed
105 list of choices from which the user can choose.
106 Use comboBox() to access the QComboBox.
107
108 \value EditableComboBox An editable QComboBox is used to provide a fixed
109 list of choices from which the user can choose, but which also
110 allows the user to enter their own value instead.
111 Use editableComboBox() to access the QComboBox.
112*/
113
114/*!
115 Constructs the dialog. The \a label is the text which is shown to the user
116 (it should tell the user what they are expected to enter). The \a parent
117 is the dialog's parent widget. The widget is called \a name. If \a
118 modal is TRUE (the default) the dialog will be modal. The \a type
119 parameter is used to specify which type of dialog to construct.
120
121 \sa getText(), getInteger(), getDouble(), getItem()
122*/
123
124QInputDialog::QInputDialog( const QString &label, QWidget* parent,
125 const char* name, bool modal, Type type )
126 : QDialog( parent, name, modal )
127{
128 d = new QInputDialogPrivate;
129 d->lineEdit = 0;
130 d->spinBox = 0;
131 d->comboBox = 0;
132
133 QVBoxLayout *vbox = new QVBoxLayout( this, 6, 6 );
134
135 d->label = new QLabel( label, this, "qt_inputdlg_lbl" );
136 vbox->addWidget( d->label );
137
138 d->stack = new QWidgetStack( this, "qt_inputdlg_ws" );
139 vbox->addWidget( d->stack );
140 d->lineEdit = new QLineEdit( d->stack, "qt_inputdlg_le" );
141 d->spinBox = new QSpinBox( d->stack, "qt_inputdlg_sb" );
142 d->comboBox = new QComboBox( FALSE, d->stack, "qt_inputdlg_cb" );
143 d->editComboBox = new QComboBox( TRUE, d->stack, "qt_inputdlg_editcb" );
144
145 QHBoxLayout *hbox = new QHBoxLayout( 6 );
146 vbox->addLayout( hbox, AlignRight );
147
148 d->ok = new QPushButton( tr( "OK" ), this, "qt_ok_btn" );
149 d->ok->setDefault( TRUE );
150 QPushButton *cancel = new QPushButton( tr( "Cancel" ), this, "qt_cancel_btn" );
151
152 QSize bs = d->ok->sizeHint().expandedTo( cancel->sizeHint() );
153 d->ok->setFixedSize( bs );
154 cancel->setFixedSize( bs );
155
156 hbox->addStretch();
157 hbox->addWidget( d->ok );
158 hbox->addWidget( cancel );
159
160 connect( d->lineEdit, SIGNAL( returnPressed() ),
161 this, SLOT( tryAccept() ) );
162 connect( d->lineEdit, SIGNAL( textChanged(const QString&) ),
163 this, SLOT( textChanged(const QString&) ) );
164
165 connect( d->ok, SIGNAL( clicked() ), this, SLOT( accept() ) );
166 connect( cancel, SIGNAL( clicked() ), this, SLOT( reject() ) );
167
168 QSize sh = sizeHint().expandedTo( QSize(400, 10) );
169 setType( type );
170 resize( sh.width(), vbox->heightForWidth(sh.width()) );
171}
172
173/*!
174 Returns the line edit which is used in LineEdit mode.
175*/
176
177QLineEdit *QInputDialog::lineEdit() const
178{
179 return d->lineEdit;
180}
181
182/*!
183 Returns the spinbox which is used in SpinBox mode.
184*/
185
186QSpinBox *QInputDialog::spinBox() const
187{
188 return d->spinBox;
189}
190
191/*!
192 Returns the combobox which is used in ComboBox mode.
193*/
194
195QComboBox *QInputDialog::comboBox() const
196{
197 return d->comboBox;
198}
199
200/*!
201 Returns the combobox which is used in EditableComboBox mode.
202*/
203
204QComboBox *QInputDialog::editableComboBox() const
205{
206 return d->editComboBox;
207}
208
209/*!
210 Sets the input type of the dialog to \a t.
211*/
212
213void QInputDialog::setType( Type t )
214{
215 QWidget *input = 0;
216 switch ( t ) {
217 case LineEdit:
218 input = d->lineEdit;
219 break;
220 case SpinBox:
221 input = d->spinBox;
222 break;
223 case ComboBox:
224 input = d->comboBox;
225 break;
226 case EditableComboBox:
227 input = d->editComboBox;
228 break;
229 default:
230#if defined(QT_CHECK_STATE)
231 qWarning( "QInputDialog::setType: Invalid type" );
232#endif
233 break;
234 }
235 if ( input ) {
236 d->stack->raiseWidget( input );
237 d->stack->setFixedHeight( input->sizeHint().height() );
238 input->setFocus();
239#ifndef QT_NO_ACCEL
240 d->label->setBuddy( input );
241#endif
242 }
243
244 d->type = t;
245}
246
247/*!
248 Returns the input type of the dialog.
249
250 \sa setType()
251*/
252
253QInputDialog::Type QInputDialog::type() const
254{
255 return d->type;
256}
257
258/*!
259 Destructor.
260*/
261
262QInputDialog::~QInputDialog()
263{
264 delete d;
265}
266
267/*!
268 Static convenience function to get a string from the user. \a
269 caption is the text which is displayed in the title bar of the
270 dialog. \a label is the text which is shown to the user (it should
271 say what should be entered). \a text is the default text which is
272 placed in the line edit. The \a mode is the echo mode the line edit
273 will use. If \a ok is not-null \e *\a ok will be set to TRUE if the
274 user pressed OK and to FALSE if the user pressed Cancel. The
275 dialog's parent is \a parent; the dialog is called \a name. The
276 dialog will be modal.
277
278 This function returns the text which has been entered in the line
279 edit. It will not return an empty string.
280
281 Use this static function like this:
282
283 \code
284 bool ok;
285 QString text = QInputDialog::getText(
286 "MyApp 3000", "Enter your name:", QLineEdit::Normal,
287 QString::null, &ok, this );
288 if ( ok && !text.isEmpty() ) {
289 // user entered something and pressed OK
290 } else {
291 // user entered nothing or pressed Cancel
292 }
293 \endcode
294*/
295
296QString QInputDialog::getText( const QString &caption, const QString &label,
297 QLineEdit::EchoMode mode, const QString &text,
298 bool *ok, QWidget *parent, const char *name )
299{
300 QInputDialog *dlg = new QInputDialog( label, parent,
301 name ? name : "qt_inputdlg_gettext",
302 TRUE, LineEdit );
303
304#ifndef QT_NO_WIDGET_TOPEXTRA
305 dlg->setCaption( caption );
306#endif
307 dlg->lineEdit()->setText( text );
308 dlg->lineEdit()->setEchoMode( mode );
309
310 bool ok_ = FALSE;
311 QString result;
312 ok_ = dlg->exec() == QDialog::Accepted;
313 if ( ok )
314 *ok = ok_;
315 if ( ok_ )
316 result = dlg->lineEdit()->text();
317
318 delete dlg;
319 return result;
320}
321
322/*!
323 Static convenience function to get an integer input from the
324 user. \a caption is the text which is displayed in the title bar
325 of the dialog. \a label is the text which is shown to the user
326 (it should say what should be entered). \a value is the default
327 integer which the spinbox will be set to. \a minValue and \a
328 maxValue are the minimum and maximum values the user may choose,
329 and \a step is the amount by which the values change as the user
330 presses the arrow buttons to increment or decrement the value.
331
332 If \a ok is not-null *\a ok will be set to TRUE if the user
333 pressed OK and to FALSE if the user pressed Cancel. The dialog's
334 parent is \a parent; the dialog is called \a name. The dialog will
335 be modal.
336
337 This function returns the integer which has been entered by the user.
338
339 Use this static function like this:
340
341 \code
342 bool ok;
343 int res = QInputDialog::getInteger(
344 "MyApp 3000", "Enter a number:", 22, 0, 1000, 2,
345 &ok, this );
346 if ( ok ) {
347 // user entered something and pressed OK
348 } else {
349 // user pressed Cancel
350 }
351 \endcode
352*/
353
354int QInputDialog::getInteger( const QString &caption, const QString &label,
355 int value, int minValue, int maxValue, int step, bool *ok,
356 QWidget *parent, const char *name )
357{
358 QInputDialog *dlg = new QInputDialog( label, parent,
359 name ? name : "qt_inputdlg_getint",
360 TRUE, SpinBox );
361#ifndef QT_NO_WIDGET_TOPEXTRA
362 dlg->setCaption( caption );
363#endif
364 dlg->spinBox()->setRange( minValue, maxValue );
365 dlg->spinBox()->setSteps( step, 0 );
366 dlg->spinBox()->setValue( value );
367
368 bool ok_ = FALSE;
369 int result;
370 ok_ = dlg->exec() == QDialog::Accepted;
371 if ( ok )
372 *ok = ok_;
373 result = dlg->spinBox()->value();
374
375 delete dlg;
376 return result;
377}
378
379/*!
380 Static convenience function to get a floating point number from
381 the user. \a caption is the text which is displayed in the title
382 bar of the dialog. \a label is the text which is shown to the user
383 (it should say what should be entered). \a value is the default
384 floating point number that the line edit will be set to. \a
385 minValue and \a maxValue are the minimum and maximum values the
386 user may choose, and \a decimals is the maximum number of decimal
387 places the number may have.
388
389 If \a ok is not-null \e *\a ok will be set to TRUE if the user
390 pressed OK and to FALSE if the user pressed Cancel. The dialog's
391 parent is \a parent; the dialog is called \a name. The dialog will
392 be modal.
393
394 This function returns the floating point number which has been
395 entered by the user.
396
397 Use this static function like this:
398
399 \code
400 bool ok;
401 double res = QInputDialog::getDouble(
402 "MyApp 3000", "Enter a decimal number:", 33.7, 0,
403 1000, 2, &ok, this );
404 if ( ok ) {
405 // user entered something and pressed OK
406 } else {
407 // user pressed Cancel
408 }
409 \endcode
410*/
411
412double QInputDialog::getDouble( const QString &caption, const QString &label,
413 double value, double minValue, double maxValue,
414 int decimals, bool *ok, QWidget *parent,
415 const char *name )
416{
417 QInputDialog dlg( label, parent,
418 name ? name : "qt_inputdlg_getdbl", TRUE, LineEdit );
419#ifndef QT_NO_WIDGET_TOPEXTRA
420 dlg.setCaption( caption );
421#endif
422 dlg.lineEdit()->setValidator( new QDoubleValidator( minValue, maxValue, decimals, dlg.lineEdit() ) );
423 dlg.lineEdit()->setText( QString::number( value, 'f', decimals ) );
424 dlg.lineEdit()->selectAll();
425
426 bool accepted = ( dlg.exec() == QDialog::Accepted );
427 if ( ok )
428 *ok = accepted;
429 return dlg.lineEdit()->text().toDouble();
430}
431
432/*!
433 Static convenience function to let the user select an item from a
434 string list. \a caption is the text which is displayed in the title
435 bar of the dialog. \a label is the text which is shown to the user (it
436 should say what should be entered). \a list is the
437 string list which is inserted into the combobox, and \a current is the number
438 of the item which should be the current item. If \a editable is TRUE
439 the user can enter their own text; if \a editable is FALSE the user
440 may only select one of the existing items.
441
442 If \a ok is not-null \e *\a ok will be set to TRUE if the user
443 pressed OK and to FALSE if the user pressed Cancel. The dialog's
444 parent is \a parent; the dialog is called \a name. The dialog will
445 be modal.
446
447 This function returns the text of the current item, or if \a
448 editable is TRUE, the current text of the combobox.
449
450 Use this static function like this:
451
452 \code
453 QStringList lst;
454 lst << "First" << "Second" << "Third" << "Fourth" << "Fifth";
455 bool ok;
456 QString res = QInputDialog::getItem(
457 "MyApp 3000", "Select an item:", lst, 1, TRUE, &ok,
458 this );
459 if ( ok ) {
460 // user selected an item and pressed OK
461 } else {
462 // user pressed Cancel
463 }
464 \endcode
465*/
466
467QString QInputDialog::getItem( const QString &caption, const QString &label, const QStringList &list,
468 int current, bool editable,
469 bool *ok, QWidget *parent, const char *name )
470{
471 QInputDialog *dlg = new QInputDialog( label, parent, name ? name : "qt_inputdlg_getitem", TRUE, editable ? EditableComboBox : ComboBox );
472#ifndef QT_NO_WIDGET_TOPEXTRA
473 dlg->setCaption( caption );
474#endif
475 if ( editable ) {
476 dlg->editableComboBox()->insertStringList( list );
477 dlg->editableComboBox()->setCurrentItem( current );
478 } else {
479 dlg->comboBox()->insertStringList( list );
480 dlg->comboBox()->setCurrentItem( current );
481 }
482
483 bool ok_ = FALSE;
484 QString result;
485 ok_ = dlg->exec() == QDialog::Accepted;
486 if ( ok )
487 *ok = ok_;
488 if ( editable )
489 result = dlg->editableComboBox()->currentText();
490 else
491 result = dlg->comboBox()->currentText();
492
493 delete dlg;
494 return result;
495}
496
497/*!
498 \internal
499
500 This slot is invoked when the text is changed; the new text is passed
501 in \a s.
502*/
503
504void QInputDialog::textChanged( const QString &s )
505{
506 bool on = TRUE;
507
508 if ( d->lineEdit->validator() ) {
509 QString str = d->lineEdit->text();
510 int index = d->lineEdit->cursorPosition();
511 on = ( d->lineEdit->validator()->validate(str, index) ==
512 QValidator::Acceptable );
513 } else if ( type() != LineEdit ) {
514 on = !s.isEmpty();
515 }
516 d->ok->setEnabled( on );
517}
518
519/*!
520 \internal
521*/
522
523void QInputDialog::tryAccept()
524{
525 if ( !d->lineEdit->text().isEmpty() )
526 accept();
527}
528
529#endif
Note: See TracBrowser for help on using the repository browser.