• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

KDEUI

  • sources
  • kde-4.14
  • kdelibs
  • kdeui
  • shortcuts
kshortcut.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE libraries
2  Copyright (C) 2001,2002 Ellis Whitehead <ellis@kde.org>
3  Copyright (C) 2006 Hamish Rodda <rodda@kde.org>
4  Copyright (C) 2006 Andreas Hartmetz <ahartmetz@gmail.com>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License as published by the Free Software Foundation; either
9  version 2 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Library General Public License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING.LIB. If not, write to
18  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  Boston, MA 02110-1301, USA.
20 */
21 
22 #include "kshortcut.h"
23 
24 #include <QtGui/QActionEvent>
25 #include <QtGui/QKeySequence>
26 #include <QtCore/QCharRef>
27 #include <QtCore/QMutableStringListIterator>
28 
29 #include "kdebug.h"
30 #include "kglobal.h"
31 #include "klocale.h"
32 
33 
34 class KShortcutPrivate
35 {
36 public:
37  KShortcutPrivate() {}
38 
39  QKeySequence primary;
40  QKeySequence alternate;
41 };
42 
43 
44 KShortcut::KShortcut()
45  : d(new KShortcutPrivate)
46 {
47  qRegisterMetaType<KShortcut>();
48 }
49 
50 KShortcut::KShortcut(const QKeySequence &primary)
51  : d(new KShortcutPrivate)
52 {
53  qRegisterMetaType<KShortcut>();
54  d->primary = primary;
55 }
56 
57 KShortcut::KShortcut(const QKeySequence &primary, const QKeySequence &alternate)
58  : d(new KShortcutPrivate)
59 {
60  qRegisterMetaType<KShortcut>();
61  d->primary = primary;
62  d->alternate = alternate;
63 }
64 
65 KShortcut::KShortcut(int keyQtPri, int keyQtAlt)
66  : d(new KShortcutPrivate)
67 {
68  qRegisterMetaType<KShortcut>();
69  d->primary = keyQtPri;
70  d->alternate = keyQtAlt;
71 }
72 
73 KShortcut::KShortcut(const KShortcut &other)
74  : d(new KShortcutPrivate)
75 {
76  d->primary = other.d->primary;
77  d->alternate = other.d->alternate;
78 }
79 
80 KShortcut::KShortcut(const QList<QKeySequence> &seqs)
81  : d(new KShortcutPrivate)
82 {
83  qRegisterMetaType<KShortcut>();
84  if (seqs.count() >= 1)
85  d->primary = seqs.at(0);
86  if (seqs.count() >= 2)
87  d->alternate = seqs.at(1);
88 }
89 
90 KShortcut::KShortcut(const QString &s)
91  : d(new KShortcutPrivate)
92 {
93  qRegisterMetaType<KShortcut>();
94  if (s == QLatin1String("none"))
95  return;
96 
97  QStringList sCuts = s.split("; ");
98  if (sCuts.count() > 2)
99  kWarning() << "asked to store more than two key sequences but can only hold two.";
100 
101  //TODO: what is the "(default)" thingie used for?
102  for( int i=0; i < sCuts.count(); i++)
103  if( sCuts[i].startsWith( QLatin1String("default(") ) )
104  sCuts[i] = sCuts[i].mid( 8, sCuts[i].length() - 9 );
105 
106  if (sCuts.count() >= 1) {
107  QString k = sCuts.at(0);
108  k.replace( "Win+", "Meta+" ); // workaround for KDE3-style shortcuts
109  k.replace("Plus", "+"); // workaround for KDE3-style "Alt+Plus"
110  k.replace("Minus", "-"); // workaround for KDE3-style "Alt+Plus"
111  d->primary = QKeySequence::fromString(k);
112  // Complain about a unusable shortcuts sequence only if we have got
113  // something.
114  if (d->primary.isEmpty() && !k.isEmpty()) {
115  kDebug(240) << "unusable primary shortcut sequence " << sCuts[0];
116  }
117  }
118 
119  if (sCuts.count() >= 2) {
120  QString k = sCuts.at(1);
121  k.replace( "Win+", "Meta+" ); // workaround for KDE3-style shortcuts
122  d->alternate = QKeySequence::fromString(k);
123  if (d->alternate.isEmpty()) {
124  kDebug(240) << "unusable alternate shortcut sequence " << sCuts[1];
125  }
126  }
127 }
128 
129 KShortcut::~KShortcut()
130 {
131  delete d;
132 }
133 
134 QKeySequence KShortcut::primary() const
135 {
136  return d->primary;
137 }
138 
139 QKeySequence KShortcut::alternate() const
140 {
141  return d->alternate;
142 }
143 
144 bool KShortcut::isEmpty() const
145 {
146  return d->primary.isEmpty() && d->alternate.isEmpty();
147 }
148 
149 bool KShortcut::contains(const QKeySequence &needle) const
150 {
151  if (needle.isEmpty())
152  return false;
153  return d->primary == needle || d->alternate == needle;
154 }
155 
156 bool KShortcut::conflictsWith(const QKeySequence &needle) const
157 {
158  if (needle.isEmpty())
159  return false;
160 
161  bool primaryConflicts = false;
162  bool alternateConflicts = false;
163 
164  if (!d->primary.isEmpty()) {
165  primaryConflicts =
166  ( d->primary.matches(needle) == QKeySequence::NoMatch
167  && needle.matches(d->primary) == QKeySequence::NoMatch )
168  ? false
169  : true;
170  }
171 
172  if (!d->alternate.isEmpty()) {
173  alternateConflicts=
174  ( d->alternate.matches(needle) == QKeySequence::NoMatch
175  && needle.matches(d->alternate) == QKeySequence::NoMatch )
176  ? false
177  : true;
178  }
179 
180  return primaryConflicts || alternateConflicts;
181 }
182 
183 
184 void KShortcut::setPrimary(const QKeySequence &newPrimary)
185 {
186  d->primary = newPrimary;
187 }
188 
189 void KShortcut::setAlternate(const QKeySequence &newAlternate)
190 {
191  d->alternate = newAlternate;
192 }
193 
194 void KShortcut::remove(const QKeySequence &keySeq, enum EmptyHandling handleEmpty)
195 {
196  if (keySeq.isEmpty())
197  return;
198 
199  if (d->primary == keySeq) {
200  if (handleEmpty == KeepEmpty)
201  d->primary = QKeySequence();
202  else {
203  d->primary = d->alternate;
204  d->alternate = QKeySequence();
205  }
206  }
207  if (d->alternate == keySeq)
208  d->alternate = QKeySequence();
209 }
210 
211 KShortcut &KShortcut::operator=(const KShortcut &other)
212 {
213  d->primary = other.d->primary;
214  d->alternate = other.d->alternate;
215  return (*this);
216 }
217 
218 bool KShortcut::operator==(const KShortcut &other) const
219 {
220  return (d->primary == other.d->primary && d->alternate == other.d->alternate);
221 }
222 
223 bool KShortcut::operator!=(const KShortcut &other) const
224 {
225  return !operator==(other);
226 }
227 
228 KShortcut::operator QList<QKeySequence>() const
229 {
230  return toList(RemoveEmpty);
231 }
232 
233 QList<QKeySequence> KShortcut::toList(enum EmptyHandling handleEmpty) const
234 {
235  QList<QKeySequence> ret;
236  if (handleEmpty == RemoveEmpty) {
237  if (!d->primary.isEmpty())
238  ret.append(d->primary);
239  if (!d->alternate.isEmpty())
240  ret.append(d->alternate);
241  } else {
242  ret.append(d->primary);
243  ret.append(d->alternate);
244  }
245 
246  return ret;
247 }
248 
249 QString KShortcut::toString() const
250 {
251  return toString(QKeySequence::PortableText);
252 }
253 
254 QString KShortcut::toString(QKeySequence::SequenceFormat format) const
255 {
256  QString ret;
257  foreach(const QKeySequence &seq, toList()) {
258  ret.append(seq.toString(format));
259  ret.append("; ");
260  }
261  ret.chop(2);
262  return ret;
263 }
264 
265 KShortcut::operator QVariant() const
266 {
267  return qVariantFromValue(*this);
268 }
KShortcut::contains
bool contains(const QKeySequence &needle) const
Returns whether at least one of the key sequences is equal to needle.
Definition: kshortcut.cpp:149
QString::append
QString & append(QChar ch)
KShortcut::KShortcut
KShortcut()
Creates a new empty shortcut.
Definition: kshortcut.cpp:44
KShortcut::RemoveEmpty
remove empty QKeySequences, possibly changing the positions of QKeySequences due to the ensuing reshu...
Definition: kshortcut.h:67
kdebug.h
QString::split
QStringList split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
QList::at
const T & at(int i) const
kshortcut.h
Defines platform-independent classes for keyboard shortcut handling.
kDebug
static QDebug kDebug(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
QString::chop
void chop(int n)
klocale.h
KShortcut::conflictsWith
bool conflictsWith(const QKeySequence &needle) const
Returns whether at least one of the key sequences conflicts witho needle.
Definition: kshortcut.cpp:156
KShortcut
Represents a keyboard shortcut.
Definition: kshortcut.h:57
KShortcut::toList
QList< QKeySequence > toList(enum EmptyHandling handleEmpty=RemoveEmpty) const
The same as operator QList() If handleEmpty equals RemoveEmpty, empty key sequences wil...
Definition: kshortcut.cpp:233
kglobal.h
QList::count
int count(const T &value) const
QList::append
void append(const T &value)
QKeySequence::matches
SequenceMatch matches(const QKeySequence &seq) const
QKeySequence::fromString
QKeySequence fromString(const QString &str, SequenceFormat format)
KShortcut::isEmpty
bool isEmpty() const
Returns whether this shortcut contains any nonempty key sequences.
Definition: kshortcut.cpp:144
KShortcut::operator=
KShortcut & operator=(const KShortcut &other)
Assignment operator.
Definition: kshortcut.cpp:211
QString::isEmpty
bool isEmpty() const
QString
QList
KShortcut::~KShortcut
~KShortcut()
Destructor.
Definition: kshortcut.cpp:129
QStringList
KShortcut::KeepEmpty
if a shortcut is or becomes empty, let it stay as a placeholder
Definition: kshortcut.h:65
KShortcut::alternate
QKeySequence alternate() const
Returns the alternate key sequence of this shortcut.
Definition: kshortcut.cpp:139
KShortcut::remove
void remove(const QKeySequence &keySeq, enum EmptyHandling handleEmpty=RemoveEmpty)
Remove keySeq from this shortcut.
Definition: kshortcut.cpp:194
QKeySequence::isEmpty
bool isEmpty() const
KShortcut::operator==
bool operator==(const KShortcut &other) const
Definition: kshortcut.cpp:218
QString::replace
QString & replace(int position, int n, QChar after)
KShortcut::EmptyHandling
EmptyHandling
An enum about the behavior of operations that treat a KShortcut like a list of QKeySequences.
Definition: kshortcut.h:63
QKeySequence::toString
QString toString(SequenceFormat format) const
QLatin1String
KShortcut::setAlternate
void setAlternate(const QKeySequence &keySeq)
Set the alternate key sequence of this shortcut to the given key sequence.
Definition: kshortcut.cpp:189
QKeySequence
KShortcut::operator!=
bool operator!=(const KShortcut &other) const
Definition: kshortcut.cpp:223
kWarning
static QDebug kWarning(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
KShortcut::setPrimary
void setPrimary(const QKeySequence &keySeq)
Set the primary key sequence of this shortcut to the given key sequence.
Definition: kshortcut.cpp:184
KShortcut::primary
QKeySequence primary() const
Returns the primary key sequence of this shortcut.
Definition: kshortcut.cpp:134
KShortcut::toString
QString toString() const
Returns a description of the shortcut as a semicolon-separated list of key sequences, as returned by QKeySequence::toString().
Definition: kshortcut.cpp:249
QVariant
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:24:00 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal