|
| 1 | +#include "qxtglobalshortcut.h" |
| 2 | +/**************************************************************************** |
| 3 | +** Copyright (c) 2006 - 2011, the LibQxt project. |
| 4 | +** See the Qxt AUTHORS file for a list of authors and copyright holders. |
| 5 | +** All rights reserved. |
| 6 | +** |
| 7 | +** Redistribution and use in source and binary forms, with or without |
| 8 | +** modification, are permitted provided that the following conditions are met: |
| 9 | +** * Redistributions of source code must retain the above copyright |
| 10 | +** notice, this list of conditions and the following disclaimer. |
| 11 | +** * Redistributions in binary form must reproduce the above copyright |
| 12 | +** notice, this list of conditions and the following disclaimer in the |
| 13 | +** documentation and/or other materials provided with the distribution. |
| 14 | +** * Neither the name of the LibQxt project nor the |
| 15 | +** names of its contributors may be used to endorse or promote products |
| 16 | +** derived from this software without specific prior written permission. |
| 17 | +** |
| 18 | +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 19 | +** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 20 | +** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 21 | +** DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY |
| 22 | +** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 23 | +** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 24 | +** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 25 | +** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 | +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 27 | +** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | +** |
| 29 | +** <http://libqxt.org> <[email protected]> |
| 30 | +*****************************************************************************/ |
| 31 | + |
| 32 | +#include "qxtglobalshortcut_p.h" |
| 33 | +#include <QAbstractEventDispatcher> |
| 34 | +#include <QtDebug> |
| 35 | + |
| 36 | +#ifndef Q_WS_MAC |
| 37 | +int QxtGlobalShortcutPrivate::ref = 0; |
| 38 | +#endif // Q_WS_MAC |
| 39 | +QHash<QPair<quint32, quint32>, QxtGlobalShortcut*> QxtGlobalShortcutPrivate::shortcuts; |
| 40 | + |
| 41 | +QxtGlobalShortcutPrivate::QxtGlobalShortcutPrivate() : enabled(true), key(Qt::Key(0)), mods(Qt::NoModifier) |
| 42 | +{ |
| 43 | +#ifndef Q_WS_MAC |
| 44 | + if (ref == 0) { |
| 45 | + QAbstractEventDispatcher::instance()->installNativeEventFilter(this); |
| 46 | + } |
| 47 | + ++ref; |
| 48 | +#endif // Q_WS_MAC |
| 49 | +} |
| 50 | + |
| 51 | +QxtGlobalShortcutPrivate::~QxtGlobalShortcutPrivate() |
| 52 | +{ |
| 53 | +#ifndef Q_WS_MAC |
| 54 | + --ref; |
| 55 | + if (ref == 0) { |
| 56 | + QAbstractEventDispatcher *ed = QAbstractEventDispatcher::instance(); |
| 57 | + if (ed != 0) { |
| 58 | + ed->removeNativeEventFilter(this); |
| 59 | + } |
| 60 | + } |
| 61 | +#endif // Q_WS_MAC |
| 62 | +} |
| 63 | + |
| 64 | +bool QxtGlobalShortcutPrivate::setShortcut(const QKeySequence& shortcut) |
| 65 | +{ |
| 66 | + Qt::KeyboardModifiers allMods = Qt::ShiftModifier | Qt::ControlModifier | Qt::AltModifier | Qt::MetaModifier; |
| 67 | + key = shortcut.isEmpty() ? Qt::Key(0) : Qt::Key((shortcut[0] ^ allMods) & shortcut[0]); |
| 68 | + mods = shortcut.isEmpty() ? Qt::KeyboardModifiers(0) : Qt::KeyboardModifiers(shortcut[0] & allMods); |
| 69 | + const quint32 nativeKey = nativeKeycode(key); |
| 70 | + const quint32 nativeMods = nativeModifiers(mods); |
| 71 | + const bool res = registerShortcut(nativeKey, nativeMods); |
| 72 | + if (res) |
| 73 | + shortcuts.insert(qMakePair(nativeKey, nativeMods), &qxt_p()); |
| 74 | + else |
| 75 | + qWarning() << "QxtGlobalShortcut failed to register:" << QKeySequence(key + mods).toString(); |
| 76 | + return res; |
| 77 | +} |
| 78 | + |
| 79 | +bool QxtGlobalShortcutPrivate::unsetShortcut() |
| 80 | +{ |
| 81 | + bool res = false; |
| 82 | + const quint32 nativeKey = nativeKeycode(key); |
| 83 | + const quint32 nativeMods = nativeModifiers(mods); |
| 84 | + if (shortcuts.value(qMakePair(nativeKey, nativeMods)) == &qxt_p()) |
| 85 | + res = unregisterShortcut(nativeKey, nativeMods); |
| 86 | + if (res) |
| 87 | + shortcuts.remove(qMakePair(nativeKey, nativeMods)); |
| 88 | + else |
| 89 | + qWarning() << "QxtGlobalShortcut failed to unregister:" << QKeySequence(key + mods).toString(); |
| 90 | + key = Qt::Key(0); |
| 91 | + mods = Qt::KeyboardModifiers(0); |
| 92 | + return res; |
| 93 | +} |
| 94 | + |
| 95 | +void QxtGlobalShortcutPrivate::activateShortcut(quint32 nativeKey, quint32 nativeMods) |
| 96 | +{ |
| 97 | + QxtGlobalShortcut* shortcut = shortcuts.value(qMakePair(nativeKey, nativeMods)); |
| 98 | + if (shortcut && shortcut->isEnabled()) |
| 99 | + emit shortcut->activated(); |
| 100 | +} |
| 101 | + |
| 102 | +/*! |
| 103 | + \class QxtGlobalShortcut |
| 104 | + \inmodule QxtWidgets |
| 105 | + \brief The QxtGlobalShortcut class provides a global shortcut aka "hotkey". |
| 106 | +
|
| 107 | + A global shortcut triggers even if the application is not active. This |
| 108 | + makes it easy to implement applications that react to certain shortcuts |
| 109 | + still if some other application is active or if the application is for |
| 110 | + example minimized to the system tray. |
| 111 | +
|
| 112 | + Example usage: |
| 113 | + \code |
| 114 | + QxtGlobalShortcut* shortcut = new QxtGlobalShortcut(window); |
| 115 | + connect(shortcut, SIGNAL(activated()), window, SLOT(toggleVisibility())); |
| 116 | + shortcut->setShortcut(QKeySequence("Ctrl+Shift+F12")); |
| 117 | + \endcode |
| 118 | +
|
| 119 | + \bold {Note:} Since Qxt 0.6 QxtGlobalShortcut no more requires QxtApplication. |
| 120 | + */ |
| 121 | + |
| 122 | +/*! |
| 123 | + \fn QxtGlobalShortcut::activated() |
| 124 | +
|
| 125 | + This signal is emitted when the user types the shortcut's key sequence. |
| 126 | +
|
| 127 | + \sa shortcut |
| 128 | + */ |
| 129 | + |
| 130 | +/*! |
| 131 | + Constructs a new QxtGlobalShortcut with \a parent. |
| 132 | + */ |
| 133 | +QxtGlobalShortcut::QxtGlobalShortcut(QObject* parent) |
| 134 | + : QObject(parent) |
| 135 | +{ |
| 136 | + QXT_INIT_PRIVATE(QxtGlobalShortcut); |
| 137 | +} |
| 138 | + |
| 139 | +/*! |
| 140 | + Constructs a new QxtGlobalShortcut with \a shortcut and \a parent. |
| 141 | + */ |
| 142 | +QxtGlobalShortcut::QxtGlobalShortcut(const QKeySequence& shortcut, QObject* parent) |
| 143 | + : QObject(parent) |
| 144 | +{ |
| 145 | + QXT_INIT_PRIVATE(QxtGlobalShortcut); |
| 146 | + setShortcut(shortcut); |
| 147 | +} |
| 148 | + |
| 149 | +/*! |
| 150 | + Destructs the QxtGlobalShortcut. |
| 151 | + */ |
| 152 | +QxtGlobalShortcut::~QxtGlobalShortcut() |
| 153 | +{ |
| 154 | + if (qxt_d().key != 0) |
| 155 | + qxt_d().unsetShortcut(); |
| 156 | +} |
| 157 | + |
| 158 | +/*! |
| 159 | + \property QxtGlobalShortcut::shortcut |
| 160 | + \brief the shortcut key sequence |
| 161 | +
|
| 162 | + \bold {Note:} Notice that corresponding key press and release events are not |
| 163 | + delivered for registered global shortcuts even if they are disabled. |
| 164 | + Also, comma separated key sequences are not supported. |
| 165 | + Only the first part is used: |
| 166 | +
|
| 167 | + \code |
| 168 | + qxtShortcut->setShortcut(QKeySequence("Ctrl+Alt+A,Ctrl+Alt+B")); |
| 169 | + Q_ASSERT(qxtShortcut->shortcut() == QKeySequence("Ctrl+Alt+A")); |
| 170 | + \endcode |
| 171 | + */ |
| 172 | +QKeySequence QxtGlobalShortcut::shortcut() const |
| 173 | +{ |
| 174 | + return QKeySequence(qxt_d().key | qxt_d().mods); |
| 175 | +} |
| 176 | + |
| 177 | +bool QxtGlobalShortcut::setShortcut(const QKeySequence& shortcut) |
| 178 | +{ |
| 179 | + if (qxt_d().key != 0) |
| 180 | + qxt_d().unsetShortcut(); |
| 181 | + return qxt_d().setShortcut(shortcut); |
| 182 | +} |
| 183 | + |
| 184 | +/*! |
| 185 | + \property QxtGlobalShortcut::enabled |
| 186 | + \brief whether the shortcut is enabled |
| 187 | +
|
| 188 | + A disabled shortcut does not get activated. |
| 189 | +
|
| 190 | + The default value is \c true. |
| 191 | +
|
| 192 | + \sa setDisabled() |
| 193 | + */ |
| 194 | +bool QxtGlobalShortcut::isEnabled() const |
| 195 | +{ |
| 196 | + return qxt_d().enabled; |
| 197 | +} |
| 198 | + |
| 199 | +void QxtGlobalShortcut::setEnabled(bool enabled) |
| 200 | +{ |
| 201 | + qxt_d().enabled = enabled; |
| 202 | +} |
| 203 | + |
| 204 | +/*! |
| 205 | + Sets the shortcut \a disabled. |
| 206 | +
|
| 207 | + \sa enabled |
| 208 | + */ |
| 209 | +void QxtGlobalShortcut::setDisabled(bool disabled) |
| 210 | +{ |
| 211 | + qxt_d().enabled = !disabled; |
| 212 | +} |
0 commit comments