source: smplayer/trunk/src/globalshortcuts/globalshortcuts.cpp

Last change on this file was 188, checked in by Silvan Scherrer, 9 years ago

SMPlayer: update trunk to version 17.1.0

File size: 3.5 KB
Line 
1/* smplayer, GUI front-end for mplayer.
2 Copyright (C) 2006-2017 Ricardo Villalba <rvm@users.sourceforge.net>
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17*/
18
19#include "globalshortcuts.h"
20#include <QAbstractEventDispatcher>
21#include <QKeySequence>
22#include <QKeyEvent>
23#include <QEvent>
24#include <QApplication>
25#include <QWidget>
26#include <QAction>
27#include <QDebug>
28
29
30GlobalShortcuts::GlobalShortcuts(QObject* parent)
31 : QObject(parent)
32 , enabled(false)
33{
34 qDebug("GlobalShortcuts::GlobalShortcuts");
35 QAbstractEventDispatcher::instance()->installNativeEventFilter(this);
36
37 createKeysList();
38 //setEnabled(true);
39}
40
41GlobalShortcuts::~GlobalShortcuts() {
42 if (enabled) setEnabled(false);
43}
44
45void GlobalShortcuts::setEnabled(bool b) {
46 qDebug() << "GlobalShortcuts::setEnabled:" << b;
47
48 if (b == enabled) return;
49
50 if (b) registerAll(); else unregisterAll();
51 enabled = b;
52}
53
54void GlobalShortcuts::registerAll() {
55 QMapIterator<quint32, Qt::Key> i(key_list);
56 while (i.hasNext()) {
57 i.next();
58 registerShortcut(i.key(), 0);
59 }
60}
61
62void GlobalShortcuts::unregisterAll() {
63 QMapIterator<quint32, Qt::Key> i(key_list);
64 while (i.hasNext()) {
65 i.next();
66 unregisterShortcut(i.key(), 0);
67 }
68}
69
70void GlobalShortcuts::activateShortcut(Qt::Key key) {
71#ifdef Q_OS_LINUX
72 //if (QApplication::activeWindow()) return;
73#endif
74
75 QKeySequence ks(key);
76
77 qDebug() << "GlobalShortcuts::activateShortcut:" << key << "shortcut:" << ks;
78
79#if 0
80 Qt::KeyboardModifiers modifier = Qt::NoModifier;
81 QString name = ks.toString();
82 QKeyEvent key_event(QEvent::KeyPress, key, modifier, name);
83
84 qDebug() << "GlobalShortcuts::activateShortcut: name:" << name;
85
86 if (parent()) QCoreApplication::sendEvent(parent(), &key_event);
87
88#else
89 // Search actions with the shortcut
90 if (isEnabled() && parent()) {
91 QWidget * w = qobject_cast<QWidget *>(parent());
92 if (!w) {
93 qWarning("GlobalShortcuts::activateShortcut: parent is not a widget");
94 return;
95 }
96 QList<QAction *> actions = w->actions();
97 foreach(QAction * action, actions) {
98 QList<QKeySequence> shortcuts = action->shortcuts();
99 foreach(QKeySequence s, shortcuts) {
100 bool match = (s == ks);
101 //qDebug() << "GlobalShortcuts::activateShortcut: action:" << action << "shortcut:" << s << "match:" << match;
102 if (match) {
103 qDebug() << "GlobalShortcuts::activateShortcut: action found:" << action->objectName() << "enabled:" << action->isEnabled();
104 }
105 if (match && action->isEnabled()) {
106 // SkinGui changes the play/pause action to checkable, which doesn't work if called toggle
107 if (action->isCheckable() && action->objectName() != "play_or_pause") {
108 action->toggle();
109 } else {
110 action->trigger();
111 }
112 return;
113 }
114 }
115 }
116 }
117#endif
118}
119
120#include "moc_globalshortcuts.cpp"
121
122#ifdef Q_OS_UNIX
123#include "globalshortcuts_linux.cpp"
124#endif
125
126#ifdef Q_OS_WIN
127#include "globalshortcuts_win.cpp"
128#endif
Note: See TracBrowser for help on using the repository browser.