File indexing completed on 2024-05-26 04:31:48
0001 /* This file is part of the KDE libraries SPDX-FileCopyrightText: 1998 Mark Donohoe <donohoe@kde.org> 0002 SPDX-FileCopyrightText: 1997 Nicolas Hadacek <hadacek@kde.org> 0003 SPDX-FileCopyrightText: 1998 Matthias Ettrich <ettrich@kde.org> 0004 SPDX-FileCopyrightText: 2001 Ellis Whitehead <ellis@kde.org> 0005 SPDX-FileCopyrightText: 2006 Hamish Rodda <rodda@kde.org> 0006 SPDX-FileCopyrightText: 2007 Roberto Raggi <roberto@kdevelop.org> 0007 SPDX-FileCopyrightText: 2007 Andreas Hartmetz <ahartmetz@gmail.com> 0008 0009 SPDX-License-Identifier: LGPL-2.0-or-later 0010 */ 0011 0012 #include "KisShortcutsDialog_p.h" 0013 0014 #include <QPainter> 0015 #include <QPen> 0016 #include <QGridLayout> 0017 #include <QRadioButton> 0018 #include <QLabel> 0019 #include <QApplication> 0020 0021 #include <klocalizedstring.h> 0022 //#include <kglobalaccel.h> 0023 0024 #include "kkeysequencewidget.h" 0025 0026 void ShortcutEditWidget::paintEvent(QPaintEvent *e) 0027 { 0028 QWidget::paintEvent(e); 0029 QPainter p(this); 0030 QPen pen(QPalette().highlight().color()); 0031 pen.setWidth(6); 0032 p.setPen(pen); 0033 p.drawLine(0, 0, width(), 0); 0034 if (qApp->isLeftToRight()) { 0035 p.drawLine(0, 0, 0, height()); 0036 } else { 0037 p.drawLine(width(), 0, width(), height()); 0038 } 0039 } 0040 0041 ShortcutEditWidget::ShortcutEditWidget(QWidget *viewport, const QKeySequence &defaultSeq, 0042 const QKeySequence &activeSeq, bool allowLetterShortcuts) 0043 : QWidget(viewport), 0044 m_defaultKeySequence(defaultSeq), 0045 m_isUpdating(false), 0046 m_action(0) 0047 { 0048 QGridLayout *layout = new QGridLayout(this); 0049 0050 m_defaultRadio = new QRadioButton(i18n("Default:"), this); 0051 m_defaultLabel = new QLabel(i18nc("No shortcut defined", "None"), this); 0052 QString defaultText = defaultSeq.toString(QKeySequence::NativeText); 0053 if (defaultText.isEmpty()) { 0054 defaultText = i18nc("No shortcut defined", "None"); 0055 } 0056 m_defaultLabel->setText(defaultText); 0057 0058 m_customRadio = new QRadioButton(i18n("Custom:"), this); 0059 m_customEditor = new KisKKeySequenceWidget(this); 0060 m_customEditor->setModifierlessAllowed(allowLetterShortcuts); 0061 0062 layout->addWidget(m_defaultRadio, 0, 0); 0063 layout->addWidget(m_defaultLabel, 0, 1); 0064 layout->addWidget(m_customRadio, 1, 0); 0065 layout->addWidget(m_customEditor, 1, 1); 0066 layout->setColumnStretch(2, 1); 0067 0068 setKeySequence(activeSeq); 0069 0070 connect(m_defaultRadio, SIGNAL(toggled(bool)), 0071 this, SLOT(defaultToggled(bool))); 0072 connect(m_customEditor, SIGNAL(keySequenceChanged(QKeySequence)), 0073 this, SLOT(setCustom(QKeySequence))); 0074 connect(m_customEditor, SIGNAL(stealShortcut(QKeySequence,QAction*)), 0075 this, SIGNAL(stealShortcut(QKeySequence,QAction*))); 0076 } 0077 0078 KisKKeySequenceWidget::ShortcutTypes ShortcutEditWidget::checkForConflictsAgainst() const 0079 { 0080 return m_customEditor->checkForConflictsAgainst(); 0081 } 0082 0083 //slot 0084 void ShortcutEditWidget::defaultToggled(bool checked) 0085 { 0086 if (m_isUpdating) { 0087 return; 0088 } 0089 0090 m_isUpdating = true; 0091 if (checked) { 0092 // The default key sequence should be activated. We check first if this 0093 // is possible. 0094 if (m_customEditor->isKeySequenceAvailable(m_defaultKeySequence)) { 0095 // Clear the customs widget 0096 m_customEditor->clearKeySequence(); 0097 emit keySequenceChanged(m_defaultKeySequence); 0098 } else { 0099 // We tried to switch to the default key sequence and failed. 0100 // Go back. 0101 m_customRadio->setChecked(true); 0102 } 0103 } else { 0104 // The empty key sequence is always valid 0105 emit keySequenceChanged(QKeySequence()); 0106 } 0107 m_isUpdating = false; 0108 } 0109 0110 void ShortcutEditWidget::setCheckActionCollections( 0111 const QList<KisKActionCollection *> checkActionCollections) 0112 { 0113 // We just forward them to out KisKKeySequenceWidget. 0114 m_customEditor->setCheckActionCollections(checkActionCollections); 0115 } 0116 0117 void ShortcutEditWidget::setCheckForConflictsAgainst(KisKKeySequenceWidget::ShortcutTypes types) 0118 { 0119 m_customEditor->setCheckForConflictsAgainst(types); 0120 } 0121 0122 void ShortcutEditWidget::setComponentName(const QString componentName) 0123 { 0124 m_customEditor->setComponentName(componentName); 0125 } 0126 0127 void ShortcutEditWidget::setMultiKeyShortcutsAllowed(bool allowed) 0128 { 0129 // We just forward them to out KisKKeySequenceWidget. 0130 m_customEditor->setMultiKeyShortcutsAllowed(allowed); 0131 } 0132 0133 bool ShortcutEditWidget::multiKeyShortcutsAllowed() const 0134 { 0135 return m_customEditor->multiKeyShortcutsAllowed(); 0136 } 0137 0138 void ShortcutEditWidget::setAction(QObject *action) 0139 { 0140 m_action = action; 0141 } 0142 0143 //slot 0144 void ShortcutEditWidget::setCustom(const QKeySequence &seq) 0145 { 0146 if (m_isUpdating) { 0147 return; 0148 } 0149 0150 // seq is a const reference to a private variable of KisKKeySequenceWidget. 0151 // Somewhere below we possible change that one. But we want to emit seq 0152 // whatever happens. So we make a copy. 0153 QKeySequence original = seq; 0154 0155 m_isUpdating = true; 0156 0157 // Check if the user typed in the default sequence into the custom field. 0158 // We do this by calling setKeySequence which will do the right thing. 0159 setKeySequence(original); 0160 0161 emit keySequenceChanged(original); 0162 m_isUpdating = false; 0163 } 0164 0165 void ShortcutEditWidget::setKeySequence(const QKeySequence &activeSeq) 0166 { 0167 if (activeSeq.toString(QKeySequence::NativeText) == m_defaultKeySequence.toString(QKeySequence::NativeText)) { 0168 m_defaultRadio->setChecked(true); 0169 m_customEditor->clearKeySequence(); 0170 } else { 0171 m_customRadio->setChecked(true); 0172 // m_customEditor->setKeySequence does some stuff we only want to 0173 // execute when the sequence really changes. 0174 if (activeSeq != m_customEditor->keySequence()) { 0175 m_customEditor->setKeySequence(activeSeq); 0176 } 0177 } 0178 } 0179