OpenCoverage

qkeysequenceedit.cpp

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

Generated by Squish Coco Non-Commercial 4.3.0-BETA-master-30-08-2018-4cb69e9