File indexing completed on 2024-04-14 03:57:12

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2007 Andreas Hartmetz <ahartmetz@gmail.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "kshortcutwidget.h"
0009 #include "ui_kshortcutwidget.h"
0010 
0011 class KShortcutWidgetPrivate
0012 {
0013 public:
0014     KShortcutWidgetPrivate(KShortcutWidget *qq)
0015         : q(qq)
0016     {
0017     }
0018 
0019     // private slots
0020     void priKeySequenceChanged(const QKeySequence &);
0021     void altKeySequenceChanged(const QKeySequence &);
0022 
0023     // members
0024     KShortcutWidget *const q;
0025     Ui::KShortcutWidget ui;
0026     QList<QKeySequence> cut;
0027     bool holdChangedSignal;
0028 };
0029 
0030 KShortcutWidget::KShortcutWidget(QWidget *parent)
0031     : QWidget(parent)
0032     , d(new KShortcutWidgetPrivate(this))
0033 {
0034     d->holdChangedSignal = false;
0035     d->ui.setupUi(this);
0036     connect(d->ui.priEditor, &KKeySequenceWidget::keySequenceChanged, this, [this](const QKeySequence &keyseq) {
0037         d->priKeySequenceChanged(keyseq);
0038     });
0039     connect(d->ui.altEditor, &KKeySequenceWidget::keySequenceChanged, this, [this](const QKeySequence &keyseq) {
0040         d->altKeySequenceChanged(keyseq);
0041     });
0042 }
0043 
0044 KShortcutWidget::~KShortcutWidget() = default;
0045 
0046 void KShortcutWidget::setModifierlessAllowed(bool allow)
0047 {
0048     d->ui.priEditor->setModifierlessAllowed(allow);
0049     d->ui.altEditor->setModifierlessAllowed(allow);
0050 }
0051 
0052 bool KShortcutWidget::isModifierlessAllowed()
0053 {
0054     return d->ui.priEditor->isModifierlessAllowed();
0055 }
0056 
0057 void KShortcutWidget::setClearButtonsShown(bool show)
0058 {
0059     d->ui.priEditor->setClearButtonShown(show);
0060     d->ui.altEditor->setClearButtonShown(show);
0061 }
0062 
0063 QList<QKeySequence> KShortcutWidget::shortcut() const
0064 {
0065     QList<QKeySequence> ret;
0066     ret << d->ui.priEditor->keySequence() << d->ui.altEditor->keySequence();
0067     return ret;
0068 }
0069 
0070 void KShortcutWidget::setCheckActionCollections(const QList<KActionCollection *> &actionCollections)
0071 {
0072     d->ui.priEditor->setCheckActionCollections(actionCollections);
0073     d->ui.altEditor->setCheckActionCollections(actionCollections);
0074 }
0075 
0076 // slot
0077 void KShortcutWidget::applyStealShortcut()
0078 {
0079     d->ui.priEditor->applyStealShortcut();
0080     d->ui.altEditor->applyStealShortcut();
0081 }
0082 
0083 // slot
0084 void KShortcutWidget::setShortcut(const QList<QKeySequence> &newSc)
0085 {
0086     if (newSc == d->cut) {
0087         return;
0088     }
0089 
0090     d->holdChangedSignal = true;
0091 
0092     if (!newSc.isEmpty()) {
0093         d->ui.priEditor->setKeySequence(newSc.first());
0094     }
0095 
0096     if (newSc.size() > 1) {
0097         d->ui.altEditor->setKeySequence(newSc.at(1));
0098     }
0099 
0100     d->holdChangedSignal = false;
0101 
0102     Q_EMIT shortcutChanged(d->cut);
0103 }
0104 
0105 // slot
0106 void KShortcutWidget::clearShortcut()
0107 {
0108     setShortcut(QList<QKeySequence>());
0109 }
0110 
0111 // private slot
0112 void KShortcutWidgetPrivate::priKeySequenceChanged(const QKeySequence &seq)
0113 {
0114     if (cut.isEmpty()) {
0115         cut << seq;
0116     } else {
0117         cut[0] = seq;
0118     }
0119 
0120     if (!holdChangedSignal) {
0121         Q_EMIT q->shortcutChanged(cut);
0122     }
0123 }
0124 
0125 // private slot
0126 void KShortcutWidgetPrivate::altKeySequenceChanged(const QKeySequence &seq)
0127 {
0128     if (cut.size() <= 1) {
0129         cut << seq;
0130     } else {
0131         cut[1] = seq;
0132     }
0133 
0134     if (!holdChangedSignal) {
0135         Q_EMIT q->shortcutChanged(cut);
0136     }
0137 }
0138 
0139 #include "moc_kshortcutwidget.cpp"