kjsembed
eventproxy.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "eventproxy.h"
00023
00024 #include <QtCore/QCoreApplication>
00025
00026 #include "qobject_binding.h"
00027 #include <kjs/interpreter.h>
00028
00029 #include "kjseglobal.h"
00030 #include "jseventmapper.h"
00031 #include "jseventutils.h"
00032
00033 using namespace KJSEmbed;
00034
00035 EventProxy::EventProxy( QObjectBinding *watch, KJS::Interpreter *interpreter ) :
00036 QObject(watch->object<QObject>()), m_watch(watch), m_interpreter(interpreter)
00037 {
00038 m_refcount = 0l;
00039 }
00040
00041 EventProxy::~EventProxy()
00042 {
00043 }
00044
00045 bool EventProxy::isFiltered( QEvent::Type t ) const
00046 {
00047 if ( m_eventMask.size() <= t )
00048 return false;
00049 return m_eventMask.testBit( t );
00050 }
00051
00052 void EventProxy::addFilter( QEvent::Type t )
00053 {
00054 if( t == QEvent::None )
00055 return;
00056 if ( !m_refcount )
00057 m_watch->object<QObject>()->installEventFilter( this );
00058
00059 if ( m_eventMask.size() <= t )
00060 m_eventMask.resize( t + 1);
00061
00062 if ( !m_eventMask.testBit(t) )
00063 {
00064 m_refcount++;
00065 m_eventMask.setBit( t );
00066 }
00067 }
00068
00069 void EventProxy::removeFilter( QEvent::Type t )
00070 {
00071 if( t == QEvent::None )
00072 return;
00073 if ( m_eventMask.size() <= t )
00074 return;
00075 m_eventMask.clearBit( t );
00076 m_refcount--;
00077 if ( !m_refcount )
00078 {
00079 m_watch->object<QObject>()->removeEventFilter( this );
00080 deleteLater();
00081 }
00082 }
00083
00084 bool EventProxy::eventFilter( QObject * , QEvent *e )
00085 {
00086 if ( isFiltered(e->type()) )
00087 {
00088 return !callHandler( e );
00089 }
00090 return false;
00091 }
00092
00093 bool EventProxy::callHandler( QEvent *e )
00094 {
00095
00096
00097
00098
00099 KJS::ExecState *exec = m_interpreter->globalExec();
00100 KJS::Identifier id = JSEventMapper::mapper()->findEventHandler( e->type() );
00101
00102 KJS::JSObject *jsobj(m_watch);
00103 KJS::JSObject *fun = jsobj->get(exec, id )->toObject( exec );
00104
00105 KJS::JSValue *retValue;
00106 if ( !fun->implementsCall() )
00107 {
00108 QString msg = i18n( "Bad event handler: Object %1 Identifier %2 Method %3 Type: %4.",
00109 jsobj->className().ascii(),
00110 id.ascii(),
00111 fun->className().ascii(),
00112 e->type());
00113 retValue = throwError(exec, KJS::TypeError, msg);
00114 }
00115 else
00116 {
00117
00118 KJS::List args;
00119 args.append( JSEventUtils::event(exec, e) );
00120
00121
00122 retValue = fun->call( exec, jsobj, args );
00123 }
00124
00125 if ( exec->hadException() )
00126 {
00127 if (m_interpreter->shouldPrintExceptions())
00128 {
00129 KJS::JSLock lock;
00130 KJS::JSObject* exceptObj = retValue->toObject(exec);
00131 QString message = toQString(exceptObj->toString(exec));
00132 QString sourceURL = toQString(exceptObj->get(exec, "sourceURL")->toString(exec));
00133 int sourceId = exceptObj->get(exec, "sourceId")->toUInt32(exec);
00134 int line = exceptObj->get(exec, "line")->toUInt32(exec);
00135 (*KJSEmbed::conerr()) << i18n("Exception calling '%1' function from %2:%3:%4", id.ascii(), !sourceURL.isEmpty() ? sourceURL : QString::number(sourceId), line, message) << endl;
00136 }
00137
00138
00139
00140 exec->clearException();
00141 return false;
00142 }
00143
00144 return true;
00145 }
00146
00147