Subversion Repositories public iLand

Rev

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

Rev 760 Rev 776
Line 107... Line 107...
107
bool ModelController::isFinished()
107
bool ModelController::isFinished()
108
{
108
{
109
    if (!mModel)
109
    if (!mModel)
110
        return false;
110
        return false;
111
    return canRun() && !isRunning()  && mFinished;
111
    return canRun() && !isRunning()  && mFinished;
-
 
112
}
-
 
113
-
 
114
bool ModelController::isPaused()
-
 
115
{
-
 
116
    return mPaused;
112
}
117
}
113
118
114
int ModelController::currentYear() const
119
int ModelController::currentYear() const
115
{
120
{
116
    return GlobalSettings::instance()->currentYear();
121
    return GlobalSettings::instance()->currentYear();
Line 167... Line 172...
167
172
168
void ModelController::runloop()
173
void ModelController::runloop()
169
{
174
{
170
    static QTime sLastTime = QTime::currentTime();
175
    static QTime sLastTime = QTime::currentTime();
171
#ifdef ILAND_GUI
176
#ifdef ILAND_GUI
172
    QApplication::processEvents();
-
 
-
 
177
 //   QApplication::processEvents();
173
#else
178
#else
174
    QCoreApplication::processEvents();
-
 
-
 
179
 //   QCoreApplication::processEvents();
175
#endif
180
#endif
176
    if (mPaused)
181
    if (mPaused)
177
        return;
182
        return;
178
    bool doStop = false;
183
    bool doStop = false;
179
    bool hasError = false;
184
    bool hasError = false;
Line 181... Line 186...
181
        sLastTime = QTime::currentTime(); // reset clock at the beginning of the simulation
186
        sLastTime = QTime::currentTime(); // reset clock at the beginning of the simulation
182
    }
187
    }
183
188
184
    if (!mCanceled && GlobalSettings::instance()->currentYear() < mYearsToRun) {
189
    if (!mCanceled && GlobalSettings::instance()->currentYear() < mYearsToRun) {
185
        emit bufferLogs(true);
190
        emit bufferLogs(true);
186
        hasError = runYear(); // do the work
-
 
-
 
191
-
 
192
        hasError = runYear(); // do the work!
-
 
193
187
        mRunning = true;
194
        mRunning = true;
188
        emit year(GlobalSettings::instance()->currentYear());
195
        emit year(GlobalSettings::instance()->currentYear());
189
        if (!hasError) {
196
        if (!hasError) {
190
            int elapsed = sLastTime.msecsTo(QTime::currentTime());
197
            int elapsed = sLastTime.msecsTo(QTime::currentTime());
191
            int time=0;
198
            int time=0;
Line 195... Line 202...
195
                time = 500; // a 500ms pause...
202
                time = 500; // a 500ms pause...
196
            }
203
            }
197
            if (time>0) {
204
            if (time>0) {
198
                sLastTime = QTime::currentTime(); // reset clock
205
                sLastTime = QTime::currentTime(); // reset clock
199
                qDebug() << "--- little break ---- (after " << elapsed << "ms).";
206
                qDebug() << "--- little break ---- (after " << elapsed << "ms).";
-
 
207
                //QTimer::singleShot(time,this, SLOT(runloop()));
200
            }
208
            }
201
            QTimer::singleShot(time,this, SLOT(runloop()));
-
 
-
 
209
202
        }
210
        }
203
        else
211
        else
204
           doStop = true; // an error occured
212
           doStop = true; // an error occured
205
213
206
    } else {
214
    } else {
207
        doStop = true; // all years simulated
215
        doStop = true; // all years simulated
208
    }
216
    }
209
217
210
    if (doStop || mCanceled) {
218
    if (doStop || mCanceled) {
211
                // finished
219
                // finished
212
        mRunning = false;
-
 
213
        GlobalSettings::instance()->outputManager()->save();
-
 
214
        DebugTimer::printAllTimers();
-
 
215
        mFinished = true;
-
 
216
        emit bufferLogs(false); // stop buffering
-
 
217
        emit finished(QString());
-
 
-
 
220
        internalStop();
218
    }
221
    }
