Commit 93a9ceb5 authored by Sebastian Sauer's avatar Sebastian Sauer
Browse files

More work on scripting functions.

A simple example works now. See functions.py


svn path=/trunk/koffice/; revision=602887
parent 75be0409
......@@ -42,7 +42,6 @@ namespace Kross {
class PythonExtension : public Py::PythonExtension<PythonExtension>
{
friend class PythonScript;
//friend class PythonModule;
public:
/**
......
......@@ -43,16 +43,17 @@ namespace Kross {
Py::Callable callable;
QByteArray stringData;
uint data[21];
QVariant tmpResult;
};
}
PythonFunction::PythonFunction(QObject* sender, const QByteArray& sendersignal, const Py::Callable& callable)
PythonFunction::PythonFunction(QObject* sender, const QByteArray& signal, const Py::Callable& callable)
: QObject()
, d( new Private() )
{
d->sender = sender;
d->signature = QMetaObject::normalizedSignature( sendersignal );
d->signature = QMetaObject::normalizedSignature( signal );
d->callable = callable;
//krossdebug(QString("PythonFunction::PythonFunction sender=\"%1\" signal=\"%2\"").arg(sender->objectName()).arg(d->signature.constData()));
......@@ -115,8 +116,8 @@ int PythonFunction::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
{
_id = QObject::qt_metacall(_c, _id, _a);
//krossdebug(QString("PythonFunction::qt_metacall id=%1").arg(_id));
if (_id >= 0 && _c == QMetaObject::InvokeMetaMethod) {
switch (_id) {
if(_id >= 0 && _c == QMetaObject::InvokeMetaMethod) {
switch(_id) {
case 0: {
// convert the arguments
QVariantList args;
......@@ -131,6 +132,7 @@ int PythonFunction::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
else {
krosswarning("PythonFunction::qt_metacall: Not supported yet!");
//TODO implement
args.append( QVariant() );
}
++idx;
}
......@@ -138,9 +140,12 @@ int PythonFunction::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
// call the python function
Py::Object result = d->callable.apply( PythonType<QVariantList,Py::Tuple>::toPyObject(args) );
//TODO finally set the returnvalue
//QVariant v = PythonType<QVariant>::toVariant(result);
//_a[0] = Kross::MetaTypeVariant<QVariant>(v).toVoidStar();
// finally set the returnvalue
QObject* sender = QObject::sender();
d->tmpResult = PythonType<QVariant>::toVariant(result);
krossdebug( QString("PythonFunction::qt_metacall sender.objectName=%1 sender.className=%2 pyresult=%3 variantresult=%4").arg(sender->objectName()).arg(sender->metaObject()->className()).arg(result.as_string().c_str()).arg(d->tmpResult.toString()) );
//_a[0] = Kross::MetaTypeVariant<QVariant>(d->tmpResult).toVoidStar();
_a[0] = &(d->tmpResult);
} break;
}
_id -= 1;
......
......@@ -39,12 +39,45 @@ namespace Kross {
class PythonFunction : public QObject
{
public:
PythonFunction(QObject* sender, const QByteArray& sendersignal, const Py::Callable& callable);
/**
* Constructor.
*
* \param sender The QObject instance that sends the signal.
* \param signal The signature of the signal the QObject emits.
* \param callable The callable python function that should
* be executed if the QObject emits the signal.
*/
PythonFunction(QObject* sender, const QByteArray& signal, const Py::Callable& callable);
/**
* Destructor.
*/
virtual ~PythonFunction();
/**
* The static \a QMetaObject instance that provides the
* QMeta-information for this QObject class.
*/
QMetaObject staticMetaObject;
/**
* \return the dynamic build \a QMetaObject instance
* for this QObject instance.
*/
const QMetaObject *metaObject() const;
/**
* Try to cast this QObject instance into the class with
* name \p _clname and return the casted pointer or NULL
* if casting failed.
*/
void *qt_metacast(const char *_clname);
/**
* This method got called if a method this QObject instance
* defines should be invoked.
*/
int qt_metacall(QMetaObject::Call _c, int _id, void **_a);
private:
......
......@@ -296,7 +296,7 @@ void PythonInterpreter::extractException(QStringList& errorlist, int& lineno)
}
}
if(lineno < 0 && value && PyObject_HasAttrString(value, "lineno")) {
if(lineno < 0 && value && PyObject_HasAttrString(value, const_cast< char* >("lineno"))) {
PyObject *getobj = PyObject_GetAttrString(value, const_cast< char* >("lineno") );
if(getobj) {
lineno = PyInt_AsLong(getobj);
......
......@@ -150,6 +150,7 @@ class TestKross(unittest.TestCase):
def callback(s):
self.assert_(s == " The Argument String ")
return "GAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA!!!"
self.assert_( self.object1.connect("signalString(const QString&)", callback) )
self.object1.signalString(" The Argument String ")
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment