class library: Spawner - don't access PriorityQueue-array
[supercollider.git] / QtCollider / QObjectProxy.h
blob744be380b7e519160acd7d8f70db0f7a161e3dd5
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
25 #include "Common.h"
27 #include <QObject>
28 #include <QString>
29 #include <QVariant>
30 #include <QHash>
31 #include <QEvent>
33 #include <PyrObject.h>
34 #include <PyrSlot.h>
35 #include <PyrSymbol.h>
37 #define qcProxyDebugMsg( LEVEL, MSG ) \
38 qcDebugMsg( LEVEL, QString("[%1]: ").arg(_scClassName) + QString(MSG) )
40 struct VariantList;
42 class QObjectProxy;
43 class QcSignalSpy;
44 class QcMethodSignalHandler;
45 class QcFunctionSignalHandler;
47 namespace QtCollider {
48 struct SetPropertyEvent;
49 class DestroyEvent;
50 struct ScMethodCallEvent;
52 class ProxyToken : public QObject {
53 Q_OBJECT
54 public:
55 ProxyToken( QObjectProxy *p, QObject *parent )
56 : QObject(parent), proxy(p) { }
57 QObjectProxy *proxy;
61 class QObjectProxy : public QObject
63 friend class QcMethodSignalHandler;
64 friend class QcFunctionSignalHandler;
66 Q_OBJECT
68 public:
70 enum DestroyAction {
71 DestroyProxy,
72 DestroyObject,
73 DestroyProxyAndObject,
76 struct EventHandlerData
78 EventHandlerData() : type( QEvent::None ) {}
79 int type;
80 PyrSymbol *method;
81 QtCollider::Synchronicity sync;
82 bool enabled;
85 QObjectProxy( QObject *qObject, PyrObject *scObject );
87 virtual ~QObjectProxy();
89 // compare current thread to proxy's thread
90 bool compareThread();
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 * );
125 protected:
127 void invokeScMethod
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; }
141 private Q_SLOTS:
143 void invalidate();
145 private:
147 void scMethodCallEvent( QtCollider::ScMethodCallEvent * );
148 bool setPropertyEvent( QtCollider::SetPropertyEvent * );
149 bool destroyEvent( QtCollider::DestroyEvent * );
151 QObject *qObject;
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;
160 QMutex mutex;
163 namespace QtCollider {
165 struct SetPropertyEvent : public QEvent
167 SetPropertyEvent() : QEvent( (QEvent::Type) QtCollider::Event_Proxy_SetProperty ) {}
168 PyrSymbol *property;
169 QVariant value;
172 class DestroyEvent : public QEvent
174 public:
175 DestroyEvent( QObjectProxy::DestroyAction act ) :
176 QEvent( (QEvent::Type) QtCollider::Event_Proxy_Destroy ),
177 _action( act )
179 QObjectProxy::DestroyAction action() { return _action; }
180 private:
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 ),
190 method( m ),
191 args( l ),
192 locked( b_locked )
195 PyrSymbol *method;
196 QList<QVariant> args;
197 bool locked;
200 } // namespace
202 Q_DECLARE_METATYPE( QObjectProxy * );
204 #endif //QC_QOBJECT_PROXY_H