Refactored and added new class ScriptEnvironment
This commit is contained in:
parent
393ee4eb02
commit
fae8606d23
5 changed files with 86 additions and 31 deletions
|
@ -11,6 +11,7 @@ endif(COMMAND cmake_policy)
|
||||||
SET( telldus-center_SRCS
|
SET( telldus-center_SRCS
|
||||||
main.cpp
|
main.cpp
|
||||||
mainwindow.cpp
|
mainwindow.cpp
|
||||||
|
scriptenvironment.cpp
|
||||||
tellduscenterapplication.cpp
|
tellduscenterapplication.cpp
|
||||||
AutoUpdater.cpp
|
AutoUpdater.cpp
|
||||||
message.cpp
|
message.cpp
|
||||||
|
@ -26,6 +27,7 @@ SET( telldus-center_HDRS
|
||||||
|
|
||||||
SET( telldus-center_MOC_HDRS
|
SET( telldus-center_MOC_HDRS
|
||||||
mainwindow.h
|
mainwindow.h
|
||||||
|
scriptenvironment.h
|
||||||
tellduscenterapplication.h
|
tellduscenterapplication.h
|
||||||
message.h
|
message.h
|
||||||
)
|
)
|
||||||
|
|
37
telldus-gui/TelldusCenter/scriptenvironment.cpp
Normal file
37
telldus-gui/TelldusCenter/scriptenvironment.cpp
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
#include "scriptenvironment.h"
|
||||||
|
#include <QScriptEngine>
|
||||||
|
#include <QStringList>
|
||||||
|
|
||||||
|
#include <QDebug>
|
||||||
|
|
||||||
|
class ScriptEnvironment::PrivateData {
|
||||||
|
public:
|
||||||
|
QScriptEngine scriptEngine;
|
||||||
|
};
|
||||||
|
|
||||||
|
ScriptEnvironment::ScriptEnvironment(QObject *parent) :
|
||||||
|
QObject(parent)
|
||||||
|
{
|
||||||
|
d = new PrivateData;
|
||||||
|
|
||||||
|
connect(&d->scriptEngine, SIGNAL(signalHandlerException(const QScriptValue &)), this, SLOT(scriptException(const QScriptValue&)));
|
||||||
|
d->scriptEngine.installTranslatorFunctions();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
ScriptEnvironment::~ScriptEnvironment() {
|
||||||
|
delete d;
|
||||||
|
}
|
||||||
|
|
||||||
|
QScriptEngine *ScriptEnvironment::engine() const {
|
||||||
|
return &d->scriptEngine;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ScriptEnvironment::scriptException(const QScriptValue & exception) {
|
||||||
|
qDebug() << "ScriptException:" << d->scriptEngine.uncaughtExceptionLineNumber() << exception.toString();
|
||||||
|
qDebug() << "Backtrace:";
|
||||||
|
foreach( QString row, d->scriptEngine.uncaughtExceptionBacktrace() ) {
|
||||||
|
qDebug() << row;
|
||||||
|
}
|
||||||
|
d->scriptEngine.clearExceptions();
|
||||||
|
}
|
29
telldus-gui/TelldusCenter/scriptenvironment.h
Normal file
29
telldus-gui/TelldusCenter/scriptenvironment.h
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
#ifndef SCRIPTENVIRONMENT_H
|
||||||
|
#define SCRIPTENVIRONMENT_H
|
||||||
|
|
||||||
|
#include <QObject>
|
||||||
|
#include <QScriptValue>
|
||||||
|
|
||||||
|
class ScriptEnvironment : public QObject
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
public:
|
||||||
|
ScriptEnvironment(QObject *parent = 0);
|
||||||
|
~ScriptEnvironment();
|
||||||
|
|
||||||
|
QScriptEngine *engine() const;
|
||||||
|
|
||||||
|
signals:
|
||||||
|
|
||||||
|
public slots:
|
||||||
|
|
||||||
|
private slots:
|
||||||
|
void scriptException( const QScriptValue &exception );
|
||||||
|
|
||||||
|
private:
|
||||||
|
class PrivateData;
|
||||||
|
PrivateData *d;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // SCRIPTENVIRONMENT_H
|
|
@ -3,12 +3,13 @@
|
||||||
#include <QDir>
|
#include <QDir>
|
||||||
#include <QIcon>
|
#include <QIcon>
|
||||||
#include <QPluginLoader>
|
#include <QPluginLoader>
|
||||||
#include <QScriptEngine>
|
|
||||||
#include <QTranslator>
|
#include <QTranslator>
|
||||||
|
#include <QScriptEngine>
|
||||||
|
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include "tellduscenterplugin.h"
|
#include "tellduscenterplugin.h"
|
||||||
#include "plugintree.h"
|
#include "plugintree.h"
|
||||||
|
#include "scriptenvironment.h"
|
||||||
|
|
||||||
class TelldusCenterPlugin;
|
class TelldusCenterPlugin;
|
||||||
|
|
||||||
|
@ -19,7 +20,7 @@ class TelldusCenterApplicationPrivate {
|
||||||
public:
|
public:
|
||||||
PluginList plugins;
|
PluginList plugins;
|
||||||
MainWindow *mainWindow;
|
MainWindow *mainWindow;
|
||||||
QScriptEngine scriptEngine;
|
ScriptEnvironment scriptEnvironment;
|
||||||
};
|
};
|
||||||
|
|
||||||
TelldusCenterApplication::TelldusCenterApplication(int &argc, char **argv)
|
TelldusCenterApplication::TelldusCenterApplication(int &argc, char **argv)
|
||||||
|
@ -36,11 +37,8 @@ TelldusCenterApplication::~TelldusCenterApplication() {
|
||||||
|
|
||||||
void TelldusCenterApplication::initialize() {
|
void TelldusCenterApplication::initialize() {
|
||||||
d->mainWindow = new MainWindow( );
|
d->mainWindow = new MainWindow( );
|
||||||
|
|
||||||
this->setActivationWindow(d->mainWindow, false);
|
this->setActivationWindow(d->mainWindow, false);
|
||||||
|
|
||||||
connect(&d->scriptEngine, SIGNAL(signalHandlerException(const QScriptValue &)), this, SLOT(scriptException(const QScriptValue&)));
|
|
||||||
d->scriptEngine.installTranslatorFunctions();
|
|
||||||
|
|
||||||
loadPlugins();
|
loadPlugins();
|
||||||
loadScripts();
|
loadScripts();
|
||||||
|
@ -57,7 +55,7 @@ PluginList TelldusCenterApplication::plugins() const {
|
||||||
}
|
}
|
||||||
|
|
||||||
QScriptValue TelldusCenterApplication::mainWindow() {
|
QScriptValue TelldusCenterApplication::mainWindow() {
|
||||||
QScriptValue value = d->scriptEngine.newQObject(d->mainWindow);
|
QScriptValue value = d->scriptEnvironment.engine()->newQObject(d->mainWindow);
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -102,11 +100,11 @@ void TelldusCenterApplication::loadPlugins() {
|
||||||
|
|
||||||
this->setLibraryPaths( QStringList(pluginsDir.absolutePath()) );
|
this->setLibraryPaths( QStringList(pluginsDir.absolutePath()) );
|
||||||
|
|
||||||
QScriptValue object = d->scriptEngine.newQObject(this);
|
QScriptValue object = d->scriptEnvironment.engine()->newQObject(this);
|
||||||
d->scriptEngine.globalObject().setProperty("application", object);
|
d->scriptEnvironment.engine()->globalObject().setProperty("application", object);
|
||||||
|
|
||||||
QScriptValue mainWindowObject = d->scriptEngine.newQObject(d->mainWindow);
|
QScriptValue mainWindowObject = d->scriptEnvironment.engine()->newQObject(d->mainWindow);
|
||||||
d->scriptEngine.globalObject().property("application").setProperty("mainwindow", mainWindowObject);
|
d->scriptEnvironment.engine()->globalObject().property("application").setProperty("mainwindow", mainWindowObject);
|
||||||
|
|
||||||
foreach (QString fileName, pluginsDir.entryList(QDir::Files)) {
|
foreach (QString fileName, pluginsDir.entryList(QDir::Files)) {
|
||||||
QPluginLoader loader(pluginsDir.absoluteFilePath(fileName));
|
QPluginLoader loader(pluginsDir.absoluteFilePath(fileName));
|
||||||
|
@ -132,7 +130,7 @@ void TelldusCenterApplication::loadPlugin(QObject *plugin) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void TelldusCenterApplication::loadScripts() {
|
void TelldusCenterApplication::loadScripts() {
|
||||||
foreach (QString extension, d->scriptEngine.availableExtensions()) {
|
foreach (QString extension, d->scriptEnvironment.engine()->availableExtensions()) {
|
||||||
if (extension.startsWith("...")) {
|
if (extension.startsWith("...")) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -151,13 +149,13 @@ void TelldusCenterApplication::loadScripts() {
|
||||||
}
|
}
|
||||||
this->installTranslator(translator);
|
this->installTranslator(translator);
|
||||||
}
|
}
|
||||||
|
|
||||||
d->scriptEngine.importExtension( extension );
|
d->scriptEnvironment.engine()->importExtension( extension );
|
||||||
if (d->scriptEngine.hasUncaughtException()) {
|
if (d->scriptEnvironment.engine()->hasUncaughtException()) {
|
||||||
qDebug() << QString("Error in %1:%2:").arg(extension).arg(d->scriptEngine.uncaughtExceptionLineNumber())
|
qDebug() << QString("Error in %1:%2:").arg(extension).arg(d->scriptEnvironment.engine()->uncaughtExceptionLineNumber())
|
||||||
<< d->scriptEngine.uncaughtException().toString();
|
<< d->scriptEnvironment.engine()->uncaughtException().toString();
|
||||||
}
|
}
|
||||||
d->scriptEngine.clearExceptions();
|
d->scriptEnvironment.engine()->clearExceptions();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -185,16 +183,6 @@ TelldusCenterApplication *TelldusCenterApplication::instance() {
|
||||||
return (static_cast<TelldusCenterApplication *>(QCoreApplication::instance()));
|
return (static_cast<TelldusCenterApplication *>(QCoreApplication::instance()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void TelldusCenterApplication::scriptException(const QScriptValue & exception) {
|
|
||||||
qDebug() << "ScriptException:" << d->scriptEngine.uncaughtExceptionLineNumber() << exception.toString();
|
|
||||||
qDebug() << "Backtrace:";
|
|
||||||
foreach( QString row, d->scriptEngine.uncaughtExceptionBacktrace() ) {
|
|
||||||
qDebug() << row;
|
|
||||||
}
|
|
||||||
d->scriptEngine.clearExceptions();
|
|
||||||
}
|
|
||||||
|
|
||||||
void TelldusCenterApplication::msgReceived(const QString & message) {
|
void TelldusCenterApplication::msgReceived(const QString & message) {
|
||||||
this->showMainWindow();
|
this->showMainWindow();
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,7 +18,7 @@ class TelldusCenterApplication : public QtSingleApplication
|
||||||
public:
|
public:
|
||||||
TelldusCenterApplication(int &argc, char **argv);
|
TelldusCenterApplication(int &argc, char **argv);
|
||||||
virtual ~TelldusCenterApplication();
|
virtual ~TelldusCenterApplication();
|
||||||
|
|
||||||
void initialize();
|
void initialize();
|
||||||
|
|
||||||
#if defined(Q_WS_MAC)
|
#if defined(Q_WS_MAC)
|
||||||
|
@ -32,7 +32,7 @@ public:
|
||||||
void loadToolbar();
|
void loadToolbar();
|
||||||
|
|
||||||
static TelldusCenterApplication *instance();
|
static TelldusCenterApplication *instance();
|
||||||
|
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void allDoneLoading();
|
void allDoneLoading();
|
||||||
|
@ -47,7 +47,6 @@ public slots:
|
||||||
void showMainWindow();
|
void showMainWindow();
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void scriptException( const QScriptValue &exception );
|
|
||||||
void msgReceived ( const QString & message );
|
void msgReceived ( const QString & message );
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue