diff options
author | Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@qt.io> | 2016-12-07 13:12:26 +0100 |
---|---|---|
committer | Simon Hausmann <simon.hausmann@qt.io> | 2017-01-04 20:54:30 +0000 |
commit | 551ee69d6ab42a3801ebbbed6c8c547dcbdcc43f (patch) | |
tree | c4022195d92230dd3477e41ac24b3a4803d1994d | |
parent | 0eb522cae723a3d7cf8b55ed237ae501ee0c7132 (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: Ib2c6329e604f3516b8118f027779284adadfc42b
(cherry picked from commit 7896ae052ad2c0c6ae2ebfc64cc2f525185198a8)
Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
-rw-r--r-- | src/gui/text/qinputcontrol.cpp | 82 | ||||
-rw-r--r-- | src/gui/text/qinputcontrol_p.h | 76 | ||||
-rw-r--r-- | src/gui/text/text.pri | 6 | ||||
-rw-r--r-- | src/widgets/widgets/qwidgetlinecontrol.cpp | 18 | ||||
-rw-r--r-- | src/widgets/widgets/qwidgetlinecontrol_p.h | 6 | ||||
-rw-r--r-- | src/widgets/widgets/qwidgettextcontrol.cpp | 17 | ||||
-rw-r--r-- | src/widgets/widgets/qwidgettextcontrol_p.h | 3 | ||||
-rw-r--r-- | tests/auto/gui/text/qinputcontrol/qinputcontrol.pro | 7 | ||||
-rw-r--r-- | tests/auto/gui/text/qinputcontrol/tst_qinputcontrol.cpp | 105 | ||||
-rw-r--r-- | tests/auto/gui/text/text.pro | 1 |
10 files changed, 293 insertions, 28 deletions
diff --git a/src/gui/text/qinputcontrol.cpp b/src/gui/text/qinputcontrol.cpp new file mode 100644 index 0000000000..c2c198866a --- /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 0000000000..c4f53d987f --- /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 <qglobal.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 = Q_NULLPTR); + + bool isAcceptableInput(const QKeyEvent *event) const; + +protected: + explicit QInputControl(Type type, QObjectPrivate &dd, QObject *parent = Q_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 be60ba72cc..3848b20dec 100644 --- a/src/gui/text/text.pri +++ b/src/gui/text/text.pri @@ -43,7 +43,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 \ @@ -76,7 +77,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 daf9f00c46..866002370a 100644 --- a/src/widgets/widgets/qwidgetlinecontrol.cpp +++ b/src/widgets/widgets/qwidgetlinecontrol.cpp @@ -1881,19 +1881,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 3d8df9e3ff..a17cd98ced 100644 --- a/src/widgets/widgets/qwidgetlinecontrol_p.h +++ b/src/widgets/widgets/qwidgetlinecontrol_p.h @@ -58,6 +58,7 @@ #include "QtCore/qpoint.h" #include "QtWidgets/qcompleter.h" #include "QtCore/qthread.h" +#include "QtGui/private/qinputcontrol_p.h" #include "qplatformdefs.h" @@ -70,13 +71,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_blinkPeriod(0), m_blinkTimer(0), m_deleteAllTimer(0), diff --git a/src/widgets/widgets/qwidgettextcontrol.cpp b/src/widgets/widgets/qwidgettextcontrol.cpp index 4199ae537d..b8effff206 100644 --- a/src/widgets/widgets/qwidgettextcontrol.cpp +++ b/src/widgets/widgets/qwidgettextcontrol.cpp @@ -829,21 +829,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); @@ -1342,14 +1342,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 @@ -1357,7 +1350,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 cd5abf4f5d..5bdd3309a7 100644 --- a/src/widgets/widgets/qwidgettextcontrol_p.h +++ b/src/widgets/widgets/qwidgettextcontrol_p.h @@ -56,6 +56,7 @@ #include <QtGui/qtextdocumentfragment.h> #include <QtGui/qclipboard.h> #include <QtCore/qmimedata.h> +#include <QtGui/private/qinputcontrol_p.h> QT_BEGIN_NAMESPACE @@ -68,7 +69,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 0000000000..811c9aab90 --- /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 0000000000..318becf6f7 --- /dev/null +++ b/tests/auto/gui/text/qinputcontrol/tst_qinputcontrol.cpp @@ -0,0 +1,105 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the test suite 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 <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 dc67794a98..6f9e4512e8 100644 --- a/tests/auto/gui/text/text.pro +++ b/tests/auto/gui/text/text.pro @@ -22,6 +22,7 @@ SUBDIRS=\ qtextpiecetable \ qtextscriptengine \ qtexttable \ + qinputcontrol contains(QT_CONFIG, OdfWriter):SUBDIRS += qzip qtextodfwriter |