1 /* This file is part of the KDE libraries
2     Copyright (C) 2007 Andreas Hartmetz <ahartmetz@gmail.com>
3 
4     This library is free software; you can redistribute it and/or
5     modify it under the terms of the GNU Library General Public
6     License as published by the Free Software Foundation; either
7     version 2 of the License, or (at your option) any later version.
8 
9     This library is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12     Library General Public License for more details.
13 
14     You should have received a copy of the GNU Library General Public License
15     along with this library; see the file COPYING.LIB.  If not, write to
16     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17     Boston, MA 02110-1301, USA.
18 */
19 
20 #include "kshortcutwidget.h"
21 #include "ui_kshortcutwidget.h"
22 
23 class KShortcutWidgetPrivate
24 {
25 public:
KShortcutWidgetPrivate(KShortcutWidget * q)26     KShortcutWidgetPrivate(KShortcutWidget *q) : q(q) {}
27 
28 //private slots
29     void priKeySequenceChanged(const QKeySequence &);
30     void altKeySequenceChanged(const QKeySequence &);
31 
32 //members
33     KShortcutWidget *const q;
34     Ui::KShortcutWidget ui;
35     QList<QKeySequence> cut;
36     bool holdChangedSignal;
37 };
38 
KShortcutWidget(QWidget * parent)39 KShortcutWidget::KShortcutWidget(QWidget *parent)
40     : QWidget(parent),
41       d(new KShortcutWidgetPrivate(this))
42 {
43     d->holdChangedSignal = false;
44     d->ui.setupUi(this);
45     connect(d->ui.priEditor, SIGNAL(keySequenceChanged(QKeySequence)),
46             this, SLOT(priKeySequenceChanged(QKeySequence)));
47     connect(d->ui.altEditor, SIGNAL(keySequenceChanged(QKeySequence)),
48             this, SLOT(altKeySequenceChanged(QKeySequence)));
49 }
50 
~KShortcutWidget()51 KShortcutWidget::~KShortcutWidget()
52 {
53     delete d;
54 }
55 
setModifierlessAllowed(bool allow)56 void KShortcutWidget::setModifierlessAllowed(bool allow)
57 {
58     d->ui.priEditor->setModifierlessAllowed(allow);
59     d->ui.altEditor->setModifierlessAllowed(allow);
60 }
61 
isModifierlessAllowed()62 bool KShortcutWidget::isModifierlessAllowed()
63 {
64     return d->ui.priEditor->isModifierlessAllowed();
65 }
66 
setClearButtonsShown(bool show)67 void KShortcutWidget::setClearButtonsShown(bool show)
68 {
69     d->ui.priEditor->setClearButtonShown(show);
70     d->ui.altEditor->setClearButtonShown(show);
71 }
72 
shortcut() const73 QList<QKeySequence> KShortcutWidget::shortcut() const
74 {
75     QList<QKeySequence> ret;
76     ret << d->ui.priEditor->keySequence()
77         << d->ui.altEditor->keySequence();
78     return ret;
79 }
80 
81 
setCheckActionCollections(const QList<KActionCollection * > & actionCollections)82 void KShortcutWidget::setCheckActionCollections(const QList<KActionCollection *> &actionCollections)
83 {
84     d->ui.priEditor->setCheckActionCollections(actionCollections);
85     d->ui.altEditor->setCheckActionCollections(actionCollections);
86 }
87 
88 //slot
applyStealShortcut()89 void KShortcutWidget::applyStealShortcut()
90 {
91     d->ui.priEditor->applyStealShortcut();
92     d->ui.altEditor->applyStealShortcut();
93 }
94 
95 //slot
setShortcut(const QList<QKeySequence> & newSc)96 void KShortcutWidget::setShortcut(const QList<QKeySequence> &newSc)
97 {
98     if (newSc == d->cut) {
99         return;
100     }
101 
102     d->holdChangedSignal = true;
103 
104     if (!newSc.isEmpty()) {
105         d->ui.priEditor->setKeySequence(newSc.first());
106     }
107 
108     if (newSc.size() > 1) {
109         d->ui.altEditor->setKeySequence(newSc.at(1));
110     }
111 
112     d->holdChangedSignal = false;
113 
114     emit shortcutChanged(d->cut);
115 }
116 
117 //slot
clearShortcut()118 void KShortcutWidget::clearShortcut()
119 {
120     setShortcut(QList<QKeySequence>());
121 }
122 
123 //private slot
priKeySequenceChanged(const QKeySequence & seq)124 void KShortcutWidgetPrivate::priKeySequenceChanged(const QKeySequence &seq)
125 {
126     if (cut.isEmpty()) {
127         cut << seq;
128     } else {
129         cut[0] = seq;
130     }
131 
132     if (!holdChangedSignal) {
133         emit q->shortcutChanged(cut);
134     }
135 }
136 
137 //private slot
altKeySequenceChanged(const QKeySequence & seq)138 void KShortcutWidgetPrivate::altKeySequenceChanged(const QKeySequence &seq)
139 {
140     if (cut.size() <= 1) {
141         cut << seq;
142     } else {
143         cut[1] = seq;
144     }
145 
146     if (!holdChangedSignal) {
147         emit q->shortcutChanged(cut);
148     }
149 }
150 
151 #include "moc_kshortcutwidget.cpp"
152