diff options
author | Martin Jones <martin.jones@nokia.com> | 2010-05-14 03:32:49 (GMT) |
---|---|---|
committer | Martin Jones <martin.jones@nokia.com> | 2010-05-14 03:32:49 (GMT) |
commit | bea52aaaaf188293a5e235d5b5f96b386efba5ac (patch) | |
tree | 779458f9465af791d76ddd6b2e4c70aa33f22238 /tests | |
parent | 1e395c0ab9676995419ae8b20e4d95ad3fe11f31 (diff) | |
download | Qt-bea52aaaaf188293a5e235d5b5f96b386efba5ac.zip Qt-bea52aaaaf188293a5e235d5b5f96b386efba5ac.tar.gz Qt-bea52aaaaf188293a5e235d5b5f96b386efba5ac.tar.bz2 |
Add a "priority" property to Keys and KeyNavigation
Allows intercepting keys before or after normal item key processing.
Task-number: QTBUG-10467
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/declarative/qdeclarativeitem/data/keyspriority.qml | 9 | ||||
-rw-r--r-- | tests/auto/declarative/qdeclarativeitem/tst_qdeclarativeitem.cpp | 131 |
2 files changed, 139 insertions, 1 deletions
diff --git a/tests/auto/declarative/qdeclarativeitem/data/keyspriority.qml b/tests/auto/declarative/qdeclarativeitem/data/keyspriority.qml new file mode 100644 index 0000000..171536b --- /dev/null +++ b/tests/auto/declarative/qdeclarativeitem/data/keyspriority.qml @@ -0,0 +1,9 @@ +import Qt 4.7 +import Test 1.0 + +KeyTestItem { + focus: true + Keys.onPressed: keysTestObject.keyPress(event.key, event.text, event.modifiers) + Keys.onReleased: { keysTestObject.keyRelease(event.key, event.text, event.modifiers); event.accepted = true; } + Keys.priority: keysTestObject.processLast ? Keys.AfterItem : Keys.BeforeItem +} diff --git a/tests/auto/declarative/qdeclarativeitem/tst_qdeclarativeitem.cpp b/tests/auto/declarative/qdeclarativeitem/tst_qdeclarativeitem.cpp index f4edeb2..ecc813e 100644 --- a/tests/auto/declarative/qdeclarativeitem/tst_qdeclarativeitem.cpp +++ b/tests/auto/declarative/qdeclarativeitem/tst_qdeclarativeitem.cpp @@ -55,7 +55,9 @@ public: tst_QDeclarativeItem(); private slots: + void initTestCase(); void keys(); + void keysProcessingOrder(); void keyNavigation(); void smooth(); void clip(); @@ -79,8 +81,11 @@ private: class KeysTestObject : public QObject { Q_OBJECT + + Q_PROPERTY(bool processLast READ processLast NOTIFY processLastChanged) + public: - KeysTestObject() : mKey(0), mModifiers(0), mForwardedKey(0) {} + KeysTestObject() : mKey(0), mModifiers(0), mForwardedKey(0), mLast(false) {} void reset() { mKey = 0; @@ -89,6 +94,14 @@ public: mForwardedKey = 0; } + bool processLast() const { return mLast; } + void setProcessLast(bool b) { + if (b != mLast) { + mLast = b; + emit processLastChanged(); + } + } + public slots: void keyPress(int key, QString text, int modifiers) { mKey = key; @@ -104,20 +117,73 @@ public slots: mForwardedKey = key; } +signals: + void processLastChanged(); + public: int mKey; QString mText; int mModifiers; int mForwardedKey; + bool mLast; private: }; +class KeyTestItem : public QDeclarativeItem +{ + Q_OBJECT +public: + KeyTestItem(QDeclarativeItem *parent=0) : QDeclarativeItem(parent), mKey(0) {} + +protected: + void keyPressEvent(QKeyEvent *e) { + keyPressPreHandler(e); + if (e->isAccepted()) + return; + + mKey = e->key(); + + if (e->key() == Qt::Key_A) + e->accept(); + else + e->ignore(); + + if (!e->isAccepted()) + QDeclarativeItem::keyPressEvent(e); + } + + void keyReleaseEvent(QKeyEvent *e) { + keyReleasePreHandler(e); + + if (e->isAccepted()) + return; + + if (e->key() == Qt::Key_B) + e->accept(); + else + e->ignore(); + + if (!e->isAccepted()) + QDeclarativeItem::keyReleaseEvent(e); + } + +public: + int mKey; +}; + +QML_DECLARE_TYPE(KeyTestItem); + tst_QDeclarativeItem::tst_QDeclarativeItem() { } +void tst_QDeclarativeItem::initTestCase() +{ + qmlRegisterType<KeyTestItem>("Test",1,0,"KeyTestItem"); +} + void tst_QDeclarativeItem::keys() { QDeclarativeView *canvas = new QDeclarativeView(0); @@ -214,6 +280,69 @@ void tst_QDeclarativeItem::keys() QCOMPARE(testObject->mKey, 0); QVERIFY(!key.isAccepted()); + canvas->rootContext()->setContextProperty("enableKeyHanding", QVariant(true)); + + key = QKeyEvent(QEvent::KeyPress, Qt::Key_Return, Qt::NoModifier, "", false, 1); + QApplication::sendEvent(canvas, &key); + QCOMPARE(testObject->mKey, int(Qt::Key_Return)); + QVERIFY(key.isAccepted()); + + delete canvas; + delete testObject; +} + +void tst_QDeclarativeItem::keysProcessingOrder() +{ + QDeclarativeView *canvas = new QDeclarativeView(0); + canvas->setFixedSize(240,320); + + KeysTestObject *testObject = new KeysTestObject; + canvas->rootContext()->setContextProperty("keysTestObject", testObject); + + canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/keyspriority.qml")); + canvas->show(); + qApp->processEvents(); + + KeyTestItem *testItem = qobject_cast<KeyTestItem*>(canvas->rootObject()); + QVERIFY(testItem); + + QEvent wa(QEvent::WindowActivate); + QApplication::sendEvent(canvas, &wa); + QFocusEvent fe(QEvent::FocusIn); + QApplication::sendEvent(canvas, &fe); + + QKeyEvent key(QEvent::KeyPress, Qt::Key_A, Qt::NoModifier, "A", false, 1); + QApplication::sendEvent(canvas, &key); + QCOMPARE(testObject->mKey, int(Qt::Key_A)); + QCOMPARE(testObject->mText, QLatin1String("A")); + QVERIFY(testObject->mModifiers == Qt::NoModifier); + QVERIFY(key.isAccepted()); + + testObject->reset(); + + testObject->setProcessLast(true); + + key = QKeyEvent(QEvent::KeyPress, Qt::Key_A, Qt::NoModifier, "A", false, 1); + QApplication::sendEvent(canvas, &key); + QCOMPARE(testObject->mKey, 0); + QVERIFY(key.isAccepted()); + + testObject->reset(); + + key = QKeyEvent(QEvent::KeyPress, Qt::Key_B, Qt::NoModifier, "B", false, 1); + QApplication::sendEvent(canvas, &key); + QCOMPARE(testObject->mKey, int(Qt::Key_B)); + QCOMPARE(testObject->mText, QLatin1String("B")); + QVERIFY(testObject->mModifiers == Qt::NoModifier); + QVERIFY(!key.isAccepted()); + + testObject->reset(); + + key = QKeyEvent(QEvent::KeyRelease, Qt::Key_B, Qt::NoModifier, "B", false, 1); + QApplication::sendEvent(canvas, &key); + QCOMPARE(testObject->mKey, 0); + QVERIFY(key.isAccepted()); + delete canvas; delete testObject; } |