1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
|
#include "qxtglobalshortcut.h"
/****************************************************************************
** Copyright (c) 2006 - 2011, the LibQxt project.
** See the Qxt AUTHORS file for a list of authors and copyright holders.
** All rights reserved.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in the
** documentation and/or other materials provided with the distribution.
** * Neither the name of the LibQxt project nor the
** names of its contributors may be used to endorse or promote products
** derived from this software without specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
** DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**
** <http://libqxt.org> <foundation@libqxt.org>
*****************************************************************************/
#include "qxtglobalshortcut_p.h"
#include <QAbstractEventDispatcher>
#include <QApplication>
#include <QMessageBox>
namespace {
int referenceCounter = 0;
QHash<QPair<quint32, quint32>, QxtGlobalShortcut*> shortcuts;
} // namespace
QxtGlobalShortcutPrivate::QxtGlobalShortcutPrivate(QxtGlobalShortcut *q)
: enabled(true)
, key(Qt::Key(0))
, mods(Qt::NoModifier)
, nativeKey(0)
, nativeMods(0)
, registered(false)
, q_ptr(q)
{
init();
}
QxtGlobalShortcutPrivate::~QxtGlobalShortcutPrivate()
{
unsetShortcut();
destroy();
}
void QxtGlobalShortcutPrivate::initFallback()
{
if (referenceCounter == 0) {
QAbstractEventDispatcher::instance()->installNativeEventFilter(this);
}
++referenceCounter;
}
void QxtGlobalShortcutPrivate::destroyFallback()
{
--referenceCounter;
if (referenceCounter == 0) {
QAbstractEventDispatcher *ed = QAbstractEventDispatcher::instance();
if (ed != nullptr) {
ed->removeNativeEventFilter(this);
}
}
}
void QxtGlobalShortcutPrivate::setKeySequence(const QKeySequence& shortcut)
{
const Qt::KeyboardModifiers allMods =
Qt::ShiftModifier | Qt::ControlModifier | Qt::AltModifier | Qt::MetaModifier | Qt::KeypadModifier;
const auto xkeyCode = static_cast<uint>( shortcut.isEmpty() ? 0 : shortcut[0] );
// WORKAROUND: Qt has convert some keys to upper case which
// breaks some shortcuts on some keyboard layouts.
const uint keyCode = QChar::toLower(xkeyCode & ~allMods);
key = Qt::Key(keyCode);
mods = Qt::KeyboardModifiers(xkeyCode & allMods);
}
bool QxtGlobalShortcutPrivate::setShortcutFallback(const QKeySequence& shortcut)
{
unsetShortcut();
setKeySequence(shortcut);
nativeKey = nativeKeycode(key, mods);
nativeMods = nativeModifiers(mods);
registered = registerShortcut(nativeKey, nativeMods);
if (registered)
shortcuts.insert(qMakePair(nativeKey, nativeMods), q_ptr);
return registered;
}
bool QxtGlobalShortcutPrivate::unsetShortcutFallback()
{
if (registered
&& shortcuts.value(qMakePair(nativeKey, nativeMods)) == q_ptr
&& unregisterShortcut(nativeKey, nativeMods))
{
shortcuts.remove(qMakePair(nativeKey, nativeMods));
registered = false;
return true;
}
return false;
}
void QxtGlobalShortcutPrivate::activateShortcut(quint32 nativeKey, quint32 nativeMods)
{
QxtGlobalShortcut* shortcut = shortcuts.value(qMakePair(nativeKey, nativeMods));
if (shortcut && shortcut->isEnabled())
emit shortcut->activated(shortcut);
}
/*!
\class QxtGlobalShortcut
\brief The QxtGlobalShortcut class provides a global shortcut aka "hotkey".
A global shortcut triggers even if the application is not active. This
makes it easy to implement applications that react to certain shortcuts
still if some other application is active or if the application is for
example minimized to the system tray.
Example usage:
\code
QxtGlobalShortcut* shortcut = new QxtGlobalShortcut(window);
connect(shortcut, SIGNAL(activated()), window, SLOT(toggleVisibility()));
shortcut->setShortcut(QKeySequence("Ctrl+Shift+F12"));
\endcode
*/
/*!
\fn QxtGlobalShortcut::activated()
This signal is emitted when the user types the shortcut's key sequence.
\sa shortcut
*/
/*!
Constructs a new QxtGlobalShortcut with \a parent.
*/
QxtGlobalShortcut::QxtGlobalShortcut(QObject* parent)
: QObject(parent)
, d_ptr(new QxtGlobalShortcutPrivate(this))
{
}
/*!
Constructs a new QxtGlobalShortcut with \a shortcut and \a parent.
*/
QxtGlobalShortcut::QxtGlobalShortcut(
const QKeySequence& shortcut, const QString &name, QObject* parent)
: QxtGlobalShortcut(parent)
{
setShortcut(shortcut);
setName(name);
}
/*!
Destructs the QxtGlobalShortcut.
*/
QxtGlobalShortcut::~QxtGlobalShortcut()
{
delete d_ptr;
}
/*!
\property QxtGlobalShortcut::shortcut
\brief the shortcut key sequence
Notice that corresponding key press and release events are not
delivered for registered global shortcuts even if they are disabled.
Also, comma separated key sequences are not supported.
Only the first part is used:
\code
qxtShortcut->setShortcut(QKeySequence("Ctrl+Alt+A,Ctrl+Alt+B"));
Q_ASSERT(qxtShortcut->shortcut() == QKeySequence("Ctrl+Alt+A"));
\endcode
\sa setShortcut()
*/
QKeySequence QxtGlobalShortcut::shortcut() const
{
return QKeySequence( static_cast<int>(d_ptr->key | d_ptr->mods) );
}
/*!
\property QxtGlobalShortcut::shortcut
\brief sets the shortcut key sequence
\sa shortcut()
*/
bool QxtGlobalShortcut::setShortcut(const QKeySequence& shortcut)
{
return d_ptr->setShortcut(shortcut);
}
QString QxtGlobalShortcut::name() const
{
return d_ptr->name;
}
void QxtGlobalShortcut::setName(const QString& name)
{
d_ptr->name = name;
}
/*!
\property QxtGlobalShortcut::enabled
\brief whether the shortcut is enabled
A disabled shortcut does not get activated.
The default value is \c true.
\sa setEnabled(), setDisabled()
*/
bool QxtGlobalShortcut::isEnabled() const
{
return d_ptr->enabled;
}
/*!
\property QxtGlobalShortcut::valid
\brief whether the shortcut was successfully set up
*/
bool QxtGlobalShortcut::isValid() const
{
return d_ptr->registered;
}
/*!
Activates the shortcut if enabled.
\sa activated()
*/
void QxtGlobalShortcut::activate()
{
if (d_ptr->enabled)
emit activated(this);
}
/*!
Notifies the user that changing global shortcuts requires application restart.
This needed on some platforms like Wayland, because global shortcuts cannot
be rebound again.
This shows a message box for the first found top level window. If no
visible window is found, nothing is shown. This avoids showing a message
box when application is exiting.
*/
void QxtGlobalShortcut::notifyRestartNeeded()
{
const QWidgetList topLevelWidgets = QApplication::topLevelWidgets();
QWidget *parentWindow = nullptr;
for (QWidget *widget : topLevelWidgets) {
if (widget->isVisible()) {
parentWindow = widget;
break;
}
}
if (parentWindow == nullptr)
return;
QMessageBox::information(
parentWindow,
tr("Restart Required"),
tr("Changing global shortcuts requires application restart.")
);
}
/*!
Sets the shortcut \a enabled.
\sa enabled
*/
void QxtGlobalShortcut::setEnabled(bool enabled)
{
d_ptr->enabled = enabled;
}
/*!
Sets the shortcut \a disabled.
\sa enabled
*/
void QxtGlobalShortcut::setDisabled(bool disabled)
{
d_ptr->enabled = !disabled;
}
|