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

KDEUI

  • 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
34class KShortcutPrivate
35{
36public:
37 KShortcutPrivate() {}
38
39 QKeySequence primary;
40 QKeySequence alternate;
41};
42
43
44KShortcut::KShortcut()
45 : d(new KShortcutPrivate)
46{
47 qRegisterMetaType<KShortcut>();
48}
49
50KShortcut::KShortcut(const QKeySequence &primary)
51 : d(new KShortcutPrivate)
52{
53 qRegisterMetaType<KShortcut>();
54 d->primary = primary;
55}
56
57KShortcut::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
65KShortcut::KShortcut(int keyQtPri, int keyQtAlt)
66 : d(new KShortcutPrivate)
67{
68 qRegisterMetaType<KShortcut>();
69 d->primary = keyQtPri;
70 d->alternate = keyQtAlt;
71}
72
73KShortcut::KShortcut(const KShortcut &other)
74 : d(new KShortcutPrivate)
75{
76 d->primary = other.d->primary;
77 d->alternate = other.d->alternate;
78}
79
80KShortcut::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
90KShortcut::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
129KShortcut::~KShortcut()
130{
131 delete d;
132}
133
134QKeySequence KShortcut::primary() const
135{
136 return d->primary;
137}
138
139QKeySequence KShortcut::alternate() const
140{
141 return d->alternate;
142}
143
144bool KShortcut::isEmpty() const
145{
146 return d->primary.isEmpty() && d->alternate.isEmpty();
147}
148
149bool KShortcut::contains(const QKeySequence &needle) const
150{
151 if (needle.isEmpty())
152 return false;
153 return d->primary == needle || d->alternate == needle;
154}
155
156bool 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
184void KShortcut::setPrimary(const QKeySequence &newPrimary)
185{
186 d->primary = newPrimary;
187}
188
189void KShortcut::setAlternate(const QKeySequence &newAlternate)
190{
191 d->alternate = newAlternate;
192}
193
194void 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
211KShortcut &KShortcut::operator=(const KShortcut &other)
212{
213 d->primary = other.d->primary;
214 d->alternate = other.d->alternate;
215 return (*this);
216}
217
218bool KShortcut::operator==(const KShortcut &other) const
219{
220 return (d->primary == other.d->primary && d->alternate == other.d->alternate);
221}
222
223bool KShortcut::operator!=(const KShortcut &other) const
224{
225 return !operator==(other);
226}
227
228KShortcut::operator QList<QKeySequence>() const
229{
230 return toList(RemoveEmpty);
231}
232
233QList<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
249QString KShortcut::toString() const
250{
251 return toString(QKeySequence::PortableText);
252}
253
254QString 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
265KShortcut::operator QVariant() const
266{
267 return qVariantFromValue(*this);
268}
KShortcut
Represents a keyboard shortcut.
Definition: kshortcut.h:58
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::contains
bool contains(const QKeySequence &needle) const
Returns whether at least one of the key sequences is equal to needle.
Definition: kshortcut.cpp:149
KShortcut::toList
QList< QKeySequence > toList(enum EmptyHandling handleEmpty=RemoveEmpty) const
The same as operator QList<QKeySequence>() If handleEmpty equals RemoveEmpty, empty key sequences wil...
Definition: kshortcut.cpp:233
KShortcut::toString
QString toString() const
Returns a description of the shortcut as a semicolon-separated list of key sequences,...
Definition: kshortcut.cpp:249
KShortcut::primary
QKeySequence primary() const
Returns the primary key sequence of this shortcut.
Definition: kshortcut.cpp:134
KShortcut::EmptyHandling
EmptyHandling
An enum about the behavior of operations that treat a KShortcut like a list of QKeySequences.
Definition: kshortcut.h:63
KShortcut::KeepEmpty
@ KeepEmpty
if a shortcut is or becomes empty, let it stay as a placeholder
Definition: kshortcut.h:65
KShortcut::RemoveEmpty
@ RemoveEmpty
remove empty QKeySequences, possibly changing the positions of QKeySequences due to the ensuing reshu...
Definition: kshortcut.h:67
KShortcut::setAlternate
void setAlternate(const QKeySequence &keySeq)
Set the alternate key sequence of this shortcut to the given key sequence.
Definition: kshortcut.cpp:189
KShortcut::operator=
KShortcut & operator=(const KShortcut &other)
Assignment operator.
Definition: kshortcut.cpp:211
KShortcut::remove
void remove(const QKeySequence &keySeq, enum EmptyHandling handleEmpty=RemoveEmpty)
Remove keySeq from this shortcut.
Definition: kshortcut.cpp:194
KShortcut::alternate
QKeySequence alternate() const
Returns the alternate key sequence of this shortcut.
Definition: kshortcut.cpp:139
KShortcut::operator!=
bool operator!=(const KShortcut &other) const
Definition: kshortcut.cpp:223
KShortcut::operator==
bool operator==(const KShortcut &other) const
Definition: kshortcut.cpp:218
KShortcut::isEmpty
bool isEmpty() const
Returns whether this shortcut contains any nonempty key sequences.
Definition: kshortcut.cpp:144
KShortcut::KShortcut
KShortcut()
Creates a new empty shortcut.
Definition: kshortcut.cpp:44
KShortcut::~KShortcut
~KShortcut()
Destructor.
Definition: kshortcut.cpp:129
KShortcut::conflictsWith
bool conflictsWith(const QKeySequence &needle) const
Returns whether at least one of the key sequences conflicts witho needle.
Definition: kshortcut.cpp:156
QList
kDebug
#define kDebug
kWarning
#define kWarning
kdebug.h
kglobal.h
klocale.h
kshortcut.h
Defines platform-independent classes for keyboard shortcut handling.
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 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-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 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
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