From d1fbfbbab7f78e0a1050acce3540c02660a0d68f Mon Sep 17 00:00:00 2001 From: Mitch Curtis Date: Wed, 25 Nov 2015 15:20:22 +0100 Subject: ListView: add keyNavigationEnabled property It is useful for applications that need to selectively enable or disable mouse and keyboard interaction. [ChangeLog][QtQuick][ListView] added keyNavigationEnabled property to allow mouse and keyboard interaction to be selectively enabled/disabled. Change-Id: I1cdb73e7be105bcc5c131e3cf2ae242dc66e8a65 Task-number: QTBUG-17051 Reviewed-by: J-P Nurmi --- .../quick/qquicklistview/tst_qquicklistview.cpp | 68 ++++++++++++++++++++++ 1 file changed, 68 insertions(+) (limited to 'tests/auto/quick/qquicklistview/tst_qquicklistview.cpp') diff --git a/tests/auto/quick/qquicklistview/tst_qquicklistview.cpp b/tests/auto/quick/qquicklistview/tst_qquicklistview.cpp index a5de266636..ad6c6ec12d 100644 --- a/tests/auto/quick/qquicklistview/tst_qquicklistview.cpp +++ b/tests/auto/quick/qquicklistview/tst_qquicklistview.cpp @@ -250,6 +250,8 @@ private slots: void QTBUG_48044_currentItemNotVisibleAfterTransition(); + void keyNavigationEnabled(); + private: template void items(const QUrl &source); template void changed(const QUrl &source); @@ -8295,6 +8297,72 @@ void tst_QQuickListView::QTBUG_48044_currentItemNotVisibleAfterTransition() QVERIFY(!currentPriv->culled); } +void tst_QQuickListView::keyNavigationEnabled() +{ + QScopedPointer window(createView()); + window->setSource(testFileUrl("simplelistview.qml")); + window->show(); + window->requestActivate(); + QVERIFY(QTest::qWaitForWindowActive(window.data())); + + QQuickListView *listView = qobject_cast(window->rootObject()); + QVERIFY(listView); + QCOMPARE(listView->isKeyNavigationEnabled(), true); + + listView->setFocus(true); + QVERIFY(listView->hasActiveFocus()); + + listView->setHighlightMoveDuration(0); + + // If keyNavigationEnabled is not explicitly set to true, respect the original behavior + // of disabling both mouse and keyboard interaction. + QSignalSpy enabledSpy(listView, SIGNAL(keyNavigationEnabledChanged())); + listView->setInteractive(false); + QCOMPARE(enabledSpy.count(), 1); + QCOMPARE(listView->isKeyNavigationEnabled(), false); + + flick(window.data(), QPoint(200, 200), QPoint(200, 50), 100); + QVERIFY(!listView->isMoving()); + QCOMPARE(listView->contentY(), 0.0); + QCOMPARE(listView->currentIndex(), 0); + + QTest::keyClick(window.data(), Qt::Key_Down); + QCOMPARE(listView->currentIndex(), 0); + + // Check that isKeyNavigationEnabled implicitly follows the value of interactive. + listView->setInteractive(true); + QCOMPARE(enabledSpy.count(), 2); + QCOMPARE(listView->isKeyNavigationEnabled(), true); + + // Change it back again for the next check. + listView->setInteractive(false); + QCOMPARE(enabledSpy.count(), 3); + QCOMPARE(listView->isKeyNavigationEnabled(), false); + + // Setting keyNavigationEnabled to true shouldn't enable mouse interaction. + listView->setKeyNavigationEnabled(true); + QCOMPARE(enabledSpy.count(), 4); + flick(window.data(), QPoint(200, 200), QPoint(200, 50), 100); + QVERIFY(!listView->isMoving()); + QCOMPARE(listView->contentY(), 0.0); + QCOMPARE(listView->currentIndex(), 0); + + // Should now work. + QTest::keyClick(window.data(), Qt::Key_Down); + QCOMPARE(listView->currentIndex(), 1); + // contentY won't change for one index change in a view this high. + + // Changing interactive now shouldn't result in keyNavigationEnabled changing, + // since we broke the "binding". + listView->setInteractive(true); + QCOMPARE(enabledSpy.count(), 4); + + // Keyboard interaction shouldn't work now. + listView->setKeyNavigationEnabled(false); + QTest::keyClick(window.data(), Qt::Key_Down); + QCOMPARE(listView->currentIndex(), 1); +} + QTEST_MAIN(tst_QQuickListView) #include "tst_qquicklistview.moc" -- cgit v1.2.3