kinputdialog.cpp
00001 /* 00002 Copyright (C) 2003 Nadeem Hasan <nhasan@kde.org> 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License as published by the Free Software Foundation; either 00007 version 2 of the License, or (at your option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to 00016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00017 Boston, MA 02110-1301, USA. 00018 */ 00019 00020 #include <qlayout.h> 00021 #include <qlabel.h> 00022 #include <qvalidator.h> 00023 #include <qwhatsthis.h> 00024 00025 #include <klineedit.h> 00026 #include <knuminput.h> 00027 #include <kcombobox.h> 00028 #include <klistbox.h> 00029 #include <ktextedit.h> 00030 00031 #include "kinputdialog.h" 00032 00033 class KInputDialogPrivate 00034 { 00035 public: 00036 KInputDialogPrivate(); 00037 00038 QLabel *m_label; 00039 KLineEdit *m_lineEdit; 00040 KIntSpinBox *m_intSpinBox; 00041 KDoubleSpinBox *m_doubleSpinBox; 00042 KComboBox *m_comboBox; 00043 KListBox *m_listBox; 00044 KTextEdit *m_textEdit; 00045 }; 00046 00047 KInputDialogPrivate::KInputDialogPrivate() 00048 : m_label( 0L ), m_lineEdit( 0L ), m_intSpinBox( 0L ), 00049 m_doubleSpinBox( 0L ), m_comboBox( 0L ) 00050 { 00051 } 00052 00053 KInputDialog::KInputDialog( const QString &caption, const QString &label, 00054 const QString &value, QWidget *parent, const char *name, 00055 QValidator *validator, const QString &mask ) 00056 : KDialogBase( parent, name, true, caption, Ok|Cancel|User1, Ok, true, 00057 KStdGuiItem::clear() ), 00058 d( new KInputDialogPrivate() ) 00059 { 00060 QFrame *frame = makeMainWidget(); 00061 QVBoxLayout *layout = new QVBoxLayout( frame, 0, spacingHint() ); 00062 00063 d->m_label = new QLabel( label, frame ); 00064 layout->addWidget( d->m_label ); 00065 00066 d->m_lineEdit = new KLineEdit( value, frame ); 00067 layout->addWidget( d->m_lineEdit ); 00068 00069 d->m_lineEdit->setFocus(); 00070 d->m_label->setBuddy( d->m_lineEdit ); 00071 00072 layout->addStretch(); 00073 00074 if ( validator ) 00075 d->m_lineEdit->setValidator( validator ); 00076 00077 if ( !mask.isEmpty() ) 00078 d->m_lineEdit->setInputMask( mask ); 00079 00080 connect( d->m_lineEdit, SIGNAL( textChanged( const QString & ) ), 00081 SLOT( slotEditTextChanged( const QString & ) ) ); 00082 connect( this, SIGNAL( user1Clicked() ), d->m_lineEdit, SLOT( clear() ) ); 00083 00084 slotEditTextChanged( value ); 00085 setMinimumWidth( 350 ); 00086 } 00087 00088 KInputDialog::KInputDialog( const QString &caption, const QString &label, 00089 const QString &value, QWidget *parent, const char *name ) 00090 : KDialogBase( parent, name, true, caption, Ok|Cancel|User1, Ok, false, 00091 KStdGuiItem::clear() ), 00092 d( new KInputDialogPrivate() ) 00093 { 00094 QFrame *frame = makeMainWidget(); 00095 QVBoxLayout *layout = new QVBoxLayout( frame, 0, spacingHint() ); 00096 00097 d->m_label = new QLabel( label, frame ); 00098 layout->addWidget( d->m_label ); 00099 00100 d->m_textEdit = new KTextEdit( frame ); 00101 d->m_textEdit->setTextFormat( PlainText ); 00102 d->m_textEdit->setText( value ); 00103 layout->addWidget( d->m_textEdit, 10 ); 00104 00105 d->m_textEdit->setFocus(); 00106 d->m_label->setBuddy( d->m_textEdit ); 00107 00108 connect( this, SIGNAL( user1Clicked() ), d->m_textEdit, SLOT( clear() ) ); 00109 00110 setMinimumWidth( 400 ); 00111 } 00112 00113 KInputDialog::KInputDialog( const QString &caption, const QString &label, 00114 int value, int minValue, int maxValue, int step, int base, 00115 QWidget *parent, const char *name ) 00116 : KDialogBase( parent, name, true, caption, Ok|Cancel, Ok, true ), 00117 d( new KInputDialogPrivate() ) 00118 { 00119 QFrame *frame = makeMainWidget(); 00120 QVBoxLayout *layout = new QVBoxLayout( frame, 0, spacingHint() ); 00121 00122 d->m_label = new QLabel( label, frame ); 00123 layout->addWidget( d->m_label ); 00124 00125 d->m_intSpinBox = new KIntSpinBox( minValue, maxValue, step, value, 00126 base, frame ); 00127 layout->addWidget( d->m_intSpinBox ); 00128 00129 layout->addStretch(); 00130 00131 d->m_intSpinBox->setFocus(); 00132 setMinimumWidth( 300 ); 00133 } 00134 00135 KInputDialog::KInputDialog( const QString &caption, const QString &label, 00136 double value, double minValue, double maxValue, double step, int decimals, 00137 QWidget *parent, const char *name ) 00138 : KDialogBase( parent, name, true, caption, Ok|Cancel, Ok, true ), 00139 d( new KInputDialogPrivate() ) 00140 { 00141 QFrame *frame = makeMainWidget(); 00142 QVBoxLayout *layout = new QVBoxLayout( frame, 0, spacingHint() ); 00143 00144 d->m_label = new QLabel( label, frame ); 00145 layout->addWidget( d->m_label ); 00146 00147 d->m_doubleSpinBox = new KDoubleSpinBox( minValue, maxValue, step, value, 00148 decimals, frame ); 00149 layout->addWidget( d->m_doubleSpinBox ); 00150 00151 layout->addStretch(); 00152 00153 d->m_doubleSpinBox->setFocus(); 00154 setMinimumWidth( 300 ); 00155 } 00156 00157 KInputDialog::KInputDialog( const QString &caption, const QString &label, 00158 const QStringList &list, int current, bool editable, QWidget *parent, 00159 const char *name ) 00160 : KDialogBase( parent, name, true, caption, Ok|Cancel|User1, Ok, true, 00161 KStdGuiItem::clear() ), 00162 d( new KInputDialogPrivate() ) 00163 { 00164 showButton( User1, editable ); 00165 00166 QFrame *frame = makeMainWidget(); 00167 QVBoxLayout *layout = new QVBoxLayout( frame, 0, spacingHint() ); 00168 00169 d->m_label = new QLabel( label, frame ); 00170 layout->addWidget( d->m_label ); 00171 00172 if ( editable ) 00173 { 00174 d->m_comboBox = new KComboBox( editable, frame ); 00175 d->m_comboBox->insertStringList( list ); 00176 d->m_comboBox->setCurrentItem( current ); 00177 layout->addWidget( d->m_comboBox ); 00178 00179 connect( d->m_comboBox, SIGNAL( textChanged( const QString & ) ), 00180 SLOT( slotUpdateButtons( const QString & ) ) ); 00181 connect( this, SIGNAL( user1Clicked() ), 00182 d->m_comboBox, SLOT( clearEdit() ) ); 00183 slotUpdateButtons( d->m_comboBox->currentText() ); 00184 d->m_comboBox->setFocus(); 00185 } else { 00186 d->m_listBox = new KListBox( frame ); 00187 d->m_listBox->insertStringList( list ); 00188 d->m_listBox->setSelected( current, true ); 00189 d->m_listBox->ensureCurrentVisible(); 00190 layout->addWidget( d->m_listBox, 10 ); 00191 connect( d->m_listBox, SIGNAL( doubleClicked( QListBoxItem * ) ), 00192 SLOT( slotOk() ) ); 00193 connect( d->m_listBox, SIGNAL( returnPressed( QListBoxItem * ) ), 00194 SLOT( slotOk() ) ); 00195 00196 d->m_listBox->setFocus(); 00197 } 00198 00199 layout->addStretch(); 00200 00201 setMinimumWidth( 320 ); 00202 } 00203 00204 KInputDialog::KInputDialog( const QString &caption, const QString &label, 00205 const QStringList &list, const QStringList &select, bool multiple, 00206 QWidget *parent, const char *name ) 00207 : KDialogBase( parent, name, true, caption, Ok|Cancel, Ok, true ), 00208 d( new KInputDialogPrivate() ) 00209 { 00210 QFrame *frame = makeMainWidget(); 00211 QVBoxLayout *layout = new QVBoxLayout( frame, 0, spacingHint() ); 00212 00213 d->m_label = new QLabel( label, frame ); 00214 layout->addWidget( d->m_label ); 00215 00216 d->m_listBox = new KListBox( frame ); 00217 d->m_listBox->insertStringList( list ); 00218 layout->addWidget( d->m_listBox ); 00219 00220 QListBoxItem *item; 00221 00222 if ( multiple ) 00223 { 00224 d->m_listBox->setSelectionMode( QListBox::Extended ); 00225 00226 for ( QStringList::ConstIterator it=select.begin(); it!=select.end(); ++it ) 00227 { 00228 item = d->m_listBox->findItem( *it, CaseSensitive|ExactMatch ); 00229 if ( item ) 00230 d->m_listBox->setSelected( item, true ); 00231 } 00232 } 00233 else 00234 { 00235 connect( d->m_listBox, SIGNAL( doubleClicked( QListBoxItem * ) ), 00236 SLOT( slotOk() ) ); 00237 connect( d->m_listBox, SIGNAL( returnPressed( QListBoxItem * ) ), 00238 SLOT( slotOk() ) ); 00239 00240 QString text = select.first(); 00241 item = d->m_listBox->findItem( text, CaseSensitive|ExactMatch ); 00242 if ( item ) 00243 d->m_listBox->setSelected( item, true ); 00244 } 00245 00246 d->m_listBox->ensureCurrentVisible(); 00247 d->m_listBox->setFocus(); 00248 00249 layout->addStretch(); 00250 00251 setMinimumWidth( 320 ); 00252 } 00253 00254 KInputDialog::~KInputDialog() 00255 { 00256 delete d; 00257 } 00258 00259 QString KInputDialog::getText( const QString &caption, const QString &label, 00260 const QString &value, bool *ok, QWidget *parent, const char *name, 00261 QValidator *validator, const QString &mask ) 00262 { 00263 return text( caption, label, value, ok, parent, name, validator, mask, 00264 QString::null ); 00265 } 00266 00267 QString KInputDialog::text( const QString &caption, 00268 const QString &label, const QString &value, bool *ok, QWidget *parent, 00269 const char *name, QValidator *validator, const QString &mask, 00270 const QString &whatsThis ) 00271 { 00272 KInputDialog dlg( caption, label, value, parent, name, validator, mask ); 00273 00274 if( !whatsThis.isEmpty() ) 00275 QWhatsThis::add( dlg.lineEdit(), whatsThis ); 00276 00277 bool _ok = ( dlg.exec() == Accepted ); 00278 00279 if ( ok ) 00280 *ok = _ok; 00281 00282 QString result; 00283 if ( _ok ) 00284 result = dlg.lineEdit()->text(); 00285 00286 // A validator may explicitly allow leading and trailing whitespace 00287 if ( !validator ) 00288 result = result.stripWhiteSpace(); 00289 00290 return result; 00291 } 00292 00293 QString KInputDialog::getMultiLineText( const QString &caption, 00294 const QString &label, const QString &value, bool *ok, 00295 QWidget *parent, const char *name ) 00296 { 00297 KInputDialog dlg( caption, label, value, parent, name ); 00298 00299 bool _ok = ( dlg.exec() == Accepted ); 00300 00301 if ( ok ) 00302 *ok = _ok; 00303 00304 QString result; 00305 if ( _ok ) 00306 result = dlg.textEdit()->text(); 00307 00308 return result; 00309 } 00310 00311 int KInputDialog::getInteger( const QString &caption, const QString &label, 00312 int value, int minValue, int maxValue, int step, int base, bool *ok, 00313 QWidget *parent, const char *name ) 00314 { 00315 KInputDialog dlg( caption, label, value, minValue, 00316 maxValue, step, base, parent, name ); 00317 00318 bool _ok = ( dlg.exec() == Accepted ); 00319 00320 if ( ok ) 00321 *ok = _ok; 00322 00323 int result=0; 00324 if ( _ok ) 00325 result = dlg.intSpinBox()->value(); 00326 00327 return result; 00328 } 00329 00330 int KInputDialog::getInteger( const QString &caption, const QString &label, 00331 int value, int minValue, int maxValue, int step, bool *ok, 00332 QWidget *parent, const char *name ) 00333 { 00334 return getInteger( caption, label, value, minValue, maxValue, step, 00335 10, ok, parent, name ); 00336 } 00337 00338 double KInputDialog::getDouble( const QString &caption, const QString &label, 00339 double value, double minValue, double maxValue, double step, int decimals, 00340 bool *ok, QWidget *parent, const char *name ) 00341 { 00342 KInputDialog dlg( caption, label, value, minValue, 00343 maxValue, step, decimals, parent, name ); 00344 00345 bool _ok = ( dlg.exec() == Accepted ); 00346 00347 if ( ok ) 00348 *ok = _ok; 00349 00350 double result=0; 00351 if ( _ok ) 00352 result = dlg.doubleSpinBox()->value(); 00353 00354 return result; 00355 } 00356 00357 double KInputDialog::getDouble( const QString &caption, const QString &label, 00358 double value, double minValue, double maxValue, int decimals, 00359 bool *ok, QWidget *parent, const char *name ) 00360 { 00361 return getDouble( caption, label, value, minValue, maxValue, 0.1, decimals, 00362 ok, parent, name ); 00363 } 00364 00365 QString KInputDialog::getItem( const QString &caption, const QString &label, 00366 const QStringList &list, int current, bool editable, bool *ok, 00367 QWidget *parent, const char *name ) 00368 { 00369 KInputDialog dlg( caption, label, list, current, 00370 editable, parent, name ); 00371 if ( !editable) 00372 { 00373 connect( dlg.listBox(), SIGNAL(doubleClicked ( QListBoxItem *)), &dlg, SLOT( slotOk())); 00374 } 00375 bool _ok = ( dlg.exec() == Accepted ); 00376 00377 if ( ok ) 00378 *ok = _ok; 00379 00380 QString result; 00381 if ( _ok ) 00382 if ( editable ) 00383 result = dlg.comboBox()->currentText(); 00384 else 00385 result = dlg.listBox()->currentText(); 00386 00387 return result; 00388 } 00389 00390 QStringList KInputDialog::getItemList( const QString &caption, 00391 const QString &label, const QStringList &list, const QStringList &select, 00392 bool multiple, bool *ok, QWidget *parent, const char *name ) 00393 { 00394 KInputDialog dlg( caption, label, list, select, 00395 multiple, parent, name ); 00396 00397 bool _ok = ( dlg.exec() == Accepted ); 00398 00399 if ( ok ) 00400 *ok = _ok; 00401 00402 QStringList result; 00403 if ( _ok ) 00404 { 00405 for (const QListBoxItem* i = dlg.listBox()->firstItem(); i != 0; i = i->next() ) 00406 if ( i->isSelected() ) 00407 result.append( i->text() ); 00408 } 00409 00410 return result; 00411 } 00412 00413 void KInputDialog::slotEditTextChanged( const QString &text ) 00414 { 00415 bool on; 00416 if ( lineEdit()->validator() ) { 00417 QString str = lineEdit()->text(); 00418 int index = lineEdit()->cursorPosition(); 00419 on = ( lineEdit()->validator()->validate( str, index ) 00420 == QValidator::Acceptable ); 00421 } else { 00422 on = !text.stripWhiteSpace().isEmpty(); 00423 } 00424 00425 enableButton( Ok, on ); 00426 enableButton( User1, !text.isEmpty() ); 00427 } 00428 00429 void KInputDialog::slotUpdateButtons( const QString &text ) 00430 { 00431 enableButton( Ok, !text.isEmpty() ); 00432 enableButton( User1, !text.isEmpty() ); 00433 } 00434 00435 KLineEdit *KInputDialog::lineEdit() const 00436 { 00437 return d->m_lineEdit; 00438 } 00439 00440 KIntSpinBox *KInputDialog::intSpinBox() const 00441 { 00442 return d->m_intSpinBox; 00443 } 00444 00445 KDoubleSpinBox *KInputDialog::doubleSpinBox() const 00446 { 00447 return d->m_doubleSpinBox; 00448 } 00449 00450 KComboBox *KInputDialog::comboBox() const 00451 { 00452 return d->m_comboBox; 00453 } 00454 00455 KListBox *KInputDialog::listBox() const 00456 { 00457 return d->m_listBox; 00458 } 00459 00460 KTextEdit *KInputDialog::textEdit() const 00461 { 00462 return d->m_textEdit; 00463 } 00464 00465 #include "kinputdialog.moc" 00466 00467 /* vim: set ai et sw=2 ts=2 00468 */