20 Q_Q(QKeySequenceEdit);
22 lineEdit =
new QLineEdit(q);
23 lineEdit->setObjectName(QStringLiteral(
"qt_keysequenceedit_lineedit"));
24 lineEdit->setClearButtonEnabled(
false);
25 q->connect(lineEdit, &QLineEdit::textChanged, q, [q](
const QString& text) {
33 finishingKeyCombinations = {Qt::Key_Tab, Qt::Key_Backtab};
35 QVBoxLayout *layout =
new QVBoxLayout(q);
36 layout->setContentsMargins(0, 0, 0, 0);
37 layout->addWidget(lineEdit);
39 std::fill_n(key, QKeySequencePrivate::MaxKeyCount, QKeyCombination::fromCombined(0));
41 lineEdit->setFocusProxy(q);
42 lineEdit->installEventFilter(q);
45 q->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
46 q->setFocusPolicy(Qt::StrongFocus);
47 q->setAttribute(Qt::WA_MacShowFocusRect,
true);
48 q->setAttribute(Qt::WA_InputMethodEnabled,
false);
188void QKeySequenceEdit::setMaximumSequenceLength(qsizetype count)
190 Q_D(QKeySequenceEdit);
192 if (count < 1 || count > QKeySequencePrivate::MaxKeyCount) {
193 qWarning(
"QKeySequenceEdit: maximumSequenceLength %lld is out of range (1..%d)",
194 qlonglong(count), QKeySequencePrivate::MaxKeyCount);
197 d->maximumSequenceLength =
int(count);
198 if (d->keyNum > count) {
199 for (qsizetype i = d->keyNum; i < count; ++i)
200 d->key[i] = QKeyCombination::fromCombined(0);
202 d->rebuildKeySequence();
231void QKeySequenceEdit::setKeySequence(
const QKeySequence &keySequence)
233 Q_D(QKeySequenceEdit);
237 if (d->keySequence == keySequence)
240 const auto desiredCount = keySequence.count();
241 if (desiredCount > d->maximumSequenceLength) {
242 qWarning(
"QKeySequenceEdit: setting a key sequence of length %d "
243 "when maximumSequenceLength is %d, truncating.",
244 desiredCount, d->maximumSequenceLength);
247 d->keyNum = std::min(desiredCount, d->maximumSequenceLength);
248 for (
int i = 0; i < d->keyNum; ++i)
249 d->key[i] = keySequence[i];
250 for (
int i = d->keyNum; i < QKeySequencePrivate::MaxKeyCount; ++i)
251 d->key[i] = QKeyCombination::fromCombined(0);
253 d->rebuildKeySequence();
255 d->lineEdit->setText(d->keySequence.toString(QKeySequence::NativeText));
257 emit keySequenceChanged(d->keySequence);
308void QKeySequenceEdit::keyPressEvent(QKeyEvent *e)
310 Q_D(QKeySequenceEdit);
312 if (d->finishingKeyCombinations.contains(e->keyCombination())) {
317 int nextKey = e->key();
319 if (d->prevKey == -1) {
321 d->prevKey = nextKey;
324 d->lineEdit->setPlaceholderText(QString());
325 if (nextKey == Qt::Key_Control
326 || nextKey == Qt::Key_Shift
327 || nextKey == Qt::Key_Meta
328 || nextKey == Qt::Key_Alt
329 || nextKey == Qt::Key_unknown) {
333 QString selectedText = d->lineEdit->selectedText();
334 if (!selectedText.isEmpty() && selectedText == d->lineEdit->text()) {
336 if (nextKey == Qt::Key_Backspace)
340 if (d->keyNum >= d->maximumSequenceLength)
343 if (e->modifiers() & Qt::ShiftModifier) {
344 const QList<QKeyCombination> possibleKeys = QKeyMapper::possibleKeys(e);
345 int pkTotal = possibleKeys.size();
349 for (
int i = 0; i < possibleKeys.size(); ++i) {
350 const int key = possibleKeys.at(i).toCombined();
351 if (key - nextKey ==
int(e->modifiers())
352 || (key == nextKey && e->modifiers() == Qt::ShiftModifier)) {
360 nextKey = possibleKeys.first().toCombined();
362 nextKey |= d->translateModifiers(e->modifiers(), e->text());
366 d->key[d->keyNum] = QKeyCombination::fromCombined(nextKey);
369 d->rebuildKeySequence();
370 QString text = d->keySequence.toString(QKeySequence::NativeText);
371 if (d->keyNum < d->maximumSequenceLength) {
373 text = tr(
"%1, ...").arg(text);
375 d->lineEdit->setText(text);
382void QKeySequenceEdit::keyReleaseEvent(QKeyEvent *e)
384 Q_D(QKeySequenceEdit);
386 if (d->prevKey == e->key()) {
387 if (d->keyNum < d->maximumSequenceLength)
388 d->releaseTimer.start(1s,
this);