Add bool QQuickKeyEvent::matches(QKeySequence::StandardKey)
authorJ-P Nurmi <jpnurmi@digia.com>
Thu, 30 May 2013 15:06:58 +0000 (17:06 +0200)
committerThe Qt Project <gerrit-noreply@qt-project.org>
Mon, 1 Jul 2013 16:02:33 +0000 (18:02 +0200)
Change-Id: Iaa8392c1b113856fa80cd2507f8640050eb9bec2
Reviewed-by: Caroline Chao <caroline.chao@digia.com>
src/quick/items/qquickevents.cpp
src/quick/items/qquickevents_p_p.h
src/quick/util/qquickutilmodule.cpp
tests/auto/quick/qquickitem2/data/standardkeys.qml [new file with mode: 0644]
tests/auto/quick/qquickitem2/tst_qquickitem.cpp

index 50c5697..6ee5e6d 100644 (file)
@@ -147,6 +147,26 @@ Item {
     \endqml
 */
 
+/*!
+    \qmlmethod bool QtQuick2::KeyEvent::matches(StandardKey key)
+    \since QtQuick 2.2
+
+    Returns \c true if the key event matches the given standard \a key; otherwise returns \c false.
+
+    \qml
+    Item {
+        focus: true
+        Keys.onPressed: {
+            if (event.matches(StandardKey.Undo))
+                myModel.undo();
+            else if (event.matches(StandardKey.Redo))
+                myModel.redo();
+        }
+    }
+    \endqml
+
+    \sa QKeySequence::StandardKey
+*/
 
 /*!
     \qmltype MouseEvent
index 6f1b152..f14e035 100644 (file)
@@ -59,6 +59,7 @@
 #include <QtCore/qobject.h>
 #include <QtGui/qvector2d.h>
 #include <QtGui/qevent.h>
+#include <QtGui/qkeysequence.h>
 
 QT_BEGIN_NAMESPACE
 
@@ -89,6 +90,8 @@ public:
     bool isAccepted() { return event.isAccepted(); }
     void setAccepted(bool accepted) { event.setAccepted(accepted); }
 
+    Q_REVISION(2) Q_INVOKABLE bool matches(QKeySequence::StandardKey key) const { return event.matches(key); }
+
 private:
     QKeyEvent event;
 };
index a45ec4e..fdf8314 100644 (file)
@@ -58,6 +58,9 @@
 #include <private/qquickanimationcontroller_p.h>
 #include <QtCore/qcoreapplication.h>
 #include <QtGui/QInputMethod>
+#include <QtGui/QKeySequence>
+
+Q_DECLARE_METATYPE(QKeySequence::StandardKey)
 
 void QQuickUtilModule::defineModule()
 {
@@ -91,4 +94,7 @@ void QQuickUtilModule::defineModule()
     qmlRegisterType<QQuickStateOperation>();
 
     qmlRegisterCustomType<QQuickPropertyChanges>("QtQuick",2,0,"PropertyChanges", new QQuickPropertyChangesParser);
+
+    qRegisterMetaType<QKeySequence::StandardKey>();
+    qmlRegisterUncreatableType<QKeySequence, 2>("QtQuick", 2, 2, "StandardKey", QStringLiteral("Cannot create an instance of StandardKey."));
 }
diff --git a/tests/auto/quick/qquickitem2/data/standardkeys.qml b/tests/auto/quick/qquickitem2/data/standardkeys.qml
new file mode 100644 (file)
index 0000000..c29a143
--- /dev/null
@@ -0,0 +1,57 @@
+/****************************************************************************
+**
+** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the test suite of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** 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 Digia.  For licensing terms and
+** conditions see http://qt.digia.com/licensing.  For further information
+** use the contact form at http://qt.digia.com/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 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file.  Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Digia gives you certain additional
+** rights.  These rights are described in the Digia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3.0 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL included in the
+** packaging of this file.  Please review the following information to
+** ensure the GNU General Public License version 3.0 requirements will be
+** met: http://www.gnu.org/copyleft/gpl.html.
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+import QtQuick 2.2
+
+Item {
+    focus: true
+
+    property bool pressed: false
+    property bool released: false
+
+    Keys.onPressed: {
+        pressed = event.matches(standardKey)
+    }
+
+    Keys.onReleased: {
+        released = event.matches(standardKey)
+    }
+}
index 992e81a..d15a46f 100644 (file)
@@ -78,6 +78,8 @@ private slots:
     void nextItemInFocusChain2();
 
     void keys();
+    void standardKeys_data();
+    void standardKeys();
     void keysProcessingOrder();
     void keysim();
     void keyNavigation();
@@ -1089,6 +1091,64 @@ void tst_QQuickItem::keys()
     delete testObject;
 }
 
+Q_DECLARE_METATYPE(QEvent::Type);
+Q_DECLARE_METATYPE(QKeySequence::StandardKey);
+
+void tst_QQuickItem::standardKeys_data()
+{
+    QTest::addColumn<QKeySequence::StandardKey>("standardKey");
+    QTest::addColumn<QKeySequence::StandardKey>("contextProperty");
+    QTest::addColumn<QEvent::Type>("eventType");
+    QTest::addColumn<bool>("pressed");
+    QTest::addColumn<bool>("released");
+
+    QTest::newRow("Press: Open") << QKeySequence::Open << QKeySequence::Open << QEvent::KeyPress << true << false;
+    QTest::newRow("Press: Close") << QKeySequence::Close << QKeySequence::Close << QEvent::KeyPress << true << false;
+    QTest::newRow("Press: Save") << QKeySequence::Save << QKeySequence::Save << QEvent::KeyPress << true << false;
+    QTest::newRow("Press: Quit") << QKeySequence::Quit << QKeySequence::Quit << QEvent::KeyPress << true << false;
+
+    QTest::newRow("Release: New") << QKeySequence::New << QKeySequence::New << QEvent::KeyRelease << false << true;
+    QTest::newRow("Release: Delete") << QKeySequence::Delete << QKeySequence::Delete << QEvent::KeyRelease << false << true;
+    QTest::newRow("Release: Undo") << QKeySequence::Undo << QKeySequence::Undo << QEvent::KeyRelease << false << true;
+    QTest::newRow("Release: Redo") << QKeySequence::Redo << QKeySequence::Redo << QEvent::KeyRelease << false << true;
+
+    QTest::newRow("Mismatch: Cut") << QKeySequence::Cut << QKeySequence::Copy << QEvent::KeyPress << false << false;
+    QTest::newRow("Mismatch: Copy") << QKeySequence::Copy << QKeySequence::Paste << QEvent::KeyPress << false << false;
+    QTest::newRow("Mismatch: Paste") << QKeySequence::Paste << QKeySequence::Cut << QEvent::KeyRelease << false << false;
+    QTest::newRow("Mismatch: Quit") << QKeySequence::Quit << QKeySequence::New << QEvent::KeyRelease << false << false;
+}
+
+void tst_QQuickItem::standardKeys()
+{
+    QFETCH(QKeySequence::StandardKey, standardKey);
+    QFETCH(QKeySequence::StandardKey, contextProperty);
+    QFETCH(QEvent::Type, eventType);
+    QFETCH(bool, pressed);
+    QFETCH(bool, released);
+
+    QKeySequence keySequence(standardKey);
+    if (keySequence.isEmpty())
+        QSKIP("Undefined key sequence.");
+
+    QQuickView view;
+    view.rootContext()->setContextProperty("standardKey", contextProperty);
+    view.setSource(testFileUrl("standardkeys.qml"));
+    view.show();
+    view.requestActivate();
+    QVERIFY(QTest::qWaitForWindowActive(&view));
+
+    QQuickItem *item = qobject_cast<QQuickItem*>(view.rootObject());
+    QVERIFY(item);
+
+    const int key = keySequence[0] & Qt::Key_unknown;
+    const int modifiers = keySequence[0] & Qt::KeyboardModifierMask;
+    QKeyEvent keyEvent(eventType, key, static_cast<Qt::KeyboardModifiers>(modifiers));
+    QGuiApplication::sendEvent(&view, &keyEvent);
+
+    QCOMPARE(item->property("pressed").toBool(), pressed);
+    QCOMPARE(item->property("released").toBool(), released);
+}
+
 void tst_QQuickItem::keysProcessingOrder()
 {
     QQuickView *window = new QQuickView(0);