Subversion Repositories public iLand

Rev

Rev 753 | Rev 779 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 753 Rev 767
Line 26... Line 26...
26
#include "tree.h"
26
#include "tree.h"
27
#include "expressionwrapper.h"
27
#include "expressionwrapper.h"
28
#include "sapling.h"
28
#include "sapling.h"
29
#include "soil.h"
29
#include "soil.h"
30
30
31
#include "climateconverter.h"
-
 
32
#include "csvfile.h"
-
 
-
 
31
//#include "climateconverter.h"
-
 
32
//#include "csvfile.h"
33
#include "scriptglobal.h"
33
#include "scriptglobal.h"
34
#include "mapgrid.h"
34
#include "mapgrid.h"
35
#include "modules.h"
-
 
-
 
35
//#include "modules.h"
36
36
37
#include <QtScript>
37
#include <QtScript>
38
#include <QTextEdit>
-
 
39
QObject *Management::scriptOutput = 0;
-
 
40
38
41
/** @class Management Management executes management routines.
39
/** @class Management Management executes management routines.
42
  @ingroup core
40
  @ingroup core
43
  The actual iLand management is based on Javascript functions. This class provides
41
  The actual iLand management is based on Javascript functions. This class provides
44
  the frame for executing the javascript as well as the functions that are called by scripts and
42
  the frame for executing the javascript as well as the functions that are called by scripts and
45
  that really do the work.
43
  that really do the work.
46
  See http://iland.boku.ac.at/iLand+scripting, http://iland.boku.ac.at/Object+Management for management Javascript API.
44
  See http://iland.boku.ac.at/iLand+scripting, http://iland.boku.ac.at/Object+Management for management Javascript API.
47
  */
45
  */
48
46
49
QScriptValue script_debug(QScriptContext *ctx, QScriptEngine *eng)
-
 
50
{
-
 
51
    QString value;
-
 
52
    for (int i = 0; i < ctx->argumentCount(); ++i) {
-
 
53
        if (i > 0)
-
 
54
            value.append(" ");
-
 
55
        value.append(ctx->argument(i).toString());
-
 
56
    }
-
 
57
    if (Management::scriptOutput) {
-
 
58
        QTextEdit *e = qobject_cast<QTextEdit*>(Management::scriptOutput);
-
 
59
        if (e)
-
 
60
            e->append(value);
-
 
61
    } else {
-
 
62
        qDebug() << "Script:" << value;
-
 
63
    }
-
 
64
    return eng->undefinedValue();
-
 
65
}
-
 
66
47
67
QScriptValue script_include(QScriptContext *ctx, QScriptEngine *eng)
-
 
68
{
-
 
69
-
 
70
    QString fileName = ctx->argument(0).toString();
-
 
71
    QString path =GlobalSettings::instance()->path(fileName, "script") ;
-
 
72
    QString includeFile=Helper::loadTextFile(path);
-
 
73
-
 
74
    ctx->setActivationObject(ctx->parentContext()->activationObject());
-
 
75
    ctx->setThisObject(ctx->parentContext()->thisObject());
-
 
76
-
 
77
    QScriptValue ret = eng->evaluate(includeFile, fileName);
-
 
78
    if (eng->hasUncaughtException())
-
 
79
        qDebug() << "Error in include:" << eng->uncaughtException().toString();
-
 
80
    return ret;
-
 
81
}
-
 
82
-
 
83
QScriptValue script_alert(QScriptContext *ctx, QScriptEngine *eng)
-
 
84
{
-
 
85
    QString value = ctx->argument(0).toString();
-
 
86
    Helper::msg(value);
-
 
87
    return eng->undefinedValue();
-
 
88
}
-
 
89
// global output function
48
// global output function
90
QString Management::executeScript(QString cmd)
49
QString Management::executeScript(QString cmd)
91
{
50
{
92
    DebugTimer t("execute javascript");
-
 
93
    if (mEngine)
-
 
94
        mEngine->evaluate(cmd);
-
 
95
    if (mEngine->hasUncaughtException()) {
-
 
96
        //int line = mEngine->uncaughtExceptionLineNumber();
-
 
97
        QString msg = QString( "Script Error occured: %1\n").arg( mEngine->uncaughtException().toString());
-
 
98
        msg+=mEngine->uncaughtExceptionBacktrace().join("\n");
-
 
99
        return msg;
-
 
100
    } else {
-
 
101
        return QString();
-
 
102
    }
-
 
-
 
51
    return ScriptGlobal::executeScript(cmd);
103
}
52
}
104
53
105
Management::Management()
54
Management::Management()
106
{
55
{
107
    // setup the scripting engine
56
    // setup the scripting engine
108
    mEngine = new QScriptEngine();
-
 
-
 
57
    mEngine = GlobalSettings::instance()->scriptEngine();
109
    QScriptValue objectValue = mEngine->newQObject(this);
58
    QScriptValue objectValue = mEngine->newQObject(this);
110
    QScriptValue dbgprint = mEngine->newFunction(script_debug);
-
 
111
    QScriptValue sinclude = mEngine->newFunction(script_include);
-
 
112
    QScriptValue alert = mEngine->newFunction(script_alert);
-
 
113
    mEngine->globalObject().setProperty("management", objectValue);
59
    mEngine->globalObject().setProperty("management", objectValue);
114
    mEngine->globalObject().setProperty("print",dbgprint);
-
 
115
    mEngine->globalObject().setProperty("include",sinclude);
-
 
116
    mEngine->globalObject().setProperty("alert", alert);
-
 
117
-
 
118
    // globals object: instatiate here, but ownership goes to script engine
-
 
119
    ScriptGlobal *global = new ScriptGlobal();
-
 
120
    QScriptValue glb = mEngine->newQObject(global,QScriptEngine::ScriptOwnership);
-
 
121
    mEngine->globalObject().setProperty("Globals", glb);
-
 
122
    // other object types
-
 
123
    ClimateConverter::addToScriptEngine(*mEngine);
-
 
124
    CSVFile::addToScriptEngine(*mEngine);
-
 
125
    MapGridWrapper::addToScriptEngine(*mEngine);
-
 
126
    // setup scripting for modules
-
 
127
    GlobalSettings::instance()->model()->modules()->setupScripting(mEngine);
-
 
128
60
129
    // default values for removal fractions
61
    // default values for removal fractions
130
    // 100% of the stem, 0% of foliage and branches
62
    // 100% of the stem, 0% of foliage and branches
131
    mRemoveFoliage = 0.;
63
    mRemoveFoliage = 0.;
132
    mRemoveBranch = 0.;
64
    mRemoveBranch = 0.;
133
    mRemoveStem = 1.;
65
    mRemoveStem = 1.;
134
-
 
135
66
136
}
67
}
137
68
138
Management::~Management()
69
Management::~Management()
139
{
70
{
140
    delete mEngine;
-
 
141
}
71
}
142
72
143
void Management::loadScript(const QString &fileName)
-
 
144
{
-
 
145
    mScriptFile = fileName;
-
 
146
    QString program = Helper::loadTextFile(fileName);
-
 
147
    if (program.isEmpty())
-
 
148
        return;
-
 
149
-
 
150
    mEngine->evaluate(program);
-
 
151
    qDebug() << "management script loaded";
-
 
152
    if (mEngine->hasUncaughtException())
-
 
153
        qDebug() << "Script Error occured: " << mEngine->uncaughtException().toString() << "\n" << mEngine->uncaughtExceptionBacktrace();
-
 
154
-
 
155
}
-
 
156
73
157
int Management::remain(int number)
74
int Management::remain(int number)
158
{
75
{
159
    qDebug() << "remain called (number): " << number;
76
    qDebug() << "remain called (number): " << number;
160
    Model *m = GlobalSettings::instance()->model();
77
    Model *m = GlobalSettings::instance()->model();
Line 360... Line 277...
360
        qDebug() << "Script Error occured: " << mEngine->uncaughtException().toString() << "\n" << mEngine->uncaughtExceptionBacktrace();
277
        qDebug() << "Script Error occured: " << mEngine->uncaughtException().toString() << "\n" << mEngine->uncaughtExceptionBacktrace();
361
278
362
    if (mRemoved>0) {
279
    if (mRemoved>0) {
363
        foreach(ResourceUnit *ru, GlobalSettings::instance()->model()->ruList())
280
        foreach(ResourceUnit *ru, GlobalSettings::instance()->model()->ruList())
364
           ru->cleanTreeList();
281
           ru->cleanTreeList();
365
   }
-
 
-
 
282
    }
-
 
283
}
-
 
284
-
 
285
void Management::loadScript(const QString &fileName)
-
 
286
{
-
 
287
    mScriptFile = fileName;
-
 
288
    ScriptGlobal::loadScript(fileName);
366
}
289
}
367
290
368
int Management::filter(QVariantList idList)
291
int Management::filter(QVariantList idList)
369
{
292
{
370
    QVector<int> ids;
293
    QVector<int> ids;