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   }
00202 
00203   layout->addStretch();
00204 
00205   setMinimumWidth( 320 );
00206 }
00207 
00208 KInputDialog::KInputDialog( const QString &caption, const QString &label,
00209     const QStringList &list, const QStringList &select, bool multiple,
00210     QWidget *parent, const char *name )
00211     : KDialogBase( parent, name, true, caption, Ok|Cancel, Ok, true ),
00212     d( 0L )
00213 {
00214   d = new KInputDialogPrivate();
00215 
00216   QFrame *frame = makeMainWidget();
00217   QVBoxLayout *layout = new QVBoxLayout( frame, 0, spacingHint() );
00218 
00219   d->m_label = new QLabel( label, frame );
00220   layout->addWidget( d->m_label );
00221 
00222   d->m_listBox = new KListBox( frame );
00223   d->m_listBox->insertStringList( list );
00224   layout->addWidget( d->m_listBox );
00225 
00226   QListBoxItem *item;
00227 
00228   if ( multiple )
00229   {
00230     d->m_listBox->setSelectionMode( QListBox::Extended );
00231 
00232     for ( QStringList::ConstIterator it=select.begin(); it!=select.end(); ++it )
00233     {
00234       item = d->m_listBox->findItem( *it, CaseSensitive|ExactMatch );
00235       if ( item )
00236         d->m_listBox->setSelected( item, true );
00237     }
00238   }
00239   else
00240   {
00241     connect( d->m_listBox, SIGNAL( doubleClicked( QListBoxItem * ) ),
00242       SLOT( slotOk() ) );
00243 
00244     QString 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 
00252   layout->addStretch();
00253 
00254   setMinimumWidth( 320 );
00255 }
00256 
00257 KInputDialog::~KInputDialog()
00258 {
00259   delete d;
00260 }
00261 
00262 QString KInputDialog::getText( const QString &caption, const QString &label,
00263     const QString &value, bool *ok, QWidget *parent, const char *name,
00264     QValidator *validator, const QString &mask )
00265 {
00266   return text( caption, label, value, ok, parent, name, validator, mask,
00267                QString::null );
00268 }
00269 
00270 QString KInputDialog::text( const QString &caption,
00271     const QString &label, const QString &value, bool *ok, QWidget *parent,
00272     const char *name, QValidator *validator, const QString &mask,
00273     const QString &whatsThis )
00274 {
00275   KInputDialog dlg( caption, label, value, parent, name, validator, mask );
00276 
00277   if( !whatsThis.isEmpty() )
00278     QWhatsThis::add( dlg.lineEdit(), whatsThis );
00279 
00280   bool _ok = ( dlg.exec() == Accepted );
00281 
00282   if ( ok )
00283     *ok = _ok;
00284 
00285   QString result;
00286   if ( _ok )
00287     result = dlg.lineEdit()->text();
00288 
00289   // A validator may explicitly allow leading and trailing whitespace
00290   if ( !validator )
00291     result = result.stripWhiteSpace();
00292 
00293   return result;
00294 }
00295 
00296 QString KInputDialog::getMultiLineText( const QString &caption,
00297     const QString &label, const QString &value, bool *ok,
00298     QWidget *parent, const char *name )
00299 {
00300   KInputDialog dlg( caption, label, value, parent, name );
00301 
00302   bool _ok = ( dlg.exec() == Accepted );
00303 
00304   if ( ok )
00305     *ok = _ok;
00306 
00307   QString result;
00308   if ( _ok )
00309     result = dlg.textEdit()->text();
00310 
00311   return result;
00312 }
00313 
00314 int KInputDialog::getInteger( const QString &caption, const QString &label,
00315     int value, int minValue, int maxValue, int step, int base, bool *ok,
00316     QWidget *parent, const char *name )
00317 {
00318   KInputDialog dlg( caption, label, value, minValue,
00319     maxValue, step, base, parent, name );
00320 
00321   bool _ok = ( dlg.exec() == Accepted );
00322 
00323   if ( ok )
00324     *ok = _ok;
00325 
00326   int result=0;
00327   if ( _ok )
00328     result = dlg.intSpinBox()->value();
00329 
00330   return result;
00331 }
00332 
00333 int KInputDialog::getInteger( const QString &caption, const QString &label,
00334     int value, int minValue, int maxValue, int step, bool *ok,
00335     QWidget *parent, const char *name )
00336 {
00337   return getInteger( caption, label, value, minValue, maxValue, step,
00338     10, ok, parent, name );
00339 }
00340 
00341 double KInputDialog::getDouble( const QString &caption, const QString &label,
00342     double value, double minValue, double maxValue, double step, int decimals,
00343     bool *ok, QWidget *parent, const char *name )
00344 {
00345   KInputDialog dlg( caption, label, value, minValue,
00346     maxValue, step, decimals, parent, name );
00347 
00348   bool _ok = ( dlg.exec() == Accepted );
00349 
00350   if ( ok )
00351     *ok = _ok;
00352 
00353   double result=0;
00354   if ( _ok )
00355     result = dlg.doubleSpinBox()->value();
00356 
00357   return result;
00358 }
00359 
00360 double KInputDialog::getDouble( const QString &caption, const QString &label,
00361     double value, double minValue, double maxValue, int decimals,
00362     bool *ok, QWidget *parent, const char *name )
00363 {
00364   return getDouble( caption, label, value, minValue, maxValue, 0.1, decimals,
00365     ok, parent, name );
00366 }
00367 
00368 QString KInputDialog::getItem( const QString &caption, const QString &label,
00369     const QStringList &list, int current, bool editable, bool *ok,
00370     QWidget *parent, const char *name )
00371 {
00372   KInputDialog dlg( caption, label, list, current,
00373     editable, parent, name );
00374   if ( !editable)
00375   {
00376       connect( dlg.listBox(),  SIGNAL(doubleClicked ( QListBoxItem *)), &dlg, SLOT( slotOk()));
00377   }
00378   bool _ok = ( dlg.exec() == Accepted );
00379 
00380   if ( ok )
00381     *ok = _ok;
00382 
00383   QString result;
00384   if ( _ok )
00385     if ( editable )
00386       result = dlg.comboBox()->currentText();
00387     else
00388       result = dlg.listBox()->currentText();
00389 
00390   return result;
00391 }
00392 
00393 QStringList KInputDialog::getItemList( const QString &caption,
00394     const QString &label, const QStringList &list, const QStringList &select,
00395     bool multiple, bool *ok, QWidget *parent, const char *name )
00396 {
00397   KInputDialog dlg( caption, label, list, select,
00398     multiple, parent, name );
00399 
00400   bool _ok = ( dlg.exec() == Accepted );
00401 
00402   if ( ok )
00403     *ok = _ok;
00404 
00405   QStringList result;
00406   if ( _ok )
00407   {
00408     for ( unsigned int i=0; i<list.count(); ++i )
00409       if ( dlg.listBox()->isSelected( i ) )
00410         result.append( dlg.listBox()->text( i ) );
00411   }
00412 
00413   return result;
00414 }
00415 
00416 void KInputDialog::slotEditTextChanged( const QString &text )
00417 {
00418   bool on;
00419   if ( lineEdit()->validator() ) {
00420     QString str = lineEdit()->text();
00421     int index = lineEdit()->cursorPosition();
00422     on = ( lineEdit()->validator()->validate( str, index )
00423       == QValidator::Acceptable );
00424   } else {
00425     on = !text.stripWhiteSpace().isEmpty();
00426   }
00427 
00428   enableButton( Ok, on );
00429   enableButton( User1, !text.isEmpty() );
00430 }
00431 
00432 void KInputDialog::slotUpdateButtons( const QString &text )
00433 {
00434   enableButton( Ok, !text.isEmpty() );
00435   enableButton( User1, !text.isEmpty() );
00436 }
00437 
00438 KLineEdit *KInputDialog::lineEdit() const
00439 {
00440   return d->m_lineEdit;
00441 }
00442 
00443 KIntSpinBox *KInputDialog::intSpinBox() const
00444 {
00445   return d->m_intSpinBox;
00446 }
00447 
00448 KDoubleSpinBox *KInputDialog::doubleSpinBox() const
00449 {
00450   return d->m_doubleSpinBox;
00451 }
00452 
00453 KComboBox *KInputDialog::comboBox() const
00454 {
00455   return d->m_comboBox;
00456 }
00457 
00458 KListBox *KInputDialog::listBox() const
00459 {
00460   return d->m_listBox;
00461 }
00462 
00463 KTextEdit *KInputDialog::textEdit() const
00464 {
00465   return d->m_textEdit;
00466 }
00467 
00468 #include "kinputdialog.moc"
00469 
00470 /* vim: set ai et sw=2 ts=2
00471 */
KDE Logo
This file is part of the documentation for kdeui Library Version 3.3.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Jan 22 16:45:11 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003