File indexing completed on 2024-07-21 12:12:46

0001 /* This file is part of the KDE libraries
0002     Copyright (C) 2001,2002,2003,2004,2005,2006 Ian Reinhart Geiser <geiseri@kde.org>
0003     Copyright (C) 2001,2002,2003,2004,2005,2006 Matt Broadstone <mbroadst@gmail.com>
0004     Copyright (C) 2001,2002,2003,2004,2005,2006 Richard J. Moore <rich@kde.org>
0005     Copyright (C) 2001,2002,2003,2004,2005,2006 Erik L. Bunce <kde@bunce.us>
0006 
0007     This library is free software; you can redistribute it and/or
0008     modify it under the terms of the GNU Library General Public
0009     License as published by the Free Software Foundation; either
0010     version 2 of the License, or (at your option) any later version.
0011 
0012     This library is distributed in the hope that it will be useful,
0013     but WITHOUT ANY WARRANTY; without even the implied warranty of
0014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015     Library General Public License for more details.
0016 
0017     You should have received a copy of the GNU Library General Public License
0018     along with this library; see the file COPYING.LIB.  If not, write to
0019     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0020     Boston, MA 02110-1301, USA.
0021 */
0022 
0023 #ifndef JSEVENTMAPPER_H
0024 #define JSEVENTMAPPER_H
0025 
0026 #include <QHash>
0027 #include <QEvent>
0028 
0029 #include <kjs/identifier.h>
0030 
0031 namespace KJSEmbed
0032 {
0033 
0034 /**
0035 * Maintains a map between the types of QEvent and the names of their event
0036 * handlers.  This class handles all of the built in Qt events. To add your own custom events you can simply
0037 * call mapper()->addEvent(KJS::Identifier("onMyCustomEvent"), QEvent::User + 1).  Then in your javascript
0038 * you can just do the following:
0039 * @code
0040 * var myQObject = new QObjectBinding(this);
0041 * myQObject.onMyCustomEvent = function () { //do stuff ;}
0042 * @endcode
0043 *
0044 * In cases where you need to handle properties on your custom event, you will need to provide a binding to
0045 * that event and KJSEmbed will create an instance of it for you as the argument in your event handler.
0046 * @author Richard Moore, rich@kde.org
0047 * @author Ian Reinhart Geiser, geiseri@kde.org
0048 */
0049 class JSEventMapper
0050 {
0051 public:
0052     virtual ~JSEventMapper();
0053 
0054     /**
0055     * Adds an event to the map. The event handler has the specified name, and
0056     * the event has the specified type.
0057     */
0058     void addEvent(const KJS::Identifier &name, QEvent::Type t);
0059 
0060     /** Returns true iff the specified name is the identifier for an event handler. */
0061     bool isEventHandler(const KJS::Identifier &name) const;
0062 
0063     /** Returns the type of the events handled by the specified handler. */
0064     QEvent::Type findEventType(const KJS::Identifier &name) const;
0065 
0066     /** Returns the name of the handler method for the specified event type. */
0067     KJS::Identifier findEventHandler(QEvent::Type t) const;
0068 
0069     /**
0070     * Return the global event mapper.
0071     */
0072     static JSEventMapper *mapper();
0073 
0074 private:
0075     JSEventMapper();
0076     static JSEventMapper *m_inst;
0077     QHash<QString, QEvent::Type> m_handlerToEvent;
0078     QHash<int, KJS::Identifier> m_eventToHandler;
0079 };
0080 
0081 } // namespace KJSEmbed
0082 
0083 #endif // JSEVENTMAPPER_H
0084