Accept ZWNJ, ZWJ and PUA characters in input widgets

Private Use Area characters are quite valid input characters when used
in combination with a custom font. Joiners also serve an important language
purpose in semitic writing systems.

Note that there is a hack where we disregard any character produced
using CTRL or CTRL+SHIFT specifically because of German keyboards. I have chosen to
keep the hack in this patch to limit the change (though I have made an exception
for ZWJ and ZWNJ since both are produced using Ctrl+Shift on Windows), but it
will probably have to be reverted.

[ChangeLog][QtWidgets][Input] Accept characters in Private Use Area, as well as
zero-width joiners and zero-width non-joiners in input in QLineEdit and QTextEdit.

Task-number: QTBUG-42074
Task-number: QTBUG-57003
Change-Id: I73f3b7d587a8670de24e902dc52a51f7721dba5a
Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
This commit is contained in:
Eskil Abrahamsen Blomfeldt 2016-12-07 13:12:26 +01:00
parent 87fefbc08e
commit 7896ae052a
10 changed files with 289 additions and 29 deletions

View File

@ -0,0 +1,82 @@
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtGui module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL21$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** As a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qinputcontrol_p.h"
#include <QtGui/qevent.h>
QT_BEGIN_NAMESPACE
QInputControl::QInputControl(Type type, QObject *parent)
: QObject(parent)
, m_type(type)
{
}
QInputControl::QInputControl(Type type, QObjectPrivate &dd, QObject *parent)
: QObject(dd, parent)
, m_type(type)
{
}
bool QInputControl::isAcceptableInput(const QKeyEvent *event) const
{
const QString text = event->text();
if (text.isEmpty())
return false;
const QChar c = text.at(0);
// ZWNJ and ZWJ. This needs to go before the next test, since CTRL+SHIFT is
// used to input it on Windows.
if (c == QChar(0x200C) || c == QChar(0x200D))
return true;
// QTBUG-35734: ignore Ctrl/Ctrl+Shift; accept only AltGr (Alt+Ctrl) on German keyboards
if (event->modifiers() == Qt::ControlModifier
|| event->modifiers() == (Qt::ShiftModifier | Qt::ControlModifier)) {
return false;
}
if (c.isPrint())
return true;
if (c.category() == QChar::Other_PrivateUse)
return true;
if (m_type == TextEdit && c == QLatin1Char('\t'))
return true;
return false;
}
QT_END_NAMESPACE

View File

@ -0,0 +1,76 @@
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtGui module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL21$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** As a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#ifndef QINPUTCONTROL_P_H
#define QINPUTCONTROL_P_H
//
// W A R N I N G
// -------------
//
// This file is not part of the Qt API. It exists purely as an
// implementation detail. This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//
#include <QtCore/qobject.h>
#include <qtguiglobal.h>
QT_BEGIN_NAMESPACE
class QKeyEvent;
class Q_GUI_EXPORT QInputControl : public QObject
{
Q_OBJECT
public:
enum Type {
LineEdit,
TextEdit
};
explicit QInputControl(Type type, QObject *parent = nullptr);
bool isAcceptableInput(const QKeyEvent *event) const;
protected:
explicit QInputControl(Type type, QObjectPrivate &dd, QObject *parent = nullptr);
private:
const Type m_type;
};
QT_END_NAMESPACE
#endif // QINPUTCONTROL_P_H

View File

@ -39,7 +39,8 @@ HEADERS += \
text/qrawfont_p.h \ text/qrawfont_p.h \
text/qglyphrun.h \ text/qglyphrun.h \
text/qglyphrun_p.h \ text/qglyphrun_p.h \
text/qdistancefield_p.h text/qdistancefield_p.h \
text/qinputcontrol_p.h
SOURCES += \ SOURCES += \
text/qfont.cpp \ text/qfont.cpp \
@ -69,7 +70,8 @@ SOURCES += \
text/qstatictext.cpp \ text/qstatictext.cpp \
text/qrawfont.cpp \ text/qrawfont.cpp \
text/qglyphrun.cpp \ text/qglyphrun.cpp \
text/qdistancefield.cpp text/qdistancefield.cpp \
text/qinputcontrol.cpp
SOURCES += \ SOURCES += \
text/qfontengine_qpf2.cpp \ text/qfontengine_qpf2.cpp \

View File

@ -1924,20 +1924,16 @@ void QWidgetLineControl::processKeyEvent(QKeyEvent* event)
unknown = false; unknown = false;
} }
// QTBUG-35734: ignore Ctrl/Ctrl+Shift; accept only AltGr (Alt+Ctrl) on German keyboards if (unknown
if (unknown && !isReadOnly() && !isReadOnly()
&& event->modifiers() != Qt::ControlModifier && isAcceptableInput(event)) {
&& event->modifiers() != (Qt::ControlModifier | Qt::ShiftModifier)) { insert(event->text());
QString t = event->text();
if (!t.isEmpty() && t.at(0).isPrint()) {
insert(t);
#ifndef QT_NO_COMPLETER #ifndef QT_NO_COMPLETER
complete(event->key()); complete(event->key());
#endif #endif
event->accept(); event->accept();
return; return;
} }
}
if (unknown) if (unknown)
event->ignore(); event->ignore();

