KDEUI
kshortcut.cpp
Go to the documentation of this file.00001 /* This file is part of the KDE libraries 00002 Copyright (C) 2001,2002 Ellis Whitehead <ellis@kde.org> 00003 Copyright (C) 2006 Hamish Rodda <rodda@kde.org> 00004 Copyright (C) 2006 Andreas Hartmetz <ahartmetz@gmail.com> 00005 00006 This library is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU Library General Public 00008 License as published by the Free Software Foundation; either 00009 version 2 of the License, or (at your option) any later version. 00010 00011 This library is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 Library General Public License for more details. 00015 00016 You should have received a copy of the GNU Library General Public License 00017 along with this library; see the file COPYING.LIB. If not, write to 00018 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00019 Boston, MA 02110-1301, USA. 00020 */ 00021 00022 #include "kshortcut.h" 00023 00024 #include <QtGui/QActionEvent> 00025 #include <QtGui/QKeySequence> 00026 #include <QtCore/QCharRef> 00027 #include <QtCore/QMutableStringListIterator> 00028 00029 #include "kdebug.h" 00030 #include "kglobal.h" 00031 #include "klocale.h" 00032 00033 00034 class KShortcutPrivate 00035 { 00036 public: 00037 KShortcutPrivate() {} 00038 00039 QKeySequence primary; 00040 QKeySequence alternate; 00041 }; 00042 00043 00044 KShortcut::KShortcut() 00045 : d(new KShortcutPrivate) 00046 { 00047 qRegisterMetaType<KShortcut>(); 00048 } 00049 00050 KShortcut::KShortcut(const QKeySequence &primary) 00051 : d(new KShortcutPrivate) 00052 { 00053 qRegisterMetaType<KShortcut>(); 00054 d->primary = primary; 00055 } 00056 00057 KShortcut::KShortcut(const QKeySequence &primary, const QKeySequence &alternate) 00058 : d(new KShortcutPrivate) 00059 { 00060 qRegisterMetaType<KShortcut>(); 00061 d->primary = primary; 00062 d->alternate = alternate; 00063 } 00064 00065 KShortcut::KShortcut(int keyQtPri, int keyQtAlt) 00066 : d(new KShortcutPrivate) 00067 { 00068 qRegisterMetaType<KShortcut>(); 00069 d->primary = keyQtPri; 00070 d->alternate = keyQtAlt; 00071 } 00072 00073 KShortcut::KShortcut(const KShortcut &other) 00074 : d(new KShortcutPrivate) 00075 { 00076 d->primary = other.d->primary; 00077 d->alternate = other.d->alternate; 00078 } 00079 00080 KShortcut::KShortcut(const QList<QKeySequence> &seqs) 00081 : d(new KShortcutPrivate) 00082 { 00083 qRegisterMetaType<KShortcut>(); 00084 if (seqs.count() >= 1) 00085 d->primary = seqs.at(0); 00086 if (seqs.count() >= 2) 00087 d->alternate = seqs.at(1); 00088 } 00089 00090 KShortcut::KShortcut(const QString &s) 00091 : d(new KShortcutPrivate) 00092 { 00093 qRegisterMetaType<KShortcut>(); 00094 if (s == QLatin1String("none")) 00095 return; 00096 00097 QStringList sCuts = s.split("; "); 00098 if (sCuts.count() > 2) 00099 kWarning() << "asked to store more than two key sequences but can only hold two."; 00100 00101 //TODO: what is the "(default)" thingie used for? 00102 for( int i=0; i < sCuts.count(); i++) 00103 if( sCuts[i].startsWith( "default(" ) ) 00104 sCuts[i] = sCuts[i].mid( 8, sCuts[i].length() - 9 ); 00105 00106 if (sCuts.count() >= 1) { 00107 QString k = sCuts.at(0); 00108 k.replace( "Win+", "Meta+" ); // workaround for KDE3-style shortcuts 00109 d->primary = QKeySequence::fromString(k); 00110 if (d->primary.isEmpty()) { 00111 kDebug(240) << "unusable primary shortcut sequence " << sCuts[0]; 00112 } 00113 } 00114 00115 if (sCuts.count() >= 2) { 00116 QString k = sCuts.at(1); 00117 k.replace( "Win+", "Meta+" ); // workaround for KDE3-style shortcuts 00118 d->alternate = QKeySequence::fromString(k); 00119 if (d->alternate.isEmpty()) { 00120 kDebug(240) << "unusable alternate shortcut sequence " << sCuts[1]; 00121 } 00122 } 00123 } 00124 00125 KShortcut::~KShortcut() 00126 { 00127 delete d; 00128 } 00129 00130 QKeySequence KShortcut::primary() const 00131 { 00132 return d->primary; 00133 } 00134 00135 QKeySequence KShortcut::alternate() const 00136 { 00137 return d->alternate; 00138 } 00139 00140 bool KShortcut::isEmpty() const 00141 { 00142 return d->primary.isEmpty() && d->alternate.isEmpty(); 00143 } 00144 00145 bool KShortcut::contains(const QKeySequence &needle) const 00146 { 00147 if (needle.isEmpty()) 00148 return false; 00149 return d->primary == needle || d->alternate == needle; 00150 } 00151 00152 void KShortcut::setPrimary(const QKeySequence &newPrimary) 00153 { 00154 d->primary = newPrimary; 00155 } 00156 00157 void KShortcut::setAlternate(const QKeySequence &newAlternate) 00158 { 00159 d->alternate = newAlternate; 00160 } 00161 00162 void KShortcut::remove(const QKeySequence &keySeq, enum EmptyHandling handleEmpty) 00163 { 00164 if (keySeq.isEmpty()) 00165 return; 00166 00167 if (d->primary == keySeq) { 00168 if (handleEmpty == KeepEmpty) 00169 d->primary = QKeySequence(); 00170 else { 00171 d->primary = d->alternate; 00172 d->alternate = QKeySequence(); 00173 } 00174 } 00175 if (d->alternate == keySeq) 00176 d->alternate = QKeySequence(); 00177 } 00178 00179 KShortcut &KShortcut::operator=(const KShortcut &other) 00180 { 00181 d->primary = other.d->primary; 00182 d->alternate = other.d->alternate; 00183 return (*this); 00184 } 00185 00186 bool KShortcut::operator==(const KShortcut &other) const 00187 { 00188 return (d->primary == other.d->primary && d->alternate == other.d->alternate); 00189 } 00190 00191 bool KShortcut::operator!=(const KShortcut &other) const 00192 { 00193 return !operator==(other); 00194 } 00195 00196 KShortcut::operator QList<QKeySequence>() const 00197 { 00198 return toList(RemoveEmpty); 00199 } 00200 00201 QList<QKeySequence> KShortcut::toList(enum EmptyHandling handleEmpty) const 00202 { 00203 QList<QKeySequence> ret; 00204 if (handleEmpty == RemoveEmpty) { 00205 if (!d->primary.isEmpty()) 00206 ret.append(d->primary); 00207 if (!d->alternate.isEmpty()) 00208 ret.append(d->alternate); 00209 } else { 00210 ret.append(d->primary); 00211 ret.append(d->alternate); 00212 } 00213 00214 return ret; 00215 } 00216 00217 QString KShortcut::toString() const 00218 { 00219 QString ret; 00220 foreach(const QKeySequence &seq, toList()) { 00221 ret.append(seq.toString()); 00222 ret.append("; "); 00223 } 00224 ret.chop(2); 00225 return ret; 00226 } 00227 00228 KShortcut::operator QVariant() const 00229 { 00230 return qVariantFromValue(*this); 00231 }