219
222
220
#ifdef ILAND_GUI
223
#ifdef ILAND_GUI
221
    QApplication::processEvents();
224
    QApplication::processEvents();
222
#else
225
#else
223
    QCoreApplication::processEvents();
226
    QCoreApplication::processEvents();
224
#endif
227
#endif
-
 
228
}
-
 
229
-
 
230
bool ModelController::internalRun()
-
 
231
{
-
 
232
    // main loop
-
 
233
-
 
234
    while (mRunning && !mPaused &&  !mFinished) {
-
 
235
        runloop(); // start the running loop
-
 
236
    }
-
 
237
    return isFinished();
-
 
238
}
-
 
239
-
 
240
void ModelController::internalStop()
-
 
241
{
-
 
242
    if (mRunning) {
-
 
243
        GlobalSettings::instance()->outputManager()->save();
-
 
244
        DebugTimer::printAllTimers();
-
 
245
        mFinished = true;
-
 
246
    }
-
 
247
    mRunning = false;
-
 
248
    emit bufferLogs(false); // stop buffering
-
 
249
    emit finished(QString());
-
 
250
    emit stateChanged();
-
 
251
225
}
252
}
226
253
227
void ModelController::run(int years)
254
void ModelController::run(int years)
228
{
255
{
229
    if (!canRun())
256
    if (!canRun())
Line 238... Line 265...
238
    //GlobalSettings::instance()->setCurrentYear(1); // reset clock
265
    //GlobalSettings::instance()->setCurrentYear(1); // reset clock
239
266
240
    DebugTimer::clearAllTimers();
267
    DebugTimer::clearAllTimers();
241
268
242
    mRunning = true;
269
    mRunning = true;
243
    while (mRunning && !mPaused && !mCanceled && !mFinished) {
-
 
244
        runloop(); // start the running loop
-
 
245
        qDebug() << "ModelControler: runloop started.";
-
 
246
    }
-
 
-
 
270
    emit stateChanged();
247
271
-
 
272
    qDebug() << "ModelControler: runloop started.";
-
 
273
    internalRun();
-
 
274
    emit stateChanged();
248
}
275
}
249
276
250
bool ModelController::runYear()
277
bool ModelController::runYear()
251
{
278
{
252
    if (!canRun()) return false;
279
    if (!canRun()) return false;
Line 278... Line 305...
278
        return mPaused;
305
        return mPaused;
279
306
280
    if (mPaused) {
307
    if (mPaused) {
281
        // currently in pause - mode -> continue
308
        // currently in pause - mode -> continue
282
        mPaused = false;
309
        mPaused = false;
283
        QTimer::singleShot(0,this, SLOT(runloop())); // continue loop
-
 
-
 
310
284
    } else {
311
    } else {
285
        // currently running -> set to pause mode
312
        // currently running -> set to pause mode
286
        GlobalSettings::instance()->outputManager()->save();
313
        GlobalSettings::instance()->outputManager()->save();
287
        mPaused = true;
314
        mPaused = true;
288
        emit bufferLogs(false);
315
        emit bufferLogs(false);
289
    }
316
    }
-
 
317
    emit stateChanged();
290
    return mPaused;
318
    return mPaused;
-
 
319
}
-
 
320
-
 
321
bool ModelController::continueRun()
-
 
322
{
-
 
323
    mRunning = true;
-
 
324
    emit stateChanged();
-
 
325
    return internalRun();
291
}
326
}
292
327
293
void ModelController::cancel()
328
void ModelController::cancel()
294
{
329
{
295
    mCanceled = true;
330
    mCanceled = true;
-
 
331
    internalStop();
-
 
332
    emit stateChanged();
296
}
333
}
297
334
298
QMutex error_mutex;
335
QMutex error_mutex;
299
void ModelController::throwError(const QString msg)
336
void ModelController::throwError(const QString msg)
300
{
337
{