1 /*
2     This file is part of the KDE libraries
3     SPDX-FileCopyrightText: 2007 Andreas Hartmetz <ahartmetz@gmail.com>
4 
5     SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7 
8 #include "kshortcutwidget.h"
9 #include "ui_kshortcutwidget.h"
10 
11 class KShortcutWidgetPrivate
12 {
13 public:
KShortcutWidgetPrivate(KShortcutWidget * qq)14     KShortcutWidgetPrivate(KShortcutWidget *qq)
15         : q(qq)
16     {
17     }
18 
19     // private slots
20     void priKeySequenceChanged(const QKeySequence &);
21     void altKeySequenceChanged(const QKeySequence &);
22 
23     // members
24     KShortcutWidget *const q;
25     Ui::KShortcutWidget ui;
26     QList<QKeySequence> cut;
27     bool holdChangedSignal;
28 };
29 
KShortcutWidget(QWidget * parent)30 KShortcutWidget::KShortcutWidget(QWidget *parent)
31     : QWidget(parent)
32     , d(new KShortcutWidgetPrivate(this))
33 {
34     d->holdChangedSignal = false;
35     d->ui.setupUi(this);
36     connect(d->ui.priEditor, &KKeySequenceWidget::keySequenceChanged, this, [this](const QKeySequence &keyseq) {
37         d->priKeySequenceChanged(keyseq);
38     });
39     connect(d->ui.altEditor, &KKeySequenceWidget::keySequenceChanged, this, [this](const QKeySequence &keyseq) {
40         d->altKeySequenceChanged(keyseq);
41     });
42 }
43 
44 KShortcutWidget::~KShortcutWidget() = default;
45 
setModifierlessAllowed(bool allow)46 void KShortcutWidget::setModifierlessAllowed(bool allow)
47 {
48     d->ui.priEditor->setModifierlessAllowed(allow);
49     d->ui.altEditor->setModifierlessAllowed(allow);
50 }
51 
isModifierlessAllowed()52 bool KShortcutWidget::isModifierlessAllowed()
53 {
54     return d->ui.priEditor->isModifierlessAllowed();
55 }
56 
setClearButtonsShown(bool show)57 void KShortcutWidget::setClearButtonsShown(bool show)
58 {
59     d->ui.priEditor->setClearButtonShown(show);
60     d->ui.altEditor->setClearButtonShown(show);
61 }
62 
shortcut() const63 QList<QKeySequence> KShortcutWidget::shortcut() const
64 {
65     QList<QKeySequence> ret;
66     ret << d->ui.priEditor->keySequence() << d->ui.altEditor->keySequence();
67     return ret;
68 }
69 
70 #if KXMLGUI_BUILD_DEPRECATED_SINCE(4, 1)
setCheckActionList(const QList<QAction * > & checkList)71 void KShortcutWidget::setCheckActionList(const QList<QAction *> &checkList)
72 {
73     d->ui.priEditor->setCheckActionList(checkList);
74     d->ui.altEditor->setCheckActionList(checkList);
75 }
76 #endif
77 
setCheckActionCollections(const QList<KActionCollection * > & actionCollections)78 void KShortcutWidget::setCheckActionCollections(const QList<KActionCollection *> &actionCollections)
79 {
80     d->ui.priEditor->setCheckActionCollections(actionCollections);
81     d->ui.altEditor->setCheckActionCollections(actionCollections);
82 }
83 
84 // slot
applyStealShortcut()85 void KShortcutWidget::applyStealShortcut()
86 {
87     d->ui.priEditor->applyStealShortcut();
88     d->ui.altEditor->applyStealShortcut();
89 }
90 
91 // slot
setShortcut(const QList<QKeySequence> & newSc)92 void KShortcutWidget::setShortcut(const QList<QKeySequence> &newSc)
93 {
94     if (newSc == d->cut) {
95         return;
96     }
97 
98     d->holdChangedSignal = true;
99 
100     if (!newSc.isEmpty()) {
101         d->ui.priEditor->setKeySequence(newSc.first());
102     }
103 
104     if (newSc.size() > 1) {
105         d->ui.altEditor->setKeySequence(newSc.at(1));
106     }
107 
108     d->holdChangedSignal = false;
109 
110     Q_EMIT shortcutChanged(d->cut);
111 }
112 
113 // slot
clearShortcut()114 void KShortcutWidget::clearShortcut()
115 {
116     setShortcut(QList<QKeySequence>());
117 }
118 
119 // private slot
priKeySequenceChanged(const QKeySequence & seq)120 void KShortcutWidgetPrivate::priKeySequenceChanged(const QKeySequence &seq)
121 {
122     if (cut.isEmpty()) {
123         cut << seq;
124     } else {
125         cut[0] = seq;
126     }
127 
128     if (!holdChangedSignal) {
129         Q_EMIT q->shortcutChanged(cut);
130     }
131 }
132 
133 // private slot
altKeySequenceChanged(const QKeySequence & seq)134 void KShortcutWidgetPrivate::altKeySequenceChanged(const QKeySequence &seq)
135 {
136     if (cut.size() <= 1) {
137         cut << seq;
138     } else {
139         cut[1] = seq;
140     }
141 
142     if (!holdChangedSignal) {
143         Q_EMIT q->shortcutChanged(cut);
144     }
145 }
146 
147 #include "moc_kshortcutwidget.cpp"
148