Qt 4.8
qshortcut.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qshortcut.h"
43 #include "private/qwidget_p.h"
44 
45 #ifndef QT_NO_SHORTCUT
46 #include <qevent.h>
47 #include <qwhatsthis.h>
48 #include <qmenu.h>
49 #include <qapplication.h>
50 #include <private/qapplication_p.h>
51 #include <private/qshortcutmap_p.h>
52 
54 
55 #define QAPP_CHECK(functionName) \
56  if (!qApp) { \
57  qWarning("QShortcut: Initialize QApplication before calling '" functionName "'."); \
58  return; \
59  }
60 
154 /*
155  \internal
156  Private data accessed through d-pointer.
157 */
159 {
161 public:
167  int sc_id;
169  void redoGrab(QShortcutMap &map);
170 };
171 
173 {
174  Q_Q(QShortcut);
175  if (!parent) {
176  qWarning("QShortcut: No widget parent defined");
177  return;
178  }
179 
180  if (sc_id)
181  map.removeShortcut(sc_id, q);
182  if (sc_sequence.isEmpty())
183  return;
185  if (!sc_enabled)
186  map.setShortcutEnabled(false, sc_id, q);
187  if (!sc_autorepeat)
188  map.setShortcutAutoRepeat(false, sc_id, q);
189 }
190 
199  : QObject(*new QShortcutPrivate, parent)
200 {
201  Q_ASSERT(parent != 0);
202 }
203 
213  const char *member, const char *ambiguousMember,
215  : QObject(*new QShortcutPrivate, parent)
216 {
217  QAPP_CHECK("QShortcut");
218 
219  Q_D(QShortcut);
220  Q_ASSERT(parent != 0);
221  d->sc_context = context;
222  d->sc_sequence = key;
223  d->redoGrab(qApp->d_func()->shortcutMap);
224  if (member)
225  connect(this, SIGNAL(activated()), parent, member);
226  if (ambiguousMember)
227  connect(this, SIGNAL(activatedAmbiguously()), parent, ambiguousMember);
228 }
229 
234 {
235  Q_D(QShortcut);
236  if (qApp)
237  qApp->d_func()->shortcutMap.removeShortcut(d->sc_id, this);
238 }
239 
255 {
256  Q_D(QShortcut);
257  if (d->sc_sequence == key)
258  return;
259  QAPP_CHECK("setKey");
260  d->sc_sequence = key;
261  d->redoGrab(qApp->d_func()->shortcutMap);
262 }
263 
265 {
266  Q_D(const QShortcut);
267  return d->sc_sequence;
268 }
269 
288 void QShortcut::setEnabled(bool enable)
289 {
290  Q_D(QShortcut);
291  if (d->sc_enabled == enable)
292  return;
293  QAPP_CHECK("setEnabled");
294  d->sc_enabled = enable;
295  qApp->d_func()->shortcutMap.setShortcutEnabled(enable, d->sc_id, this);
296 }
297 
299 {
300  Q_D(const QShortcut);
301  return d->sc_enabled;
302 }
303 
320 {
321  Q_D(QShortcut);
322  if(d->sc_context == context)
323  return;
324  QAPP_CHECK("setContext");
325  d->sc_context = context;
326  d->redoGrab(qApp->d_func()->shortcutMap);
327 }
328 
330 {
331  Q_D(QShortcut);
332  return d->sc_context;
333 }
334 
353 {
354  Q_D(QShortcut);
355  d->sc_whatsthis = text;
356 }
357 
359 {
360  Q_D(const QShortcut);
361  return d->sc_whatsthis;
362 }
363 
378 {
379  Q_D(QShortcut);
380  if (d->sc_autorepeat == on)
381  return;
382  QAPP_CHECK("setAutoRepeat");
383  d->sc_autorepeat = on;
384  qApp->d_func()->shortcutMap.setShortcutAutoRepeat(on, d->sc_id, this);
385 }
386 
387 bool QShortcut::autoRepeat() const
388 {
389  Q_D(const QShortcut);
390  return d->sc_autorepeat;
391 }
392 
398 int QShortcut::id() const
399 {
400  Q_D(const QShortcut);
401  return d->sc_id;
402 }
403 
408 {
409  Q_D(QShortcut);
410  bool handled = false;
411  if (d->sc_enabled && e->type() == QEvent::Shortcut) {
412  QShortcutEvent *se = static_cast<QShortcutEvent *>(e);
413  if (se->shortcutId() == d->sc_id && se->key() == d->sc_sequence){
414 #ifndef QT_NO_WHATSTHIS
416  QWhatsThis::showText(QCursor::pos(), d->sc_whatsthis);
417  handled = true;
418  } else
419 #endif
420  if (se->isAmbiguous())
422  else
423  emit activated();
424  handled = true;
425  }
426  }
427  return handled;
428 }
429 #endif // QT_NO_SHORTCUT
430 
double d
Definition: qnumeric_p.h:62
#define QAPP_CHECK(functionName)
Definition: qshortcut.cpp:55
int id() const
Returns the shortcut&#39;s ID.
Definition: qshortcut.cpp:398
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
int setShortcutEnabled(bool enable, int id, QObject *owner, const QKeySequence &key=QKeySequence())
Changes the enable state of a shortcut to enable.
~QShortcut()
Destroys the shortcut.
Definition: qshortcut.cpp:233
void setAutoRepeat(bool on)
Definition: qshortcut.cpp:377
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
The QShortcutEvent class provides an event which is generated when the user presses a key combination...
Definition: qevent.h:675
The QString class provides a Unicode character string.
Definition: qstring.h:83
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
#define Q_D(Class)
Definition: qglobal.h:2482
void setWhatsThis(const QString &text)
Definition: qshortcut.cpp:352
The QShortcut class is used to create keyboard shortcuts.
Definition: qshortcut.h:57
#define Q_Q(Class)
Definition: qglobal.h:2483
#define SIGNAL(a)
Definition: qobjectdefs.h:227
void setEnabled(bool enable)
Definition: qshortcut.cpp:288
QString sc_whatsthis
Definition: qshortcut.cpp:168
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
Qt::ShortcutContext sc_context
Definition: qshortcut.cpp:164
static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the rece...
Definition: qobject.cpp:2580
#define qApp
#define emit
Definition: qobjectdefs.h:76
Q_CORE_EXPORT void qWarning(const char *,...)
void setKey(const QKeySequence &key)
Definition: qshortcut.cpp:254
static bool inWhatsThisMode()
Returns true if the user interface is in "What&#39;s This?" mode; otherwise returns false.
Definition: qwhatsthis.cpp:648
Qt::ShortcutContext context()
bool isEmpty() const
Returns true if the key sequence is empty; otherwise returns false.
void redoGrab(QShortcutMap &map)
Definition: qshortcut.cpp:172
bool event(QEvent *e)
Definition: qshortcut.cpp:407
QKeySequence sc_sequence
Definition: qshortcut.cpp:163
const QKeySequence & key()
Definition: qevent.h:681
int setShortcutAutoRepeat(bool on, int id, QObject *owner, const QKeySequence &key=QKeySequence())
Changes the auto repeat state of a shortcut to enable.
int shortcutId()
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qevent.h:683
QShortcut(QWidget *parent)
Constructs a QShortcut object for the parent widget.
Definition: qshortcut.cpp:198
#define Q_DECLARE_PUBLIC(Class)
Definition: qglobal.h:2477
The QKeySequence class encapsulates a key sequence as used by shortcuts.
Definition: qkeysequence.h:72
QString whatsThis() const
void activated()
This signal is emitted when the user types the shortcut&#39;s key sequence.
QKeySequence key() const
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
bool isEnabled() const
Definition: qshortcut.cpp:298
int removeShortcut(int id, QObject *owner, const QKeySequence &key=QKeySequence())
Removes a shortcut from the global map.
Definition: qnamespace.h:54
QObject * parent
Definition: qobject.h:92
int addShortcut(QObject *owner, const QKeySequence &key, Qt::ShortcutContext context)
Adds a shortcut to the global map.
bool isAmbiguous()
Definition: qevent.h:685
ShortcutContext
Definition: qnamespace.h:1478
void activatedAmbiguously()
When a key sequence is being typed at the keyboard, it is said to be ambiguous as long as it matches ...
static void showText(const QPoint &pos, const QString &text, QWidget *w=0)
Shows text as a "What&#39;s This?" window, at global position pos.
Definition: qwhatsthis.cpp:754
The QEvent class is the base class of all event classes.
Definition: qcoreevent.h:56
Type type() const
Returns the event type.
Definition: qcoreevent.h:303
void setContext(Qt::ShortcutContext context)
Definition: qshortcut.cpp:319
bool autoRepeat() const
#define text
Definition: qobjectdefs.h:80
static QPoint pos()
Returns the position of the cursor (hot spot) in global screen coordinates.
Definition: qcursor_mac.mm:310