krestrictedline.cpp

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 <qkeycode.h>
00025 
00026 #include "krestrictedline.h"
00027 
00028 KRestrictedLine::KRestrictedLine( QWidget *parent,
00029                   const char *name,
00030                   const QString& valid )
00031   : KLineEdit( parent, name )
00032 {
00033     qsValidChars = valid;
00034 }
00035 
00036 KRestrictedLine::~KRestrictedLine()
00037 {
00038   ;
00039 }
00040 
00041 
00042 void KRestrictedLine::keyPressEvent( QKeyEvent *e )
00043 {
00044   // let QLineEdit process "special" keys and return/enter
00045   // so that we still can use the default key binding
00046   if (e->key() == Key_Enter || e->key() == Key_Return || e->key() == Key_Delete || e->ascii() < 32)
00047     {
00048       QLineEdit::keyPressEvent(e);
00049       return;
00050     }
00051 
00052   // do we have a list of valid chars &&
00053   // is the pressed key in the list of valid chars?
00054   if (!qsValidChars.isEmpty() && !qsValidChars.contains(e->ascii()))
00055     {
00056       // invalid char, emit signal and return
00057       emit (invalidChar(e->key()));
00058       return;
00059     }
00060   else
00061     // valid char: let QLineEdit process this key as usual
00062     QLineEdit::keyPressEvent(e);
00063 
00064   return;
00065 }
00066 
00067 
00068 void KRestrictedLine::setValidChars( const QString& valid)
00069 {
00070   qsValidChars = valid;
00071 }
00072 
00073 QString KRestrictedLine::validChars() const
00074 {
00075   return qsValidChars;
00076 }
00077 
00078 void KRestrictedLine::virtual_hook( int id, void* data )
00079 { KLineEdit::virtual_hook( id, data ); }
00080 
00081 #include "krestrictedline.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys