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 <tqvalidator.h> 00022 #include <tqpushbutton.h> 00023 #include <tqlineedit.h> 00024 #include <tqlabel.h> 00025 #include <tqlayout.h> 00026 #undef Unsorted // Required for --enable-final (tqdir.h) 00027 #include <tqfiledialog.h> 00028 00029 #include <kbuttonbox.h> 00030 #include <tdelocale.h> 00031 #include <tdeapplication.h> 00032 #include <klineedit.h> 00033 #include <kstdguiitem.h> 00034 00035 #include "klineeditdlg.h" 00036 00037 KLineEditDlg::KLineEditDlg( const TQString&_text, const TQString& _value, 00038 TQWidget *parent ) 00039 : KDialogBase( Plain, TQString::null, Ok|Cancel|User1, Ok, parent, 0L, true, 00040 true, KStdGuiItem::clear() ) 00041 { 00042 TQVBoxLayout *topLayout = new TQVBoxLayout( plainPage(), 0, spacingHint() ); 00043 TQLabel *label = new TQLabel(_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, TQT_SIGNAL(returnPressed()), TQT_SLOT(accept()) ); 00050 connect( edit, TQT_SIGNAL(textChanged(const TQString&)), 00051 TQT_SLOT(slotTextChanged(const TQString&)) ); 00052 topLayout->addWidget( edit, 1 ); 00053 00054 connect( this, TQT_SIGNAL(user1Clicked()), this, TQT_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 TQString&_text, const TQString& _value, 00069 TQWidget *parent, bool _file_mode ) 00070 : TQDialog( parent, 0L, true ) 00071 { 00072 TQGridLayout *layout = new TQGridLayout(this, 4, 3, 10); 00073 00074 TQLabel *label = new TQLabel(_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, TQT_SIGNAL(returnPressed()), TQT_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 TQPushButton *browse = new TQPushButton(i18n("&Browse..."), this); 00093 layout->addWidget(browse, 1, 2, AlignCenter); 00094 connect(browse, TQT_SIGNAL(clicked()), 00095 TQT_SLOT(slotBrowse())); 00096 } 00097 00098 TQFrame *hLine = new TQFrame(this); 00099 hLine->setFrameStyle(TQFrame::Sunken|TQFrame::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 TQPushButton *ok = bBox->addButton(KStdGuiItem::ok()); 00106 ok->setDefault(true); 00107 connect( ok, TQT_SIGNAL(clicked()), TQT_SLOT(accept())); 00108 00109 bBox->addStretch(1); 00110 00111 TQPushButton *clear = bBox->addButton(KStdGuiItem::clear()); 00112 connect( clear, TQT_SIGNAL(clicked()), TQT_SLOT(slotClear())); 00113 00114 bBox->addStretch(1); 00115 00116 TQPushButton *cancel = bBox->addButton(KStdGuiItem::cancel()); 00117 connect( cancel, TQT_SIGNAL(clicked()), TQT_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(TQString::null); 00137 } 00138 00139 void KLineEditDlg::slotTextChanged(const TQString &text) 00140 { 00141 bool on; 00142 if ( edit->validator() ) { 00143 TQString str = edit->text(); 00144 int index = edit->cursorPosition(); 00145 on = ( edit->validator()->validate( str, index ) 00146 == TQValidator::Acceptable ); 00147 } else { 00148 on = !text.isEmpty(); 00149 } 00150 enableButtonOK( on ); 00151 enableButton(KDialogBase::User1, text.length()); 00152 } 00153 00154 TQString KLineEditDlg::text() const 00155 { 00156 return edit->text(); 00157 } 00158 00159 TQString KLineEditDlg::getText(const TQString &_text, const TQString& _value, 00160 bool *ok, TQWidget *parent, TQValidator *_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() == TQDialog::Accepted; 00167 if ( ok ) 00168 *ok = ok_; 00169 if ( ok_ ) 00170 return dlg.text(); 00171 return TQString::null; 00172 } 00173 00174 TQString KLineEditDlg::getText(const TQString &_caption, const TQString &_text, 00175 const TQString& _value, 00176 bool *ok, TQWidget *parent, TQValidator *_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() == TQDialog::Accepted; 00184 if ( ok ) 00185 *ok = ok_; 00186 if ( ok_ ) 00187 return dlg.text(); 00188 return TQString::null; 00189 } 00190 00191 void KLineEditDlg::virtual_hook( int id, void* data ) 00192 { KDialogBase::virtual_hook( id, data ); } 00193 00194 #include "klineeditdlg.moc"