KDEUI
kshortcut.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
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+" );
00109 d->primary = QKeySequence::fromString(k);
00110
00111
00112 if (d->primary.isEmpty() && !k.isEmpty()) {
00113 kDebug(240) << "unusable primary shortcut sequence " << sCuts[0];
00114 }
00115 }
00116
00117 if (sCuts.count() >= 2) {
00118 QString k = sCuts.at(1);
00119 k.replace( "Win+", "Meta+" );
00120 d->alternate = QKeySequence::fromString(k);
00121 if (d->alternate.isEmpty()) {
00122 kDebug(240) << "unusable alternate shortcut sequence " << sCuts[1];
00123 }
00124 }
00125 }
00126
00127 KShortcut::~KShortcut()
00128 {
00129 delete d;
00130 }
00131
00132 QKeySequence KShortcut::primary() const
00133 {
00134 return d->primary;
00135 }
00136
00137 QKeySequence KShortcut::alternate() const
00138 {
00139 return d->alternate;
00140 }
00141
00142 bool KShortcut::isEmpty() const
00143 {
00144 return d->primary.isEmpty() && d->alternate.isEmpty();
00145 }
00146
00147 bool KShortcut::contains(const QKeySequence &needle) const
00148 {
00149 if (needle.isEmpty())
00150 return false;
00151 return d->primary == needle || d->alternate == needle;
00152 }
00153
00154 bool KShortcut::conflictsWith(const QKeySequence &needle) const
00155 {
00156 if (needle.isEmpty())
00157 return false;
00158
00159 bool primaryConflicts = false;
00160 bool alternateConflicts = false;
00161
00162 if (!d->primary.isEmpty()) {
00163 primaryConflicts =
00164 ( d->primary.matches(needle) == QKeySequence::NoMatch
00165 && needle.matches(d->primary) == QKeySequence::NoMatch )
00166 ? false
00167 : true;
00168 }
00169
00170 if (!d->alternate.isEmpty()) {
00171 alternateConflicts=
00172 ( d->alternate.matches(needle) == QKeySequence::NoMatch
00173 && needle.matches(d->alternate) == QKeySequence::NoMatch )
00174 ? false
00175 : true;
00176 }
00177
00178 return primaryConflicts || alternateConflicts;
00179 }
00180
00181
00182 void KShortcut::setPrimary(const QKeySequence &newPrimary)
00183 {
00184 d->primary = newPrimary;
00185 }
00186
00187 void KShortcut::setAlternate(const QKeySequence &newAlternate)
00188 {
00189 d->alternate = newAlternate;
00190 }
00191
00192 void KShortcut::remove(const QKeySequence &keySeq, enum EmptyHandling handleEmpty)
00193 {
00194 if (keySeq.isEmpty())
00195 return;
00196
00197 if (d->primary == keySeq) {
00198 if (handleEmpty == KeepEmpty)
00199 d->primary = QKeySequence();
00200 else {
00201 d->primary = d->alternate;
00202 d->alternate = QKeySequence();
00203 }
00204 }
00205 if (d->alternate == keySeq)
00206 d->alternate = QKeySequence();
00207 }
00208
00209 KShortcut &KShortcut::operator=(const KShortcut &other)
00210 {
00211 d->primary = other.d->primary;
00212 d->alternate = other.d->alternate;
00213 return (*this);
00214 }
00215
00216 bool KShortcut::operator==(const KShortcut &other) const
00217 {
00218 return (d->primary == other.d->primary && d->alternate == other.d->alternate);
00219 }
00220
00221 bool KShortcut::operator!=(const KShortcut &other) const
00222 {
00223 return !operator==(other);
00224 }
00225
00226 KShortcut::operator QList<QKeySequence>() const
00227 {
00228 return toList(RemoveEmpty);
00229 }
00230
00231 QList<QKeySequence> KShortcut::toList(enum EmptyHandling handleEmpty) const
00232 {
00233 QList<QKeySequence> ret;
00234 if (handleEmpty == RemoveEmpty) {
00235 if (!d->primary.isEmpty())
00236 ret.append(d->primary);
00237 if (!d->alternate.isEmpty())
00238 ret.append(d->alternate);
00239 } else {
00240 ret.append(d->primary);
00241 ret.append(d->alternate);
00242 }
00243
00244 return ret;
00245 }
00246
00247 QString KShortcut::toString() const
00248 {
00249 return toString(QKeySequence::PortableText);
00250 }
00251
00252 QString KShortcut::toString(QKeySequence::SequenceFormat format) const
00253 {
00254 QString ret;
00255 foreach(const QKeySequence &seq, toList()) {
00256 ret.append(seq.toString(format));
00257 ret.append("; ");
00258 }
00259 ret.chop(2);
00260 return ret;
00261 }
00262
00263 KShortcut::operator QVariant() const
00264 {
00265 return qVariantFromValue(*this);
00266 }