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

kjsembed

  • sources
  • kde-4.12
  • kdelibs
  • 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 
33 using namespace KJSEmbed;
34 
35 EventProxy::EventProxy( QObjectBinding *watch, KJS::Interpreter *interpreter ) :
36  QObject(watch->object<QObject>()), m_watch(watch), m_interpreter(interpreter)
37 {
38  m_refcount = 0l;
39 }
40 
41 EventProxy::~EventProxy()
42 {
43 }
44 
45 bool EventProxy::isFiltered( QEvent::Type t ) const
46 {
47  if ( m_eventMask.size() <= t )
48  return false;
49  return m_eventMask.testBit( t );
50 }
51 
52 void 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 
69 void 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 
84 bool EventProxy::eventFilter( QObject * /*watched*/, QEvent *e )
85 {
86  if ( isFiltered(e->type()) )
87  {
88  return !callHandler( e );
89  }
90  return false;
91 }
92 
93 bool 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::eventFilter
bool eventFilter(QObject *watched, QEvent *e)
Reimplemented to forward events to JS.
Definition: eventproxy.cpp:84
KJSEmbed::QObjectBinding
Definition: qobject_binding.h:79
None
Definition: variant_binding.cpp:130
jseventutils.h
KJSEmbed::EventProxy::addFilter
void addFilter(QEvent::Type t)
Adds an event type to those we forward to JS.
Definition: eventproxy.cpp:52
object
return object
Definition: qpainter_binding.cpp:514
KJSEmbed::EventProxy::callHandler
bool callHandler(QEvent *e)
Definition: eventproxy.cpp:93
QObject
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::EventProxy::~EventProxy
~EventProxy()
Definition: eventproxy.cpp:41
KJSEmbed::JSEventUtils::event
KJS::JSObject * event(KJS::ExecState *exec, const QEvent *ev)
Definition: jseventutils.cpp:31
KJSEmbed::ObjectBinding::object
T * object() const
Definition: object_binding.h:119
DomDocumentNS::message
QString message
Definition: dom.cpp:358
eventproxy.h
kjseglobal.h
KJSEmbed::EventProxy::isFiltered
bool isFiltered(QEvent::Type t) const
Returns true iff we forward the event type to JS.
Definition: eventproxy.cpp:45
KJSEmbed::JSEventMapper::mapper
static JSEventMapper * mapper()
Return the global event mapper.
Definition: jseventmapper.cpp:284
KJSEmbed::EventProxy::EventProxy
EventProxy(QObjectBinding *watch, KJS::Interpreter *interpreter)
Definition: eventproxy.cpp:35
List
Definition: variant_binding.cpp:130
jseventmapper.h
qobject_binding.h
KJSEmbed::EventProxy::removeFilter
void removeFilter(QEvent::Type t)
Removes an event type from those we forward to JS.
Definition: eventproxy.cpp:69
KJS::throwError
JSObject * throwError(ExecState *e, ErrorType t, const QString &m)
Definition: binding_support.h:241
KJSEmbed::conerr
KJSEMBED_EXPORT QTextStream * conerr()
Definition: kjseglobal.cpp:143
KJSEmbed::toQString
QString toQString(const KJS::UString &u)
Definition: kjseglobal.h:58
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:47:53 by doxygen 1.8.7 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 API Reference

Skip menu "kdelibs 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
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



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