klineeditdlg.cpp
/* This file is part of the KDE libraries
Copyright (C) 1999 Preston Brown <pbrown@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include <config.h>
#include <qvalidator.h>
#include <qpushbutton.h>
#include <qlineedit.h>
#include <qlabel.h>
#include <qlayout.h>
#undef Unsorted // Required for --enable-final (qdir.h)
#include <qfiledialog.h>
#include <kbuttonbox.h>
#include <klocale.h>
#include <kapplication.h>
#include <klineedit.h>
#include <kstdguiitem.h>
#include "klineeditdlg.h"
KLineEditDlg::KLineEditDlg( const QString&_text, const QString& _value,
QWidget *parent )
: KDialogBase( Plain, QString::null, Ok|Cancel|User1, Ok, parent, 0L, true,
true, KStdGuiItem::clear() )
{
QVBoxLayout *topLayout = new QVBoxLayout( plainPage(), 0, spacingHint() );
QLabel *label = new QLabel(_text, plainPage() );
topLayout->addWidget( label, 1 );
edit = new KLineEdit( plainPage(), 0L );
edit->setMinimumWidth(edit->sizeHint().width() * 3);
label->setBuddy(edit); // please "scheck" style
// connect( edit, SIGNAL(returnPressed()), SLOT(accept()) );
connect( edit, SIGNAL(textChanged(const QString&)),
SLOT(slotTextChanged(const QString&)) );
topLayout->addWidget( edit, 1 );
connect( this, SIGNAL(user1Clicked()), this, SLOT(slotClear()) );
edit->setText( _value );
if ( _value.isEmpty() )
{
enableButtonOK( false );
enableButton(KDialogBase::User1, false);
}
edit->setSelection(0, edit->text().length());
edit->setFocus();
}
#if 0
KLineEditDlg::KLineEditDlg( const QString&_text, const QString& _value,
QWidget *parent, bool _file_mode )
: QDialog( parent, 0L, true )
{
QGridLayout *layout = new QGridLayout(this, 4, 3, 10);
QLabel *label = new QLabel(_text, this);
layout->addWidget(label, 0, 0, AlignLeft);
edit = new KLineEdit( this, 0L );
edit->setMinimumWidth(edit->sizeHint().width() * 3);
connect( edit, SIGNAL(returnPressed()), SLOT(accept()) );
if ( _file_mode ) {
completion = new KURLCompletion();
edit->setCompletionObject( completion );
edit->setAutoDeleteCompletionObject( true );
} else
completion = 0L;
layout->addMultiCellWidget(edit, 1, 1, 0, _file_mode ? 1 : 2);
layout->setColStretch(1, 1);
if (_file_mode) {
QPushButton *browse = new QPushButton(i18n("&Browse..."), this);
layout->addWidget(browse, 1, 2, AlignCenter);
connect(browse, SIGNAL(clicked()),
SLOT(slotBrowse()));
}
QFrame *hLine = new QFrame(this);
hLine->setFrameStyle(QFrame::Sunken|QFrame::HLine);
layout->addMultiCellWidget(hLine, 2, 2, 0, 2);
KButtonBox *bBox = new KButtonBox(this);
layout->addMultiCellWidget(bBox, 3, 3, 0, 2);
QPushButton *ok = bBox->addButton(KStdGuiItem::ok());
ok->setDefault(true);
connect( ok, SIGNAL(clicked()), SLOT(accept()));
bBox->addStretch(1);
QPushButton *clear = bBox->addButton(KStdGuiItem::clear());
connect( clear, SIGNAL(clicked()), SLOT(slotClear()));
bBox->addStretch(1);
QPushButton *cancel = bBox->addButton(KStdGuiItem::cancel());
connect( cancel, SIGNAL(clicked()), SLOT(reject()));
bBox->layout();
layout->activate();
edit->setText( _value );
edit->setSelection(0, edit->text().length());
edit->setFocus();
}
#endif
KLineEditDlg::~KLineEditDlg()
{
}
void KLineEditDlg::slotClear()
{
edit->setText(QString::null);
}
void KLineEditDlg::slotTextChanged(const QString &text)
{
bool on;
if ( edit->validator() ) {
QString str = edit->text();
int index = edit->cursorPosition();
on = ( edit->validator()->validate( str, index )
== QValidator::Acceptable );
} else {
on = !text.isEmpty();
}
enableButtonOK( on );
enableButton(KDialogBase::User1, text.length());
}
QString KLineEditDlg::text() const
{
return edit->text();
}
QString KLineEditDlg::getText(const QString &_text, const QString& _value,
bool *ok, QWidget *parent, QValidator *_validator )
{
KLineEditDlg dlg(_text, _value, parent );
dlg.lineEdit()->setValidator( _validator );
dlg.slotTextChanged( _value ); // trigger validation
bool ok_ = dlg.exec() == QDialog::Accepted;
if ( ok )
*ok = ok_;
if ( ok_ )
return dlg.text();
return QString::null;
}
QString KLineEditDlg::getText(const QString &_caption, const QString &_text,
const QString& _value,
bool *ok, QWidget *parent, QValidator *_validator )
{
KLineEditDlg dlg( _text, _value, parent );
dlg.setCaption( _caption );
dlg.lineEdit()->setValidator( _validator );
dlg.slotTextChanged( _value ); // trigger validation
bool ok_ = dlg.exec() == QDialog::Accepted;
if ( ok )
*ok = ok_;
if ( ok_ )
return dlg.text();
return QString::null;
}
void KLineEditDlg::virtual_hook( int id, void* data )
{ KDialogBase::virtual_hook( id, data ); }
#include "klineeditdlg.moc"
KLineEditDlg::slotClear
void slotClear()
Clears the edit widget.
Definition: klineeditdlg.cpp:134
KStdAccel::completion
const KShortcut & completion()
KDialogBase::enableButtonOK
void enableButtonOK(bool state)
Enable or disable (gray out) the OK button.
Definition: kdialogbase.cpp:848
KLineEdit::setCompletionObject
virtual void setCompletionObject(KCompletion *, bool hsig=true)
Reimplemented for internal reasons, the API is not affected.
Definition: klineedit.cpp:1272
KDialogBase::enableButton
void enableButton(ButtonCode id, bool state)
Enable or disable (gray out) a general action button.
Definition: kdialogbase.cpp:838
klocale.h
KDialogBase::cancel
void cancel()
Force closing the dialog, setting its result code to the one Esc would set.
Definition: kdialogbase.cpp:1632
KDialog::setCaption
virtual void setCaption(const QString &caption)
Make a KDE compliant caption.
Definition: kdialog.cpp:115
KDialogBase
A dialog base class with standard buttons and predefined layouts.
Definition: kdialogbase.h:191
KButtonBox
Container widget for buttons.
Definition: kbuttonbox.h:42
KLineEditDlg::edit
KLineEdit * edit
The line edit widget.
Definition: klineeditdlg.h:117
KButtonBox::layout
void layout()
This function must be called once after all buttons have been inserted.
Definition: kbuttonbox.cpp:163
KDialogBase::User1
Show User defined button 1.
Definition: kdialogbase.h:206
KDialog::spacingHint
static int spacingHint()
Return the number of pixels you shall use between widgets inside a dialog according to the KDE standa...
Definition: kdialog.cpp:103
KCompletionBase::setAutoDeleteCompletionObject
void setAutoDeleteCompletionObject(bool autoDelete)
KStdAction::clear
KAction * clear(const QObject *recvr, const char *slot, KActionCollection *parent, const char *name)
Clear the content of the focus widget.
Definition: kstdaction.cpp:176
KLineEditDlg::lineEdit
KLineEdit * lineEdit() const
Definition: klineeditdlg.h:69
KLineEditDlg::KLineEditDlg
KLineEditDlg(const QString &_text, const QString &_value, QWidget *parent) KDE_DEPRECATED
Create a dialog that asks for a single line of text.
Definition: klineeditdlg.cpp:37
KButtonBox::addButton
QPushButton * addButton(const QString &text, bool noexpand=false)
Add a new QPushButton.
Definition: kbuttonbox.cpp:98
KDialogBase::user1Clicked
void user1Clicked()
The User1 button was pressed.
KLineEdit
An enhanced QLineEdit widget for inputting text.
Definition: klineedit.h:145
KLineEditDlg
Definition: klineeditdlg.h:38
KLineEditDlg::getText
static QString getText(const QString &text, const QString &value, bool *ok, QWidget *parent, QValidator *validator=0) KDE_DEPRECATED
Static convenience function to get a textual input from the user.
Definition: klineeditdlg.cpp:159
KLineEditDlg::text
QString text() const
Definition: klineeditdlg.cpp:154
KStdGuiItem
Provides a set of standardized KGuiItems.
Definition: kstdguiitem.h:37
KButtonBox::addStretch
void addStretch(int scale=1)
Add a stretch to the buttonbox.
Definition: kbuttonbox.cpp:154
KLineEdit::setText
virtual void setText(const QString &)
Re-implemented to enable text squeezing.
Definition: klineedit.cpp:310
KLineEditDlg::slotTextChanged
void slotTextChanged(const QString &)
Enables and disables the OK button depending on the state returned by the lineedit's QValidator...
Definition: klineeditdlg.cpp:139
KDialogBase::plainPage
QFrame * plainPage()
Retrieve the empty page when the predefined layout is used in Plain mode.
Definition: kdialogbase.cpp:420

KDE Home | KDE Accessibility Home | Description of Access Keys