File indexing completed on 2024-12-22 04:14:08

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