Hi QML Developers,

we've been developing a Python <-> QML interface. It's already working
fine for us and we plan to release it soon.

Unfortunately we need two hooks in the Declarative source code. First we
need access to the QDeclarativeEngines QScriptEngine. This access is
necessary to create QScriptValues via QScripEngine::newFunction. We use
this to make it possible to call Python methods and functions from QML /
ECMA-Script (via a C++-wrapper function). 
Second, we need access to the QDeclarativeOpenMetaObject to build a
dynamic ProxyObject, which represents Python objects in QML.  

Our current solution is to add a proxy-object implementation and a
header-file (files are attached) to the declarative source and build our
"own" version off Qt-Declarative. Of course, we would be very happy if
we could skip this workaround. Is there any chance that:
1. QDeclarativeEngine allows access to its QScriptEngine
2. QDeclarativeOpenMetaObject will become part of the Public API
in the near future? 

It's unfortunate that we've missed the 4.7 release with that, but it
would be nice if we could integrate those hooks.

What do you think? 


Greetings from Berlin
Georg & Kristian
-- 
mixd.tv - mix your own tv
/*
proxyproperty.cpp

(C) 2009, 2010  MWG Media Wedding GmbH <[email protected]>
All Rights Reserved.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/



#include "proxyproperty.h"
#include <private/qdeclarativeopenmetaobject_p.h>
#include <private/qdeclarativeengine_p.h>


class ProxyPropertyMetaObject : public QDeclarativeOpenMetaObject
{
public:
	ProxyPropertyMetaObject(ProxyProperty *parent) : QDeclarativeOpenMetaObject(parent), proxy(parent)
	{

	}
	virtual ~ProxyPropertyMetaObject() {

	}

protected:
	virtual void propertyRead(int idx) {
		static_cast<ProxyProperty*>(object())->extract(name(idx));
	};
	virtual void propertyWritten(int idx) {

    	static_cast<ProxyProperty*>(object())->propertyChanged(name(idx), value(idx));
    };


private:
	ProxyProperty* proxy;
};



class ProxyPropertyPrivate : public QObjectPrivate
{
    Q_DECLARE_PUBLIC(ProxyProperty)
public:
    ProxyPropertyMetaObject *mo;
};



ProxyProperty::ProxyProperty() : QObject()
{
    Q_D(ProxyProperty);
	qDebug() << Q_FUNC_INFO;

    meta_object = new ProxyPropertyMetaObject(this);
    d->mo = meta_object;
}

ProxyProperty::~ProxyProperty()
{
    qDebug() << Q_FUNC_INFO;
}


void ProxyProperty::createProperty(const char *key)
{
	meta_object->setValue(QByteArray(key),QVariant());
}

void ProxyProperty::setValue(QByteArray key, QVariant value)
{
	meta_object->setValue(QByteArray(key), value);
}




void ProxyProperty::attach(QObject*o)
{
	if (o == 0)
    	return;
    if (owners.contains(o))
    	return;
    owners.append(o);
    connect(o,SIGNAL(destroyed(QObject*)),this,SLOT(detach(QObject*)));
}

void ProxyProperty::detach(QObject *o)
{
    qDebug() << Q_FUNC_INFO;
	owners.removeAll(o);
	qDebug() << owners.size();
    if  (owners.size() == 0)
        delete this;
}


QScriptEngine* get_script_engine(QDeclarativeEngine *engine)
{
	return QDeclarativeEnginePrivate::getScriptEngine(engine);

};



/*
proxyproperty.h

(C) 2009, 2010  MWG Media Wedding GmbH <[email protected]>
All Rights Reserved.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/


#ifndef PROXYPROPERTY_H
#define PROXYPROPERTY_H  PROXYPROPERTY_H

#include <QtCore>
#include <QScriptEngine>
#include <QDeclarativeEngine>

QT_BEGIN_HEADER
QT_BEGIN_NAMESPACE

class ProxyPropertyMetaObject;
class ProxyPropertyPrivate;
//
class Q_DECLARATIVE_EXPORT ProxyProperty : public QObject
{
    Q_OBJECT
public:
	ProxyProperty();
	virtual ~ProxyProperty();

    virtual	void setProperty(QByteArray key, QVariant value) = 0;
    virtual void extract(QByteArray) = 0;
    virtual void propertyChanged(QByteArray, QVariant) = 0;

    void attach(QObject*o);

public slots:
    void detach(QObject*o);
protected:
    void setValue(QByteArray key, QVariant value);
    void createProperty(const char*);
private:
    QList<QObject*>owners;

	ProxyPropertyMetaObject *meta_object;
    Q_DECLARE_PRIVATE(ProxyProperty)
};


Q_DECLARATIVE_EXPORT QScriptEngine* get_script_engine(QDeclarativeEngine *);


QT_END_NAMESPACE

QT_END_HEADER

#endif
_______________________________________________
Qt-qml mailing list
[email protected]
http://lists.trolltech.com/mailman/listinfo/qt-qml

Reply via email to