iLand
scriptglobal.h
Go to the documentation of this file.
1/********************************************************************************************
2** iLand - an individual based forest landscape and disturbance model
3** http://iland-model.org
4** Copyright (C) 2009- Werner Rammer, Rupert Seidl
5**
6** This program is free software: you can redistribute it and/or modify
7** it under the terms of the GNU General Public License as published by
8** the Free Software Foundation, either version 3 of the License, or
9** (at your option) any later version.
10**
11** This program is distributed in the hope that it will be useful,
12** but WITHOUT ANY WARRANTY; without even the implied warranty of
13** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14** GNU General Public License for more details.
15**
16** You should have received a copy of the GNU General Public License
17** along with this program. If not, see <http://www.gnu.org/licenses/>.
18********************************************************************************************/
19
20#ifndef SCRIPTGLOBAL_H
21#define SCRIPTGLOBAL_H
22
23#include <QObject>
24#include <QVariant>
25#include <QJSValue>
26
27
28// Scripting Interface for MapGrid
29class MapGrid; // forward
30class MapGridWrapper: public QObject
31{
32 Q_OBJECT
33 Q_PROPERTY(int valid READ isValid)
34 Q_PROPERTY(QString name READ name)
35public:
36 MapGridWrapper(QObject *parent=nullptr);
38 static void addToScriptEngine(QJSEngine &engine);
39 MapGrid *map() const { return mMap; }
40 bool isValid() const;
41 QString name() const;
42 //
43
44public slots:
45 // query
46 double area(int id);
47 // actions
48 void load(QString file_name);
49 void saveAsImage(QString file);
50 void paint(double min_value, double max_value);
51 // active modifications of the map
52 void clear();
53 void clearProjectArea();
57 void createStand(int stand_id, QString paint_function, bool wrap_around);
58
64 double copyPolygonFromRect(QJSValue source, int id_in, int id, double destx, double desty, double x1, double y1, double x2, double y2);
65
66 void createMapIndex();
67
68private:
69 MapGrid *mMap;
70 bool mCreated;
71
72};
73
77class Model;
78class ScriptResourceUnit; // forward
79class ScriptGlobal : public QObject
80{
81 Q_OBJECT
82 // read only properties
83 Q_PROPERTY(int year READ year)
84 Q_PROPERTY(int resourceUnitCount READ resourceUnitCount)
85 Q_PROPERTY(QString currentDir WRITE setCurrentDir READ currentDir)
86 Q_PROPERTY(double worldX READ worldX)
87 Q_PROPERTY(double worldY READ worldY)
88 Q_PROPERTY(bool qt5 READ qt5)
89 Q_PROPERTY(int msec READ msec)
90 Q_PROPERTY(QJSValue viewOptions READ viewOptions WRITE setViewOptions)
91
92
93public:
94 ScriptGlobal(QObject *parent=nullptr);
95 static void setupGlobalScripting();
96 // properties accesible by scripts
97 bool qt5() const {return true; }
98 int msec() const;
99 int year() const;
100 int resourceUnitCount() const;
101 QString currentDir() const { return mCurrentDir; }
102 void setCurrentDir(QString newDir) { mCurrentDir = newDir; }
103 double worldX();
104 double worldY();
105
106 // general functions
107 static void loadScript(const QString &fileName);
108 static QString executeScript(QString cmd);
109 static QString executeJSFunction(QString function);
110 static QObject *scriptOutput;
111 static QString formattedErrorMessage(const QJSValue &error_value, const QString &sourcecode);
112
113 // view options
114 /* View options:
115 * * type: {...}
116 * * species: bool
117 * * shade: bool
118 *
119 */
120 QJSValue viewOptions();
121 void setViewOptions(QJSValue opts);
122
123 // static functions
124 static QJSValue valueFromJs(const QJSValue &js_value, const QString &key, const QString default_value=QLatin1Literal(""), const QString &errorMessage=QLatin1Literal(""));
125 static QString JStoString(QJSValue value);
126
127public slots:
128 // system stuff
129 QVariant setting(QString key);
130 void set(QString key, QString value);
131 void print(QString message);
132 void alert(QString message);
133 void include(QString filename);
134 // file stuff
135 QString defaultDirectory(QString dir);
136 QString path(QString filename);
137 QString loadTextFile(QString fileName);
138 void saveTextFile(QString fileName, QString content);
139 bool fileExists(QString fileName);
140 void systemCmd(QString command);
141 // add trees
142 int addSingleTrees(const int resourceIndex, QString content);
143 int addTrees(const int resourceIndex, QString content);
144 int addTreesOnMap(const int standID, QString content);
145 // add saplings
146 int addSaplingsOnMap(MapGridWrapper *map, const int mapID, QString species, int px_per_hectare, double height, int age);
151 int addSaplings(int standId, double x, double y, double width, double height, QString species, double treeheight, int age);
153 void removeSaplings(int standId, double x, double y, double width, double height);
154 // enable/disable outputs
155 bool startOutput(QString table_name);
156 bool stopOutput(QString table_name);
157 // debug outputs
158 void debugOutputFilter(QList<int> ru_indices);
159 bool saveDebugOutputs(bool do_clear);
160 // miscellaneous stuff
161 void setViewport(double x, double y, double scale_px_per_m);
162 bool screenshot(QString file_name);
163 void repaint();
164 bool gridToFile(QString grid_type, QString file_name);
165
167 QJSValue grid(QString type);
169 QJSValue speciesShareGrid(QString species);
171 QJSValue resourceUnitGrid(QString expression);
172
174 QJSValue resourceUnit(int index);
175
176
177 // DOES NOT FULLY WORK
178 bool seedMapToFile(QString species, QString file_name);
179 void wait(int milliseconds);
180 // vegetation snapshots
181 bool saveModelSnapshot(QString file_name);
182 bool loadModelSnapshot(QString file_name);
183 bool saveStandSnapshot(int stand_id, QString file_name);
184 bool loadStandSnapshot(int stand_id, QString file_name);
185 bool saveStandCarbon(int stand_id, QList<int> ru_ids, bool rid_mode=true);
186 bool loadStandCarbon();
187 // agent-based-model of forest management
188 void reloadABE();
189
190 // UI interface
191 void setUIshortcuts(QJSValue shortcuts);
192
193 void test_tree_mortality(double thresh, int years, double p_death);
194private:
195 void throwError(const QString &errormessage);
196 QString mCurrentDir;
197 Model *mModel;
198 QJSValue mRUValue;
199 ScriptResourceUnit *mSRU;
200};
201
206class ScriptObjectFactory: public QObject
207{
208 Q_OBJECT
209public:
210 ScriptObjectFactory(QObject *parent=nullptr);
211public slots:
212 QJSValue newCSVFile(QString filename);
213 QJSValue newClimateConverter();
214 QJSValue newMap();
215 QJSValue newDBHDistribution();
216 QJSValue newGrid();
217 QJSValue newSpatialAnalysis();
218 int stats() {return mObjCreated;}
219private:
220 int mObjCreated;
221
222};
223
224#endif // SCRIPTGLOBAL_H
Definition: mapgrid.h:32
Definition: scriptglobal.h:31
double copyPolygonFromRect(QJSValue source, int id_in, int id, double destx, double desty, double x1, double y1, double x2, double y2)
copy a bit of the source-map 'source' to this map.
Definition: scriptglobal.cpp:378
QString name
Definition: scriptglobal.h:34
int valid
Definition: scriptglobal.h:33
~MapGridWrapper()
Definition: scriptglobal.cpp:267
void load(QString file_name)
Definition: scriptglobal.cpp:274
void createMapIndex()
call after creating stands with copyPolygonFromRect
Definition: scriptglobal.cpp:439
bool isValid() const
returns true if map is successfully loaded
Definition: scriptglobal.cpp:282
double area(int id)
retrieve the area (m2) of the polygon denoted by 'id'
Definition: scriptglobal.cpp:452
MapGridWrapper(QObject *parent=nullptr)
Definition: scriptglobal.cpp:258
MapGrid * map() const
acccess for C++ classes
Definition: scriptglobal.h:39
void paint(double min_value, double max_value)
Definition: scriptglobal.cpp:292
void clear()
clears the map (set all values to 0)
Definition: scriptglobal.cpp:303
static void addToScriptEngine(QJSEngine &engine)
Definition: scriptglobal.cpp:247
void createStand(int stand_id, QString paint_function, bool wrap_around)
paint a shape on the stand grid with id stand_id paint_function is a valid expression (paramters: x,...
Definition: scriptglobal.cpp:331
void clearProjectArea()
clear the project area (set to 0), but copy mask with pixels from "outside of project area" (i....
Definition: scriptglobal.cpp:314
void saveAsImage(QString file)
Definition: scriptglobal.cpp:287
Main object of the iLand model composited of various sub models / sub components.
Definition: model.h:90
This is a global interface providing useful functionality for javascripts.
Definition: scriptglobal.h:80
bool loadStandCarbon()
Definition: scriptglobal.cpp:871
void setViewOptions(QJSValue opts)
set current view options
Definition: scriptglobal.cpp:1010
bool qt5() const
is this the qt5-model? (changes in script object creation)
Definition: scriptglobal.h:97
QString currentDir
Definition: scriptglobal.h:85
static QString formattedErrorMessage(const QJSValue &error_value, const QString &sourcecode)
Definition: scriptglobal.cpp:979
void setUIshortcuts(QJSValue shortcuts)
set a list of JS shortcuts in the UI
Definition: scriptglobal.cpp:889
void set(QString key, QString value)
set the value of a setting
Definition: scriptglobal.cpp:104
static void setupGlobalScripting()
Definition: scriptglobal.cpp:1134
void test_tree_mortality(double thresh, int years, double p_death)
Definition: scriptglobal.cpp:898
QVariant setting(QString key)
get a value from the global xml-settings (returns undefined if not present)
Definition: scriptglobal.cpp:95
QString loadTextFile(QString fileName)
load content from a text file in a String (
Definition: scriptglobal.cpp:186
bool screenshot(QString file_name)
make a screenshot from the central viewing widget
Definition: scriptglobal.cpp:543
bool saveModelSnapshot(QString file_name)
saves a snapshot of the current model state (trees, soil, etc.) to a dedicated SQLite database.
Definition: scriptglobal.cpp:806
bool qt5
Definition: scriptglobal.h:88
void repaint()
force a repainting of the GUI visualization (if available)
Definition: scriptglobal.cpp:550
int addSaplingsOnMap(MapGridWrapper *map, const int mapID, QString species, int px_per_hectare, double height, int age)
Definition: scriptglobal.cpp:755
static void loadScript(const QString &fileName)
Definition: scriptglobal.cpp:917
int addTrees(const int resourceIndex, QString content)
add tree distribution
Definition: scriptglobal.cpp:226
bool loadModelSnapshot(QString file_name)
loads a snapshot of the current model state (trees, soil, etc.) from a dedicated SQLite database.
Definition: scriptglobal.cpp:820
QString defaultDirectory(QString dir)
get default directory of category 'dir'
Definition: scriptglobal.cpp:154
void removeSaplings(int standId, double x, double y, double width, double height)
remove all saplings from a metric rectangle. See also addSaplings().
Definition: scriptglobal.cpp:787
ScriptGlobal(QObject *parent=nullptr)
Definition: scriptglobal.cpp:82
bool loadStandSnapshot(int stand_id, QString file_name)
Definition: scriptglobal.cpp:846
QJSValue resourceUnitGrid(QString expression)
return a grid (level of resource units) with the result of an expression evaluated in the context of ...
Definition: scriptglobal.cpp:679
void setViewport(double x, double y, double scale_px_per_m)
set the viewport of the main project area view
Definition: scriptglobal.cpp:556
QJSValue speciesShareGrid(QString species)
return a grid with the basal area of the given species (resource unit resolution)
Definition: scriptglobal.cpp:659
void include(QString filename)
"include" the given script file and evaluate. The path is relative to the "script" path
Definition: scriptglobal.cpp:137
double worldY
Definition: scriptglobal.h:87
bool fileExists(QString fileName)
return true if the given file exists.
Definition: scriptglobal.cpp:194
void saveTextFile(QString fileName, QString content)
save string (content) to a text file.
Definition: scriptglobal.cpp:190
void print(QString message)
print the contents of the message to the log
Definition: scriptglobal.cpp:116
bool saveStandCarbon(int stand_id, QList< int > ru_ids, bool rid_mode=true)
Definition: scriptglobal.cpp:860
bool saveStandSnapshot(int stand_id, QString file_name)
Definition: scriptglobal.cpp:832
int year
Definition: scriptglobal.h:83
void reloadABE()
Definition: scriptglobal.cpp:883
void setCurrentDir(QString newDir)
set current working dir
Definition: scriptglobal.h:102
int addSingleTrees(const int resourceIndex, QString content)
add single trees
Definition: scriptglobal.cpp:215
bool gridToFile(QString grid_type, QString file_name)
create a "ESRI-grid" text file 'grid_type' is one of a fixed list of names, 'file_name' the ouptut fi...
Definition: scriptglobal.cpp:568
static QObject * scriptOutput
public "pipe" for script output (is redirected to GUI if available)
Definition: scriptglobal.h:110
QString currentDir() const
current execution directory (default is the Script execution directory)
Definition: scriptglobal.h:101
QJSValue viewOptions()
retrieve current viewing options (JS - object)
void debugOutputFilter(QList< int > ru_indices)
enable debug outputs for a list of resource units (output for other RUs are suppressed)
Definition: scriptglobal.cpp:519
double worldY()
extent of the world (without buffer) in meters (y-direction)
int resourceUnitCount
Definition: scriptglobal.h:84
QJSValue grid(QString type)
return Javascript grid for given type
Definition: scriptglobal.cpp:589
int addSaplings(int standId, double x, double y, double width, double height, QString species, double treeheight, int age)
add sapling on a metric rectangle given with width and height at x/y.
Definition: scriptglobal.cpp:768
static QJSValue valueFromJs(const QJSValue &js_value, const QString &key, const QString default_value=QLatin1Literal(""), const QString &errorMessage=QLatin1Literal(""))
Definition: scriptglobal.cpp:1102
static QString executeJSFunction(QString function)
Definition: scriptglobal.cpp:959
bool stopOutput(QString table_name)
stops output 'table_name'. return true if successful
Definition: scriptglobal.cpp:489
double worldX()
extent of the world (without buffer) in meters (x-direction)
bool saveDebugOutputs(bool do_clear)
save debug outputs to file; if do_clear=true then debug data is cleared from memorey
Definition: scriptglobal.cpp:536
double worldX
Definition: scriptglobal.h:86
int addTreesOnMap(const int standID, QString content)
add trees (distribution mode) for stand 'standID'
Definition: scriptglobal.cpp:235
void wait(int milliseconds)
wait for 'milliseconds' or (if ms=-1 until a key is pressed)
Definition: scriptglobal.cpp:745
static QString JStoString(QJSValue value)
Definition: scriptglobal.cpp:1123
void alert(QString message)
shows a message box to the user (if in GUI mode)
Definition: scriptglobal.cpp:130
QString path(QString filename)
get a path relative to the project main folder
Definition: scriptglobal.cpp:160
static QString executeScript(QString cmd)
Definition: scriptglobal.cpp:941
QJSValue resourceUnit(int index)
access to single resource unit (returns a reference)
Definition: scriptglobal.cpp:701
QJSValue viewOptions
Definition: scriptglobal.h:90
bool startOutput(QString table_name)
starts output 'table_name'. return true if successful
Definition: scriptglobal.cpp:459
bool seedMapToFile(QString species, QString file_name)
save the "seedmap" (i.e. a grid showing the seed distribution) as ESRI rastser file
Definition: scriptglobal.cpp:715
void systemCmd(QString command)
execute system command (e.g., copy files)
Definition: scriptglobal.cpp:199
int msec
Definition: scriptglobal.h:89
The ScriptObjectFactory can instantiate objects of other C++ (QObject-based) types.
Definition: scriptglobal.h:207
QJSValue newSpatialAnalysis()
creates a new SpatialAnalysis object and returns it
Definition: scriptglobal.cpp:1238
QJSValue newMap()
create new instance of Map and return it
Definition: scriptglobal.cpp:1215
ScriptObjectFactory(QObject *parent=nullptr)
Definition: scriptglobal.cpp:1186
int stats()
return the number of created objects
Definition: scriptglobal.h:218
QJSValue newCSVFile(QString filename)
create a new instance of CSVFile and return it
Definition: scriptglobal.cpp:1192
QJSValue newDBHDistribution()
create new instance of DBHDistribution and return it
Definition: scriptglobal.cpp:1224
QJSValue newClimateConverter()
create new instance of ClimateConverter and return it
Definition: scriptglobal.cpp:1205
QJSValue newGrid()
creates a new grid object and return it
Definition: scriptglobal.cpp:1232
Definition: scriptresourceunit.h:8