File indexing completed on 2024-09-29 09:33:15

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 #if KXMLGUI_BUILD_DEPRECATED_SINCE(4, 1)
0071 void KShortcutWidget::setCheckActionList(const QList<QAction *> &checkList)
0072 {
0073     d->ui.priEditor->setCheckActionList(checkList);
0074     d->ui.altEditor->setCheckActionList(checkList);
0075 }
0076 #endif
0077 
0078 void KShortcutWidget::setCheckActionCollections(const QList<KActionCollection *> &actionCollections)
0079 {
0080     d->ui.priEditor->setCheckActionCollections(actionCollections);
0081     d->ui.altEditor->setCheckActionCollections(actionCollections);
0082 }
0083 
0084 // slot
0085 void KShortcutWidget::applyStealShortcut()
0086 {
0087     d->ui.priEditor->applyStealShortcut();
0088     d->ui.altEditor->applyStealShortcut();
0089 }
0090 
0091 // slot
0092 void KShortcutWidget::setShortcut(const QList<QKeySequence> &newSc)
0093 {
0094     if (newSc == d->cut) {
0095         return;
0096     }
0097 
0098     d->holdChangedSignal = true;
0099 
0100     if (!newSc.isEmpty()) {
0101         d->ui.priEditor->setKeySequence(newSc.first());
0102     }
0103 
0104     if (newSc.size() > 1) {
0105         d->ui.altEditor->setKeySequence(newSc.at(1));
0106     }
0107 
0108     d->holdChangedSignal = false;
0109 
0110     Q_EMIT shortcutChanged(d->cut);
0111 }
0112 
0113 // slot
0114 void KShortcutWidget::clearShortcut()
0115 {
0116     setShortcut(QList<QKeySequence>());
0117 }
0118 
0119 // private slot
0120 void KShortcutWidgetPrivate::priKeySequenceChanged(const QKeySequence &seq)
0121 {
0122     if (cut.isEmpty()) {
0123         cut << seq;
0124     } else {
0125         cut[0] = seq;
0126     }
0127 
0128     if (!holdChangedSignal) {
0129         Q_EMIT q->shortcutChanged(cut);
0130     }
0131 }
0132 
0133 // private slot
0134 void KShortcutWidgetPrivate::altKeySequenceChanged(const QKeySequence &seq)
0135 {
0136     if (cut.size() <= 1) {
0137         cut << seq;
0138     } else {
0139         cut[1] = seq;
0140     }
0141 
0142     if (!holdChangedSignal) {
0143         Q_EMIT q->shortcutChanged(cut);
0144     }
0145 }
0146 
0147 #include "moc_kshortcutwidget.cpp"