KDEUI
krestrictedline.cpp
Go to the documentation of this file.00001 /* 00002 * 00003 * 00004 * Implementation of KRestrictedLine 00005 * 00006 * Copyright (C) 1997 Michael Wiedmann, <mw@miwie.in-berlin.de> 00007 * 00008 * This library is free software; you can redistribute it and/or 00009 * modify it under the terms of the GNU Library General Public 00010 * License as published by the Free Software Foundation; either 00011 * version 2 of the License, or (at your option) any later version. 00012 * 00013 * This library is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 * Library General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU Library General Public 00019 * License along with this library; if not, write to the Free 00020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00021 * 00022 */ 00023 00024 #include "krestrictedline.h" 00025 00026 #include <QtCore/QCOORD> 00027 #include <QtGui/QKeyEvent> 00028 00029 class KRestrictedLinePrivate 00030 { 00031 public: 00033 QString qsValidChars; 00034 }; 00035 00036 KRestrictedLine::KRestrictedLine( QWidget *parent ) 00037 : KLineEdit( parent ) 00038 , d( new KRestrictedLinePrivate ) 00039 { 00040 } 00041 00042 KRestrictedLine::~KRestrictedLine() 00043 { 00044 delete d; 00045 } 00046 00047 00048 void KRestrictedLine::keyPressEvent( QKeyEvent *e ) 00049 { 00050 // let KLineEdit process "special" keys and return/enter 00051 // so that we still can use the default key binding 00052 if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return || e->key() == Qt::Key_Delete || e->text().length() < 32) 00053 { 00054 KLineEdit::keyPressEvent(e); 00055 return; 00056 } 00057 00058 // do we have a list of valid chars && 00059 // is the pressed key in the list of valid chars? 00060 if (!d->qsValidChars.isEmpty() && !d->qsValidChars.contains(e->text())) 00061 { 00062 // invalid char, emit signal and return 00063 emit (invalidChar(e->key())); 00064 return; 00065 } 00066 else 00067 // valid char: let KLineEdit process this key as usual 00068 KLineEdit::keyPressEvent(e); 00069 00070 return; 00071 } 00072 00073 00074 void KRestrictedLine::setValidChars( const QString& valid) 00075 { 00076 d->qsValidChars = valid; 00077 } 00078 00079 QString KRestrictedLine::validChars() const 00080 { 00081 return d->qsValidChars; 00082 } 00083 00084 #include "krestrictedline.moc"