| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #include "qinputcontrol_p.h" |
| 5 | |
| 6 | #include <QtCore/qmimedata.h> |
| 7 | #include <QtGui/qevent.h> |
| 8 | |
| 9 | QT_BEGIN_NAMESPACE |
| 10 | |
| 11 | QInputControl::QInputControl(Type type, QObject *parent) |
| 12 | : QObject(parent) |
| 13 | , m_type(type) |
| 14 | { |
| 15 | } |
| 16 | |
| 17 | QInputControl::QInputControl(Type type, QObjectPrivate &dd, QObject *parent) |
| 18 | : QObject(dd, parent) |
| 19 | , m_type(type) |
| 20 | { |
| 21 | } |
| 22 | |
| 23 | bool QInputControl::isAcceptableInput(const QKeyEvent *event) const |
| 24 | { |
| 25 | const QString text = event->text(); |
| 26 | if (text.isEmpty()) |
| 27 | return false; |
| 28 | |
| 29 | const QChar c = text.at(i: 0); |
| 30 | |
| 31 | // Formatting characters such as ZWNJ, ZWJ, RLM, etc. This needs to go before the |
| 32 | // next test, since CTRL+SHIFT is sometimes used to input it on Windows. |
| 33 | if (c.category() == QChar::Other_Format) |
| 34 | return true; |
| 35 | |
| 36 | // QTBUG-35734: ignore Ctrl/Ctrl+Shift; accept only AltGr (Alt+Ctrl) on German keyboards |
| 37 | if (event->modifiers() == Qt::ControlModifier |
| 38 | || event->modifiers() == (Qt::ShiftModifier | Qt::ControlModifier)) { |
| 39 | return false; |
| 40 | } |
| 41 | |
| 42 | if (c.isPrint()) |
| 43 | return true; |
| 44 | |
| 45 | if (c.category() == QChar::Other_PrivateUse) |
| 46 | return true; |
| 47 | |
| 48 | if (c.isHighSurrogate() && text.size() > 1 && text.at(i: 1).isLowSurrogate()) |
| 49 | return true; |
| 50 | |
| 51 | if (m_type == TextEdit && c == u'\t') |
| 52 | return true; |
| 53 | |
| 54 | return false; |
| 55 | } |
| 56 | |
| 57 | bool QInputControl::isCommonTextEditShortcut(const QKeyEvent *ke) |
| 58 | { |
| 59 | if (ke->modifiers() == Qt::NoModifier |
| 60 | || ke->modifiers() == Qt::ShiftModifier |
| 61 | || ke->modifiers() == Qt::KeypadModifier) { |
| 62 | if (ke->key() < Qt::Key_Escape) { |
| 63 | return true; |
| 64 | } else { |
| 65 | switch (ke->key()) { |
| 66 | case Qt::Key_Return: |
| 67 | case Qt::Key_Enter: |
| 68 | case Qt::Key_Delete: |
| 69 | case Qt::Key_Home: |
| 70 | case Qt::Key_End: |
| 71 | case Qt::Key_Backspace: |
| 72 | case Qt::Key_Left: |
| 73 | case Qt::Key_Right: |
| 74 | case Qt::Key_Up: |
| 75 | case Qt::Key_Down: |
| 76 | case Qt::Key_Tab: |
| 77 | return true; |
| 78 | default: |
| 79 | break; |
| 80 | } |
| 81 | } |
| 82 | #if QT_CONFIG(shortcut) |
| 83 | } else if (ke->matches(key: QKeySequence::Copy) |
| 84 | || ke->matches(key: QKeySequence::Paste) |
| 85 | || ke->matches(key: QKeySequence::Cut) |
| 86 | || ke->matches(key: QKeySequence::Redo) |
| 87 | || ke->matches(key: QKeySequence::Undo) |
| 88 | || ke->matches(key: QKeySequence::MoveToNextWord) |
| 89 | || ke->matches(key: QKeySequence::MoveToPreviousWord) |
| 90 | || ke->matches(key: QKeySequence::MoveToStartOfDocument) |
| 91 | || ke->matches(key: QKeySequence::MoveToEndOfDocument) |
| 92 | || ke->matches(key: QKeySequence::SelectNextWord) |
| 93 | || ke->matches(key: QKeySequence::SelectPreviousWord) |
| 94 | || ke->matches(key: QKeySequence::SelectStartOfLine) |
| 95 | || ke->matches(key: QKeySequence::SelectEndOfLine) |
| 96 | || ke->matches(key: QKeySequence::SelectStartOfBlock) |
| 97 | || ke->matches(key: QKeySequence::SelectEndOfBlock) |
| 98 | || ke->matches(key: QKeySequence::SelectStartOfDocument) |
| 99 | || ke->matches(key: QKeySequence::SelectEndOfDocument) |
| 100 | || ke->matches(key: QKeySequence::SelectAll) |
| 101 | ) { |
| 102 | return true; |
| 103 | #endif |
| 104 | } |
| 105 | return false; |
| 106 | } |
| 107 | |
| 108 | /*! |
| 109 | \internal |
| 110 | |
| 111 | Creates a wrapper for returning QMimeData in response to |
| 112 | Qt::ImCurrentSelection, while being backwards compatible |
| 113 | with clients who only read the plain text string. |
| 114 | */ |
| 115 | QVariant QInputControl::selectionWrapper(QMimeData *mimeData) |
| 116 | { |
| 117 | struct MimeDataSelection |
| 118 | { |
| 119 | QMimeData *mimeData = nullptr; |
| 120 | operator QMimeData*() const { return mimeData; } |
| 121 | operator QString() const { return mimeData->text(); } |
| 122 | }; |
| 123 | |
| 124 | static bool registeredConversions = []{ |
| 125 | return QMetaType::registerConverter<MimeDataSelection, QMimeData*>() |
| 126 | && QMetaType::registerConverter<MimeDataSelection, QString>(); |
| 127 | }(); |
| 128 | Q_ASSERT(registeredConversions); |
| 129 | return QVariant::fromValue(value: MimeDataSelection{.mimeData: mimeData}); |
| 130 | } |
| 131 | |
| 132 | QMimeData *QInputControl::mimeDataForInputEvent(QInputMethodEvent *event) |
| 133 | { |
| 134 | const auto &attributes = event->attributes(); |
| 135 | auto mimeDataAttr = std::find_if(first: attributes.begin(), last: attributes.end(), |
| 136 | pred: [](auto a) { return a.type == QInputMethodEvent::MimeData; }); |
| 137 | return mimeDataAttr != event->attributes().end() ? |
| 138 | mimeDataAttr->value.value<QMimeData*>() : nullptr; |
| 139 | } |
| 140 | |
| 141 | QT_END_NAMESPACE |
| 142 | |
| 143 | #include "moc_qinputcontrol_p.cpp" |
| 144 | |