View File

@ -64,6 +64,7 @@
#include "QtCore/qpoint.h" #include "QtCore/qpoint.h"
#include "QtWidgets/qcompleter.h" #include "QtWidgets/qcompleter.h"
#include "QtCore/qthread.h" #include "QtCore/qthread.h"
#include "QtGui/private/qinputcontrol_p.h"
#include "qplatformdefs.h" #include "qplatformdefs.h"
@ -76,13 +77,14 @@
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
class Q_WIDGETS_EXPORT QWidgetLineControl : public QObject class Q_WIDGETS_EXPORT QWidgetLineControl : public QInputControl
{ {
Q_OBJECT Q_OBJECT
public: public:
QWidgetLineControl(const QString &txt = QString()) QWidgetLineControl(const QString &txt = QString())
: m_cursor(0), m_preeditCursor(0), m_cursorWidth(0), m_layoutDirection(Qt::LayoutDirectionAuto), : QInputControl(LineEdit)
, m_cursor(0), m_preeditCursor(0), m_cursorWidth(0), m_layoutDirection(Qt::LayoutDirectionAuto),
m_hideCursor(false), m_separator(0), m_readOnly(0), m_hideCursor(false), m_separator(0), m_readOnly(0),
m_dragEnabled(0), m_echoMode(0), m_textDirty(0), m_selDirty(0), m_dragEnabled(0), m_echoMode(0), m_textDirty(0), m_selDirty(0),
m_validInput(1), m_blinkStatus(0), m_blinkEnabled(false), m_blinkTimer(0), m_deleteAllTimer(0), m_validInput(1), m_blinkStatus(0), m_blinkEnabled(false), m_blinkTimer(0), m_deleteAllTimer(0),

View File

@ -848,21 +848,21 @@ void QWidgetTextControl::redo()
} }
QWidgetTextControl::QWidgetTextControl(QObject *parent) QWidgetTextControl::QWidgetTextControl(QObject *parent)
: QObject(*new QWidgetTextControlPrivate, parent) : QInputControl(QInputControl::TextEdit, *new QWidgetTextControlPrivate, parent)
{ {
Q_D(QWidgetTextControl); Q_D(QWidgetTextControl);
d->init(); d->init();
} }
QWidgetTextControl::QWidgetTextControl(const QString &text, QObject *parent) QWidgetTextControl::QWidgetTextControl(const QString &text, QObject *parent)
: QObject(*new QWidgetTextControlPrivate, parent) : QInputControl(QInputControl::TextEdit, *new QWidgetTextControlPrivate, parent)
{ {
Q_D(QWidgetTextControl); Q_D(QWidgetTextControl);
d->init(Qt::RichText, text); d->init(Qt::RichText, text);
} }
QWidgetTextControl::QWidgetTextControl(QTextDocument *doc, QObject *parent) QWidgetTextControl::QWidgetTextControl(QTextDocument *doc, QObject *parent)
: QObject(*new QWidgetTextControlPrivate, parent) : QInputControl(QInputControl::TextEdit, *new QWidgetTextControlPrivate, parent)
{ {
Q_D(QWidgetTextControl); Q_D(QWidgetTextControl);
d->init(Qt::RichText, QString(), doc); d->init(Qt::RichText, QString(), doc);
@ -1361,14 +1361,7 @@ void QWidgetTextControlPrivate::keyPressEvent(QKeyEvent *e)
process: process:
{ {
// QTBUG-35734: ignore Ctrl/Ctrl+Shift; accept only AltGr (Alt+Ctrl) on German keyboards if (q->isAcceptableInput(e)) {
if (e->modifiers() == Qt::ControlModifier
|| e->modifiers() == (Qt::ShiftModifier | Qt::ControlModifier)) {
e->ignore();
return;
}
QString text = e->text();
if (!text.isEmpty() && (text.at(0).isPrint() || text.at(0) == QLatin1Char('\t'))) {
if (overwriteMode if (overwriteMode
// no need to call deleteChar() if we have a selection, insertText // no need to call deleteChar() if we have a selection, insertText
// does it already // does it already
@ -1376,7 +1369,7 @@ process:
&& !cursor.atBlockEnd()) && !cursor.atBlockEnd())
cursor.deleteChar(); cursor.deleteChar();
cursor.insertText(text); cursor.insertText(e->text());
selectionChanged(); selectionChanged();
} else { } else {
e->ignore(); e->ignore();

View File

@ -63,6 +63,7 @@
#include <QtGui/qtextdocumentfragment.h> #include <QtGui/qtextdocumentfragment.h>
#include <QtGui/qclipboard.h> #include <QtGui/qclipboard.h>
#include <QtCore/qmimedata.h> #include <QtCore/qmimedata.h>
#include <QtGui/private/qinputcontrol_p.h>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
@ -75,7 +76,7 @@ class QAbstractScrollArea;
class QEvent; class QEvent;
class QTimerEvent; class QTimerEvent;
class Q_WIDGETS_EXPORT QWidgetTextControl : public QObject class Q_WIDGETS_EXPORT QWidgetTextControl : public QInputControl
{ {
Q_OBJECT Q_OBJECT
Q_DECLARE_PRIVATE(QWidgetTextControl) Q_DECLARE_PRIVATE(QWidgetTextControl)

View File

@ -0,0 +1,7 @@
CONFIG += testcase
TARGET = tst_qinputcontrol
QT = core gui gui-private testlib
SOURCES += \
tst_qinputcontrol.cpp

View File

@ -0,0 +1,100 @@
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QtTest/QtTest>
#include <private/qinputcontrol_p.h>
#include <QtGui/QKeyEvent>
class tst_QInputControl: public QObject
{
Q_OBJECT
private slots:
void isAcceptableInput_data();
void isAcceptableInput();
void tabOnlyAcceptableInputForTextEdit();
};
void tst_QInputControl::isAcceptableInput_data()
{
QTest::addColumn<QString>("text");
QTest::addColumn<Qt::KeyboardModifiers>("modifiers");
QTest::addColumn<bool>("acceptable");
QTest::newRow("empty-string") << QString() << Qt::KeyboardModifiers() << false;
QTest::newRow("zwnj") << QString(QChar(0x200C)) << Qt::KeyboardModifiers() << true;
QTest::newRow("zwnj-with-ctrl") << QString(QChar(0x200C)) << Qt::KeyboardModifiers(Qt::ControlModifier) << true;
QTest::newRow("zwnj-with-ctrl-shift") << QString(QChar(0x200C)) << Qt::KeyboardModifiers(Qt::ControlModifier | Qt::ShiftModifier) << true;
QTest::newRow("zwj") << QString(QChar(0x200D)) << Qt::KeyboardModifiers() << true;
QTest::newRow("zwj-with-ctrl") << QString(QChar(0x200D)) << Qt::KeyboardModifiers(Qt::ControlModifier) << true;
QTest::newRow("zwj-with-ctrl-shift") << QString(QChar(0x200D)) << Qt::KeyboardModifiers(Qt::ControlModifier | Qt::ShiftModifier) << true;
QTest::newRow("printable-latin") << QString(QLatin1Char('a')) << Qt::KeyboardModifiers() << true;
QTest::newRow("printable-latin-with-ctrl") << QString(QLatin1Char('a')) << Qt::KeyboardModifiers(Qt::ControlModifier) << false;
QTest::newRow("printable-latin-with-ctrl-shift") << QString(QLatin1Char('a')) << Qt::KeyboardModifiers(Qt::ControlModifier | Qt::ShiftModifier) << false;
QTest::newRow("printable-hebrew") << QString(QChar(0x2135)) << Qt::KeyboardModifiers() << true;
QTest::newRow("private-use-area") << QString(QChar(0xE832)) << Qt::KeyboardModifiers() << true;
QTest::newRow("multiple-printable") << QStringLiteral("foobar") << Qt::KeyboardModifiers() << true;
}
void tst_QInputControl::isAcceptableInput()
{
QFETCH(QString, text);
QFETCH(Qt::KeyboardModifiers, modifiers);
QFETCH(bool, acceptable);
QKeyEvent keyEvent(QKeyEvent::KeyPress, Qt::Key_unknown, modifiers, text);
{
QInputControl inputControl(QInputControl::TextEdit);
QCOMPARE(inputControl.isAcceptableInput(&keyEvent), acceptable);
}
{
QInputControl inputControl(QInputControl::LineEdit);
QCOMPARE(inputControl.isAcceptableInput(&keyEvent), acceptable);
}
}
void tst_QInputControl::tabOnlyAcceptableInputForTextEdit()
{
QKeyEvent keyEvent(QKeyEvent::KeyPress, Qt::Key_unknown, Qt::KeyboardModifiers(), QLatin1String("\t"));
{
QInputControl inputControl(QInputControl::TextEdit);
QCOMPARE(inputControl.isAcceptableInput(&keyEvent), true);
}
{
QInputControl inputControl(QInputControl::LineEdit);
QCOMPARE(inputControl.isAcceptableInput(&keyEvent), false);
}
}
QTEST_MAIN(tst_QInputControl)
#include "tst_qinputcontrol.moc"

View File

@ -23,7 +23,8 @@ SUBDIRS=\
qtextscriptengine \ qtextscriptengine \
qtexttable \ qtexttable \
qzip \ qzip \
qtextodfwriter qtextodfwriter \
qinputcontrol
win32:SUBDIRS -= qtextpiecetable win32:SUBDIRS -= qtextpiecetable