We now have a current svn snapshot of libqxt in our contrib dir, and
[quassel.git] / src / contrib / libqxt-2007-10-24 / src / gui / qxtapplication.cpp
diff --git a/src/contrib/libqxt-2007-10-24/src/gui/qxtapplication.cpp b/src/contrib/libqxt-2007-10-24/src/gui/qxtapplication.cpp
new file mode 100644 (file)
index 0000000..08bdb78
--- /dev/null
@@ -0,0 +1,154 @@
+/****************************************************************************\r
+**\r
+** Copyright (C) Qxt Foundation. Some rights reserved.\r
+**\r
+** This file is part of the QxtGui module of the Qt eXTension library\r
+**\r
+** This library is free software; you can redistribute it and/or modify it\r
+** under the terms of th Common Public License, version 1.0, as published by\r
+** IBM.\r
+**\r
+** This file is provided "AS IS", without WARRANTIES OR CONDITIONS OF ANY\r
+** KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY\r
+** WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR\r
+** FITNESS FOR A PARTICULAR PURPOSE.\r
+**\r
+** You should have received a copy of the CPL along with this file.\r
+** See the LICENSE file and the cpl1.0.txt file included with the source\r
+** distribution for more information. If you did not receive a copy of the\r
+** license, contact the Qxt Foundation.\r
+**\r
+** <http://libqxt.sourceforge.net>  <foundation@libqxt.org>\r
+**\r
+****************************************************************************/\r
+#include "qxtapplication.h"\r
+#include "qxtapplication_p.h"\r
+#include <QWidget>\r
+\r
+/*!\r
+    \class QxtApplication QxtApplication\r
+    \ingroup QxtGui\r
+    \brief An extended QApplication with support for hotkeys aka "global shortcuts".\r
+\r
+    QxtApplication introduces hotkeys which trigger even if the application is not\r
+    active. This makes it easy to implement applications that react to certain\r
+    shortcuts still if some other application is active or if the application is\r
+    for example minimized to the system tray.\r
+\r
+    QxtApplication also lets you install native event filters. This makes it\r
+    possible to access platform specific native events without subclassing\r
+    QApplication.\r
+ */\r
+\r
+/*!\r
+    \fn QxtApplication::instance()\r
+\r
+    Returns a pointer to the instance of the application object.\r
+\r
+    A convenience macro \b qxtApp is also available.\r
+ */\r
+\r
+QxtApplication::QxtApplication(int& argc, char** argv)\r
+        : QApplication(argc, argv)\r
+{}\r
+\r
+QxtApplication::QxtApplication(int& argc, char** argv, bool GUIenabled)\r
+        : QApplication(argc, argv, GUIenabled)\r
+{}\r
+\r
+QxtApplication::QxtApplication(int& argc, char** argv, Type type)\r
+        : QApplication(argc, argv, type)\r
+{}\r
+\r
+QxtApplication::~QxtApplication()\r
+{}\r
+\r
+/*!\r
+    Installs a native event \a filter.\r
+\r
+    A native event filter is an object that receives all native events before they reach\r
+    the application object. The filter can either stop the native event or forward it to\r
+    the application object. The filter receives native events via its platform specific\r
+    native event filter function. The native event filter function must return \b true\r
+    if the event should be filtered, (i.e. stopped); otherwise it must return \b false.\r
+\r
+    If multiple native event filters are installed, the filter that was installed last\r
+    is activated first.\r
+\r
+    \sa removeNativeEventFilter()\r
+*/\r
+void QxtApplication::installNativeEventFilter(QxtNativeEventFilter* filter)\r
+{\r
+    if (!filter)\r
+        return;\r
+\r
+    qxt_d().nativeFilters.removeAll(filter);\r
+    qxt_d().nativeFilters.prepend(filter);\r
+}\r
+\r
+/*!\r
+    Removes a native event \a filter. The request is ignored if such a native\r
+    event filter has not been installed.\r
+\r
+    \sa installNativeEventFilter()\r
+*/\r
+void QxtApplication::removeNativeEventFilter(QxtNativeEventFilter* filter)\r
+{\r
+    qxt_d().nativeFilters.removeAll(filter);\r
+}\r
+\r
+/*!\r
+    Adds a hotkey using \a modifiers and \a key. The \a member\r
+    of \a receiver is invoked upon hotkey trigger.\r
+\r
+    \return \b true if hotkey registration succeed, \b false otherwise.\r
+\r
+    Example usage:\r
+    \code\r
+    QxtLabel* label = new QxtLabel("Hello world!");\r
+    qxtApp->addHotKey(Qt::ShiftModifier | Qt::ControlModifier, Qt::Key_S, label, "show");\r
+    \endcode\r
+*/\r
+bool QxtApplication::addHotKey(Qt::KeyboardModifiers modifiers, Qt::Key key, QWidget* receiver, const char* member)\r
+{\r
+    Q_ASSERT(receiver);\r
+    Q_ASSERT(member);\r
+    uint mods = qxt_d().nativeModifiers(modifiers);\r
+    uint keycode = qxt_d().nativeKeycode(key);\r
+    if (keycode)\r
+    {\r
+        qxt_d().hotkeys.insert(qMakePair(mods, keycode), qMakePair(receiver, member));\r
+        return qxt_d().registerHotKey(mods, keycode, receiver);\r
+    }\r
+    return false;\r
+}\r
+\r
+/*!\r
+    Removes the hotkey using \a modifiers and \a key mapped to\r
+    \a member of \a receiver.\r
+\r
+    \return \b true if hotkey unregistration succeed, \b false otherwise.\r
+*/\r
+bool QxtApplication::removeHotKey(Qt::KeyboardModifiers modifiers, Qt::Key key, QWidget* receiver, const char* member)\r
+{\r
+    Q_ASSERT(receiver);\r
+    Q_UNUSED(member);\r
+    uint mods = qxt_d().nativeModifiers(modifiers);\r
+    uint keycode = qxt_d().nativeKeycode(key);\r
+    if (keycode)\r
+    {\r
+        qxt_d().hotkeys.remove(qMakePair(mods, keycode));\r
+        return qxt_d().unregisterHotKey(mods, keycode, receiver);\r
+    }\r
+    return false;\r
+}\r
+\r
+void QxtApplicationPrivate::activateHotKey(uint modifiers, uint keycode) const\r
+{\r
+    Receivers receivers = hotkeys.values(qMakePair(modifiers, keycode));\r
+    foreach (Receiver receiver, receivers)\r
+    {\r
+        // QMetaObject::invokeMethod() has appropriate null checks\r
+        QMetaObject::invokeMethod(receiver.first, receiver.second);\r
+    }\r
+}\r