08bdb7867f33e3fb009a6a28077d976f68867d58
[quassel.git] / src / contrib / libqxt-2007-10-24 / src / gui / qxtapplication.cpp
1 /****************************************************************************\r
2 **\r
3 ** Copyright (C) Qxt Foundation. Some rights reserved.\r
4 **\r
5 ** This file is part of the QxtGui module of the Qt eXTension library\r
6 **\r
7 ** This library is free software; you can redistribute it and/or modify it\r
8 ** under the terms of th Common Public License, version 1.0, as published by\r
9 ** IBM.\r
10 **\r
11 ** This file is provided "AS IS", without WARRANTIES OR CONDITIONS OF ANY\r
12 ** KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY\r
13 ** WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR\r
14 ** FITNESS FOR A PARTICULAR PURPOSE.\r
15 **\r
16 ** You should have received a copy of the CPL along with this file.\r
17 ** See the LICENSE file and the cpl1.0.txt file included with the source\r
18 ** distribution for more information. If you did not receive a copy of the\r
19 ** license, contact the Qxt Foundation.\r
20 **\r
21 ** <http://libqxt.sourceforge.net>  <foundation@libqxt.org>\r
22 **\r
23 ****************************************************************************/\r
24 #include "qxtapplication.h"\r
25 #include "qxtapplication_p.h"\r
26 #include <QWidget>\r
27 \r
28 /*!\r
29     \class QxtApplication QxtApplication\r
30     \ingroup QxtGui\r
31     \brief An extended QApplication with support for hotkeys aka "global shortcuts".\r
32 \r
33     QxtApplication introduces hotkeys which trigger even if the application is not\r
34     active. This makes it easy to implement applications that react to certain\r
35     shortcuts still if some other application is active or if the application is\r
36     for example minimized to the system tray.\r
37 \r
38     QxtApplication also lets you install native event filters. This makes it\r
39     possible to access platform specific native events without subclassing\r
40     QApplication.\r
41  */\r
42 \r
43 /*!\r
44     \fn QxtApplication::instance()\r
45 \r
46     Returns a pointer to the instance of the application object.\r
47 \r
48     A convenience macro \b qxtApp is also available.\r
49  */\r
50 \r
51 QxtApplication::QxtApplication(int& argc, char** argv)\r
52         : QApplication(argc, argv)\r
53 {}\r
54 \r
55 QxtApplication::QxtApplication(int& argc, char** argv, bool GUIenabled)\r
56         : QApplication(argc, argv, GUIenabled)\r
57 {}\r
58 \r
59 QxtApplication::QxtApplication(int& argc, char** argv, Type type)\r
60         : QApplication(argc, argv, type)\r
61 {}\r
62 \r
63 QxtApplication::~QxtApplication()\r
64 {}\r
65 \r
66 /*!\r
67     Installs a native event \a filter.\r
68 \r
69     A native event filter is an object that receives all native events before they reach\r
70     the application object. The filter can either stop the native event or forward it to\r
71     the application object. The filter receives native events via its platform specific\r
72     native event filter function. The native event filter function must return \b true\r
73     if the event should be filtered, (i.e. stopped); otherwise it must return \b false.\r
74 \r
75     If multiple native event filters are installed, the filter that was installed last\r
76     is activated first.\r
77 \r
78     \sa removeNativeEventFilter()\r
79 */\r
80 void QxtApplication::installNativeEventFilter(QxtNativeEventFilter* filter)\r
81 {\r
82     if (!filter)\r
83         return;\r
84 \r
85     qxt_d().nativeFilters.removeAll(filter);\r
86     qxt_d().nativeFilters.prepend(filter);\r
87 }\r
88 \r
89 /*!\r
90     Removes a native event \a filter. The request is ignored if such a native\r
91     event filter has not been installed.\r
92 \r
93     \sa installNativeEventFilter()\r
94 */\r
95 void QxtApplication::removeNativeEventFilter(QxtNativeEventFilter* filter)\r
96 {\r
97     qxt_d().nativeFilters.removeAll(filter);\r
98 }\r
99 \r
100 /*!\r
101     Adds a hotkey using \a modifiers and \a key. The \a member\r
102     of \a receiver is invoked upon hotkey trigger.\r
103 \r
104     \return \b true if hotkey registration succeed, \b false otherwise.\r
105 \r
106     Example usage:\r
107     \code\r
108     QxtLabel* label = new QxtLabel("Hello world!");\r
109     qxtApp->addHotKey(Qt::ShiftModifier | Qt::ControlModifier, Qt::Key_S, label, "show");\r
110     \endcode\r
111 */\r
112 bool QxtApplication::addHotKey(Qt::KeyboardModifiers modifiers, Qt::Key key, QWidget* receiver, const char* member)\r
113 {\r
114     Q_ASSERT(receiver);\r
115     Q_ASSERT(member);\r
116     uint mods = qxt_d().nativeModifiers(modifiers);\r
117     uint keycode = qxt_d().nativeKeycode(key);\r
118     if (keycode)\r
119     {\r
120         qxt_d().hotkeys.insert(qMakePair(mods, keycode), qMakePair(receiver, member));\r
121         return qxt_d().registerHotKey(mods, keycode, receiver);\r
122     }\r
123     return false;\r
124 }\r
125 \r
126 /*!\r
127     Removes the hotkey using \a modifiers and \a key mapped to\r
128     \a member of \a receiver.\r
129 \r
130     \return \b true if hotkey unregistration succeed, \b false otherwise.\r
131 */\r
132 bool QxtApplication::removeHotKey(Qt::KeyboardModifiers modifiers, Qt::Key key, QWidget* receiver, const char* member)\r
133 {\r
134     Q_ASSERT(receiver);\r
135     Q_UNUSED(member);\r
136     uint mods = qxt_d().nativeModifiers(modifiers);\r
137     uint keycode = qxt_d().nativeKeycode(key);\r
138     if (keycode)\r
139     {\r
140         qxt_d().hotkeys.remove(qMakePair(mods, keycode));\r
141         return qxt_d().unregisterHotKey(mods, keycode, receiver);\r
142     }\r
143     return false;\r
144 }\r
145 \r
146 void QxtApplicationPrivate::activateHotKey(uint modifiers, uint keycode) const\r
147 {\r
148     Receivers receivers = hotkeys.values(qMakePair(modifiers, keycode));\r
149     foreach (Receiver receiver, receivers)\r
150     {\r
151         // QMetaObject::invokeMethod() has appropriate null checks\r
152         QMetaObject::invokeMethod(receiver.first, receiver.second);\r
153     }\r
154 }\r