File indexing completed on 2024-04-21 14:55:58

0001 /*
0002  *
0003  *
0004  * Implementation of KRestrictedLine
0005  *
0006  * Copyright (C) 1997 Michael Wiedmann, <mw@miwie.in-berlin.de>
0007  *
0008  * This library is free software; you can redistribute it and/or
0009  * modify it under the terms of the GNU Library General Public
0010  * License as published by the Free Software Foundation; either
0011  * version 2 of the License, or (at your option) any later version.
0012  *
0013  * This library is distributed in the hope that it will be useful,
0014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0016  * Library General Public License for more details.
0017  *
0018  * You should have received a copy of the GNU Library General Public
0019  * License along with this library; if not, write to the Free
0020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
0021  *
0022  */
0023 
0024 #include "krestrictedline.h"
0025 #include <kdebug.h>
0026 
0027 #include <QKeyEvent>
0028 
0029 class KRestrictedLinePrivate
0030 {
0031 public:
0032     /// QString of valid characters for this line
0033     QString qsValidChars;
0034 };
0035 
0036 KRestrictedLine::KRestrictedLine(QWidget *parent)
0037     : KLineEdit(parent)
0038     , d(new KRestrictedLinePrivate)
0039 {
0040 }
0041 
0042 KRestrictedLine::~KRestrictedLine()
0043 {
0044     delete d;
0045 }
0046 
0047 void KRestrictedLine::keyPressEvent(QKeyEvent *e)
0048 {
0049     // let KLineEdit process "special" keys and return/enter
0050     // so that we still can use the default key binding
0051     if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return || e->key() == Qt::Key_Delete
0052             || e->key() == Qt::Key_Backspace
0053             || (e->modifiers() & (Qt::ControlModifier | Qt::AltModifier
0054                                   | Qt::MetaModifier | Qt::GroupSwitchModifier))) {
0055         KLineEdit::keyPressEvent(e);
0056         return;
0057     }
0058 
0059     // do we have a list of valid chars &&
0060     // is the pressed key in the list of valid chars?
0061     if (!d->qsValidChars.isEmpty() && !d->qsValidChars.contains(e->text())) {
0062         // invalid char, emit signal and return
0063         emit invalidChar(e->key());
0064     } else {
0065         // valid char: let KLineEdit process this key as usual
0066         KLineEdit::keyPressEvent(e);
0067     }
0068 }
0069 
0070 void KRestrictedLine::inputMethodEvent(QInputMethodEvent *e)
0071 {
0072     const QString str = e->commitString();
0073     if (!d->qsValidChars.isEmpty() && !str.isEmpty()) {
0074         bool allOK = true;
0075         Q_FOREACH (QChar ch, str) {
0076             if (!d->qsValidChars.contains(ch)) {
0077                 emit invalidChar(ch.unicode());
0078                 allOK = false;
0079             }
0080         }
0081         // ## we can't remove invalid chars from the string, however.
0082         // we really need a validator (with a different signal like invalidChar(QChar)
0083         // or invalidCharacters(QString) maybe.
0084 
0085         if (!allOK) {
0086             return;
0087         }
0088     }
0089 
0090     KLineEdit::inputMethodEvent(e);
0091 }
0092 
0093 void KRestrictedLine::setValidChars(const QString &valid)
0094 {
0095     d->qsValidChars = valid;
0096 }
0097 
0098 QString KRestrictedLine::validChars() const
0099 {
0100     return d->qsValidChars;
0101 }
0102 
0103 #include "moc_krestrictedline.cpp"