From 6265ac63fe85e1ff8b2d9ca3d023b3a1a8b281f0 Mon Sep 17 00:00:00 2001 From: Jani Honkonen Date: Fri, 3 Feb 2012 17:04:32 +0200 Subject: Fix limitting shortcuts to keypad only The Qt::KeypadModifier modifier is internally masked away from all shortcuts. So it is not possible to set a keypad only shortcut. Changed the implementation so that first a full keysequence match is searched. Then if no match is found the same sequence is tried without the keypad modifer. Added a autotest for this also to cover the basic use cases relating to this. This is a backport of qt5 commit: 547a1bea492954d828aa0798be93384669812489 Task-number: QTBUG-20191 Change-Id: Ibb6809050ff6da23bdb1c087b93e517d1963d34c Reviewed-by: Stephen Kelly --- src/gui/kernel/qshortcutmap.cpp | 9 ++++-- tests/auto/qpushbutton/tst_qpushbutton.cpp | 47 ++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/src/gui/kernel/qshortcutmap.cpp b/src/gui/kernel/qshortcutmap.cpp index 4ff1ca3..51172de 100644 --- a/src/gui/kernel/qshortcutmap.cpp +++ b/src/gui/kernel/qshortcutmap.cpp @@ -390,6 +390,12 @@ QKeySequence::SequenceMatch QShortcutMap::nextState(QKeyEvent *e) d->identicals.resize(0); result = find(e); + if (result == QKeySequence::NoMatch && (e->modifiers() & Qt::KeypadModifier)) { + // Try to find a match without keypad modifier + QKeyEvent event = *e; + event.setModifiers(e->modifiers() & ~Qt::KeypadModifier); + result = find(&event); + } if (result == QKeySequence::NoMatch && e->modifiers() & Qt::ShiftModifier) { // If Shift + Key_Backtab, also try Shift + Qt::Key_Tab if (e->key() == Qt::Key_Backtab) { @@ -579,8 +585,7 @@ void QShortcutMap::createNewSequences(QKeyEvent *e, QVector &ksl) curKsl.setKey(0, 2); curKsl.setKey(0, 3); } - // Filtering keycode here with 0xdfffffff to ignore the Keypad modifier - curKsl.setKey(possibleKeys.at(pkNum) & 0xdfffffff, index); + curKsl.setKey(possibleKeys.at(pkNum), index); } } } diff --git a/tests/auto/qpushbutton/tst_qpushbutton.cpp b/tests/auto/qpushbutton/tst_qpushbutton.cpp index 1c7c0b6..376b687 100644 --- a/tests/auto/qpushbutton/tst_qpushbutton.cpp +++ b/tests/auto/qpushbutton/tst_qpushbutton.cpp @@ -99,6 +99,7 @@ private slots: void defaultAndAutoDefault(); void sizeHint_data(); void sizeHint(); + void taskQTBUG_20191_shortcutWithKeypadModifer(); /* void state(); void group(); @@ -702,5 +703,51 @@ void tst_QPushButton::sizeHint() } } +void tst_QPushButton::taskQTBUG_20191_shortcutWithKeypadModifer() +{ + // setup a dialog with two buttons + QPushButton *button1 = new QPushButton("5"); + QPushButton *button2 = new QPushButton("5 + KeypadModifier"); + QVBoxLayout *layout = new QVBoxLayout(); + layout->addWidget(button1); + layout->addWidget(button2); + QDialog dialog; + dialog.setLayout(layout); + dialog.show(); + QTest::qWaitForWindowShown(&dialog); + QApplication::setActiveWindow(&dialog); + + // add shortcut '5' to button1 and test with keyboard and keypad '5' keys + QSignalSpy spy1(button1, SIGNAL(clicked())); + button1->setShortcut(Qt::Key_5); + QTest::keyClick(&dialog, Qt::Key_5); + QTest::qWait(300); + QTest::keyClick(&dialog, Qt::Key_5, Qt::KeypadModifier); + QTest::qWait(300); + QCOMPARE(spy1.count(), 2); + + // add shortcut 'keypad 5' to button2 + spy1.clear(); + QSignalSpy spy2(button2, SIGNAL(clicked())); + button2->setShortcut(Qt::Key_5 + Qt::KeypadModifier); + QTest::keyClick(&dialog, Qt::Key_5); + QTest::qWait(300); + QTest::keyClick(&dialog, Qt::Key_5, Qt::KeypadModifier); + QTest::qWait(300); + QCOMPARE(spy1.count(), 1); + QCOMPARE(spy2.count(), 1); + + // remove shortcut from button1 + spy1.clear(); + spy2.clear(); + button1->setShortcut(QKeySequence()); + QTest::keyClick(&dialog, Qt::Key_5); + QTest::qWait(300); + QTest::keyClick(&dialog, Qt::Key_5, Qt::KeypadModifier); + QTest::qWait(300); + QCOMPARE(spy1.count(), 0); + QCOMPARE(spy2.count(), 1); +} + QTEST_MAIN(tst_QPushButton) #include "tst_qpushbutton.moc" -- cgit v0.12