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