1
/****************************************************************************
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
5
** This file is part of the gui module of the Qt Toolkit.
7
** This file may be distributed under the terms of the Q Public License
8
** as defined by Trolltech AS of Norway and appearing in the file
9
** LICENSE.QPL included in the packaging of this file.
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 as published by the Free Software
13
** Foundation and appearing in the file LICENSE.GPL included in the
14
** packaging of this file.
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
17
** information about Qt Commercial License Agreements.
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
21
** Contact info@trolltech.com if any conditions of this licensing are
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27
****************************************************************************/
29
#include "qshortcut.h"
30
#include "private/qwidget_p.h"
33
#include <qwhatsthis.h>
35
#include <qapplication.h>
36
#include <private/qapplication_p.h>
37
#include <private/qshortcutmap_p.h>
41
\brief The QShortcut class is used to create keyboard shortcuts.
46
The QShortcut class provides a way of connecting keyboard
47
shortcuts to Qt's \l{signals and slots} mechanism, so that
48
objects can be informed when a shortcut is executed. The shortcut
49
can be set up to contain all the key presses necessary to
50
describe a keyboard shortcut, including the states of modifier
51
keys such as \gui Shift, \gui Ctrl, and \gui Alt.
53
For applications that use menus, it may be more convenient to
54
use the convenience functions provided in the QMenu class to
55
assign keyboard shortcuts to menu items as they are created.
56
Alternatively, shortcuts may be associated with other types of
57
actions in the QAction class.
59
The simplest way to create a shortcut for a particular widget is
60
to construct the shortcut with a key sequence. For example:
63
shortcut = QShortcut(QKeySequence(tr("Ctrl+O", "File|Open")),
67
When the user types the \l{QKeySequence}{key sequence}
68
for a given shortcut, the shortcut's activated() signal is
69
emitted. (In the case of ambiguity, the activatedAmbiguously()
70
signal is emitted.) A shortcut is "listened for" by Qt's event
71
loop when the shortcut's parent widget is receiving events.
73
A shortcut's key sequence can be set with setKey() and retrieved
74
with key(). A shortcut can be enabled or disabled with
75
setEnabled(), and can have "What's This?" help text set with
78
\sa QShortcutEvent, QKeySequence, QAction
82
\fn QWidget *QShortcut::parentWidget() const
84
Returns the shortcut's parent widget.
88
\fn void QShortcut::activated()
90
This signal is emitted when the user types the shortcut's key
93
\sa activatedAmbiguously()
97
\fn void QShortcut::activatedAmbiguously()
99
This signal is emitted when the user types a shortcut key
100
sequence that is ambiguous. For example, if one key sequence is a
101
"prefix" for another and the user types these keys it isn't clear
102
if they want the shorter key sequence, or if they're about to type
103
more to complete the longer key sequence.
110
Private data accessed through d-pointer.
112
class QShortcutPrivate : public QObjectPrivate
114
Q_DECLARE_PUBLIC(QShortcut)
116
QShortcutPrivate() : sc_context(Qt::WindowShortcut), sc_enabled(true), sc_id(0) {}
117
QKeySequence sc_sequence;
118
Qt::ShortcutContext sc_context;
121
QString sc_whatsthis;
122
void redoGrab(QShortcutMap &map);
125
void QShortcutPrivate::redoGrab(QShortcutMap &map)
128
QWidget *parent = q->parentWidget();
130
qWarning("QShortcut: no widget parent defined");
135
map.removeShortcut(sc_id, q);
136
if (sc_sequence.isEmpty())
138
sc_id = map.addShortcut(q, sc_sequence, sc_context);
140
map.setShortcutEnabled(false, sc_id, q);
144
Constructs a QShortcut object for the \a parent widget. Since no
145
shortcut key sequence is specified, the shortcut will not emit any
150
QShortcut::QShortcut(QWidget *parent)
151
: QObject(*new QShortcutPrivate, parent)
153
Q_ASSERT(parent != 0);
157
Constructs a QShortcut object for the \a parent widget. The shortcut
158
operates on its parent, listening for \l{QShortcutEvent}s that
159
match the \a key sequence. Depending on the ambiguity of the
160
event, the shortcut will call the \a member function, or the \a
161
ambiguousMember function, if the key press was in the shortcut's
164
QShortcut::QShortcut(const QKeySequence &key, QWidget *parent,
165
const char *member, const char *ambiguousMember,
166
Qt::ShortcutContext context)
167
: QObject(*new QShortcutPrivate, parent)
170
Q_ASSERT(parent != 0);
171
d->sc_context = context;
172
d->sc_sequence = key;
173
d->redoGrab(qApp->d_func()->shortcutMap);
175
connect(this, SIGNAL(activated()), parent, member);
177
connect(this, SIGNAL(activatedAmbiguously()), parent, ambiguousMember);
181
Destroys the shortcut.
183
QShortcut::~QShortcut()
187
qApp->d_func()->shortcutMap.removeShortcut(d->sc_id, this);
191
\property QShortcut::key
192
\brief the shortcut's key sequence
194
This is a key sequence with an optional combination of Shift, Ctrl,
195
and Alt. The key sequence may be supplied in a number of ways:
198
setKey(0); // no signal emitted
199
setKey(QKeySequence()); // no signal emitted
200
setKey(0x3b1); // Greek letter alpha
201
setKey(Qt::Key_D); // 'd', e.g. to delete
202
setKey('q'); // 'q', e.g. to quit
203
setKey(Qt::CTRL + Qt::Key_P); // Ctrl+P, e.g. to print document
204
setKey("Ctrl+P"); // Ctrl+P, e.g. to print document
207
void QShortcut::setKey(const QKeySequence &key)
210
if (d->sc_sequence == key)
212
d->sc_sequence = key;
213
d->redoGrab(qApp->d_func()->shortcutMap);
216
QKeySequence QShortcut::key() const
218
Q_D(const QShortcut);
219
return d->sc_sequence;
223
\property QShortcut::enabled
224
\brief whether the shortcut is enabled
226
An enabled shortcut emits the activated() or activatedAmbiguously()
227
signal when a QShortcutEvent occurs that matches the shortcut's
230
If the application is in \c WhatsThis mode the shortcut will not emit
231
the signals, but will show the "What's This?" text instead.
235
void QShortcut::setEnabled(bool enable)
238
if (d->sc_enabled == enable)
240
d->sc_enabled = enable;
241
qApp->d_func()->shortcutMap.setShortcutEnabled(enable, d->sc_id, this);
244
bool QShortcut::isEnabled() const
246
Q_D(const QShortcut);
247
return d->sc_enabled;
251
\property QShortcut::context
252
\brief the context in which the shortcut is valid
254
A shortcut's context decides in which circumstances a shortcut is
255
allowed to be triggered. The normal context is Qt::WindowShortcut,
256
which allows the shortcut to trigger if the parent (the widget
257
containing the shortcut) is a subwidget of the active top-level
260
void QShortcut::setContext(Qt::ShortcutContext context)
263
if(d->sc_context == context)
265
d->sc_context = context;
266
d->redoGrab(qApp->d_func()->shortcutMap);
269
Qt::ShortcutContext QShortcut::context()
272
return d->sc_context;
276
\property QShortcut::whatsThis
277
\brief the shortcut's "What's This?" help text
279
The text will be shown when the application is in "What's
280
This?" mode and the user types the shortcut key() sequence.
282
To set "What's This?" help on a menu item (with or without a
283
shortcut key), set the help on the item's action.
285
\sa QWhatsThis::inWhatsThisMode(), QAction::setWhatsThis()
287
void QShortcut::setWhatsThis(const QString &text)
290
d->sc_whatsthis = text;
293
QString QShortcut::whatsThis() const
295
Q_D(const QShortcut);
296
return d->sc_whatsthis;
300
Returns the shortcut's ID.
302
\sa QShortcutEvent::shortcutId()
304
int QShortcut::id() const
306
Q_D(const QShortcut);
313
bool QShortcut::event(QEvent *e)
316
bool handled = false;
317
if (d->sc_enabled && e->type() == QEvent::Shortcut) {
318
QShortcutEvent *se = static_cast<QShortcutEvent *>(e);
319
if (se->shortcutId() == d->sc_id && se->key() == d->sc_sequence){
320
#ifndef QT_NO_WHATSTHIS
321
if (QWhatsThis::inWhatsThisMode()) {
322
QWhatsThis::showText(QCursor::pos(), d->sc_whatsthis);
326
if (se->isAmbiguous())
327
emit activatedAmbiguously();