summaryrefslogtreecommitdiff
path: root/libqtaux/qinputdialog.cpp
Unidiff
Diffstat (limited to 'libqtaux/qinputdialog.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--libqtaux/qinputdialog.cpp493
1 files changed, 493 insertions, 0 deletions
diff --git a/libqtaux/qinputdialog.cpp b/libqtaux/qinputdialog.cpp
new file mode 100644
index 0000000..821c74d
--- a/dev/null
+++ b/libqtaux/qinputdialog.cpp
@@ -0,0 +1,493 @@
1/****************************************************************************
2** $Id$
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#include <qlayout.h>
41#include <qlabel.h>
42#include <qlineedit.h>
43#include <qpushbutton.h>
44#include <qspinbox.h>
45#include <qcombobox.h>
46#include <qwidgetstack.h>
47#include <qvalidator.h>
48#include <qapplication.h>
49
50class QInputDialogPrivate
51{
52public:
53 friend class QInputDialog;
54 QLineEdit *lineEdit;
55 QSpinBox *spinBox;
56 QComboBox *comboBox, *editComboBox;
57 QPushButton *ok;
58 QWidgetStack *stack;
59 QInputDialog::Type type;
60};
61
62/*!
63 \class QInputDialog qinputdialog.h
64 \brief A convenience dialog to get a simple input from the user
65 \ingroup dialogs
66
67 The QInputDialog is a simple dialog which can be used if you
68 need a simple input from the user. This can be text, a number or
69 an item from a list. Also a label has to be set to tell the user
70 what he/she should input.
71
72 In this Qt version only the 4 static convenience functions
73 getText(), getInteger(), getDouble() and getItem() of QInputDialog
74 are available.
75
76 Use it like this:
77
78 \code
79 bool ok = FALSE;
80 QString text = QInputDialog::getText( tr( "Make an input" ), tr( "Please enter your name" ), QString::null, &ok, this );
81 if ( ok && !text.isEmpty() )
82 ;// user entered something and pressed ok
83 else
84 ;// user entered nothing or pressed cancel
85 \endcode
86
87 There are more static convenience methods!
88
89 \sa getText(), getInteger(), getDouble(), getItem()
90*/
91
92/*!
93 \enum QInputDialog::Type
94
95 This enum type specifies the type of the dialog
96 (which kind of input can be done):
97
98 <ul>
99 <li>\c LineEdit - A QLineEdit is used for taking the input, so a textual or
100 (e.g. using a QValidator) a numerical input can be done. Using lineEdit()
101 the QLineEdit can be accessed.
102 <li>\c SpinBox - A QSpinBox is used for taking the input, so a decimal
103 input can be done. Using spinBox() the QSpinBox can be accessed.
104 <li>\c ComboBox - A read-only QComboBox is used for taking the input,
105 so one item of a list can be chosen. Using comboBox() the QComboBox
106 can be accessed.
107 <li>\c EditableComboBox - An editable QComboBox is used for taking the input,
108 so either one item of a list can be chosen or a text can be entered. Using
109 editableComboBox() the QComboBox can be accessed.
110 </ul>
111*/
112
113/*!
114 Constructs the dialog. \a label is the text which is shown to the user (it should mention
115 to the user what he/she should input), \a parent the parent widget of the dialog, \a name
116 the name of it and if you set \a modal to TRUE, the dialog pops up modally, else it pops
117 up modeless. With \a type you specify the type of the dialog.
118
119 \sa getText(), getInteger(), getDouble(), getItem()
120*/
121
122QInputDialog::QInputDialog( const QString &label, QWidget* parent, const char* name,
123 bool modal, Type type)
124 : QDialog( parent, name, modal )
125{
126 if ( parent && parent->icon() &&!parent->icon()->isNull() )
127 setIcon( *parent->icon() );
128 else if ( qApp->mainWidget() && qApp->mainWidget()->icon() && !qApp->mainWidget()->icon()->isNull() )
129 QDialog::setIcon( *qApp->mainWidget()->icon() );
130
131 d = new QInputDialogPrivate;
132 d->lineEdit = 0;
133 d->spinBox = 0;
134 d->comboBox = 0;
135
136 QVBoxLayout *vbox = new QVBoxLayout( this, 6, 6 );
137
138 QLabel* l = new QLabel( label, this );
139 vbox->addWidget( l );
140
141 d->stack = new QWidgetStack( this );
142 vbox->addWidget( d->stack );
143 d->lineEdit = new QLineEdit( d->stack );
144 d->spinBox = new QSpinBox( d->stack );
145 d->comboBox = new QComboBox( FALSE, d->stack );
146 d->editComboBox = new QComboBox( TRUE, d->stack );
147
148 QHBoxLayout *hbox = new QHBoxLayout( 6 );
149 vbox->addLayout( hbox, AlignRight );
150
151 d->ok = new QPushButton( tr( "&OK" ), this );
152 d->ok->setDefault( TRUE );
153 QPushButton *cancel = new QPushButton( tr( "&Cancel" ), this );
154
155 QSize bs( d->ok->sizeHint() );
156 if ( cancel->sizeHint().width() > bs.width() )
157 bs.setWidth( cancel->sizeHint().width() );
158
159 d->ok->setFixedSize( bs );
160 cancel->setFixedSize( bs );
161
162 hbox->addWidget( new QWidget( this ) );
163 hbox->addWidget( d->ok );
164 hbox->addWidget( cancel );
165
166 connect( d->lineEdit, SIGNAL( returnPressed() ),
167 this, SLOT( tryAccept() ) );
168 connect( d->lineEdit, SIGNAL( textChanged(const QString&) ),
169 this, SLOT( textChanged(const QString&) ) );
170
171 connect( d->ok, SIGNAL( clicked() ), this, SLOT( accept() ) );
172 connect( cancel, SIGNAL( clicked() ), this, SLOT( reject() ) );
173
174 resize( QMAX( sizeHint().width(), 400 ), sizeHint().height() );
175
176 setType( type );
177}
178
179/*!
180 Returns the line edit, which is used in the LineEdit mode
181*/
182
183QLineEdit *QInputDialog::lineEdit() const
184{
185 return d->lineEdit;
186}
187
188/*!
189 Returns the spinbox, which is used in the SpinBox mode
190*/
191
192QSpinBox *QInputDialog::spinBox() const
193{
194 return d->spinBox;
195}
196
197/*!
198 Returns the combobox, which is used in the ComboBox mode
199*/
200
201QComboBox *QInputDialog::comboBox() const
202{
203 return d->comboBox;
204}
205
206/*!
207 Returns the combobox, which is used in the EditableComboBox mode
208*/
209
210QComboBox *QInputDialog::editableComboBox() const
211{
212 return d->editComboBox;
213}
214
215/*!
216 Sets the input type of the dialog to \a t.
217*/
218
219void QInputDialog::setType( Type t )
220{
221 switch ( t ) {
222 case LineEdit:
223 d->stack->raiseWidget( d->lineEdit );
224 d->lineEdit->setFocus();
225 break;
226 case SpinBox:
227 d->stack->raiseWidget( d->spinBox );
228 d->spinBox->setFocus();
229 break;
230 case ComboBox:
231 d->stack->raiseWidget( d->comboBox );
232 d->comboBox->setFocus();
233 break;
234 case EditableComboBox:
235 d->stack->raiseWidget( d->editComboBox );
236 d->editComboBox->setFocus();
237 break;
238 }
239
240 d->type = t;
241}
242
243/*!
244 Returns the input type of the dialog.
245
246 \sa setType()
247*/
248
249QInputDialog::Type QInputDialog::type() const
250{
251 return d->type;
252}
253
254/*!
255 Destructor.
256*/
257
258QInputDialog::~QInputDialog()
259{
260 delete d;
261}
262
263/*!
264 Static convenience function to get a textual input from the user. \a caption is the text
265 which is displayed in the title bar of the dialog. \a label is the text which
266 is shown to the user (it should mention to the user what he/she should input), \a text
267 the default text which will be initially set to the line edit, \a ok a pointer to
268 a bool which will be (if not 0!) set to TRUE if the user pressed ok or to FALSE if the
269 user pressed cancel, \a parent the parent widget of the dialog and \a name
270 the name of it. The dialogs pops up modally!
271
272 This method returns the text which has been entered in the line edit.
273
274 You will use this static method like this:
275
276 \code
277 bool ok = FALSE;
278 QString text = QInputDialog::getText( tr( "Please enter your name" ), QString::null, &ok, this );
279 if ( ok && !text.isEmpty() )
280 ;// user entered something and pressed ok
281 else
282 ;// user entered nothing or pressed cancel
283 \endcode
284*/
285
286QString QInputDialog::getText( const QString &caption, const QString &label, const QString &text,
287 bool *ok, QWidget *parent, const char *name )
288{
289 return getText( caption, label, QLineEdit::Normal, text, ok, parent, name );
290}
291
292/*!
293 Like above, but accepts an a \a mode which the line edit will use to display text.
294
295 \sa getText()
296*/
297
298QString QInputDialog::getText( const QString &caption, const QString &label, QLineEdit::EchoMode mode,
299 const QString &text, bool *ok, QWidget *parent, const char *name )
300{
301 QInputDialog *dlg = new QInputDialog( label, parent, name, TRUE, LineEdit );
302 dlg->setCaption( caption );
303 dlg->lineEdit()->setText( text );
304 dlg->lineEdit()->setEchoMode( mode );
305 if ( !text.isEmpty() )
306 dlg->lineEdit()->selectAll();
307
308 bool ok_ = FALSE;
309 QString result;
310 ok_ = dlg->exec() == QDialog::Accepted;
311 if ( ok )
312 *ok = ok_;
313 if ( ok_ )
314 result = dlg->lineEdit()->text();
315
316 delete dlg;
317 return result;
318}
319
320/*!
321 Static convenience function to get an integral input from the user. \a caption is the text
322 which is displayed in the title bar of the dialog. \a label is the text which
323 is shown to the user (it should mention to the user what he/she should input), \a num
324 the default number which will be initially set to the spinbox, \a from and \a to the
325 range in which the entered number has to be, \a step the step in which the number can
326 be increased/decreased by the spinbox, \a ok a pointer to
327 a bool which will be (if not 0!) set to TRUE if the user pressed ok or to FALSE if the
328 user pressed cancel, \a parent the parent widget of the dialog and \a name
329 the name of it. The dialogs pops up modally!
330
331 This method returns the number which has been entered by the user.
332
333 You will use this static method like this:
334
335 \code
336 bool ok = FALSE;
337 int res = QInputDialog::getInteger( tr( "Please enter a number" ), 22, 0, 1000, 2, &ok, this );
338 if ( ok )
339 ;// user entered something and pressed ok
340 else
341 ;// user pressed cancel
342 \endcode
343*/
344
345int QInputDialog::getInteger( const QString &caption, const QString &label, int num, int from, int to, int step,
346 bool *ok, QWidget *parent, const char *name )
347{
348 QInputDialog *dlg = new QInputDialog( label, parent, name, TRUE, SpinBox );
349 dlg->setCaption( caption );
350 dlg->spinBox()->setRange( from, to );
351 dlg->spinBox()->setSteps( step, 0 );
352 dlg->spinBox()->setValue( num );
353
354 bool ok_ = FALSE;
355 int result;
356 ok_ = dlg->exec() == QDialog::Accepted;
357 if ( ok )
358 *ok = ok_;
359 result = dlg->spinBox()->value();
360
361 delete dlg;
362 return result;
363}
364
365/*!
366 Static convenience function to get a decimal input from the user. \a caption is the text
367 which is displayed in the title bar of the dialog. \a label is the text which
368 is shown to the user (it should mention to the user what he/she should input), \a num
369 the default decimal number which will be initially set to the line edit, \a from and \a to the
370 range in which the entered number has to be, \a decimals the number of decimal which
371 the number may have, \a ok a pointer to
372 a bool which will be (if not 0!) set to TRUE if the user pressed ok or to FALSE if the
373 user pressed cancel, \a parent the parent widget of the dialog and \a name
374 the name of it. The dialogs pops up modally!
375
376 This method returns the number which has been entered by the user.
377
378 You will use this static method like this:
379
380 \code
381 bool ok = FALSE;
382 double res = QInputDialog::getDouble( tr( "Please enter a decimal number" ), 33.7, 0, 1000, 2, &ok, this );
383 if ( ok )
384 ;// user entered something and pressed ok
385 else
386 ;// user pressed cancel
387 \endcode
388*/
389
390double QInputDialog::getDouble( const QString &caption, const QString &label, double num,
391 double from, double to, int decimals,
392 bool *ok, QWidget *parent, const char *name )
393{
394 QInputDialog *dlg = new QInputDialog( label, parent, name, TRUE, LineEdit );
395 dlg->setCaption( caption );
396 dlg->lineEdit()->setValidator( new QDoubleValidator( from, to, decimals, dlg->lineEdit() ) );
397 dlg->lineEdit()->setText( QString::number( num, 'f', decimals ) );
398 dlg->lineEdit()->selectAll();
399
400 bool accepted = ( dlg->exec() == QDialog::Accepted );
401 if ( ok )
402 *ok = accepted;
403
404 double result = dlg->lineEdit()->text().toDouble();
405
406 delete dlg;
407 return result;
408}
409
410/*!
411 Static convenience function to let the user select an item from a string list. \a caption is the text
412 which is displayed in the title bar of the dialog. \a label is the text which
413 is shown to the user (it should mention to the user what he/she should input), \a list the
414 string list which is inserted into the combobox, \a current the number of the item which should
415 be initially the current item, \a editable specifies if the combobox should be editable (if it is TRUE)
416 or read-only (if \a editable is FALSE), \a ok a pointer to
417 a bool which will be (if not 0!) set to TRUE if the user pressed ok or to FALSE if the
418 user pressed cancel, \a parent the parent widget of the dialog and \a name
419 the name of it. The dialogs pops up modally!
420
421 This method returns the text of the current item, or if \a editable was TRUE, the current
422 text of the combobox.
423
424 You will use this static method like this:
425
426 \code
427 QStringList lst;
428 lst << "First" << "Second" << "Third" << "Fourth" << "Fifth";
429 bool ok = FALSE;
430 QString res = QInputDialog::getItem( tr( "Please select an item" ), lst, 1, TRUE, &ok, this );
431 if ( ok )
432 ;// user selected an item and pressed ok
433 else
434 ;// user pressed cancel
435 \endcode
436*/
437
438QString QInputDialog::getItem( const QString &caption, const QString &label, const QStringList &list,
439 int current, bool editable,
440 bool *ok, QWidget *parent, const char *name )
441{
442 QInputDialog *dlg = new QInputDialog( label, parent, name, TRUE, editable ? EditableComboBox : ComboBox );
443 dlg->setCaption( caption );
444 if ( editable ) {
445 dlg->editableComboBox()->insertStringList( list );
446 dlg->editableComboBox()->setCurrentItem( current );
447 } else {
448 dlg->comboBox()->insertStringList( list );
449 dlg->comboBox()->setCurrentItem( current );
450 }
451
452 bool ok_ = FALSE;
453 QString result;
454 ok_ = dlg->exec() == QDialog::Accepted;
455 if ( ok )
456 *ok = ok_;
457 if ( editable )
458 result = dlg->editableComboBox()->currentText();
459 else
460 result = dlg->comboBox()->currentText();
461
462 delete dlg;
463 return result;
464}
465
466/*!
467 \internal
468*/
469
470void QInputDialog::textChanged( const QString &s )
471{
472 bool on;
473 if ( d->lineEdit->validator() ) {
474 QString str = d->lineEdit->text();
475 int index = d->lineEdit->cursorPosition();
476 on = ( d->lineEdit->validator()->validate(str, index) ==
477 QValidator::Acceptable );
478 } else {
479 on = !s.isEmpty();
480 }
481 d->ok->setEnabled( on );
482}
483
484/*!
485 \internal
486*/
487
488void QInputDialog::tryAccept()
489{
490 if ( !d->lineEdit->text().isEmpty() )
491 accept();
492}
493