Added function ScriptEnvironment::currentDir()
This commit is contained in:
parent
594a51acce
commit
30713febc2
2 changed files with 18 additions and 12 deletions
|
@ -63,6 +63,12 @@ ScriptEnvironment::~ScriptEnvironment() {
|
||||||
delete d;
|
delete d;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QDir ScriptEnvironment::currentDir() const {
|
||||||
|
QScriptContextInfo info(d->scriptEngine.currentContext()->parentContext());
|
||||||
|
QFileInfo fileinfo(info.fileName());
|
||||||
|
return fileinfo.dir();
|
||||||
|
}
|
||||||
|
|
||||||
QScriptEngine *ScriptEnvironment::engine() const {
|
QScriptEngine *ScriptEnvironment::engine() const {
|
||||||
return &d->scriptEngine;
|
return &d->scriptEngine;
|
||||||
}
|
}
|
||||||
|
@ -77,9 +83,7 @@ void ScriptEnvironment::scriptException(const QScriptValue & exception) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void ScriptEnvironment::include(const QString &filename) {
|
void ScriptEnvironment::include(const QString &filename) {
|
||||||
QScriptContextInfo info(d->scriptEngine.currentContext()->parentContext());
|
QDir dir = this->currentDir();
|
||||||
QFileInfo fileinfo(info.fileName());
|
|
||||||
QDir dir = fileinfo.dir();
|
|
||||||
|
|
||||||
QFile file(dir.filePath(filename));
|
QFile file(dir.filePath(filename));
|
||||||
file.open(QFile::ReadOnly);
|
file.open(QFile::ReadOnly);
|
||||||
|
@ -106,18 +110,18 @@ void ScriptEnvironment::timerEvent(QTimerEvent *event) {
|
||||||
if(remainingDelay > 0){
|
if(remainingDelay > 0){
|
||||||
return; //just run same timer again with same interval (max int)
|
return; //just run same timer again with same interval (max int)
|
||||||
}
|
}
|
||||||
|
|
||||||
TimerObj *to = d->timeoutHash.value(id);
|
TimerObj *to = d->timeoutHash.value(id);
|
||||||
d->timeoutHash.remove(to->originalTimerId);
|
d->timeoutHash.remove(to->originalTimerId);
|
||||||
int newTimerId = startTimer(delay); //delay differs from last time, start a new timer
|
int newTimerId = startTimer(delay); //delay differs from last time, start a new timer
|
||||||
killTimer(id);
|
killTimer(id);
|
||||||
|
|
||||||
d->timeoutHash.insert(newTimerId, to);
|
d->timeoutHash.insert(newTimerId, to);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
expression = d->timeoutHash.value(id)->expression;
|
expression = d->timeoutHash.value(id)->expression;
|
||||||
this->clearTimeout(d->timeoutHash.value(id)->originalTimerId);
|
this->clearTimeout(d->timeoutHash.value(id)->originalTimerId);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (expression.isString()) {
|
if (expression.isString()) {
|
||||||
|
@ -129,11 +133,11 @@ void ScriptEnvironment::timerEvent(QTimerEvent *event) {
|
||||||
|
|
||||||
int ScriptEnvironment::setTimeout(const QScriptValue &expression, qint64 delay) {
|
int ScriptEnvironment::setTimeout(const QScriptValue &expression, qint64 delay) {
|
||||||
if (expression.isString() || expression.isFunction()) {
|
if (expression.isString() || expression.isFunction()) {
|
||||||
|
|
||||||
if (delay < 0) {
|
if (delay < 0) {
|
||||||
delay = 0;
|
delay = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
TimerObj *to = new TimerObj;
|
TimerObj *to = new TimerObj;
|
||||||
to->expression = expression;
|
to->expression = expression;
|
||||||
to->remainingDelay = delay - std::numeric_limits<int>::max();
|
to->remainingDelay = delay - std::numeric_limits<int>::max();
|
||||||
|
@ -142,7 +146,7 @@ int ScriptEnvironment::setTimeout(const QScriptValue &expression, qint64 delay)
|
||||||
}
|
}
|
||||||
int timerId = startTimer(delay);
|
int timerId = startTimer(delay);
|
||||||
to->originalTimerId = timerId;
|
to->originalTimerId = timerId;
|
||||||
|
|
||||||
d->timeoutHash.insert(timerId, to);
|
d->timeoutHash.insert(timerId, to);
|
||||||
return timerId;
|
return timerId;
|
||||||
}
|
}
|
||||||
|
@ -166,7 +170,7 @@ void ScriptEnvironment::clearTimeout(int timerId) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
killTimer(timerId);
|
killTimer(timerId);
|
||||||
if(!found){
|
if(!found){
|
||||||
return;
|
return;
|
||||||
|
@ -193,4 +197,4 @@ int ScriptEnvironment::setInterval(const QScriptValue &expression, int delay) {
|
||||||
void ScriptEnvironment::clearInterval(int timerId) {
|
void ScriptEnvironment::clearInterval(int timerId) {
|
||||||
killTimer(timerId);
|
killTimer(timerId);
|
||||||
d->intervalHash.remove(timerId);
|
d->intervalHash.remove(timerId);
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
#include <QScriptValue>
|
#include <QScriptValue>
|
||||||
|
#include <QDir>
|
||||||
|
|
||||||
class ScriptEnvironment : public QObject
|
class ScriptEnvironment : public QObject
|
||||||
{
|
{
|
||||||
|
@ -12,6 +13,7 @@ public:
|
||||||
~ScriptEnvironment();
|
~ScriptEnvironment();
|
||||||
|
|
||||||
QScriptEngine *engine() const;
|
QScriptEngine *engine() const;
|
||||||
|
QDir currentDir() const;
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue