Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/widgets/widgets/qkeysequenceedit.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||||||||
2 | ** | - | ||||||||||||
3 | ** Copyright (C) 2015 The Qt Company Ltd. | - | ||||||||||||
4 | ** Copyright (C) 2013 Ivan Komissarov. | - | ||||||||||||
5 | ** Contact: http://www.qt.io/licensing/ | - | ||||||||||||
6 | ** | - | ||||||||||||
7 | ** This file is part of the QtWidgets module of the Qt Toolkit. | - | ||||||||||||
8 | ** | - | ||||||||||||
9 | ** $QT_BEGIN_LICENSE:LGPL21$ | - | ||||||||||||
10 | ** Commercial License Usage | - | ||||||||||||
11 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||||||||
12 | ** accordance with the commercial license agreement provided with the | - | ||||||||||||
13 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||||||||
14 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||||||||
15 | ** and conditions see http://www.qt.io/terms-conditions. For further | - | ||||||||||||
16 | ** information use the contact form at http://www.qt.io/contact-us. | - | ||||||||||||
17 | ** | - | ||||||||||||
18 | ** GNU Lesser General Public License Usage | - | ||||||||||||
19 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||||||||
20 | ** General Public License version 2.1 or version 3 as published by the Free | - | ||||||||||||
21 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | - | ||||||||||||
22 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the | - | ||||||||||||
23 | ** following information to ensure the GNU Lesser General Public License | - | ||||||||||||
24 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and | - | ||||||||||||
25 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - | ||||||||||||
26 | ** | - | ||||||||||||
27 | ** As a special exception, The Qt Company gives you certain additional | - | ||||||||||||
28 | ** rights. These rights are described in The Qt Company LGPL Exception | - | ||||||||||||
29 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - | ||||||||||||
30 | ** | - | ||||||||||||
31 | ** $QT_END_LICENSE$ | - | ||||||||||||
32 | ** | - | ||||||||||||
33 | ****************************************************************************/ | - | ||||||||||||
34 | - | |||||||||||||
35 | #include "qkeysequenceedit.h" | - | ||||||||||||
36 | #include "qkeysequenceedit_p.h" | - | ||||||||||||
37 | - | |||||||||||||
38 | #include "qboxlayout.h" | - | ||||||||||||
39 | #include "qlineedit.h" | - | ||||||||||||
40 | - | |||||||||||||
41 | QT_BEGIN_NAMESPACE | - | ||||||||||||
42 | - | |||||||||||||
43 | #ifndef QT_NO_KEYSEQUENCEEDIT | - | ||||||||||||
44 | - | |||||||||||||
45 | Q_STATIC_ASSERT(QKeySequencePrivate::MaxKeyCount == 4); // assumed by the code around here | - | ||||||||||||
46 | - | |||||||||||||
47 | void QKeySequenceEditPrivate::init() | - | ||||||||||||
48 | { | - | ||||||||||||
49 | Q_Q(QKeySequenceEdit); | - | ||||||||||||
50 | - | |||||||||||||
51 | lineEdit = new QLineEdit(q); | - | ||||||||||||
52 | lineEdit->setObjectName(QStringLiteral("qt_keysequenceedit_lineedit")); never executed: return qstring_literal_temp; | 0 | ||||||||||||
53 | keyNum = 0; | - | ||||||||||||
54 | prevKey = -1; | - | ||||||||||||
55 | releaseTimer = 0; | - | ||||||||||||
56 | - | |||||||||||||
57 | QVBoxLayout *layout = new QVBoxLayout(q); | - | ||||||||||||
58 | layout->setContentsMargins(0, 0, 0, 0); | - | ||||||||||||
59 | layout->addWidget(lineEdit); | - | ||||||||||||
60 | - | |||||||||||||
61 | key[0] = key[1] = key[2] = key[3] = 0; | - | ||||||||||||
62 | - | |||||||||||||
63 | lineEdit->setFocusProxy(q); | - | ||||||||||||
64 | lineEdit->installEventFilter(q); | - | ||||||||||||
65 | resetState(); | - | ||||||||||||
66 | - | |||||||||||||
67 | q->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); | - | ||||||||||||
68 | q->setFocusPolicy(Qt::StrongFocus); | - | ||||||||||||
69 | q->setAttribute(Qt::WA_MacShowFocusRect, true); | - | ||||||||||||
70 | q->setAttribute(Qt::WA_InputMethodEnabled, false); | - | ||||||||||||
71 | - | |||||||||||||
72 | // TODO: add clear button | - | ||||||||||||
73 | } never executed: end of block | 0 | ||||||||||||
74 | - | |||||||||||||
75 | int QKeySequenceEditPrivate::translateModifiers(Qt::KeyboardModifiers state, const QString &text) | - | ||||||||||||
76 | { | - | ||||||||||||
77 | int result = 0; | - | ||||||||||||
78 | // The shift modifier only counts when it is not used to type a symbol | - | ||||||||||||
79 | // that is only reachable using the shift key anyway | - | ||||||||||||
80 | if ((state & Qt::ShiftModifier) && (text.isEmpty() ||
| 0 | ||||||||||||
81 | !text.at(0).isPrint() ||
| 0 | ||||||||||||
82 | text.at(0).isLetterOrNumber() ||
| 0 | ||||||||||||
83 | text.at(0).isSpace()))
| 0 | ||||||||||||
84 | result |= Qt::SHIFT; never executed: result |= Qt::SHIFT; | 0 | ||||||||||||
85 | - | |||||||||||||
86 | if (state & Qt::ControlModifier)
| 0 | ||||||||||||
87 | result |= Qt::CTRL; never executed: result |= Qt::CTRL; | 0 | ||||||||||||
88 | if (state & Qt::MetaModifier)
| 0 | ||||||||||||
89 | result |= Qt::META; never executed: result |= Qt::META; | 0 | ||||||||||||
90 | if (state & Qt::AltModifier)
| 0 | ||||||||||||
91 | result |= Qt::ALT; never executed: result |= Qt::ALT; | 0 | ||||||||||||
92 | return result; never executed: return result; | 0 | ||||||||||||
93 | } | - | ||||||||||||
94 | - | |||||||||||||
95 | void QKeySequenceEditPrivate::resetState() | - | ||||||||||||
96 | { | - | ||||||||||||
97 | Q_Q(QKeySequenceEdit); | - | ||||||||||||
98 | - | |||||||||||||
99 | if (releaseTimer) {
| 0 | ||||||||||||
100 | q->killTimer(releaseTimer); | - | ||||||||||||
101 | releaseTimer = 0; | - | ||||||||||||
102 | } never executed: end of block | 0 | ||||||||||||
103 | prevKey = -1; | - | ||||||||||||
104 | lineEdit->setText(keySequence.toString(QKeySequence::NativeText)); | - | ||||||||||||
105 | lineEdit->setPlaceholderText(QKeySequenceEdit::tr("Press shortcut")); | - | ||||||||||||
106 | } never executed: end of block | 0 | ||||||||||||
107 | - | |||||||||||||
108 | void QKeySequenceEditPrivate::finishEditing() | - | ||||||||||||
109 | { | - | ||||||||||||
110 | Q_Q(QKeySequenceEdit); | - | ||||||||||||
111 | - | |||||||||||||
112 | resetState(); | - | ||||||||||||
113 | emit q->keySequenceChanged(keySequence); | - | ||||||||||||
114 | emit q->editingFinished(); | - | ||||||||||||
115 | } never executed: end of block | 0 | ||||||||||||
116 | - | |||||||||||||
117 | /*! | - | ||||||||||||
118 | \class QKeySequenceEdit | - | ||||||||||||
119 | \brief The QKeySequenceEdit widget allows to input a QKeySequence. | - | ||||||||||||
120 | - | |||||||||||||
121 | \inmodule QtWidgets | - | ||||||||||||
122 | - | |||||||||||||
123 | \since 5.2 | - | ||||||||||||
124 | - | |||||||||||||
125 | This widget lets the user choose a QKeySequence, which is usually used as | - | ||||||||||||
126 | a shortcut. The recording is initiated when the widget receives the focus | - | ||||||||||||
127 | and ends one second after the user releases the last key. | - | ||||||||||||
128 | - | |||||||||||||
129 | \sa QKeySequenceEdit::keySequence | - | ||||||||||||
130 | */ | - | ||||||||||||
131 | - | |||||||||||||
132 | /*! | - | ||||||||||||
133 | Constructs a QKeySequenceEdit widget with the given \a parent. | - | ||||||||||||
134 | */ | - | ||||||||||||
135 | QKeySequenceEdit::QKeySequenceEdit(QWidget *parent) : | - | ||||||||||||
136 | QWidget(*new QKeySequenceEditPrivate, parent, 0) | - | ||||||||||||
137 | { | - | ||||||||||||
138 | Q_D(QKeySequenceEdit); | - | ||||||||||||
139 | d->init(); | - | ||||||||||||
140 | } never executed: end of block | 0 | ||||||||||||
141 | - | |||||||||||||
142 | /*! | - | ||||||||||||
143 | Constructs a QKeySequenceEdit widget with the given \a keySequence and \a parent. | - | ||||||||||||
144 | */ | - | ||||||||||||
145 | QKeySequenceEdit::QKeySequenceEdit(const QKeySequence &keySequence, QWidget *parent) : | - | ||||||||||||
146 | QWidget(*new QKeySequenceEditPrivate, parent, 0) | - | ||||||||||||
147 | { | - | ||||||||||||
148 | Q_D(QKeySequenceEdit); | - | ||||||||||||
149 | d->init(); | - | ||||||||||||
150 | setKeySequence(keySequence); | - | ||||||||||||
151 | } never executed: end of block | 0 | ||||||||||||
152 | - | |||||||||||||
153 | /*! | - | ||||||||||||
154 | \internal | - | ||||||||||||
155 | */ | - | ||||||||||||
156 | QKeySequenceEdit::QKeySequenceEdit(QKeySequenceEditPrivate &dd, QWidget *parent, Qt::WindowFlags f) : | - | ||||||||||||
157 | QWidget(dd, parent, f) | - | ||||||||||||
158 | { | - | ||||||||||||
159 | Q_D(QKeySequenceEdit); | - | ||||||||||||
160 | d->init(); | - | ||||||||||||
161 | } never executed: end of block | 0 | ||||||||||||
162 | - | |||||||||||||
163 | /*! | - | ||||||||||||
164 | Destroys the QKeySequenceEdit object. | - | ||||||||||||
165 | */ | - | ||||||||||||
166 | QKeySequenceEdit::~QKeySequenceEdit() | - | ||||||||||||
167 | { | - | ||||||||||||
168 | } | - | ||||||||||||
169 | - | |||||||||||||
170 | /*! | - | ||||||||||||
171 | \property QKeySequenceEdit::keySequence | - | ||||||||||||
172 | - | |||||||||||||
173 | \brief This property contains the currently chosen key sequence. | - | ||||||||||||
174 | - | |||||||||||||
175 | The shortcut can be changed by the user or via setter function. | - | ||||||||||||
176 | */ | - | ||||||||||||
177 | QKeySequence QKeySequenceEdit::keySequence() const | - | ||||||||||||
178 | { | - | ||||||||||||
179 | Q_D(const QKeySequenceEdit); | - | ||||||||||||
180 | - | |||||||||||||
181 | return d->keySequence; never executed: return d->keySequence; | 0 | ||||||||||||
182 | } | - | ||||||||||||
183 | - | |||||||||||||
184 | void QKeySequenceEdit::setKeySequence(const QKeySequence &keySequence) | - | ||||||||||||
185 | { | - | ||||||||||||
186 | Q_D(QKeySequenceEdit); | - | ||||||||||||
187 | - | |||||||||||||
188 | d->resetState(); | - | ||||||||||||
189 | - | |||||||||||||
190 | if (d->keySequence == keySequence)
| 0 | ||||||||||||
191 | return; never executed: return; | 0 | ||||||||||||
192 | - | |||||||||||||
193 | d->keySequence = keySequence; | - | ||||||||||||
194 | - | |||||||||||||
195 | d->key[0] = d->key[1] = d->key[2] = d->key[3] = 0; | - | ||||||||||||
196 | d->keyNum = keySequence.count(); | - | ||||||||||||
197 | for (int i = 0; i < d->keyNum; ++i)
| 0 | ||||||||||||
198 | d->key[i] = keySequence[i]; never executed: d->key[i] = keySequence[i]; | 0 | ||||||||||||
199 | - | |||||||||||||
200 | d->lineEdit->setText(keySequence.toString(QKeySequence::NativeText)); | - | ||||||||||||
201 | - | |||||||||||||
202 | emit keySequenceChanged(keySequence); | - | ||||||||||||
203 | } never executed: end of block | 0 | ||||||||||||
204 | - | |||||||||||||
205 | /*! | - | ||||||||||||
206 | \fn void QKeySequenceEdit::editingFinished() | - | ||||||||||||
207 | - | |||||||||||||
208 | This signal is emitted when the user finishes entering the shortcut. | - | ||||||||||||
209 | - | |||||||||||||
210 | \note there is a one second delay before releasing the last key and | - | ||||||||||||
211 | emitting this signal. | - | ||||||||||||
212 | */ | - | ||||||||||||
213 | - | |||||||||||||
214 | /*! | - | ||||||||||||
215 | \brief Clears the current key sequence. | - | ||||||||||||
216 | */ | - | ||||||||||||
217 | void QKeySequenceEdit::clear() | - | ||||||||||||
218 | { | - | ||||||||||||
219 | setKeySequence(QKeySequence()); | - | ||||||||||||
220 | } never executed: end of block | 0 | ||||||||||||
221 | - | |||||||||||||
222 | /*! | - | ||||||||||||
223 | \reimp | - | ||||||||||||
224 | */ | - | ||||||||||||
225 | bool QKeySequenceEdit::event(QEvent *e) | - | ||||||||||||
226 | { | - | ||||||||||||
227 | switch (e->type()) { | - | ||||||||||||
228 | case QEvent::Shortcut: never executed: case QEvent::Shortcut: | 0 | ||||||||||||
229 | return true; never executed: return true; | 0 | ||||||||||||
230 | case QEvent::ShortcutOverride: never executed: case QEvent::ShortcutOverride: | 0 | ||||||||||||
231 | e->accept(); | - | ||||||||||||
232 | return true; never executed: return true; | 0 | ||||||||||||
233 | default : never executed: default : | 0 | ||||||||||||
234 | break; never executed: break; | 0 | ||||||||||||
235 | } | - | ||||||||||||
236 | - | |||||||||||||
237 | return QWidget::event(e); never executed: return QWidget::event(e); | 0 | ||||||||||||
238 | } | - | ||||||||||||
239 | - | |||||||||||||
240 | /*! | - | ||||||||||||
241 | \reimp | - | ||||||||||||
242 | */ | - | ||||||||||||
243 | void QKeySequenceEdit::keyPressEvent(QKeyEvent *e) | - | ||||||||||||
244 | { | - | ||||||||||||
245 | Q_D(QKeySequenceEdit); | - | ||||||||||||
246 | - | |||||||||||||
247 | int nextKey = e->key(); | - | ||||||||||||
248 | - | |||||||||||||
249 | if (d->prevKey == -1) {
| 0 | ||||||||||||
250 | clear(); | - | ||||||||||||
251 | d->prevKey = nextKey; | - | ||||||||||||
252 | } never executed: end of block | 0 | ||||||||||||
253 | - | |||||||||||||
254 | d->lineEdit->setPlaceholderText(QString()); | - | ||||||||||||
255 | if (nextKey == Qt::Key_Control
| 0 | ||||||||||||
256 | || nextKey == Qt::Key_Shift
| 0 | ||||||||||||
257 | || nextKey == Qt::Key_Meta
| 0 | ||||||||||||
258 | || nextKey == Qt::Key_Alt) {
| 0 | ||||||||||||
259 | return; never executed: return; | 0 | ||||||||||||
260 | } | - | ||||||||||||
261 | - | |||||||||||||
262 | QString selectedText = d->lineEdit->selectedText(); | - | ||||||||||||
263 | if (!selectedText.isEmpty() && selectedText == d->lineEdit->text()) {
| 0 | ||||||||||||
264 | clear(); | - | ||||||||||||
265 | if (nextKey == Qt::Key_Backspace)
| 0 | ||||||||||||
266 | return; never executed: return; | 0 | ||||||||||||
267 | } never executed: end of block | 0 | ||||||||||||
268 | - | |||||||||||||
269 | if (d->keyNum >= QKeySequencePrivate::MaxKeyCount)
| 0 | ||||||||||||
270 | return; never executed: return; | 0 | ||||||||||||
271 | - | |||||||||||||
272 | nextKey |= d->translateModifiers(e->modifiers(), e->text()); | - | ||||||||||||
273 | - | |||||||||||||
274 | d->key[d->keyNum] = nextKey; | - | ||||||||||||
275 | d->keyNum++; | - | ||||||||||||
276 | - | |||||||||||||
277 | QKeySequence key(d->key[0], d->key[1], d->key[2], d->key[3]); | - | ||||||||||||
278 | d->keySequence = key; | - | ||||||||||||
279 | QString text = key.toString(QKeySequence::NativeText); | - | ||||||||||||
280 | if (d->keyNum < QKeySequencePrivate::MaxKeyCount) {
| 0 | ||||||||||||
281 | //: This text is an "unfinished" shortcut, expands like "Ctrl+A, ..." | - | ||||||||||||
282 | text = tr("%1, ...").arg(text); | - | ||||||||||||
283 | } never executed: end of block | 0 | ||||||||||||
284 | d->lineEdit->setText(text); | - | ||||||||||||
285 | e->accept(); | - | ||||||||||||
286 | } never executed: end of block | 0 | ||||||||||||
287 | - | |||||||||||||
288 | /*! | - | ||||||||||||
289 | \reimp | - | ||||||||||||
290 | */ | - | ||||||||||||
291 | void QKeySequenceEdit::keyReleaseEvent(QKeyEvent *e) | - | ||||||||||||
292 | { | - | ||||||||||||
293 | Q_D(QKeySequenceEdit); | - | ||||||||||||
294 | - | |||||||||||||
295 | if (d->prevKey == e->key()) {
| 0 | ||||||||||||
296 | if (d->keyNum < QKeySequencePrivate::MaxKeyCount)
| 0 | ||||||||||||
297 | d->releaseTimer = startTimer(1000); never executed: d->releaseTimer = startTimer(1000); | 0 | ||||||||||||
298 | else | - | ||||||||||||
299 | d->finishEditing(); never executed: d->finishEditing(); | 0 | ||||||||||||
300 | } | - | ||||||||||||
301 | e->accept(); | - | ||||||||||||
302 | } never executed: end of block | 0 | ||||||||||||
303 | - | |||||||||||||
304 | /*! | - | ||||||||||||
305 | \reimp | - | ||||||||||||
306 | */ | - | ||||||||||||
307 | void QKeySequenceEdit::timerEvent(QTimerEvent *e) | - | ||||||||||||
308 | { | - | ||||||||||||
309 | Q_D(QKeySequenceEdit); | - | ||||||||||||
310 | if (e->timerId() == d->releaseTimer) {
| 0 | ||||||||||||
311 | d->finishEditing(); | - | ||||||||||||
312 | return; never executed: return; | 0 | ||||||||||||
313 | } | - | ||||||||||||
314 | - | |||||||||||||
315 | QWidget::timerEvent(e); | - | ||||||||||||
316 | } never executed: end of block | 0 | ||||||||||||
317 | - | |||||||||||||
318 | #endif // QT_NO_KEYSEQUENCEEDIT | - | ||||||||||||
319 | - | |||||||||||||
320 | QT_END_NAMESPACE | - | ||||||||||||
321 | - | |||||||||||||
322 | #include "moc_qkeysequenceedit.cpp" | - | ||||||||||||
Source code | Switch to Preprocessed file |