1 /************************************************************************
3 * Copyright 2010-2011 Jakob Leben (jakob.leben@gmail.com)
5 * This file is part of SuperCollider Qt GUI.
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program 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
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ************************************************************************/
22 #ifndef QC_QOBJECT_PROXY_H
23 #define QC_QOBJECT_PROXY_H
33 #include <PyrObject.h>
35 #include <PyrSymbol.h>
37 #define qcProxyDebugMsg( LEVEL, MSG ) \
38 qcDebugMsg( LEVEL, QString("[%1]: ").arg(_scClassName) + QString(MSG) )
44 class QcMethodSignalHandler
;
45 class QcFunctionSignalHandler
;
47 namespace QtCollider
{
48 struct SetPropertyEvent
;
50 struct ScMethodCallEvent
;
52 class ProxyToken
: public QObject
{
55 ProxyToken( QObjectProxy
*p
, QObject
*parent
)
56 : QObject(parent
), proxy(p
) { }
61 class QObjectProxy
: public QObject
63 friend class QcMethodSignalHandler
;
64 friend class QcFunctionSignalHandler
;
73 DestroyProxyAndObject
,
76 struct EventHandlerData
78 EventHandlerData() : type( QEvent::None
) {}
81 QtCollider::Synchronicity sync
;
85 QObjectProxy( QObject
*qObject
, PyrObject
*scObject
);
87 virtual ~QObjectProxy();
89 // compare current thread to proxy's thread
92 // WARNING: must be called with language locked!
93 void finalize() { _scObject
= 0; }
95 inline QObject
*object() const { return qObject
; }
96 inline PyrObject
*scObject() const { return _scObject
; }
98 // Lock for usage of object() outside Qt thread.
99 inline void lock() { mutex
.lock(); }
100 inline void unlock() { mutex
.unlock(); }
102 QString
scClassName() const { return _scClassName
; }
104 QList
<PyrObject
*> children( PyrSymbol
*className
);
105 PyrObject
*parent( PyrSymbol
*className
);
106 virtual bool setParent( QObjectProxy
*parent
);
108 bool setProperty( const char *property
, const QVariant
& val
);
109 QVariant
property( const char *property
);
111 bool connectObject( const char *signal
, PyrObject
*object
, Qt::ConnectionType
);
112 bool connectMethod( const char *signal
, PyrSymbol
*method
, Qt::ConnectionType
);
113 bool disconnectObject( const char *signal
, PyrObject
*object
);
114 bool disconnectMethod( const char *signal
, PyrSymbol
*method
);
115 bool setEventHandler( int eventType
, PyrSymbol
*method
, QtCollider::Synchronicity
, bool enabled
= true );
116 bool setEventHandlerEnabled( int eventType
, bool enabled
);
118 // thread-safe if connection == queued
119 bool invokeMethod( const char *method
, PyrSlot
*ret
, PyrSlot
*arg
, Qt::ConnectionType
);
121 void destroy( DestroyAction
);
123 static QObjectProxy
*fromObject( QObject
* );
128 ( PyrSymbol
*method
, const QList
<QVariant
> & args
= QList
<QVariant
>(),
129 PyrSlot
*result
= 0, bool locked
= false );
131 virtual bool eventFilter( QObject
* watched
, QEvent
* event
);
133 virtual void customEvent( QEvent
* );
135 virtual bool filterEvent( QObject
*, QEvent
*, EventHandlerData
&, QList
<QVariant
> & args
);
137 bool invokeEventHandler( QEvent
*e
, EventHandlerData
&, QList
<QVariant
> & args
);
139 const QHash
<int,EventHandlerData
> & eventHandlers() { return _eventHandlers
; }
147 void scMethodCallEvent( QtCollider::ScMethodCallEvent
* );
148 bool setPropertyEvent( QtCollider::SetPropertyEvent
* );
149 bool destroyEvent( QtCollider::DestroyEvent
* );
152 // NOTE: scObject is protected by the language lock. Should not use it without it!
153 PyrObject
*_scObject
;
154 // NOTE: for the reason above we extract SC class name at construction
155 QString _scClassName
;
157 QHash
<int,EventHandlerData
> _eventHandlers
;
158 QList
<QcMethodSignalHandler
*> methodSigHandlers
;
159 QList
<QcFunctionSignalHandler
*> funcSigHandlers
;
163 namespace QtCollider
{
165 struct SetPropertyEvent
: public QEvent
167 SetPropertyEvent() : QEvent( (QEvent::Type
) QtCollider::Event_Proxy_SetProperty
) {}
172 class DestroyEvent
: public QEvent
175 DestroyEvent( QObjectProxy::DestroyAction act
) :
176 QEvent( (QEvent::Type
) QtCollider::Event_Proxy_Destroy
),
179 QObjectProxy::DestroyAction
action() { return _action
; }
181 QObjectProxy::DestroyAction _action
;
184 struct ScMethodCallEvent
: public QEvent
186 ScMethodCallEvent( PyrSymbol
*m
,
187 const QList
<QVariant
> &l
= QList
<QVariant
>(),
188 bool b_locked
= false )
189 : QEvent( (QEvent::Type
) QtCollider::Event_ScMethodCall
),
196 QList
<QVariant
> args
;
202 Q_DECLARE_METATYPE( QObjectProxy
* );
204 #endif //QC_QOBJECT_PROXY_H