• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

kjsembed

  • kjsembed
  • kjsembed
eventproxy.cpp
Go to the documentation of this file.
1/* This file is part of the KDE libraries
2 Copyright (C) 2003,2004,2005,2006 Ian Reinhart Geiser <geiseri@kde.org>
3 Copyright (C) 2003,2004,2005,2006 Matt Broadstone <mbroadst@gmail.com>
4 Copyright (C) 2003,2004,2005,2006 Richard J. Moore <rich@kde.org>
5 Copyright (C) 2003,2004,2005,2006 Erik L. Bunce <kde@bunce.us>
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
16
17 You should have received a copy of the GNU Library General Public License
18 along with this library; see the file COPYING.LIB. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA.
21*/
22#include "eventproxy.h"
23
24#include <QtCore/QCoreApplication>
25
26#include "qobject_binding.h"
27#include <kjs/interpreter.h>
28
29#include "kjseglobal.h"
30#include "jseventmapper.h"
31#include "jseventutils.h"
32
33using namespace KJSEmbed;
34
35EventProxy::EventProxy( QObjectBinding *watch, KJS::Interpreter *interpreter ) :
36 QObject(watch->object<QObject>()), m_watch(watch), m_interpreter(interpreter)
37{
38 m_refcount = 0l;
39}
40
41EventProxy::~EventProxy()
42{
43}
44
45bool EventProxy::isFiltered( QEvent::Type t ) const
46{
47 if ( m_eventMask.size() <= t )
48 return false;
49 return m_eventMask.testBit( t );
50}
51
52void EventProxy::addFilter( QEvent::Type t )
53{
54 if( t == QEvent::None )
55 return;
56 if ( !m_refcount )
57 m_watch->object<QObject>()->installEventFilter( this );
58
59 if ( m_eventMask.size() <= t )
60 m_eventMask.resize( t + 1);
61
62 if ( !m_eventMask.testBit(t) )
63 {
64 m_refcount++;
65 m_eventMask.setBit( t );
66 }
67}
68
69void EventProxy::removeFilter( QEvent::Type t )
70{
71 if( t == QEvent::None )
72 return;
73 if ( m_eventMask.size() <= t )
74 return;
75 m_eventMask.clearBit( t );
76 m_refcount--;
77 if ( !m_refcount )
78 {
79 m_watch->object<QObject>()->removeEventFilter( this );
80 deleteLater();
81 }
82}
83
84bool EventProxy::eventFilter( QObject * /*watched*/, QEvent *e )
85{
86 if ( isFiltered(e->type()) )
87 {
88 return !callHandler( e );
89 }
90 return false;
91}
92
93bool EventProxy::callHandler( QEvent *e )
94{
95// Be careful enabling this as if there are a lot of events then the event loop times
96// out and the app crashes with 'Alarm Clock'.
97// qDebug("JSObjectEventProxy::callHandler() event type %d" , e->type() );
98
99 KJS::ExecState *exec = m_interpreter->globalExec();
100 KJS::Identifier id = JSEventMapper::mapper()->findEventHandler( e->type() );
101
102 KJS::JSObject *jsobj(m_watch);
103 KJS::JSObject *fun = jsobj->get(exec, id )->toObject( exec );
104
105 KJS::JSValue *retValue;
106 if ( !fun->implementsCall() )
107 {
108 QString msg = i18n( "Bad event handler: Object %1 Identifier %2 Method %3 Type: %4.",
109 jsobj->className().ascii(),
110 id.ascii(),
111 fun->className().ascii(),
112 e->type());
113 retValue = throwError(exec, KJS::TypeError, msg);
114 }
115 else
116 {
117 // Process args
118 KJS::List args;
119 args.append( JSEventUtils::event(exec, e) );
120
121 // Call handler
122 retValue = fun->call( exec, jsobj, args );
123 }
124
125 if ( exec->hadException() )
126 {
127 if (m_interpreter->shouldPrintExceptions())
128 {
129 KJS::JSLock lock;
130 KJS::JSObject* exceptObj = retValue->toObject(exec);
131 QString message = toQString(exceptObj->toString(exec));
132 QString sourceURL = toQString(exceptObj->get(exec, "sourceURL")->toString(exec));
133 int sourceId = exceptObj->get(exec, "sourceId")->toUInt32(exec);
134 int line = exceptObj->get(exec, "line")->toUInt32(exec);
135 (*KJSEmbed::conerr()) << i18n("Exception calling '%1' function from %2:%3:%4", id.ascii(), !sourceURL.isEmpty() ? sourceURL : QString::number(sourceId), line, message) << endl;
136 }
137
138
139 // clear it so it doesn't stop other things
140 exec->clearException();
141 return false;
142 }
143
144 return true;
145}
146
147//kate: indent-spaces on; indent-width 4; replace-tabs on; indent-mode cstyle;
KJSEmbed::EventProxy::isFiltered
bool isFiltered(QEvent::Type t) const
Returns true iff we forward the event type to JS.
Definition: eventproxy.cpp:45
KJSEmbed::EventProxy::eventFilter
bool eventFilter(QObject *watched, QEvent *e)
Reimplemented to forward events to JS.
Definition: eventproxy.cpp:84
KJSEmbed::EventProxy::~EventProxy
~EventProxy()
Definition: eventproxy.cpp:41
KJSEmbed::EventProxy::addFilter
void addFilter(QEvent::Type t)
Adds an event type to those we forward to JS.
Definition: eventproxy.cpp:52
KJSEmbed::EventProxy::EventProxy
EventProxy(QObjectBinding *watch, KJS::Interpreter *interpreter)
Definition: eventproxy.cpp:35
KJSEmbed::EventProxy::callHandler
bool callHandler(QEvent *e)
Definition: eventproxy.cpp:93
KJSEmbed::EventProxy::removeFilter
void removeFilter(QEvent::Type t)
Removes an event type from those we forward to JS.
Definition: eventproxy.cpp:69
KJSEmbed::JSEventMapper::mapper
static JSEventMapper * mapper()
Return the global event mapper.
Definition: jseventmapper.cpp:284
KJSEmbed::JSEventMapper::findEventHandler
KJS::Identifier findEventHandler(QEvent::Type t) const
Returns the name of the handler method for the specified event type.
Definition: jseventmapper.cpp:279
KJSEmbed::ObjectBinding::object
T * object() const
Definition: object_binding.h:119
KJSEmbed::QObjectBinding
Definition: qobject_binding.h:80
QObject
eventproxy.h
jseventmapper.h
jseventutils.h
kjseglobal.h
KJSEmbed::JSEventUtils::event
KJS::JSObject * event(KJS::ExecState *exec, const QEvent *ev)
Definition: jseventutils.cpp:31
KJSEmbed
Definition: application.h:33
KJSEmbed::toQString
QString toQString(const KJS::UString &u)
Definition: kjseglobal.h:58
KJSEmbed::conerr
KJSEMBED_EXPORT QTextStream * conerr()
Definition: kjseglobal.cpp:143
qobject_binding.h
object
return object
Definition: qpainter_binding.cpp:514
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kjsembed

Skip menu "kjsembed"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal