diff options
author | Elvis Angelaccio <elvis.angelaccio@kde.org> | 2017-10-26 18:22:14 +0200 |
---|---|---|
committer | Elvis Angelaccio <elvis.angelaccio@kde.org> | 2018-02-03 16:34:05 +0000 |
commit | 4772ac90fa84a81292477a4c9a283437fb5d791e (patch) | |
tree | ff3966771c90647ed330511ab73d82b426fcda3e /tests/auto/widgets/widgets/qlineedit | |
parent | 8db29d92df0ef9fbc39d88945d16a16f43edf20e (diff) |
QLineEdit: implement quick text selection by mouse
This is a standard feature in GtkEntry widgets or HTML
<input type="text"> elements. During a normal text selection by mouse
(LeftButton press + mouse move event), it's now possible to quickly
select all the text from the start of the selection to the end
of the line edit by moving the mouse cursor down.
By moving it up instead, all the text up to the start of the line edit
gets selected. If the layout direction is right-to-left, the semantic of
the mouse movement is inverted.
This feature is only enabled if the y() of the mouse move event is
bigger than a fixed threshold, to avoid unexpected selections in the
normal case. This threshold is set by the QPlatformTheme and a value
smaller than zero disables this feature.
The threshold is updated whenever the style or the screen changes.
[ChangeLog][QtWidgets][QLineEdit] Implemented quick text selection by
mouse in QLineEdit.
Change-Id: I4de33c2d11c033ec295de2b2ea81adf786324f4b
Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
Diffstat (limited to 'tests/auto/widgets/widgets/qlineedit')
-rw-r--r-- | tests/auto/widgets/widgets/qlineedit/tst_qlineedit.cpp | 89 |
1 files changed, 88 insertions, 1 deletions
diff --git a/tests/auto/widgets/widgets/qlineedit/tst_qlineedit.cpp b/tests/auto/widgets/widgets/qlineedit/tst_qlineedit.cpp index 68e6f0267e..1513025f16 100644 --- a/tests/auto/widgets/widgets/qlineedit/tst_qlineedit.cpp +++ b/tests/auto/widgets/widgets/qlineedit/tst_qlineedit.cpp @@ -67,6 +67,8 @@ #include "../../../shared/platforminputcontext.h" #include <private/qinputmethod_p.h> +Q_LOGGING_CATEGORY(lcTests, "qt.widgets.tests") + QT_BEGIN_NAMESPACE class QPainter; QT_END_NAMESPACE @@ -300,8 +302,8 @@ private slots: void shortcutOverrideOnReadonlyLineEdit_data(); void shortcutOverrideOnReadonlyLineEdit(); void QTBUG59957_clearButtonLeftmostAction(); - void QTBUG_60319_setInputMaskCheckImSurroundingText(); + void testQuickSelectionWithMouse(); protected slots: void editingFinished(); @@ -4699,5 +4701,90 @@ void tst_QLineEdit::QTBUG_60319_setInputMaskCheckImSurroundingText() QCOMPARE(surroundingText.length(), cursorPosition); } +void tst_QLineEdit::testQuickSelectionWithMouse() +{ + const auto text = QStringLiteral("This is quite a long line of text."); + const auto prefix = QStringLiteral("Th"); + const auto suffix = QStringLiteral("t."); + QVERIFY(text.startsWith(prefix)); + QVERIFY(text.endsWith(suffix)); + + QLineEdit lineEdit; + lineEdit.setText(text); + lineEdit.show(); + + const QPoint center = lineEdit.contentsRect().center(); + + // Normal mouse selection from left to right, y doesn't change. + QTest::mousePress(lineEdit.windowHandle(), Qt::LeftButton, Qt::NoModifier, center); + QTest::mouseMove(lineEdit.windowHandle(), center + QPoint(20, 0)); + qCDebug(lcTests) << "Selected text:" << lineEdit.selectedText(); + QVERIFY(!lineEdit.selectedText().isEmpty()); + QVERIFY(!lineEdit.selectedText().endsWith(suffix)); + + // Normal mouse selection from left to right, y change is below threshold. + QTest::mousePress(lineEdit.windowHandle(), Qt::LeftButton, Qt::NoModifier, center); + QTest::mouseMove(lineEdit.windowHandle(), center + QPoint(20, 5)); + qCDebug(lcTests) << "Selected text:" << lineEdit.selectedText(); + QVERIFY(!lineEdit.selectedText().isEmpty()); + QVERIFY(!lineEdit.selectedText().endsWith(suffix)); + + // Normal mouse selection from right to left, y doesn't change. + QTest::mousePress(lineEdit.windowHandle(), Qt::LeftButton, Qt::NoModifier, center); + QTest::mouseMove(lineEdit.windowHandle(), center + QPoint(-20, 0)); + qCDebug(lcTests) << "Selected text:" << lineEdit.selectedText(); + QVERIFY(!lineEdit.selectedText().isEmpty()); + QVERIFY(!lineEdit.selectedText().startsWith(prefix)); + + // Normal mouse selection from right to left, y change is below threshold. + QTest::mousePress(lineEdit.windowHandle(), Qt::LeftButton, Qt::NoModifier, center); + QTest::mouseMove(lineEdit.windowHandle(), center + QPoint(-20, -5)); + qCDebug(lcTests) << "Selected text:" << lineEdit.selectedText(); + QVERIFY(!lineEdit.selectedText().isEmpty()); + QVERIFY(!lineEdit.selectedText().startsWith(prefix)); + + const int offset = QGuiApplication::styleHints()->mouseQuickSelectionThreshold() + 1; + + // Select the whole right half. + QTest::mousePress(lineEdit.windowHandle(), Qt::LeftButton, Qt::NoModifier, center); + QTest::mouseMove(lineEdit.windowHandle(), center + QPoint(1, offset)); + qCDebug(lcTests) << "Selected text:" << lineEdit.selectedText(); + QVERIFY(lineEdit.selectedText().endsWith(suffix)); + + // Select the whole left half. + QTest::mousePress(lineEdit.windowHandle(), Qt::LeftButton, Qt::NoModifier, center); + QTest::mouseMove(lineEdit.windowHandle(), center + QPoint(1, -offset)); + qCDebug(lcTests) << "Selected text:" << lineEdit.selectedText(); + QVERIFY(lineEdit.selectedText().startsWith(prefix)); + + // Normal selection -> quick selection -> back to normal selection. + QTest::mousePress(lineEdit.windowHandle(), Qt::LeftButton, Qt::NoModifier, center); + QTest::mouseMove(lineEdit.windowHandle(), center + QPoint(20, 0)); + const auto partialSelection = lineEdit.selectedText(); + qCDebug(lcTests) << "Selected text:" << lineEdit.selectedText(); + QVERIFY(!partialSelection.endsWith(suffix)); + QTest::mouseMove(lineEdit.windowHandle(), center + QPoint(20, offset)); + qCDebug(lcTests) << "Selected text:" << lineEdit.selectedText(); + QVERIFY(lineEdit.selectedText().endsWith(suffix)); + QTest::mouseMove(lineEdit.windowHandle(), center + QPoint(20, 0)); + qCDebug(lcTests) << "Selected text:" << lineEdit.selectedText(); +#ifdef Q_PROCESSOR_ARM + QEXPECT_FAIL("", "Currently fails on gcc-armv7, needs investigation.", Continue); +#endif + QCOMPARE(lineEdit.selectedText(), partialSelection); + + lineEdit.setLayoutDirection(Qt::RightToLeft); + + // Select the whole left half (RTL layout). + QTest::mousePress(lineEdit.windowHandle(), Qt::LeftButton, Qt::NoModifier, center); + QTest::mouseMove(lineEdit.windowHandle(), center + QPoint(1, offset)); + QVERIFY(lineEdit.selectedText().startsWith(prefix)); + + // Select the whole right half (RTL layout). + QTest::mousePress(lineEdit.windowHandle(), Qt::LeftButton, Qt::NoModifier, center); + QTest::mouseMove(lineEdit.windowHandle(), center + QPoint(1, -offset)); + QVERIFY(lineEdit.selectedText().endsWith(suffix)); +} + QTEST_MAIN(tst_QLineEdit) #include "tst_qlineedit.moc" |