KDELibs4Support

krestrictedline.cpp
1 /*
2  *
3  *
4  * Implementation of KRestrictedLine
5  *
6  * Copyright (C) 1997 Michael Wiedmann, <[email protected]>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  *
22  */
23 
24 #include "krestrictedline.h"
25 #include <kdebug.h>
26 
27 #include <QKeyEvent>
28 
29 class KRestrictedLinePrivate
30 {
31 public:
33  QString qsValidChars;
34 };
35 
37  : KLineEdit(parent)
38  , d(new KRestrictedLinePrivate)
39 {
40 }
41 
43 {
44  delete d;
45 }
46 
47 void KRestrictedLine::keyPressEvent(QKeyEvent *e)
48 {
49  // let KLineEdit process "special" keys and return/enter
50  // so that we still can use the default key binding
51  if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return || e->key() == Qt::Key_Delete
52  || e->key() == Qt::Key_Backspace
56  return;
57  }
58 
59  // do we have a list of valid chars &&
60  // is the pressed key in the list of valid chars?
61  if (!d->qsValidChars.isEmpty() && !d->qsValidChars.contains(e->text())) {
62  // invalid char, emit signal and return
63  emit invalidChar(e->key());
64  } else {
65  // valid char: let KLineEdit process this key as usual
67  }
68 }
69 
70 void KRestrictedLine::inputMethodEvent(QInputMethodEvent *e)
71 {
72  const QString str = e->commitString();
73  if (!d->qsValidChars.isEmpty() && !str.isEmpty()) {
74  bool allOK = true;
75  Q_FOREACH (QChar ch, str) {
76  if (!d->qsValidChars.contains(ch)) {
77  emit invalidChar(ch.unicode());
78  allOK = false;
79  }
80  }
81  // ## we can't remove invalid chars from the string, however.
82  // we really need a validator (with a different signal like invalidChar(QChar)
83  // or invalidCharacters(QString) maybe.
84 
85  if (!allOK) {
86  return;
87  }
88  }
89 
91 }
92 
94 {
95  d->qsValidChars = valid;
96 }
97 
99 {
100  return d->qsValidChars;
101 }
102 
ControlModifier
Qt::KeyboardModifiers modifiers() const const
void keyPressEvent(QKeyEvent *) override
void invalidChar(int)
Emitted when an invalid character was typed.
KRestrictedLine(QWidget *parent=nullptr)
Constructor.
bool isEmpty() const const
QString text() const const
ushort unicode() const const
int key() const const
~KRestrictedLine()
Destructs the restricted line editor.
void setValidChars(const QString &valid)
All characters in the string valid are treated as acceptable characters.
virtual void inputMethodEvent(QInputMethodEvent *e) override
QString validChars() const
const QString & commitString() const const
Key_Enter
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Aug 3 2020 22:58:00 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.