kdeui Library API Documentation

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., 59 Temple Place - Suite 330,
00017   Boston, MA 02111-1307, 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( 0L )
00059 {
00060   d = new KInputDialogPrivate();
00061 
00062   QFrame *frame = makeMainWidget();
00063   QVBoxLayout *layout = new QVBoxLayout( frame, 0, spacingHint() );
00064 
00065   d->m_label = new QLabel( label, frame );
00066   layout->addWidget( d->m_label );
00067 
00068   d->m_lineEdit = new KLineEdit( value, frame );
00069   layout->addWidget( d->m_lineEdit );
00070 
00071   d->m_lineEdit->setFocus();
00072   d->m_label->setBuddy( d->m_lineEdit );
00073 
00074   layout->addStretch();
00075 
00076   if ( validator )
00077     d->m_lineEdit->setValidator( validator );
00078 
00079   if ( !mask.isEmpty() )
00080     d->m_lineEdit->setInputMask( mask );
00081 
00082   connect( d->m_lineEdit, SIGNAL( textChanged( const QString & ) ),
00083       SLOT( slotEditTextChanged( const QString & ) ) );
00084   connect( this, SIGNAL( user1Clicked() ), d->m_lineEdit, SLOT( clear() ) );
00085 
00086   slotEditTextChanged( value );
00087   setMinimumWidth( 350 );
00088 }
00089 
00090 KInputDialog::KInputDialog( const QString &caption, const QString &label,
00091     const QString &value, QWidget *parent, const char *name )
00092     : KDialogBase( parent, name, true, caption, Ok|Cancel|User1, Ok, false,
00093     KStdGuiItem::clear() ),
00094     d( 0L )
00095 {
00096   d = new KInputDialogPrivate();
00097 
00098   QFrame *frame = makeMainWidget();
00099   QVBoxLayout *layout = new QVBoxLayout( frame, 0, spacingHint() );
00100 
00101   d->m_label = new QLabel( 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, SIGNAL( user1Clicked() ), d->m_textEdit, SLOT( clear() ) );
00113 
00114   setMinimumWidth( 400 );
00115 }
00116 
00117 KInputDialog::KInputDialog( const QString &caption, const QString &label,
00118     int value, int minValue, int maxValue, int step, int base,
00119     QWidget *parent, const char *name )
00120     : KDialogBase( parent, name, true, caption, Ok|Cancel, Ok, true ),
00121     d( 0L )
00122 {
00123   d = new KInputDialogPrivate();
00124 
00125   QFrame *frame = makeMainWidget();
00126   QVBoxLayout *layout = new QVBoxLayout( frame, 0, spacingHint() );
00127 
00128   d->m_label = new QLabel( label, frame );
00129   layout->addWidget( d->m_label );
00130 
00131   d->m_intSpinBox = new KIntSpinBox( minValue, maxValue, step, value,
00132       base, frame );
00133   layout->addWidget( d->m_intSpinBox );
00134 
00135   layout->addStretch();
00136 
00137   d->m_intSpinBox->setFocus();
00138   setMinimumWidth( 300 );
00139 }
00140 
00141 KInputDialog::KInputDialog( const QString &caption, const QString &label,
00142     double value, double minValue, double maxValue, double step, int decimals,
00143     QWidget *parent, const char *name )
00144     : KDialogBase( parent, name, true, caption, Ok|Cancel, Ok, true ),
00145     d( 0L )
00146 {
00147   d = new KInputDialogPrivate();
00148 
00149   QFrame *frame = makeMainWidget();
00150   QVBoxLayout *layout = new QVBoxLayout( frame, 0, spacingHint() );
00151 
00152   d->m_label = new QLabel( label, frame );
00153   layout->addWidget( d->m_label );
00154 
00155   d->m_doubleSpinBox = new KDoubleSpinBox( minValue, maxValue, step, value,
00156       decimals, frame );
00157   layout->addWidget( d->m_doubleSpinBox );
00158 
00159   layout->addStretch();
00160 
00161   d->m_doubleSpinBox->setFocus();
00162   setMinimumWidth( 300 );
00163 }
00164 
00165 KInputDialog::KInputDialog( const QString &caption, const QString &label,
00166     const QStringList &list, int current, bool editable, QWidget *parent,
00167     const char *name )
00168     : KDialogBase( parent, name, true, caption, Ok|Cancel|User1, Ok, true,
00169     KStdGuiItem::clear() ),
00170     d( 0L )
00171 {
00172   d = new KInputDialogPrivate();
00173 
00174   showButton( User1, editable );
00175 
00176   QFrame *frame = makeMainWidget();
00177   QVBoxLayout *layout = new QVBoxLayout( frame, 0, spacingHint() );
00178 
00179   d->m_label = new QLabel( label, frame );
00180   layout->addWidget( d->m_label );
00181 
00182   if ( editable )
00183   {
00184     d->m_comboBox = new KComboBox( editable, frame );
00185     d->m_comboBox->insertStringList( list );
00186     d->m_comboBox->setCurrentItem( current );
00187     layout->addWidget( d->m_comboBox );
00188 
00189     connect( d->m_comboBox, SIGNAL( textChanged( const QString & ) ),
00190       SLOT( slotUpdateButtons( const QString & ) ) );
00191     connect( this, SIGNAL( user1Clicked() ),
00192       d->m_comboBox, SLOT( clearEdit() ) );
00193     slotUpdateButtons( d->m_comboBox->currentText() );
00194     d->m_comboBox->setFocus();
00195   } else {
00196     d->m_listBox = new KListBox( frame );
00197     d->m_listBox->insertStringList( list );
00198     d->m_listBox->setSelected( current, true );
00199     d->m_listBox->ensureCurrentVisible();
00200     layout->addWidget( d->m_listBox, 10 );
00201     connect( d->m_listBox, SIGNAL( doubleClicked( QListBoxItem * ) ),
00202       SLOT( slotOk() ) );
00203     connect( d->m_listBox, SIGNAL( returnPressed( QListBoxItem * ) ),
00204       SLOT( slotOk() ) );
00205 
00206     d->m_listBox->setFocus();
00207   }
00208 
00209   layout->addStretch();
00210 
00211   setMinimumWidth( 320 );
00212 }
00213 
00214 KInputDialog::KInputDialog( const QString &caption, const QString &label,
00215     const QStringList &list, const QStringList &select, bool multiple,
00216     QWidget *parent, const char *name )
00217     : KDialogBase( parent, name, true, caption, Ok|Cancel, Ok, true ),
00218     d( 0L )
00219 {
00220   d = new KInputDialogPrivate();
00221 
00222   QFrame *frame = makeMainWidget();
00223   QVBoxLayout *layout = new QVBoxLayout( frame, 0, spacingHint() );
00224 
00225   d->m_label = new QLabel( label, frame );
00226   layout->addWidget( d->m_label );
00227 
00228   d->m_listBox = new KListBox( frame );
00229   d->m_listBox->insertStringList( list );
00230   layout->addWidget( d->m_listBox );
00231 
00232   QListBoxItem *item;
00233 
00234   if ( multiple )
00235   {
00236     d->m_listBox->setSelectionMode( QListBox::Extended );
00237 
00238     for ( QStringList::ConstIterator it=select.begin(); it!=select.end(); ++it )
00239     {
00240       item = d->m_listBox->findItem( *it, CaseSensitive|ExactMatch );
00241       if ( item )
00242         d->m_listBox->setSelected( item, true );
00243     }
00244   }
00245   else
00246   {
00247     connect( d->m_listBox, SIGNAL( doubleClicked( QListBoxItem * ) ),
00248       SLOT( slotOk() ) );
00249     connect( d->m_listBox, SIGNAL( returnPressed( QListBoxItem * ) ),
00250       SLOT( slotOk() ) );
00251 
00252     QString text = select.first();
00253     item = d->m_listBox->findItem( text, CaseSensitive|ExactMatch );
00254     if ( item )
00255       d->m_listBox->setSelected( item, true );
00256   }
00257 
00258   d->m_listBox->ensureCurrentVisible();
00259   d->m_listBox->setFocus();
00260 
00261   layout->addStretch();
00262 
00263   setMinimumWidth( 320 );
00264 }
00265 
00266 KInputDialog::~KInputDialog()
00267 {
00268   delete d;
00269 }
00270 
00271 QString KInputDialog::getText( const QString &caption, const QString &label,
00272     const QString &value, bool *ok, QWidget *parent, const char *name,
00273     QValidator *validator, const QString &mask )
00274 {
00275   return text( caption, label, value, ok, parent, name, validator, mask,
00276                QString::null );
00277 }
00278 
00279 QString KInputDialog::text( const QString &caption,
00280     const QString &label, const QString &value, bool *ok, QWidget *parent,
00281     const char *name, QValidator *validator, const QString &mask,
00282     const QString &whatsThis )
00283 {
00284   KInputDialog dlg( caption, label, value, parent, name, validator, mask );
00285 
00286   if( !whatsThis.isEmpty() )
00287     QWhatsThis::add( dlg.lineEdit(), whatsThis );
00288 
00289   bool _ok = ( dlg.exec() == Accepted );
00290 
00291   if ( ok )
00292     *ok = _ok;
00293 
00294   QString result;
00295   if ( _ok )
00296     result = dlg.lineEdit()->text();
00297 
00298   // A validator may explicitly allow leading and trailing whitespace
00299   if ( !validator )
00300     result = result.stripWhiteSpace();
00301 
00302   return result;
00303 }
00304 
00305 QString KInputDialog::getMultiLineText( const QString &caption,
00306     const QString &label, const QString &value, bool *ok,
00307     QWidget *parent, const char *name )
00308 {
00309   KInputDialog dlg( caption, label, value, parent, name );
00310 
00311   bool _ok = ( dlg.exec() == Accepted );
00312 
00313   if ( ok )
00314     *ok = _ok;
00315 
00316   QString result;
00317   if ( _ok )
00318     result = dlg.textEdit()->text();
00319 
00320   return result;
00321 }
00322 
00323 int KInputDialog::getInteger( const QString &caption, const QString &label,
00324     int value, int minValue, int maxValue, int step, int base, bool *ok,
00325     QWidget *parent, const char *name )
00326 {
00327   KInputDialog dlg( caption, label, value, minValue,
00328     maxValue, step, base, parent, name );
00329 
00330   bool _ok = ( dlg.exec() == Accepted );
00331 
00332   if ( ok )
00333     *ok = _ok;
00334 
00335   int result=0;
00336   if ( _ok )
00337     result = dlg.intSpinBox()->value();
00338 
00339   return result;
00340 }
00341 
00342 int KInputDialog::getInteger( const QString &caption, const QString &label,
00343     int value, int minValue, int maxValue, int step, bool *ok,
00344     QWidget *parent, const char *name )
00345 {
00346   return getInteger( caption, label, value, minValue, maxValue, step,
00347     10, ok, parent, name );
00348 }
00349 
00350 double KInputDialog::getDouble( const QString &caption, const QString &label,
00351     double value, double minValue, double maxValue, double step, int decimals,
00352     bool *ok, QWidget *parent, const char *name )
00353 {
00354   KInputDialog dlg( caption, label, value, minValue,
00355     maxValue, step, decimals, parent, name );
00356 
00357   bool _ok = ( dlg.exec() == Accepted );
00358 
00359   if ( ok )
00360     *ok = _ok;
00361 
00362   double result=0;
00363   if ( _ok )
00364     result = dlg.doubleSpinBox()->value();
00365 
00366   return result;
00367 }
00368 
00369 double KInputDialog::getDouble( const QString &caption, const QString &label,
00370     double value, double minValue, double maxValue, int decimals,
00371     bool *ok, QWidget *parent, const char *name )
00372 {
00373   return getDouble( caption, label, value, minValue, maxValue, 0.1, decimals,
00374     ok, parent, name );
00375 }
00376 
00377 QString KInputDialog::getItem( const QString &caption, const QString &label,
00378     const QStringList &list, int current, bool editable, bool *ok,
00379     QWidget *parent, const char *name )
00380 {
00381   KInputDialog dlg( caption, label, list, current,
00382     editable, parent, name );
00383   if ( !editable)
00384   {
00385       connect( dlg.listBox(),  SIGNAL(doubleClicked ( QListBoxItem *)), &dlg, SLOT( slotOk()));
00386   }
00387   bool _ok = ( dlg.exec() == Accepted );
00388 
00389   if ( ok )
00390     *ok = _ok;
00391 
00392   QString result;
00393   if ( _ok )
00394     if ( editable )
00395       result = dlg.comboBox()->currentText();
00396     else
00397       result = dlg.listBox()->currentText();
00398 
00399   return result;
00400 }
00401 
00402 QStringList KInputDialog::getItemList( const QString &caption,
00403     const QString &label, const QStringList &list, const QStringList &select,
00404     bool multiple, bool *ok, QWidget *parent, const char *name )
00405 {
00406   KInputDialog dlg( caption, label, list, select,
00407     multiple, parent, name );
00408 
00409   bool _ok = ( dlg.exec() == Accepted );
00410 
00411   if ( ok )
00412     *ok = _ok;
00413 
00414   QStringList result;
00415   if ( _ok )
00416   {
00417     for ( unsigned int i=0; i<list.count(); ++i )
00418       if ( dlg.listBox()->isSelected( i ) )
00419         result.append( dlg.listBox()->text( i ) );
00420   }
00421 
00422   return result;
00423 }
00424 
00425 void KInputDialog::slotEditTextChanged( const QString &text )
00426 {
00427   bool on;
00428   if ( lineEdit()->validator() ) {
00429     QString str = lineEdit()->text();
00430     int index = lineEdit()->cursorPosition();
00431     on = ( lineEdit()->validator()->validate( str, index )
00432       == QValidator::Acceptable );
00433   } else {
00434     on = !text.stripWhiteSpace().isEmpty();
00435   }
00436 
00437   enableButton( Ok, on );
00438   enableButton( User1, !text.isEmpty() );
00439 }
00440 
00441 void KInputDialog::slotUpdateButtons( const QString &text )
00442 {
00443   enableButton( Ok, !text.isEmpty() );
00444   enableButton( User1, !text.isEmpty() );
00445 }
00446 
00447 KLineEdit *KInputDialog::lineEdit() const
00448 {
00449   return d->m_lineEdit;
00450 }
00451 
00452 KIntSpinBox *KInputDialog::intSpinBox() const
00453 {
00454   return d->m_intSpinBox;
00455 }
00456 
00457 KDoubleSpinBox *KInputDialog::doubleSpinBox() const
00458 {
00459   return d->m_doubleSpinBox;
00460 }
00461 
00462 KComboBox *KInputDialog::comboBox() const
00463 {
00464   return d->m_comboBox;
00465 }
00466 
00467 KListBox *KInputDialog::listBox() const
00468 {
00469   return d->m_listBox;
00470 }
00471 
00472 KTextEdit *KInputDialog::textEdit() const
00473 {
00474   return d->m_textEdit;
00475 }
00476 
00477 #include "kinputdialog.moc"
00478 
00479 /* vim: set ai et sw=2 ts=2
00480 */
KDE Logo
This file is part of the documentation for kdeui Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sun Jan 15 13:32:53 2006 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003