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
11class KShortcutWidgetPrivate
12{
13public:
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
30KShortcutWidget::KShortcutWidget(QWidget *parent)
31 : QWidget(parent)
32 , d(new KShortcutWidgetPrivate(this))
33{
34 d->holdChangedSignal = false;
35 d->ui.setupUi(this);
36 connect(sender: d->ui.priEditor, signal: &KKeySequenceWidget::keySequenceChanged, context: this, slot: [this](const QKeySequence &keyseq) {
37 d->priKeySequenceChanged(keyseq);
38 });
39 connect(sender: d->ui.altEditor, signal: &KKeySequenceWidget::keySequenceChanged, context: this, slot: [this](const QKeySequence &keyseq) {
40 d->altKeySequenceChanged(keyseq);
41 });
42}
43
44KShortcutWidget::~KShortcutWidget() = default;
45
46void KShortcutWidget::setModifierlessAllowed(bool allow)
47{
48 d->ui.priEditor->setModifierlessAllowed(allow);
49 d->ui.altEditor->setModifierlessAllowed(allow);
50}
51
52bool KShortcutWidget::isModifierlessAllowed()
53{
54 return d->ui.priEditor->isModifierlessAllowed();
55}
56
57void KShortcutWidget::setClearButtonsShown(bool show)
58{
59 d->ui.priEditor->setClearButtonShown(show);
60 d->ui.altEditor->setClearButtonShown(show);
61}
62
63QList<QKeySequence> KShortcutWidget::shortcut() const
64{
65 QList<QKeySequence> ret;
66 ret << d->ui.priEditor->keySequence() << d->ui.altEditor->keySequence();
67 return ret;
68}
69
70void KShortcutWidget::setCheckActionCollections(const QList<KActionCollection *> &actionCollections)
71{
72 d->ui.priEditor->setCheckActionCollections(actionCollections);
73 d->ui.altEditor->setCheckActionCollections(actionCollections);
74}
75
76// slot
77void KShortcutWidget::applyStealShortcut()
78{
79 d->ui.priEditor->applyStealShortcut();
80 d->ui.altEditor->applyStealShortcut();
81}
82
83// slot
84void KShortcutWidget::setShortcut(const QList<QKeySequence> &newSc)
85{
86 if (newSc == d->cut) {
87 return;
88 }
89
90 d->holdChangedSignal = true;
91
92 if (!newSc.isEmpty()) {
93 d->ui.priEditor->setKeySequence(seq: newSc.first());
94 }
95
96 if (newSc.size() > 1) {
97 d->ui.altEditor->setKeySequence(seq: newSc.at(i: 1));
98 }
99
100 d->holdChangedSignal = false;
101
102 Q_EMIT shortcutChanged(cut: d->cut);
103}
104
105// slot
106void KShortcutWidget::clearShortcut()
107{
108 setShortcut(QList<QKeySequence>());
109}
110
111// private slot
112void KShortcutWidgetPrivate::priKeySequenceChanged(const QKeySequence &seq)
113{
114 if (cut.isEmpty()) {
115 cut << seq;
116 } else {
117 cut[0] = seq;
118 }
119
120 if (!holdChangedSignal) {
121 Q_EMIT q->shortcutChanged(cut);
122 }
123}
124
125// private slot
126void KShortcutWidgetPrivate::altKeySequenceChanged(const QKeySequence &seq)
127{
128 if (cut.size() <= 1) {
129 cut << seq;
130 } else {
131 cut[1] = seq;
132 }
133
134 if (!holdChangedSignal) {
135 Q_EMIT q->shortcutChanged(cut);
136 }
137}
138
139#include "moc_kshortcutwidget.cpp"
140

source code of kxmlgui/src/kshortcutwidget.cpp