KDEUI
kshortcuteditwidget.cpp
Go to the documentation of this file.
00001 /* This file is part of the KDE libraries Copyright (C) 1998 Mark Donohoe <donohoe@kde.org> 00002 Copyright (C) 1997 Nicolas Hadacek <hadacek@kde.org> 00003 Copyright (C) 1998 Matthias Ettrich <ettrich@kde.org> 00004 Copyright (C) 2001 Ellis Whitehead <ellis@kde.org> 00005 Copyright (C) 2006 Hamish Rodda <rodda@kde.org> 00006 Copyright (C) 2007 Roberto Raggi <roberto@kdevelop.org> 00007 Copyright (C) 2007 Andreas Hartmetz <ahartmetz@gmail.com> 00008 00009 This library is free software; you can redistribute it and/or 00010 modify it under the terms of the GNU Library General Public 00011 License as published by the Free Software Foundation; either 00012 version 2 of the License, or (at your option) any later version. 00013 00014 This library is distributed in the hope that it will be useful, 00015 but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00017 Library General Public License for more details. 00018 00019 You should have received a copy of the GNU Library General Public License 00020 along with this library; see the file COPYING.LIB. If not, write to 00021 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00022 Boston, MA 02110-1301, USA. 00023 */ 00024 00025 #include "kshortcutsdialog_p.h" 00026 00027 #include <QPainter> 00028 #include <QPen> 00029 #include <QGridLayout> 00030 #include <QRadioButton> 00031 #include <QLabel> 00032 00033 #include "kkeysequencewidget.h" 00034 #include "klocale.h" 00035 00036 void TabConnectedWidget::paintEvent(QPaintEvent *e) 00037 { 00038 QWidget::paintEvent(e); 00039 QPainter p(this); 00040 QPen pen(QPalette().highlight().color()); 00041 pen.setWidth(6); 00042 p.setPen(pen); 00043 p.drawLine(0, 0, width(), 0); 00044 if (qApp->isLeftToRight()) { 00045 p.drawLine(0, 0, 0, height()); 00046 } else { 00047 p.drawLine(width(), 0, width(), height()); 00048 } 00049 } 00050 00051 ShortcutEditWidget::ShortcutEditWidget(QWidget *viewport, const QKeySequence &defaultSeq, 00052 const QKeySequence &activeSeq, bool allowLetterShortcuts) 00053 : TabConnectedWidget(viewport), 00054 m_defaultKeySequence(defaultSeq), 00055 m_isUpdating(false) 00056 { 00057 QGridLayout *layout = new QGridLayout(this); 00058 00059 m_defaultRadio = new QRadioButton(i18n("Default:"), this); 00060 m_defaultLabel = new QLabel(i18nc("No shortcut defined", "None"), this); 00061 QString defaultText = defaultSeq.toString(QKeySequence::NativeText); 00062 if (defaultText.isEmpty()) 00063 defaultText = i18nc("No shortcut defined", "None"); 00064 m_defaultLabel->setText(defaultText); 00065 00066 m_customRadio = new QRadioButton(i18n("Custom:"), this); 00067 m_customEditor = new KKeySequenceWidget(this); 00068 m_customEditor->setModifierlessAllowed(allowLetterShortcuts); 00069 00070 layout->addWidget(m_defaultRadio, 0, 0); 00071 layout->addWidget(m_defaultLabel, 0, 1); 00072 layout->addWidget(m_customRadio, 1, 0); 00073 layout->addWidget(m_customEditor, 1, 1); 00074 layout->setColumnStretch(2, 1); 00075 00076 setKeySequence(activeSeq); 00077 00078 connect(m_defaultRadio, SIGNAL(toggled(bool)), 00079 this, SLOT(defaultToggled(bool))); 00080 connect(m_customEditor, SIGNAL(keySequenceChanged(const QKeySequence &)), 00081 this, SLOT(setCustom(const QKeySequence &))); 00082 connect(m_customEditor, SIGNAL(stealShortcut(const QKeySequence &, KAction *)), 00083 this, SIGNAL(stealShortcut(const QKeySequence &, KAction *))); 00084 } 00085 00086 00087 KKeySequenceWidget::ShortcutTypes ShortcutEditWidget::checkForConflictsAgainst() const 00088 { 00089 return m_customEditor->checkForConflictsAgainst(); 00090 } 00091 00092 //slot 00093 void ShortcutEditWidget::defaultToggled(bool checked) 00094 { 00095 if (m_isUpdating) 00096 return; 00097 00098 m_isUpdating = true; 00099 if (checked) { 00100 // The default key sequence should be activated. We check first if this is 00101 // possible. 00102 if (m_customEditor->isKeySequenceAvailable(m_defaultKeySequence)) { 00103 // Clear the customs widget 00104 m_customEditor->clearKeySequence(); 00105 emit keySequenceChanged(m_defaultKeySequence); 00106 } else { 00107 // We tried to switch to the default key sequence and failed. Go 00108 // back. 00109 m_customRadio->setChecked(true); 00110 } 00111 } else { 00112 // The empty key sequence is always valid 00113 emit keySequenceChanged(QKeySequence()); 00114 } 00115 m_isUpdating = false; 00116 } 00117 00118 00119 void ShortcutEditWidget::setCheckActionCollections( 00120 const QList<KActionCollection*> checkActionCollections) 00121 { 00122 // We just forward them to out KKeySequenceWidget. 00123 m_customEditor->setCheckActionCollections(checkActionCollections); 00124 } 00125 00126 00127 void ShortcutEditWidget::setCheckForConflictsAgainst(KKeySequenceWidget::ShortcutTypes types) 00128 { 00129 m_customEditor->setCheckForConflictsAgainst(types); 00130 } 00131 00132 00133 void ShortcutEditWidget::setComponentName(const QString componentName) 00134 { 00135 m_customEditor->setComponentName(componentName); 00136 } 00137 00138 00139 void ShortcutEditWidget::setMultiKeyShortcutsAllowed(bool allowed) 00140 { 00141 // We just forward them to out KKeySequenceWidget. 00142 m_customEditor->setMultiKeyShortcutsAllowed(allowed); 00143 } 00144 00145 00146 bool ShortcutEditWidget::multiKeyShortcutsAllowed() const 00147 { 00148 return m_customEditor->multiKeyShortcutsAllowed(); 00149 } 00150 00151 //slot 00152 void ShortcutEditWidget::setCustom(const QKeySequence &seq) 00153 { 00154 if (m_isUpdating) 00155 return; 00156 00157 // seq is a const reference to a private variable of KKeySequenceWidget. 00158 // Somewhere below we possible change that one. But we want to emit seq 00159 // whatever happens. So we make a copy. 00160 QKeySequence original = seq; 00161 00162 m_isUpdating = true; 00163 00164 // Check if the user typed in the default sequence into the custom field. 00165 // We do this by calling setKeySequence which will do the right thing. 00166 setKeySequence(original); 00167 00168 emit keySequenceChanged(original); 00169 m_isUpdating = false; 00170 } 00171 00172 00173 void ShortcutEditWidget::setKeySequence(const QKeySequence &activeSeq) 00174 { 00175 if (activeSeq == m_defaultLabel->text()) { 00176 m_defaultRadio->setChecked(true); 00177 m_customEditor->clearKeySequence(); 00178 } else { 00179 m_customRadio->setChecked(true); 00180 // m_customEditor->setKeySequence does some stuff we only want to 00181 // execute when the sequence really changes. 00182 if (activeSeq!=m_customEditor->keySequence()) { 00183 m_customEditor->setKeySequence(activeSeq); 00184 } 00185 } 00186 } 00187
KDE 4.6 API Reference