1
0
mirror of https://github.com/thp/pyotherside.git synced 2025-01-28 23:52:55 +08:00

Fix build, move worker code to qpython_worker

This commit is contained in:
Thomas Perl 2013-08-08 18:06:19 +02:00
parent ad62b4dc2b
commit defbab1467
8 changed files with 108 additions and 47 deletions

View File

@ -16,8 +16,11 @@
* PERFORMANCE OF THIS SOFTWARE. * PERFORMANCE OF THIS SOFTWARE.
**/ **/
#include "pyotherside_plugin.h"
#include "qpython_priv.h" #include "qpython_priv.h"
#include "qpython.h"
#include "pyotherside_plugin.h"
static void static void
pyotherside_atexit() pyotherside_atexit()

View File

@ -20,55 +20,13 @@
#include "qpython.h" #include "qpython.h"
#include "qpython_priv.h" #include "qpython_priv.h"
#include "qpython_worker.h"
#include <QDebug> #include <QDebug>
#include <QJSEngine> #include <QJSEngine>
class QPythonWorker : public QObject {
Q_OBJECT
public:
QPythonWorker(QPython *qpython);
~QPythonWorker();
public slots:
void process(QString func, QVariant args, QJSValue callback);
void import(QString func, QJSValue callback);
signals:
void finished(QVariant result, QJSValue callback);
void imported(bool result, QJSValue callback);
private:
QPython *qpython;
};
QPythonWorker::QPythonWorker(QPython *qpython)
: QObject()
, qpython(qpython)
{
}
QPythonWorker::~QPythonWorker()
{
}
void
QPythonWorker::process(QString func, QVariant args, QJSValue callback)
{
QVariant result = qpython->call_sync(func, args);
emit finished(result, callback);
}
void
QPythonWorker::import(QString name, QJSValue callback)
{
bool result = qpython->importModule_sync(name);
emit imported(result, callback);
}
QPythonPriv * QPythonPriv *
QPython::priv = NULL; QPython::priv = NULL;

View File

@ -259,9 +259,11 @@ class QPython : public QObject {
**/ **/
void error(QString traceback); void error(QString traceback);
private slots: /* For internal use only */
void process(QString func, QVariant args, QJSValue callback); void process(QString func, QVariant args, QJSValue callback);
void import(QString name, QJSValue callback); void import(QString name, QJSValue callback);
private slots:
void receive(QVariant data); void receive(QVariant data);
void finished(QVariant result, QJSValue callback); void finished(QVariant result, QJSValue callback);

View File

@ -192,7 +192,7 @@ QPythonPriv::eval(QString expr)
void void
QPythonPriv::closing() QPythonPriv::closing()
{ {
enter(); priv->enter();
if (priv->atexit_callback != NULL) { if (priv->atexit_callback != NULL) {
PyObject *args = PyTuple_New(0); PyObject *args = PyTuple_New(0);
PyObject *result = PyObject_Call(priv->atexit_callback, args, NULL); PyObject *result = PyObject_Call(priv->atexit_callback, args, NULL);
@ -202,6 +202,6 @@ QPythonPriv::closing()
Py_DECREF(priv->atexit_callback); Py_DECREF(priv->atexit_callback);
priv->atexit_callback = NULL; priv->atexit_callback = NULL;
} }
leave(); priv->leave();
} }

46
src/qpython_worker.cpp Normal file
View File

@ -0,0 +1,46 @@
/**
* PyOtherSide: Asynchronous Python 3 Bindings for Qt 5
* Copyright (c) 2011, 2013, Thomas Perl <m@thp.io>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
**/
#include "qpython.h"
#include "qpython_worker.h"
QPythonWorker::QPythonWorker(QPython *qpython)
: QObject()
, qpython(qpython)
{
}
QPythonWorker::~QPythonWorker()
{
}
void
QPythonWorker::process(QString func, QVariant args, QJSValue callback)
{
QVariant result = qpython->call_sync(func, args);
emit finished(result, callback);
}
void
QPythonWorker::import(QString name, QJSValue callback)
{
bool result = qpython->importModule_sync(name);
emit imported(result, callback);
}

48
src/qpython_worker.h Normal file
View File

@ -0,0 +1,48 @@
/**
* PyOtherSide: Asynchronous Python 3 Bindings for Qt 5
* Copyright (c) 2011, 2013, Thomas Perl <m@thp.io>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
**/
#ifndef PYOTHERSIDE_QPYTHON_WORKER_H
#define PYOTHERSIDE_QPYTHON_WORKER_H
#include <QObject>
#include <QString>
#include <QVariant>
#include <QJSValue>
class QPython;
class QPythonWorker : public QObject {
Q_OBJECT
public:
QPythonWorker(QPython *qpython);
~QPythonWorker();
public slots:
void process(QString func, QVariant args, QJSValue callback);
void import(QString func, QJSValue callback);
signals:
void finished(QVariant result, QJSValue callback);
void imported(bool result, QJSValue callback);
private:
QPython *qpython;
};
#endif /* PYOTHERSIDE_QPYTHON_WORKER_H */

View File

@ -23,6 +23,8 @@ HEADERS += pyotherside_plugin.h
# Python QML Object # Python QML Object
SOURCES += qpython.cpp SOURCES += qpython.cpp
HEADERS += qpython.h HEADERS += qpython.h
SOURCES += qpython_worker.cpp
HEADERS += qpython_worker.h
SOURCES += qpython_priv.cpp SOURCES += qpython_priv.cpp
HEADERS += qpython_priv.h HEADERS += qpython_priv.h

View File

@ -5,9 +5,11 @@ SOURCES += tests.cpp
HEADERS += tests.h HEADERS += tests.h
SOURCES += ../src/qpython.cpp SOURCES += ../src/qpython.cpp
SOURCES += ../src/qpython_worker.cpp
SOURCES += ../src/qpython_priv.cpp SOURCES += ../src/qpython_priv.cpp
HEADERS += ../src/qpython.h HEADERS += ../src/qpython.h
HEADERS += ../src/qpython_worker.h
HEADERS += ../src/qpython_priv.h HEADERS += ../src/qpython_priv.h
HEADERS += ../src/converter.h HEADERS += ../src/converter.h
HEADERS += ../src/qvariant_converter.h HEADERS += ../src/qvariant_converter.h