Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qinputcontrol.cpp
Go to the documentation of this file.
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
5
6#include <QtCore/qmimedata.h>
7#include <QtGui/qevent.h>
8
10
11QInputControl::QInputControl(Type type, QObject *parent)
12 : QObject(parent)
13 , m_type(type)
14{
15}
16
17QInputControl::QInputControl(Type type, QObjectPrivate &dd, QObject *parent)
18 : QObject(dd, parent)
19 , m_type(type)
20{
21}
22
23bool 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(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(1).isLowSurrogate())
49 return true;
50
51 if (m_type == TextEdit && c == u'\t')
52 return true;
53
54 return false;
55}
56
57bool 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(QKeySequence::Copy)
84 || ke->matches(QKeySequence::Paste)
85 || ke->matches(QKeySequence::Cut)
86 || ke->matches(QKeySequence::Redo)
87 || ke->matches(QKeySequence::Undo)
88 || ke->matches(QKeySequence::MoveToNextWord)
89 || ke->matches(QKeySequence::MoveToPreviousWord)
90 || ke->matches(QKeySequence::MoveToStartOfDocument)
91 || ke->matches(QKeySequence::MoveToEndOfDocument)
92 || ke->matches(QKeySequence::SelectNextWord)
93 || ke->matches(QKeySequence::SelectPreviousWord)
94 || ke->matches(QKeySequence::SelectStartOfLine)
95 || ke->matches(QKeySequence::SelectEndOfLine)
96 || ke->matches(QKeySequence::SelectStartOfBlock)
97 || ke->matches(QKeySequence::SelectEndOfBlock)
98 || ke->matches(QKeySequence::SelectStartOfDocument)
99 || ke->matches(QKeySequence::SelectEndOfDocument)
100 || ke->matches(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*/
115QVariant 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(MimeDataSelection{mimeData});
130}
131
132QMimeData *QInputControl::mimeDataForInputEvent(QInputMethodEvent *event)
133{
134 const auto &attributes = event->attributes();
135 auto mimeDataAttr = std::find_if(attributes.begin(), attributes.end(),
136 [](auto a) { return a.type == QInputMethodEvent::MimeData; });
137 return mimeDataAttr != event->attributes().end() ?
138 mimeDataAttr->value.value<QMimeData*>() : nullptr;
139}
140
141QT_END_NAMESPACE
142
143#include "moc_qinputcontrol_p.cpp"