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 
00024 #include <klineedit.h>
00025 #include <knuminput.h>
00026 #include <kcombobox.h>
00027 #include <klistbox.h>
00028 
00029 #include "kinputdialog.h"
00030 
00031 class KInputDialogPrivate
00032 {
00033   public:
00034     KInputDialogPrivate();
00035 
00036     QLabel *m_label;
00037     KLineEdit *m_lineEdit;
00038     KIntSpinBox *m_intSpinBox;
00039     KDoubleSpinBox *m_doubleSpinBox;
00040     KComboBox *m_comboBox;
00041     KListBox *m_listBox;
00042 };
00043 
00044 KInputDialogPrivate::KInputDialogPrivate()
00045     : m_label( 0L ), m_lineEdit( 0L ), m_intSpinBox( 0L ),
00046       m_doubleSpinBox( 0L ), m_comboBox( 0L )
00047 {
00048 }
00049 
00050 KInputDialog::KInputDialog( const QString &caption, const QString &label,
00051     const QString &value, QWidget *parent, const char *name,
00052     QValidator *validator, const QString &mask )
00053     : KDialogBase( parent, name, true, caption, Ok|Cancel|User1, Ok, true,
00054     KStdGuiItem::clear() ),
00055     d( 0L )
00056 {
00057   d = new KInputDialogPrivate();
00058 
00059   QFrame *frame = makeMainWidget();
00060   QVBoxLayout *layout = new QVBoxLayout( frame, 0, spacingHint() );
00061 
00062   d->m_label = new QLabel( label, frame );
00063   layout->addWidget( d->m_label );
00064 
00065   d->m_lineEdit = new KLineEdit( value, frame );
00066   layout->addWidget( d->m_lineEdit );
00067 
00068   d->m_lineEdit->setFocus();
00069   d->m_label->setBuddy( d->m_lineEdit );
00070 
00071   layout->addStretch();
00072 
00073   if ( validator )
00074     d->m_lineEdit->setValidator( validator );
00075 
00076   if ( !mask.isEmpty() )
00077     d->m_lineEdit->setInputMask( mask );
00078 
00079   connect( d->m_lineEdit, SIGNAL( textChanged( const QString & ) ),
00080       SLOT( slotEditTextChanged( const QString & ) ) );
00081   connect( this, SIGNAL( user1Clicked() ), d->m_lineEdit, SLOT( clear() ) );
00082 
00083   slotEditTextChanged( value );
00084   setMinimumWidth( 350 );
00085 }
00086 
00087 KInputDialog::KInputDialog( const QString &caption, const QString &label,
00088     int value, int minValue, int maxValue, int step, int base,
00089     QWidget *parent, const char *name )
00090     : KDialogBase( parent, name, true, caption, Ok|Cancel, Ok, true ),
00091     d( 0L )
00092 {
00093   d = new KInputDialogPrivate();
00094 
00095   QFrame *frame = makeMainWidget();
00096   QVBoxLayout *layout = new QVBoxLayout( frame, 0, spacingHint() );
00097 
00098   d->m_label = new QLabel( label, frame );
00099   layout->addWidget( d->m_label );
00100 
00101   d->m_intSpinBox = new KIntSpinBox( minValue, maxValue, step, value,
00102       base, frame );
00103   layout->addWidget( d->m_intSpinBox );
00104 
00105   layout->addStretch();
00106 
00107   d->m_intSpinBox->setFocus();
00108   setMinimumWidth( 300 );
00109 }
00110 
00111 KInputDialog::KInputDialog( const QString &caption, const QString &label,
00112     double value, double minValue, double maxValue, double step, int decimals,
00113     QWidget *parent, const char *name )
00114     : KDialogBase( parent, name, true, caption, Ok|Cancel, Ok, true ),
00115     d( 0L )
00116 {
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_doubleSpinBox = new KDoubleSpinBox( minValue, maxValue, step, value,
00126       decimals, frame );
00127   layout->addWidget( d->m_doubleSpinBox );
00128 
00129   layout->addStretch();
00130 
00131   d->m_doubleSpinBox->setFocus();
00132   setMinimumWidth( 300 );
00133 }
00134 
00135 KInputDialog::KInputDialog( const QString &caption, const QString &label,
00136     const QStringList &list, int current, bool editable, QWidget *parent,
00137     const char *name )
00138     : KDialogBase( parent, name, true, caption, Ok|Cancel|User1, Ok, true,
00139     KStdGuiItem::clear() ),
00140     d( 0L )
00141 {
00142   d = new KInputDialogPrivate();
00143 
00144   showButton( User1, editable );
00145 
00146   QFrame *frame = makeMainWidget();
00147   QVBoxLayout *layout = new QVBoxLayout( frame, 0, spacingHint() );
00148 
00149   d->m_label = new QLabel( label, frame );
00150   layout->addWidget( d->m_label );
00151 
00152   if ( editable )
00153   {
00154     d->m_comboBox = new KComboBox( editable, frame );
00155     d->m_comboBox->insertStringList( list );
00156     d->m_comboBox->setCurrentItem( current );
00157     layout->addWidget( d->m_comboBox );
00158 
00159     connect( d->m_comboBox, SIGNAL( textChanged( const QString & ) ),
00160       SLOT( slotUpdateButtons( const QString & ) ) );
00161     connect( this, SIGNAL( user1Clicked() ),
00162       d->m_comboBox, SLOT( clearEdit() ) );
00163     slotUpdateButtons( d->m_comboBox->currentText() );
00164     d->m_comboBox->setFocus();
00165   } else {
00166     d->m_listBox = new KListBox( frame );
00167     d->m_listBox->insertStringList( list );
00168     d->m_listBox->setSelected( current, true );
00169     d->m_listBox->ensureCurrentVisible();
00170     layout->addWidget( d->m_listBox );
00171   }
00172 
00173   layout->addStretch();
00174 
00175   setMinimumWidth( 320 );
00176 }
00177 
00178 KInputDialog::KInputDialog( const QString &caption, const QString &label,
00179     const QStringList &list, const QStringList &select, bool multiple,
00180     QWidget *parent, const char *name )
00181     : KDialogBase( parent, name, true, caption, Ok|Cancel, Ok, true ),
00182     d( 0L )
00183 {
00184   d = new KInputDialogPrivate();
00185 
00186   QFrame *frame = makeMainWidget();
00187   QVBoxLayout *layout = new QVBoxLayout( frame, 0, spacingHint() );
00188 
00189   d->m_label = new QLabel( label, frame );
00190   layout->addWidget( d->m_label );
00191 
00192   d->m_listBox = new KListBox( frame );
00193   d->m_listBox->insertStringList( list );
00194   layout->addWidget( d->m_listBox );
00195 
00196   QListBoxItem *item;
00197 
00198   if ( multiple )
00199   {
00200     d->m_listBox->setSelectionMode( QListBox::Extended );
00201 
00202     for ( QStringList::ConstIterator it=select.begin(); it!=select.end(); ++it )
00203     {
00204       item = d->m_listBox->findItem( *it, CaseSensitive|ExactMatch );
00205       if ( item )
00206         d->m_listBox->setSelected( item, true );
00207     }
00208   }
00209   else
00210   {
00211     connect( d->m_listBox, SIGNAL( doubleClicked( QListBoxItem * ) ),
00212       SLOT( slotOk() ) );
00213 
00214     QString text = select.first();
00215     item = d->m_listBox->findItem( text, CaseSensitive|ExactMatch );
00216     if ( item )
00217       d->m_listBox->setSelected( item, true );
00218   }
00219 
00220   d->m_listBox->ensureCurrentVisible();
00221 
00222   layout->addStretch();
00223 
00224   setMinimumWidth( 320 );
00225 }
00226 
00227 KInputDialog::~KInputDialog()
00228 {
00229   delete d;
00230 }
00231 
00232 QString KInputDialog::getText( const QString &caption, const QString &label,
00233     const QString &value, bool *ok, QWidget *parent, const char *name,
00234     QValidator *validator, const QString &mask )
00235 {
00236   KInputDialog *dlg = new KInputDialog( caption, label, value, parent, name,
00237     validator, mask );
00238 
00239   bool _ok = ( dlg->exec() == Accepted );
00240 
00241   if ( ok )
00242     *ok = _ok;
00243 
00244   QString result;
00245   if ( _ok )
00246     result = dlg->lineEdit()->text();
00247 
00248   // A validator may explicitly allow leading and trailing whitespace
00249   if ( !validator )
00250     result = result.stripWhiteSpace();
00251 
00252   delete dlg;
00253   return result;
00254 }
00255 
00256 int KInputDialog::getInteger( const QString &caption, const QString &label,
00257     int value, int minValue, int maxValue, int step, int base, bool *ok,
00258     QWidget *parent, const char *name )
00259 {
00260   KInputDialog *dlg = new KInputDialog( caption, label, value, minValue,
00261     maxValue, step, base, parent, name );
00262 
00263   bool _ok = ( dlg->exec() == Accepted );
00264 
00265   if ( ok )
00266     *ok = _ok;
00267 
00268   int result=0;
00269   if ( _ok )
00270     result = dlg->intSpinBox()->value();
00271 
00272   delete dlg;
00273   return result;
00274 }
00275 
00276 int KInputDialog::getInteger( const QString &caption, const QString &label,
00277     int value, int minValue, int maxValue, int step, bool *ok,
00278     QWidget *parent, const char *name )
00279 {
00280   return getInteger( caption, label, value, minValue, maxValue, step,
00281     10, ok, parent, name );
00282 }
00283 
00284 double KInputDialog::getDouble( const QString &caption, const QString &label,
00285     double value, double minValue, double maxValue, double step, int decimals,
00286     bool *ok, QWidget *parent, const char *name )
00287 {
00288   KInputDialog *dlg = new KInputDialog( caption, label, value, minValue,
00289     maxValue, step, decimals, parent, name );
00290 
00291   bool _ok = ( dlg->exec() == Accepted );
00292 
00293   if ( ok )
00294     *ok = _ok;
00295 
00296   double result=0;
00297   if ( _ok )
00298     result = dlg->doubleSpinBox()->value();
00299 
00300   delete dlg;
00301   return result;
00302 }
00303 
00304 double KInputDialog::getDouble( const QString &caption, const QString &label,
00305     double value, double minValue, double maxValue, int decimals,
00306     bool *ok, QWidget *parent, const char *name )
00307 {
00308   return getDouble( caption, label, value, minValue, maxValue, 0.1, decimals,
00309     ok, parent, name );
00310 }
00311 
00312 QString KInputDialog::getItem( const QString &caption, const QString &label,
00313     const QStringList &list, int current, bool editable, bool *ok,
00314     QWidget *parent, const char *name )
00315 {
00316   KInputDialog *dlg = new KInputDialog( caption, label, list, current,
00317     editable, parent, name );
00318   if ( !editable)
00319   {
00320       connect( dlg->listBox(),  SIGNAL(doubleClicked ( QListBoxItem *)), dlg, SLOT( slotOk()));
00321   }
00322   bool _ok = ( dlg->exec() == Accepted );
00323 
00324   if ( ok )
00325     *ok = _ok;
00326 
00327   QString result;
00328   if ( _ok )
00329     if ( editable )
00330       result = dlg->comboBox()->currentText();
00331     else
00332       result = dlg->listBox()->currentText();
00333 
00334   delete dlg;
00335   return result;
00336 }
00337 
00338 QStringList KInputDialog::getItemList( const QString &caption,
00339     const QString &label, const QStringList &list, const QStringList &select,
00340     bool multiple, bool *ok, QWidget *parent, const char *name )
00341 {
00342   KInputDialog *dlg = new KInputDialog( caption, label, list, select,
00343     multiple, parent, name );
00344 
00345   bool _ok = ( dlg->exec() == Accepted );
00346 
00347   if ( ok )
00348     *ok = _ok;
00349 
00350   QStringList result;
00351   if ( _ok )
00352   {
00353     for ( unsigned int i=0; i<list.count(); ++i )
00354       if ( dlg->listBox()->isSelected( i ) )
00355         result.append( dlg->listBox()->text( i ) );
00356   }
00357 
00358   delete dlg;
00359   return result;
00360 }
00361 
00362 void KInputDialog::slotEditTextChanged( const QString &text )
00363 {
00364   bool on;
00365   if ( lineEdit()->validator() ) {
00366     QString str = lineEdit()->text();
00367     int index = lineEdit()->cursorPosition();
00368     on = ( lineEdit()->validator()->validate( str, index )
00369       == QValidator::Acceptable );
00370   } else {
00371     on = !text.stripWhiteSpace().isEmpty();
00372   }
00373 
00374   enableButton( Ok, on );
00375   enableButton( User1, !text.isEmpty() );
00376 }
00377 
00378 void KInputDialog::slotUpdateButtons( const QString &text )
00379 {
00380   enableButton( Ok, !text.isEmpty() );
00381   enableButton( User1, !text.isEmpty() );
00382 }
00383 
00384 KLineEdit *KInputDialog::lineEdit() const
00385 {
00386   return d->m_lineEdit;
00387 }
00388 
00389 KIntSpinBox *KInputDialog::intSpinBox() const
00390 {
00391   return d->m_intSpinBox;
00392 }
00393 
00394 KDoubleSpinBox *KInputDialog::doubleSpinBox() const
00395 {
00396   return d->m_doubleSpinBox;
00397 }
00398 
00399 KComboBox *KInputDialog::comboBox() const
00400 {
00401   return d->m_comboBox;
00402 }
00403 
00404 KListBox *KInputDialog::listBox() const
00405 {
00406   return d->m_listBox;
00407 }
00408 
00409 #include "kinputdialog.moc"
00410 
00411 /* vim: set ai et sw=2 ts=2
00412 */
KDE Logo
This file is part of the documentation for kdeui Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sun May 16 22:02:03 2004 by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2003