summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEskil Abrahamsen Blomfeldt <[email protected]>2016-12-07 13:12:26 +0100
committerEskil Abrahamsen Blomfeldt <[email protected]>2016-12-12 11:13:53 +0000
commit7896ae052ad2c0c6ae2ebfc64cc2f525185198a8 (patch)
treea78f830a205ea16690f8d6940a1a6059fea36bf6
parent87fefbc08e80119dba24767bfc9b6ecc64be5fcd (diff)
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 <[email protected]>
-rw-r--r--src/gui/text/qinputcontrol.cpp82
-rw-r--r--src/gui/text/qinputcontrol_p.h76
-rw-r--r--src/gui/text/text.pri6
-rw-r--r--src/widgets/widgets/qwidgetlinecontrol.cpp18
-rw-r--r--src/widgets/widgets/qwidgetlinecontrol_p.h6
-rw-r--r--src/widgets/widgets/qwidgettextcontrol.cpp17
-rw-r--r--src/widgets/widgets/qwidgettextcontrol_p.h3
-rw-r--r--tests/auto/gui/text/qinputcontrol/qinputcontrol.pro7
-rw-r--r--tests/auto/gui/text/qinputcontrol/tst_qinputcontrol.cpp100
-rw-r--r--tests/auto/gui/text/text.pro3
10 files changed, 289 insertions, 29 deletions
diff --git a/src/gui/text/qinputcontrol.cpp b/src/gui/text/qinputcontrol.cpp
new file mode 100644
index 00000000000..c2c198866a2
--- /dev/null
+++ b/src/gui/text/qinputcontrol.cpp
@@ -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
diff --git a/src/gui/text/qinputcontrol_p.h b/src/gui/text/qinputcontrol_p.h
new file mode 100644
index 00000000000..3b46067ba9f
--- /dev/null
+++ b/src/gui/text/qinputcontrol_p.h
@@ -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
diff --git a/src/gui/text/text.pri b/src/gui/text/text.pri
index efd041a5af9..abe20abe024 100644
--- a/src/gui/text/text.pri
+++ b/src/gui/text/text.pri
@@ -39,7 +39,8 @@ HEADERS += \
text/qrawfont_p.h \
text/qglyphrun.h \
text/qglyphrun_p.h \
- text/qdistancefield_p.h
+ text/qdistancefield_p.h \
+ text/qinputcontrol_p.h
SOURCES += \
text/qfont.cpp \
@@ -69,7 +70,8 @@ SOURCES += \
text/qstatictext.cpp \
text/qrawfont.cpp \
text/qglyphrun.cpp \
- text/qdistancefield.cpp
+ text/qdistancefield.cpp \
+ text/qinputcontrol.cpp
SOURCES += \
text/qfontengine_qpf2.cpp \
diff --git a/src/widgets/widgets/qwidgetlinecontrol.cpp b/src/widgets/widgets/qwidgetlinecontrol.cpp
index 9b65dc43d11..a862274a3dc 100644
--- a/src/widgets/widgets/qwidgetlinecontrol.cpp
+++ b/src/widgets/widgets/qwidgetlinecontrol.cpp
@@ -1924,19 +1924,15 @@ void QWidgetLineControl::processKeyEvent(QKeyEvent* event)
unknown = false;
}
- // QTBUG-35734: ignore Ctrl/Ctrl+Shift; accept only AltGr (Alt+Ctrl) on German keyboards
- if (unknown && !isReadOnly()
- && event->modifiers() != Qt::ControlModifier
- && event->modifiers() != (Qt::ControlModifier | Qt::ShiftModifier)) {
- QString t = event->text();
- if (!t.isEmpty() && t.at(0).isPrint()) {
- insert(t);
+ if (unknown
+ && !isReadOnly()
+ && isAcceptableInput(event)) {
+ insert(event->text());
#ifndef QT_NO_COMPLETER
- complete(event->key());
+ complete(event->key());
#endif
- event->accept();
- return;
- }
+ event->accept();
+ return;
}
if (unknown)
diff --git a/src/widgets/widgets/qwidgetlinecontrol_p.h b/src/widgets/widgets/qwidgetlinecontrol_p.h
index d4a4534fb5d..b9340c0aff7 100644
--- a/src/widgets/widgets/qwidgetlinecontrol_p.h
+++ b/src/widgets/widgets/qwidgetlinecontrol_p.h
@@ -64,6 +64,7 @@
#include "QtCore/qpoint.h"
#include "QtWidgets/qcompleter.h"
#include "QtCore/qthread.h"
+#include "QtGui/private/qinputcontrol_p.h"
#include "qplatformdefs.h"
@@ -76,13 +77,14 @@
QT_BEGIN_NAMESPACE
-class Q_WIDGETS_EXPORT QWidgetLineControl : public QObject
+class Q_WIDGETS_EXPORT QWidgetLineControl : public QInputControl
{
Q_OBJECT
public:
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_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),
diff --git a/src/widgets/widgets/qwidgettextcontrol.cpp b/src/widgets/widgets/qwidgettextcontrol.cpp
index 47806a194e3..f5672bd87a1 100644
--- a/src/widgets/widgets/qwidgettextcontrol.cpp
+++ b/src/widgets/widgets/qwidgettextcontrol.cpp
@@ -848,21 +848,21 @@ void QWidgetTextControl::redo()
}
QWidgetTextControl::QWidgetTextControl(QObject *parent)
- : QObject(*new QWidgetTextControlPrivate, parent)
+ : QInputControl(QInputControl::TextEdit, *new QWidgetTextControlPrivate, parent)
{
Q_D(QWidgetTextControl);
d->init();
}
QWidgetTextControl::QWidgetTextControl(const QString &text, QObject *parent)
- : QObject(*new QWidgetTextControlPrivate, parent)
+ : QInputControl(QInputControl::TextEdit, *new QWidgetTextControlPrivate, parent)
{
Q_D(QWidgetTextControl);
d->init(Qt::RichText, text);
}
QWidgetTextControl::QWidgetTextControl(QTextDocument *doc, QObject *parent)
- : QObject(*new QWidgetTextControlPrivate, parent)
+ : QInputControl(QInputControl::TextEdit, *new QWidgetTextControlPrivate, parent)
{
Q_D(QWidgetTextControl);
d->init(Qt::RichText, QString(), doc);
@@ -1361,14 +1361,7 @@ void QWidgetTextControlPrivate::keyPressEvent(QKeyEvent *e)
process:
{
- // QTBUG-35734: ignore Ctrl/Ctrl+Shift; accept only AltGr (Alt+Ctrl) on German keyboards
- 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 (q->isAcceptableInput(e)) {
if (overwriteMode
// no need to call deleteChar() if we have a selection, insertText
// does it already
@@ -1376,7 +1369,7 @@ process:
&& !cursor.atBlockEnd())
cursor.deleteChar();
- cursor.insertText(text);
+ cursor.insertText(e->text());
selectionChanged();
} else {
e->ignore();
diff --git a/src/widgets/widgets/qwidgettextcontrol_p.h b/src/widgets/widgets/qwidgettextcontrol_p.h
index b45f4fff746..f540a3c9ada 100644
--- a/src/widgets/widgets/qwidgettextcontrol_p.h
+++ b/src/widgets/widgets/qwidgettextcontrol_p.h
@@ -63,6 +63,7 @@
#include <QtGui/qtextdocumentfragment.h>
#include <QtGui/qclipboard.h>
#include <QtCore/qmimedata.h>
+#include <QtGui/private/qinputcontrol_p.h>
QT_BEGIN_NAMESPACE
@@ -75,7 +76,7 @@ class QAbstractScrollArea;
class QEvent;
class QTimerEvent;
-class Q_WIDGETS_EXPORT QWidgetTextControl : public QObject
+class Q_WIDGETS_EXPORT QWidgetTextControl : public QInputControl
{
Q_OBJECT
Q_DECLARE_PRIVATE(QWidgetTextControl)
diff --git a/tests/auto/gui/text/qinputcontrol/qinputcontrol.pro b/tests/auto/gui/text/qinputcontrol/qinputcontrol.pro
new file mode 100644
index 00000000000..811c9aab90a
--- /dev/null
+++ b/tests/auto/gui/text/qinputcontrol/qinputcontrol.pro
@@ -0,0 +1,7 @@
+CONFIG += testcase
+TARGET = tst_qinputcontrol
+
+QT = core gui gui-private testlib
+
+SOURCES += \
+ tst_qinputcontrol.cpp
diff --git a/tests/auto/gui/text/qinputcontrol/tst_qinputcontrol.cpp b/tests/auto/gui/text/qinputcontrol/tst_qinputcontrol.cpp
new file mode 100644
index 00000000000..ad5ba6affb3
--- /dev/null
+++ b/tests/auto/gui/text/qinputcontrol/tst_qinputcontrol.cpp
@@ -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"
+
diff --git a/tests/auto/gui/text/text.pro b/tests/auto/gui/text/text.pro
index bb4984767fd..d1a3eda4fc0 100644
--- a/tests/auto/gui/text/text.pro
+++ b/tests/auto/gui/text/text.pro
@@ -23,7 +23,8 @@ SUBDIRS=\
qtextscriptengine \
qtexttable \
qzip \
- qtextodfwriter
+ qtextodfwriter \
+ qinputcontrol
win32:SUBDIRS -= qtextpiecetable