iLand
modelcontroller.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 MODELCONTROLLER_H
21#define MODELCONTROLLER_H
22#include <QObject>
23#include <QHash>
24#include "grid.h"
25#include "layeredgrid.h"
26class Model;
27class MainWindow;
28class MapGrid;
29class Species;
30
31class ModelController: public QObject
32{
33 Q_OBJECT
34public:
37 void setMainWindow(MainWindow *mw) { mViewerWindow = mw; }
38 MainWindow *mainWindow() { return mViewerWindow; }
39 void connectSignals(); // connect signal/slots to the main window if available
40 Model *model() const { return mModel; }
41 // bool checkers...
42 bool canCreate();
43 bool canDestroy();
44 bool canRun();
45 bool isRunning();
46 bool isStartingUp() { return mIsStartingUp; }
47 bool isFinished();
48 bool isPaused();
49 bool isBusy() { return mIsBusy; }
50 bool hasError() { return mHasError; }
51 QString lastError() { return mLastError; }
52 // simulation length
53 int currentYear() const;
54 int totalYears() const { return mYearsToRun; }
55 QString timeString() const;
56 // error handling
57 void throwError(const QString msg);
58 // dynamic outputs (variable fields)
59 void setDynamicOutputEnabled(bool enabled) { mDynamicOutputEnabled = enabled; }
60 void setupDynamicOutput(QString fieldList);
61 QString dynamicOutput();
62 // some informational services
63 QList<const Species *> availableSpecies();
64 void setLoadedJavascriptFile(QString filename) { mLastLoadedJSFile = filename; }
65 QString loadedJavascriptFile() const { return mLastLoadedJSFile; }
66
67
68 void saveScreenshot(QString file_name);
69 void addGrid(const FloatGrid *grid, const QString &name, const GridViewType view_type, double min_value, double max_value);
70 void paintMap(MapGrid *map, double min_value, double max_value);
71
72 void addLayers(const LayeredGridBase *layers, const QString &name);
73 void removeLayers(const LayeredGridBase *layers);
74 void addPaintLayers(QObject *handler, const QStringList names, const QVector<GridViewType> view_types=QVector<GridViewType>());
75 void removePaintLayers(QObject *handler);
76 Grid<double> *preparePaintGrid(QObject *handler, QString name, std::pair<QStringList, QStringList> *rNamesColors);
77 QStringList evaluateClick(QObject *handler, const QPointF coord, const QString &grid_name);
78 double valueAtHandledGrid(QObject *handler, const QPointF coord, const int layer_id);
79 void setViewport(QPointF center_point, double scale_px_per_m);
80
81 void setUIShortcuts(QVariantMap shortcuts);
82signals:
83 void finished(QString errorMessage);
84 void year(int year);
85 void bufferLogs(bool do_buffer);
86 void stateChanged();
87public slots:
88 void setFileName(QString initFileName);
89 void create();
90 void destroy();
91 void run(int years);
92 bool runYear();
93 bool pause();
94 bool continueRun();
95 void cancel();
96 void repaint();
97 void saveDebugOutputJs(bool do_clear);
98private slots:
99 void runloop();
100private:
101 bool internalRun();
102 void internalStop();
103 void fetchDynamicOutput();
104 void saveDebugOutputs(bool is_final);
105 void saveDebugOutputsCore(QString p, bool do_append);
106 MainWindow *mViewerWindow;
107 Model *mModel;
108 bool mPaused;
109 bool mRunning;
110 bool mFinished;
111 bool mCanceled;
112 bool mHasError;
113 bool mIsStartingUp;
114 bool mIsBusy;
115 QString mLastError;
116 int mYearsToRun;
117 QString mInitFile;
118 bool mDynamicOutputEnabled;
119 QStringList mDynFieldList;
120 QStringList mDynData;
121 QString mLastLoadedJSFile;
122 QTime mStartTime;
123
124};
125
126#endif // MODELCONTROLLER_H
Definition: layeredgrid.h:32
The main window of the iLand viewer.
Definition: mainwindow.h:21
Definition: mapgrid.h:32
Definition: modelcontroller.h:32
QStringList evaluateClick(QObject *handler, const QPointF coord, const QString &grid_name)
Definition: modelcontroller.cpp:690
bool canDestroy()
model may be destroyed
Definition: modelcontroller.cpp:99
double valueAtHandledGrid(QObject *handler, const QPointF coord, const int layer_id)
Definition: modelcontroller.cpp:695
void paintMap(MapGrid *map, double min_value, double max_value)
Definition: modelcontroller.cpp:601
bool runYear()
runs a single time step
Definition: modelcontroller.cpp:341
~ModelController()
Definition: modelcontroller.cpp:63
void setMainWindow(MainWindow *mw)
Definition: modelcontroller.h:37
void run(int years)
run the model
Definition: modelcontroller.cpp:318
void addPaintLayers(QObject *handler, const QStringList names, const QVector< GridViewType > view_types=QVector< GridViewType >())
Definition: modelcontroller.cpp:647
QList< const Species * > availableSpecies()
prepare a list of all (active) species
Definition: modelcontroller.cpp:78
QString lastError()
error message of the last received error
Definition: modelcontroller.h:51
bool isBusy()
model is running an operation (startup or simulation)
Definition: modelcontroller.h:49
void stateChanged()
is emitted when model started/stopped/paused
bool canCreate()
return true if the model can be created (settings loaded and model does not exist)
Definition: modelcontroller.cpp:92
void finished(QString errorMessage)
model has finished run (errorMessage is empty in case of success)
void bufferLogs(bool do_buffer)
signal indicating that logs should be buffered (true, model run mode) or that buffering should stop (...
Model * model() const
Definition: modelcontroller.h:40
void removeLayers(const LayeredGridBase *layers)
Definition: modelcontroller.cpp:636
Grid< double > * preparePaintGrid(QObject *handler, QString name, std::pair< QStringList, QStringList > *rNamesColors)
Definition: modelcontroller.cpp:668
void addGrid(const FloatGrid *grid, const QString &name, const GridViewType view_type, double min_value, double max_value)
Definition: modelcontroller.cpp:613
QString loadedJavascriptFile() const
Definition: modelcontroller.h:65
MainWindow * mainWindow()
Definition: modelcontroller.h:38
bool isPaused()
returns true if the model is currently paused
Definition: modelcontroller.cpp:123
bool pause()
pause execution, and if paused, continue to run. returns state after change, i.e. true=now in paused ...
Definition: modelcontroller.cpp:373
void setLoadedJavascriptFile(QString filename)
Definition: modelcontroller.h:64
QString timeString() const
return a string with elapsed time and estimated remaining time
Definition: modelcontroller.cpp:133
void setUIShortcuts(QVariantMap shortcuts)
Definition: modelcontroller.cpp:710
bool isRunning()
model is running
Definition: modelcontroller.cpp:111
void setFileName(QString initFileName)
set project file name
Definition: modelcontroller.cpp:146
void setDynamicOutputEnabled(bool enabled)
Definition: modelcontroller.h:59
void removePaintLayers(QObject *handler)
Definition: modelcontroller.cpp:658
void setViewport(QPointF center_point, double scale_px_per_m)
Definition: modelcontroller.cpp:700
void setupDynamicOutput(QString fieldList)
Definition: modelcontroller.cpp:427
void connectSignals()
Definition: modelcontroller.cpp:68
bool isFinished()
returns true if there is a valid model state, but the run is finished
Definition: modelcontroller.cpp:116
void saveScreenshot(QString file_name)
saves a screenshot of the central view widget to 'file_name'
Definition: modelcontroller.cpp:589
void repaint()
force a repaint of the main drawing window
Definition: modelcontroller.cpp:720
void year(int year)
signal indicating a year of the simulation has been processed
void create()
create the model
Definition: modelcontroller.cpp:160
bool hasError()
returns true if an error occured during the last operation
Definition: modelcontroller.h:50
bool continueRun()
continues execution if simulation was paused
Definition: modelcontroller.cpp:392
void destroy()
delete the model
Definition: modelcontroller.cpp:208
void cancel()
cancel execution of the model
Definition: modelcontroller.cpp:399
bool canRun()
model may be run
Definition: modelcontroller.cpp:104
bool isStartingUp()
model is in the creation phase
Definition: modelcontroller.h:46
int totalYears() const
returns total number of years to simulate
Definition: modelcontroller.h:54
QString dynamicOutput()
Definition: modelcontroller.cpp:447
int currentYear() const
return current year of the model
Definition: modelcontroller.cpp:128
void saveDebugOutputJs(bool do_clear)
save debug outputs, called from Javascript
Definition: modelcontroller.cpp:576
void addLayers(const LayeredGridBase *layers, const QString &name)
Definition: modelcontroller.cpp:626
void throwError(const QString msg)
Definition: modelcontroller.cpp:409
ModelController()
ModelController is a helper class used to control the flow of operations during a model run.
Definition: modelcontroller.cpp:50
Main object of the iLand model composited of various sub models / sub components.
Definition: model.h:90
The behavior and general properties of tree species.
Definition: species.h:75
GridViewType
Definition: grid.h:193