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