klineeditdlg.cpp
00001 /* This file is part of the KDE libraries 00002 Copyright (C) 1999 Preston Brown <pbrown@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 #include <config.h> 00020 00021 #include <qvalidator.h> 00022 #include <qpushbutton.h> 00023 #include <qlineedit.h> 00024 #include <qlabel.h> 00025 #include <qlayout.h> 00026 #undef Unsorted // Required for --enable-final (qdir.h) 00027 #include <qfiledialog.h> 00028 00029 #include <kbuttonbox.h> 00030 #include <klocale.h> 00031 #include <kapplication.h> 00032 #include <klineedit.h> 00033 #include <kstdguiitem.h> 00034 00035 #include "klineeditdlg.h" 00036 00037 KLineEditDlg::KLineEditDlg( const QString&_text, const QString& _value, 00038 QWidget *parent ) 00039 : KDialogBase( Plain, QString::null, Ok|Cancel|User1, Ok, parent, 0L, true, 00040 true, KStdGuiItem::clear() ) 00041 { 00042 QVBoxLayout *topLayout = new QVBoxLayout( plainPage(), 0, spacingHint() ); 00043 QLabel *label = new QLabel(_text, plainPage() ); 00044 topLayout->addWidget( label, 1 ); 00045 00046 edit = new KLineEdit( plainPage(), 0L ); 00047 edit->setMinimumWidth(edit->sizeHint().width() * 3); 00048 label->setBuddy(edit); // please "scheck" style 00049 // connect( edit, SIGNAL(returnPressed()), SLOT(accept()) ); 00050 connect( edit, SIGNAL(textChanged(const QString&)), 00051 SLOT(slotTextChanged(const QString&)) ); 00052 topLayout->addWidget( edit, 1 ); 00053 00054 connect( this, SIGNAL(user1Clicked()), this, SLOT(slotClear()) ); 00055 edit->setText( _value ); 00056 if ( _value.isEmpty() ) 00057 { 00058 enableButtonOK( false ); 00059 enableButton(KDialogBase::User1, false); 00060 } 00061 edit->setSelection(0, edit->text().length()); 00062 edit->setFocus(); 00063 } 00064 00065 00066 00067 #if 0 00068 KLineEditDlg::KLineEditDlg( const QString&_text, const QString& _value, 00069 QWidget *parent, bool _file_mode ) 00070 : QDialog( parent, 0L, true ) 00071 { 00072 QGridLayout *layout = new QGridLayout(this, 4, 3, 10); 00073 00074 QLabel *label = new QLabel(_text, this); 00075 layout->addWidget(label, 0, 0, AlignLeft); 00076 00077 edit = new KLineEdit( this, 0L ); 00078 edit->setMinimumWidth(edit->sizeHint().width() * 3); 00079 connect( edit, SIGNAL(returnPressed()), SLOT(accept()) ); 00080 00081 if ( _file_mode ) { 00082 completion = new KURLCompletion(); 00083 edit->setCompletionObject( completion ); 00084 edit->setAutoDeleteCompletionObject( true ); 00085 } else 00086 completion = 0L; 00087 00088 layout->addMultiCellWidget(edit, 1, 1, 0, _file_mode ? 1 : 2); 00089 layout->setColStretch(1, 1); 00090 00091 if (_file_mode) { 00092 QPushButton *browse = new QPushButton(i18n("&Browse..."), this); 00093 layout->addWidget(browse, 1, 2, AlignCenter); 00094 connect(browse, SIGNAL(clicked()), 00095 SLOT(slotBrowse())); 00096 } 00097 00098 QFrame *hLine = new QFrame(this); 00099 hLine->setFrameStyle(QFrame::Sunken|QFrame::HLine); 00100 layout->addMultiCellWidget(hLine, 2, 2, 0, 2); 00101 00102 KButtonBox *bBox = new KButtonBox(this); 00103 layout->addMultiCellWidget(bBox, 3, 3, 0, 2); 00104 00105 QPushButton *ok = bBox->addButton(KStdGuiItem::ok()); 00106 ok->setDefault(true); 00107 connect( ok, SIGNAL(clicked()), SLOT(accept())); 00108 00109 bBox->addStretch(1); 00110 00111 QPushButton *clear = bBox->addButton(KStdGuiItem::clear()); 00112 connect( clear, SIGNAL(clicked()), SLOT(slotClear())); 00113 00114 bBox->addStretch(1); 00115 00116 QPushButton *cancel = bBox->addButton(KStdGuiItem::cancel()); 00117 connect( cancel, SIGNAL(clicked()), SLOT(reject())); 00118 00119 bBox->layout(); 00120 00121 layout->activate(); 00122 00123 edit->setText( _value ); 00124 edit->setSelection(0, edit->text().length()); 00125 edit->setFocus(); 00126 } 00127 #endif 00128 00129 00130 KLineEditDlg::~KLineEditDlg() 00131 { 00132 } 00133 00134 void KLineEditDlg::slotClear() 00135 { 00136 edit->setText(QString::null); 00137 } 00138 00139 void KLineEditDlg::slotTextChanged(const QString &text) 00140 { 00141 bool on; 00142 if ( edit->validator() ) { 00143 QString str = edit->text(); 00144 int index = edit->cursorPosition(); 00145 on = ( edit->validator()->validate( str, index ) 00146 == QValidator::Acceptable ); 00147 } else { 00148 on = !text.isEmpty(); 00149 } 00150 enableButtonOK( on ); 00151 enableButton(KDialogBase::User1, text.length()); 00152 } 00153 00154 QString KLineEditDlg::text() const 00155 { 00156 return edit->text(); 00157 } 00158 00159 QString KLineEditDlg::getText(const QString &_text, const QString& _value, 00160 bool *ok, QWidget *parent, QValidator *_validator ) 00161 { 00162 KLineEditDlg dlg(_text, _value, parent ); 00163 dlg.lineEdit()->setValidator( _validator ); 00164 dlg.slotTextChanged( _value ); // trigger validation 00165 00166 bool ok_ = dlg.exec() == QDialog::Accepted; 00167 if ( ok ) 00168 *ok = ok_; 00169 if ( ok_ ) 00170 return dlg.text(); 00171 return QString::null; 00172 } 00173 00174 QString KLineEditDlg::getText(const QString &_caption, const QString &_text, 00175 const QString& _value, 00176 bool *ok, QWidget *parent, QValidator *_validator ) 00177 { 00178 KLineEditDlg dlg( _text, _value, parent ); 00179 dlg.setCaption( _caption ); 00180 dlg.lineEdit()->setValidator( _validator ); 00181 dlg.slotTextChanged( _value ); // trigger validation 00182 00183 bool ok_ = dlg.exec() == QDialog::Accepted; 00184 if ( ok ) 00185 *ok = ok_; 00186 if ( ok_ ) 00187 return dlg.text(); 00188 return QString::null; 00189 } 00190 00191 void KLineEditDlg::virtual_hook( int id, void* data ) 00192 { KDialogBase::virtual_hook( id, data ); } 00193 00194 #include "klineeditdlg.moc"