00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include "qinputdialog.h"
00039
00040 #include <qlayout.h>
00041 #include <qlabel.h>
00042 #include <qpushbutton.h>
00043 #include <qspinbox.h>
00044 #include <qcombobox.h>
00045 #include <qwidgetstack.h>
00046 #include <qvalidator.h>
00047 #include <qapplication.h>
00048
00049 class QInputDialogPrivate
00050 {
00051 public:
00052 friend class QInputDialog;
00053 QLineEdit *lineEdit;
00054 QSpinBox *spinBox;
00055 QComboBox *comboBox, *editComboBox;
00056 QPushButton *ok;
00057 QWidgetStack *stack;
00058 QInputDialog::Type type;
00059 };
00060
00121 QInputDialog::QInputDialog( const QString &label, QWidget* parent, const char* name,
00122 bool modal, Type type)
00123 : QDialog( parent, name, modal )
00124 {
00125 if ( parent && parent->icon() &&!parent->icon()->isNull() )
00126 setIcon( *parent->icon() );
00127 else if ( qApp->mainWidget() && qApp->mainWidget()->icon() && !qApp->mainWidget()->icon()->isNull() )
00128 QDialog::setIcon( *qApp->mainWidget()->icon() );
00129
00130 d = new QInputDialogPrivate;
00131 d->lineEdit = 0;
00132 d->spinBox = 0;
00133 d->comboBox = 0;
00134
00135 QVBoxLayout *vbox = new QVBoxLayout( this, 6, 6 );
00136
00137 QLabel* l = new QLabel( label, this );
00138 vbox->addWidget( l );
00139
00140 d->stack = new QWidgetStack( this );
00141 vbox->addWidget( d->stack );
00142 d->lineEdit = new QLineEdit( d->stack );
00143 d->spinBox = new QSpinBox( d->stack );
00144 d->comboBox = new QComboBox( FALSE, d->stack );
00145 d->editComboBox = new QComboBox( TRUE, d->stack );
00146
00147 QHBoxLayout *hbox = new QHBoxLayout( 6 );
00148 vbox->addLayout( hbox, AlignRight );
00149
00150 d->ok = new QPushButton( tr( "&OK" ), this );
00151 d->ok->setDefault( TRUE );
00152 QPushButton *cancel = new QPushButton( tr( "&Cancel" ), this );
00153
00154 QSize bs( d->ok->sizeHint() );
00155 if ( cancel->sizeHint().width() > bs.width() )
00156 bs.setWidth( cancel->sizeHint().width() );
00157
00158 d->ok->setFixedSize( bs );
00159 cancel->setFixedSize( bs );
00160
00161 hbox->addWidget( new QWidget( this ) );
00162 hbox->addWidget( d->ok );
00163 hbox->addWidget( cancel );
00164
00165 connect( d->lineEdit, SIGNAL( returnPressed() ),
00166 this, SLOT( tryAccept() ) );
00167 connect( d->lineEdit, SIGNAL( textChanged(const QString&) ),
00168 this, SLOT( textChanged(const QString&) ) );
00169
00170 connect( d->ok, SIGNAL( clicked() ), this, SLOT( accept() ) );
00171 connect( cancel, SIGNAL( clicked() ), this, SLOT( reject() ) );
00172
00173 resize( QMAX( sizeHint().width(), 400 ), sizeHint().height() );
00174
00175 setType( type );
00176 }
00177
00182 QLineEdit *QInputDialog::lineEdit() const
00183 {
00184 return d->lineEdit;
00185 }
00186
00191 QSpinBox *QInputDialog::spinBox() const
00192 {
00193 return d->spinBox;
00194 }
00195
00200 QComboBox *QInputDialog::comboBox() const
00201 {
00202 return d->comboBox;
00203 }
00204
00209 QComboBox *QInputDialog::editableComboBox() const
00210 {
00211 return d->editComboBox;
00212 }
00213
00218 void QInputDialog::setType( Type t )
00219 {
00220 switch ( t ) {
00221 case LineEdit:
00222 d->stack->raiseWidget( d->lineEdit );
00223 d->lineEdit->setFocus();
00224 break;
00225 case SpinBox:
00226 d->stack->raiseWidget( d->spinBox );
00227 d->spinBox->setFocus();
00228 break;
00229 case ComboBox:
00230 d->stack->raiseWidget( d->comboBox );
00231 d->comboBox->setFocus();
00232 break;
00233 case EditableComboBox:
00234 d->stack->raiseWidget( d->editComboBox );
00235 d->editComboBox->setFocus();
00236 break;
00237 }
00238
00239 d->type = t;
00240 }
00241
00248 QInputDialog::Type QInputDialog::type() const
00249 {
00250 return d->type;
00251 }
00252
00257 QInputDialog::~QInputDialog()
00258 {
00259 delete d;
00260 }
00261
00285 QString QInputDialog::getText( const QString &caption, const QString &label, const QString &text,
00286 bool *ok, QWidget *parent, const char *name )
00287 {
00288 return getText( caption, label, QLineEdit::Normal, text, ok, parent, name );
00289 }
00290
00297 QString QInputDialog::getText( const QString &caption, const QString &label, QLineEdit::EchoMode mode,
00298 const QString &text, bool *ok, QWidget *parent, const char *name )
00299 {
00300 QInputDialog *dlg = new QInputDialog( label, parent, name, TRUE, LineEdit );
00301 dlg->setCaption( caption );
00302 dlg->lineEdit()->setText( text );
00303 dlg->lineEdit()->setEchoMode( mode );
00304 if ( !text.isEmpty() )
00305 dlg->lineEdit()->selectAll();
00306
00307 bool ok_ = FALSE;
00308 QString result;
00309 ok_ = dlg->exec() == QDialog::Accepted;
00310 if ( ok )
00311 *ok = ok_;
00312 if ( ok_ )
00313 result = dlg->lineEdit()->text();
00314
00315 delete dlg;
00316 return result;
00317 }
00318
00344 int QInputDialog::getInteger( const QString &caption, const QString &label, int num, int from, int to, int step,
00345 bool *ok, QWidget *parent, const char *name )
00346 {
00347 QInputDialog *dlg = new QInputDialog( label, parent, name, TRUE, SpinBox );
00348 dlg->setCaption( caption );
00349 dlg->spinBox()->setRange( from, to );
00350 dlg->spinBox()->setSteps( step, 0 );
00351 dlg->spinBox()->setValue( num );
00352
00353 bool ok_ = FALSE;
00354 int result;
00355 ok_ = dlg->exec() == QDialog::Accepted;
00356 if ( ok )
00357 *ok = ok_;
00358 result = dlg->spinBox()->value();
00359
00360 delete dlg;
00361 return result;
00362 }
00363
00389 double QInputDialog::getDouble( const QString &caption, const QString &label, double num,
00390 double from, double to, int decimals,
00391 bool *ok, QWidget *parent, const char *name )
00392 {
00393 QInputDialog *dlg = new QInputDialog( label, parent, name, TRUE, LineEdit );
00394 dlg->setCaption( caption );
00395 dlg->lineEdit()->setValidator( new QDoubleValidator( from, to, decimals, dlg->lineEdit() ) );
00396 dlg->lineEdit()->setText( QString::number( num, 'f', decimals ) );
00397 dlg->lineEdit()->selectAll();
00398
00399 bool accepted = ( dlg->exec() == QDialog::Accepted );
00400 if ( ok )
00401 *ok = accepted;
00402
00403 double result = dlg->lineEdit()->text().toDouble();
00404
00405 delete dlg;
00406 return result;
00407 }
00408
00437 QString QInputDialog::getItem( const QString &caption, const QString &label, const QStringList &list,
00438 int current, bool editable,
00439 bool *ok, QWidget *parent, const char *name )
00440 {
00441 QInputDialog *dlg = new QInputDialog( label, parent, name, TRUE, editable ? EditableComboBox : ComboBox );
00442 dlg->setCaption( caption );
00443 if ( editable ) {
00444 dlg->editableComboBox()->insertStringList( list );
00445 dlg->editableComboBox()->setCurrentItem( current );
00446 } else {
00447 dlg->comboBox()->insertStringList( list );
00448 dlg->comboBox()->setCurrentItem( current );
00449 }
00450
00451 bool ok_ = FALSE;
00452 QString result;
00453 ok_ = dlg->exec() == QDialog::Accepted;
00454 if ( ok )
00455 *ok = ok_;
00456 if ( editable )
00457 result = dlg->editableComboBox()->currentText();
00458 else
00459 result = dlg->comboBox()->currentText();
00460
00461 delete dlg;
00462 return result;
00463 }
00464
00469 void QInputDialog::textChanged( const QString &s )
00470 {
00471 bool on;
00472 if ( d->lineEdit->validator() ) {
00473 QString str = d->lineEdit->text();
00474 int index = d->lineEdit->cursorPosition();
00475 on = ( d->lineEdit->validator()->validate(str, index) ==
00476 QValidator::Acceptable );
00477 } else {
00478 on = !s.isEmpty();
00479 }
00480 d->ok->setEnabled( on );
00481 }
00482
00487 void QInputDialog::tryAccept()
00488 {
00489 if ( !d->lineEdit->text().isEmpty() )
00490 accept();
00491 }
00492