• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KDEUI

  • kdeui
  • widgets
krestrictedline.cpp
Go to the documentation of this file.
1/*
2 *
3 *
4 * Implementation of KRestrictedLine
5 *
6 * Copyright (C) 1997 Michael Wiedmann, <mw@miwie.in-berlin.de>
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 <QtGui/QKeyEvent>
28
29class KRestrictedLinePrivate
30{
31public:
33 QString qsValidChars;
34};
35
36KRestrictedLine::KRestrictedLine( QWidget *parent )
37 : KLineEdit( parent )
38 , d( new KRestrictedLinePrivate )
39{
40}
41
42KRestrictedLine::~KRestrictedLine()
43{
44 delete d;
45}
46
47
48void KRestrictedLine::keyPressEvent( QKeyEvent *e )
49{
50 // let KLineEdit process "special" keys and return/enter
51 // so that we still can use the default key binding
52 if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return || e->key() == Qt::Key_Delete
53 || e->key() == Qt::Key_Backspace
54 || (e->modifiers() & (Qt::ControlModifier | Qt::AltModifier
55 | Qt::MetaModifier | Qt::GroupSwitchModifier)))
56 {
57 KLineEdit::keyPressEvent(e);
58 return;
59 }
60
61 // do we have a list of valid chars &&
62 // is the pressed key in the list of valid chars?
63 if (!d->qsValidChars.isEmpty() && !d->qsValidChars.contains(e->text())) {
64 // invalid char, emit signal and return
65 emit invalidChar(e->key());
66 } else {
67 // valid char: let KLineEdit process this key as usual
68 KLineEdit::keyPressEvent(e);
69 }
70}
71
72void KRestrictedLine::inputMethodEvent(QInputMethodEvent *e)
73{
74 const QString str = e->commitString();
75 if (!d->qsValidChars.isEmpty() && !str.isEmpty()) {
76 bool allOK = true;
77 Q_FOREACH(QChar ch, str) {
78 if (!d->qsValidChars.contains(ch)) {
79 emit invalidChar(ch.unicode());
80 allOK = false;
81 }
82 }
83 // ## we can't remove invalid chars from the string, however.
84 // we really need a validator (with a different signal like invalidChar(QChar)
85 // or invalidCharacters(QString) maybe.
86
87 if (!allOK)
88 return;
89 }
90
91 KLineEdit::inputMethodEvent(e);
92}
93
94void KRestrictedLine::setValidChars( const QString& valid)
95{
96 d->qsValidChars = valid;
97}
98
99QString KRestrictedLine::validChars() const
100{
101 return d->qsValidChars;
102}
103
104#include "krestrictedline.moc"
KLineEdit
An enhanced QLineEdit widget for inputting text.
Definition: klineedit.h:150
KLineEdit::keyPressEvent
virtual void keyPressEvent(QKeyEvent *)
Re-implemented for internal reasons.
Definition: klineedit.cpp:702
KRestrictedLine::keyPressEvent
void keyPressEvent(QKeyEvent *e)
Re-implemented for internal reasons.
Definition: krestrictedline.cpp:48
KRestrictedLine::inputMethodEvent
void inputMethodEvent(QInputMethodEvent *e)
Definition: krestrictedline.cpp:72
KRestrictedLine::validChars
QString validChars
Definition: krestrictedline.h:50
KRestrictedLine::KRestrictedLine
KRestrictedLine(QWidget *parent=0)
Constructor.
Definition: krestrictedline.cpp:36
KRestrictedLine::invalidChar
void invalidChar(int)
Emitted when an invalid character was typed.
KRestrictedLine::setValidChars
void setValidChars(const QString &valid)
All characters in the string valid are treated as acceptable characters.
Definition: krestrictedline.cpp:94
KRestrictedLine::~KRestrictedLine
~KRestrictedLine()
Destructs the restricted line editor.
Definition: krestrictedline.cpp:42
QWidget
kdebug.h
krestrictedline.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal