Introduce QInputControl::isCommonTextEditShortcut

For handling ShortcutOverride events in text-edit-like controls one must
know if a key event matches a common text edit short cut. The code that
was formerly in QWidgetTextControl is now moved into
QInputControl::isCommonTextEditShortcut to remove duplicated code in
QtQuick and to avoid adding adding a third duplicate in QtWebEngine.

Task-number: QTBUG-59053
Change-Id: I18723bb0224acd33c8ea4a8d0a601bb5e274a7a9
Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
Reviewed-by: Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@qt.io>
This commit is contained in:
Joerg Bornemann 2017-03-23 17:13:10 +01:00
parent 9ccc3070dd
commit 0ec871dac8
3 changed files with 53 additions and 45 deletions

View File

@ -85,4 +85,55 @@ bool QInputControl::isAcceptableInput(const QKeyEvent *event) const
return false;
}
bool QInputControl::isCommonTextEditShortcut(const QKeyEvent *ke)
{
if (ke->modifiers() == Qt::NoModifier
|| ke->modifiers() == Qt::ShiftModifier
|| ke->modifiers() == Qt::KeypadModifier) {
if (ke->key() < Qt::Key_Escape) {
return true;
} else {
switch (ke->key()) {
case Qt::Key_Return:
case Qt::Key_Enter:
case Qt::Key_Delete:
case Qt::Key_Home:
case Qt::Key_End:
case Qt::Key_Backspace:
case Qt::Key_Left:
case Qt::Key_Right:
case Qt::Key_Up:
case Qt::Key_Down:
case Qt::Key_Tab:
return true;
default:
break;
}
}
#if QT_CONFIG(shortcut)
} else if (ke->matches(QKeySequence::Copy)
|| ke->matches(QKeySequence::Paste)
|| ke->matches(QKeySequence::Cut)
|| ke->matches(QKeySequence::Redo)
|| ke->matches(QKeySequence::Undo)
|| ke->matches(QKeySequence::MoveToNextWord)
|| ke->matches(QKeySequence::MoveToPreviousWord)
|| ke->matches(QKeySequence::MoveToStartOfDocument)
|| ke->matches(QKeySequence::MoveToEndOfDocument)
|| ke->matches(QKeySequence::SelectNextWord)
|| ke->matches(QKeySequence::SelectPreviousWord)
|| ke->matches(QKeySequence::SelectStartOfLine)
|| ke->matches(QKeySequence::SelectEndOfLine)
|| ke->matches(QKeySequence::SelectStartOfBlock)
|| ke->matches(QKeySequence::SelectEndOfBlock)
|| ke->matches(QKeySequence::SelectStartOfDocument)
|| ke->matches(QKeySequence::SelectEndOfDocument)
|| ke->matches(QKeySequence::SelectAll)
) {
return true;
#endif
}
return false;
}
QT_END_NAMESPACE

View File

@ -69,6 +69,7 @@ public:
explicit QInputControl(Type type, QObject *parent = nullptr);
bool isAcceptableInput(const QKeyEvent *event) const;
static bool isCommonTextEditShortcut(const QKeyEvent *ke);
protected:
explicit QInputControl(Type type, QObjectPrivate &dd, QObject *parent = nullptr);

View File

@ -1141,52 +1141,8 @@ void QWidgetTextControl::processEvent(QEvent *e, const QMatrix &matrix, QWidget
case QEvent::ShortcutOverride:
if (d->interactionFlags & Qt::TextEditable) {
QKeyEvent* ke = static_cast<QKeyEvent *>(e);
if (ke->modifiers() == Qt::NoModifier
|| ke->modifiers() == Qt::ShiftModifier
|| ke->modifiers() == Qt::KeypadModifier) {
if (ke->key() < Qt::Key_Escape) {
ke->accept();
} else {
switch (ke->key()) {
case Qt::Key_Return:
case Qt::Key_Enter:
case Qt::Key_Delete:
case Qt::Key_Home:
case Qt::Key_End:
case Qt::Key_Backspace:
case Qt::Key_Left:
case Qt::Key_Right:
case Qt::Key_Up:
case Qt::Key_Down:
case Qt::Key_Tab:
ke->accept();
default:
break;
}
}
#ifndef QT_NO_SHORTCUT
} else if (ke == QKeySequence::Copy
|| ke == QKeySequence::Paste
|| ke == QKeySequence::Cut
|| ke == QKeySequence::Redo
|| ke == QKeySequence::Undo
|| ke == QKeySequence::MoveToNextWord
|| ke == QKeySequence::MoveToPreviousWord
|| ke == QKeySequence::MoveToStartOfDocument
|| ke == QKeySequence::MoveToEndOfDocument
|| ke == QKeySequence::SelectNextWord
|| ke == QKeySequence::SelectPreviousWord
|| ke == QKeySequence::SelectStartOfLine
|| ke == QKeySequence::SelectEndOfLine
|| ke == QKeySequence::SelectStartOfBlock
|| ke == QKeySequence::SelectEndOfBlock
|| ke == QKeySequence::SelectStartOfDocument
|| ke == QKeySequence::SelectEndOfDocument
|| ke == QKeySequence::SelectAll
) {
if (isCommonTextEditShortcut(ke))
ke->accept();
#endif
}
}
break;
default: