| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #include "dynamic-shortcuts/shortcutcatcher.h" |
| 4 | |
| 5 | #include "gui/reusable/plaintoolbutton.h" |
| 6 | #include "miscellaneous/iconfactory.h" |
| 7 | |
| 8 | #include <QHBoxLayout> |
| 9 | #include <QKeySequenceEdit> |
| 10 | |
| 11 | ShortcutCatcher::ShortcutCatcher(QWidget* parent) |
| 12 | : QWidget(parent), m_isRecording(false), m_numKey(0), m_modifierKeys(0U) { |
| 13 | // Setup layout of the control |
| 14 | m_layout = new QHBoxLayout(this); |
| 15 | |
| 16 | m_layout->setContentsMargins({}); |
| 17 | m_layout->setSpacing(1); |
| 18 | |
| 19 | // Create reset button. |
| 20 | m_btnReset = new PlainToolButton(this); |
| 21 | m_btnReset->setIcon(qApp->icons()->fromTheme(QSL("document-revert"))); |
| 22 | m_btnReset->setFocusPolicy(Qt::FocusPolicy::NoFocus); |
| 23 | m_btnReset->setToolTip(tr("Reset to original shortcut.")); |
| 24 | |
| 25 | // Create clear button. |
| 26 | m_btnClear = new PlainToolButton(this); |
| 27 | m_btnClear->setIcon(qApp->icons()->fromTheme(QSL("list-remove"))); |
| 28 | m_btnClear->setFocusPolicy(Qt::FocusPolicy::NoFocus); |
| 29 | m_btnClear->setToolTip(tr("Clear current shortcut.")); |
| 30 | |
| 31 | // Clear main shortcut catching button. |
| 32 | m_shortcutBox = new QKeySequenceEdit(this); |
| 33 | m_shortcutBox->setFocusPolicy(Qt::FocusPolicy::StrongFocus); |
| 34 | m_shortcutBox->setMinimumWidth(170); |
| 35 | m_shortcutBox->setToolTip(tr("Click and hit new shortcut.")); |
| 36 | |
| 37 | // Add all buttons to the layout. |
| 38 | m_layout->addWidget(m_shortcutBox); |
| 39 | m_layout->addWidget(m_btnReset); |
| 40 | m_layout->addWidget(m_btnClear); |
| 41 | |
| 42 | // Establish needed connections. |
| 43 | connect(m_btnReset, &QToolButton::clicked, this, &ShortcutCatcher::resetShortcut); |
| 44 | connect(m_btnClear, &QToolButton::clicked, this, &ShortcutCatcher::clearShortcut); |
| 45 | connect(m_shortcutBox, &QKeySequenceEdit::keySequenceChanged, this, &ShortcutCatcher::shortcutChanged); |
| 46 | } |
| 47 | |
| 48 | QKeySequence ShortcutCatcher::shortcut() const { |
| 49 | return m_shortcutBox->keySequence(); |
| 50 | } |
| 51 | |
| 52 | void ShortcutCatcher::setDefaultShortcut(const QKeySequence& key) { |
| 53 | m_defaultSequence = key; |
| 54 | setShortcut(key); |
| 55 | } |
| 56 | |
| 57 | void ShortcutCatcher::setShortcut(const QKeySequence& key) { |
| 58 | m_shortcutBox->setKeySequence(key); |
| 59 | } |
| 60 | |
| 61 | void ShortcutCatcher::resetShortcut() { |
| 62 | setShortcut(m_defaultSequence); |
| 63 | } |
| 64 | |
| 65 | void ShortcutCatcher::clearShortcut() { |
| 66 | setShortcut(QKeySequence()); |
| 67 | } |
| 68 | |
| 69 | QAction* ShortcutCatcher::action() const { |
| 70 | return m_action; |
| 71 | } |
| 72 | |
| 73 | void ShortcutCatcher::setAction(QAction* act) { |
| 74 | m_action = act; |
| 75 | } |