| 1 | // SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. |
|---|---|
| 2 | // |
| 3 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | |
| 5 | #include "hotkeylineedit.h" |
| 6 | |
| 7 | class HotkeyLineEditPrivate |
| 8 | { |
| 9 | HotkeyLineEditPrivate(); |
| 10 | bool hotkeyMode; |
| 11 | int key; |
| 12 | |
| 13 | friend class HotkeyLineEdit; |
| 14 | }; |
| 15 | |
| 16 | HotkeyLineEditPrivate::HotkeyLineEditPrivate() |
| 17 | : hotkeyMode(false) |
| 18 | , key(Qt::Key_unknown) |
| 19 | { |
| 20 | |
| 21 | } |
| 22 | |
| 23 | HotkeyLineEdit::HotkeyLineEdit(QWidget *parent) |
| 24 | : QLineEdit(parent) |
| 25 | , d(new HotkeyLineEditPrivate()) |
| 26 | { |
| 27 | setAttribute(Qt::WA_InputMethodEnabled, false); |
| 28 | } |
| 29 | |
| 30 | HotkeyLineEdit::~HotkeyLineEdit() |
| 31 | { |
| 32 | |
| 33 | } |
| 34 | |
| 35 | void HotkeyLineEdit::setHotkeyMode(bool hotkeyMode) |
| 36 | { |
| 37 | d->hotkeyMode = hotkeyMode; |
| 38 | } |
| 39 | |
| 40 | bool HotkeyLineEdit::isHotkeyMode() |
| 41 | { |
| 42 | return d->hotkeyMode; |
| 43 | } |
| 44 | |
| 45 | void HotkeyLineEdit::setKey(int key) |
| 46 | { |
| 47 | Qt::Key qKey = static_cast<Qt::Key>(key); |
| 48 | if (qKey != Qt::Key_unknown) |
| 49 | { |
| 50 | d->key = qKey; |
| 51 | setText(QKeySequence(qKey).toString()); |
| 52 | } |
| 53 | else |
| 54 | { |
| 55 | d->key = Qt::Key_unknown; |
| 56 | setText(""); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | int HotkeyLineEdit::getKey() |
| 61 | { |
| 62 | return d->key; |
| 63 | } |
| 64 | |
| 65 | void HotkeyLineEdit::setText(QString text) |
| 66 | { |
| 67 | QLineEdit::setText(text); |
| 68 | } |
| 69 | |
| 70 | void HotkeyLineEdit::keyPressEvent(QKeyEvent *e) |
| 71 | { |
| 72 | if (!d->hotkeyMode) { |
| 73 | QLineEdit::keyPressEvent(e); |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | int key = e->key(); |
| 78 | Qt::Key qKey = static_cast<Qt::Key>(key); |
| 79 | if (qKey == Qt::Key_unknown |
| 80 | || qKey == Qt::Key_Control |
| 81 | || qKey == Qt::Key_Shift |
| 82 | || qKey == Qt::Key_Alt |
| 83 | || qKey == Qt::Key_Enter |
| 84 | || qKey == Qt::Key_Return |
| 85 | || qKey == Qt::Key_Tab |
| 86 | || qKey == Qt::Key_CapsLock |
| 87 | || qKey == Qt::Key_Escape |
| 88 | || qKey == Qt::Key_Meta) |
| 89 | { |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | Qt::KeyboardModifiers modifiers = e->modifiers(); |
| 94 | if (!(modifiers & Qt::ShiftModifier |
| 95 | || modifiers & Qt::ControlModifier |
| 96 | || modifiers & Qt::AltModifier |
| 97 | || (key >= Qt::Key_F1 && key <= Qt::Key_F35))) |
| 98 | { |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | if (modifiers & Qt::ShiftModifier) { |
| 103 | key += Qt::SHIFT; |
| 104 | } |
| 105 | |
| 106 | if (modifiers & Qt::ControlModifier) { |
| 107 | key += Qt::CTRL; |
| 108 | } |
| 109 | |
| 110 | if (modifiers & Qt::AltModifier) { |
| 111 | key += Qt::ALT; |
| 112 | } |
| 113 | |
| 114 | setText(QKeySequence(key).toString()); |
| 115 | d->key = key; |
| 116 | emit sigKeyChanged(key); |
| 117 | } |
| 118 | |
| 119 |