MainWindow.cpp 63.9 KB
Newer Older
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1
/**
2
 * @file MainWindow.cpp
Ulrich Kemloh's avatar
Ulrich Kemloh committed
3
4
5
6
7
 * @author   Ulrich Kemloh <kemlohulrich@gmail.com>
 * @version 0.1
 * Copyright (C) <2009-2010>
 *
 * @section LICENSE
Mohcine Chraibi's avatar
Mohcine Chraibi committed
8
 * This file is part of JuPedSim.
Ulrich Kemloh's avatar
Ulrich Kemloh committed
9
 *
10
 * JuPedSim is free software: you can redistribute it and/or modify
Ulrich Kemloh's avatar
Ulrich Kemloh committed
11
12
13
14
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
15
 * JuPedSim is distributed in the hope that it will be useful,
Ulrich Kemloh's avatar
Ulrich Kemloh committed
16
17
18
19
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
21
 * along with JuPedSim. If not, see <http://www.gnu.org/licenses/>.
Ulrich Kemloh's avatar
Ulrich Kemloh committed
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
 *
 * @section DESCRIPTION
 *
 * \brief main program class
 *
 *
 *  Created on: 11.05.2009
 *
 */


#include "MainWindow.h"
#include "SaxParser.h"
#include "SystemSettings.h"
#include "./forms/Settings.h"
Mohcine Chraibi's avatar
Mohcine Chraibi committed
37
#include <sstream>
Ulrich Kemloh's avatar
Ulrich Kemloh committed
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63


#include "extern_var.h"
#include "TrajectoryPoint.h"
#include "Frame.h"
#include "Debug.h"
#include "ThreadVisualisation.h"
#include "ThreadDataTransfert.h"
#include "SimpleVisualisationWindow.h"

#include "geometry/FacilityGeometry.h"

#include <QMessageBox>
#include <QtXml/QDomDocument>
#include <QTime>
#include <QApplication>
#include <QCloseEvent>
#include <QThread>
#include <QIODevice>
#include <QDir>
#include <QFile>
#include <QFileDialog>
#include <QInputDialog>
#include <QString>
#include <QColorDialog>
#include <QtXml>
64
#include <QTemporaryFile>
65
66
67
68
69
70
#include <QListView>
#include <QFileSystemModel>
#include <QTableView>
#include <QSplitter>
#include <QStandardItemModel>

71

Ulrich Kemloh's avatar
Ulrich Kemloh committed
72
73
74
75
76
77
78
#include <iostream>
#include <limits>
#include <string>
#include <vector>

using namespace std;

Mohcine Chraibi's avatar
Mohcine Chraibi committed
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//----------- @todo: this part is copy/paste from main.cpp.
std::string ver_string1(int a, int b, int c) {
      std::ostringstream ss;
      ss << a << '.' << b << '.' << c;
      return ss.str();
}

std::string true_cxx1 =
#ifdef __clang__
      "clang++";
#elif defined(__GNUC__)
"g++";
#elif defined(__MINGW32__)
   "MinGW";
#elif defined(_MSC_VER)
  "Visual Studio";
#else
"Compiler not identified";
#endif

std::string true_cxx_ver1 =
#ifdef __clang__
    ver_string1(__clang_major__, __clang_minor__, __clang_patchlevel__);
#elif defined(__GNUC__)
    ver_string1(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__);
#elif defined(__MINGW32__)
ver_string1(__MINGW32__, __MINGW32_MAJOR_VERSION, __MINGW32_MINOR_VERSION);
#elif defined( _MSC_VER)
    ver_string1(_MSC_VER, _MSC_FULL_VER,_MSC_BUILD);
#else
"";
#endif
//----------
112
113
114
115
116
117
118
119
120
121
122
123

#ifdef __APPLE__
#include <thread>
#include <dispatch/dispatch.h>
#include "fix/osx_thread_fix.h"

std::thread::id main_thread_id = std::this_thread::get_id();
dispatch_queue_t main_q = dispatch_get_main_queue();

void is_main_thread() {

    if ( main_thread_id == std::this_thread::get_id() )
Mohcine Chraibi's avatar
Mohcine Chraibi committed
124
125
126
    {
         std::cout << "This is the main thread.\n";
    }
127
128
129
130
131
132
    else
        std::cout << "This is not the main thread.\n";
}
#endif


Ulrich Kemloh's avatar
Ulrich Kemloh committed
133
MainWindow::MainWindow(QWidget *parent) :
Ulrich Kemloh's avatar
Ulrich Kemloh committed
134
135
    QMainWindow(parent)
{
Ulrich Kemloh's avatar
Ulrich Kemloh committed
136
137
138
139
    ui.setupUi(this);
    this->setWindowTitle("JPSvis");

    //disable the maximize Button
140
141
142
    setWindowFlags( Qt::Window | Qt::WindowMinimizeButtonHint | Qt::WindowCloseButtonHint);

    //used for saving the settings in a persistant way
Mohcine Chraibi's avatar
Mohcine Chraibi committed
143
    QCoreApplication::setOrganizationName("Forschungszentrum_Juelich_GmbH");
144
145
    QCoreApplication::setOrganizationDomain("jupedsim.org");
    QCoreApplication::setApplicationName("jupedsim");
Ulrich Kemloh's avatar
Ulrich Kemloh committed
146
147
148

    //create the 2 threads and connect them
    dataTransferThread = new ThreadDataTransfer(this);
149
    _visualisationThread = new ThreadVisualisation(this);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
150
151
152
153
154
155
156
157
158


    travistoOptions = new Settings(this);
    travistoOptions->setWindowTitle("Settings");
    travistoOptions->setWindowFlags(Qt::Window | Qt::WindowCloseButtonHint);


    if(!QObject::connect(dataTransferThread,
                         SIGNAL(signal_controlSequence(const char*)), this,
Ulrich Kemloh's avatar
Ulrich Kemloh committed
159
                         SLOT(slotControlSequence(const char *)))) {
Ulrich Kemloh's avatar
Ulrich Kemloh committed
160
161
162
163
164
        Debug::Error("dataTransferThread Thread: control sequence could not be connected");
    }

    if(!QObject::connect(dataTransferThread,
                         SIGNAL(signal_startVisualisationThread(QString,int,float )), this,
Ulrich Kemloh's avatar
Ulrich Kemloh committed
165
                         SLOT(slotStartVisualisationThread(QString,int,float )))) {
Ulrich Kemloh's avatar
Ulrich Kemloh committed
166
167
168
169
170
        Debug::Error(" signal_startVisualisationThread not connected");
    }

    if(!QObject::connect(dataTransferThread,
                         SIGNAL(signal_stopVisualisationThread(bool )), this,
Ulrich Kemloh's avatar
Ulrich Kemloh committed
171
                         SLOT(slotShutdownVisualisationThread(bool )))) {
Ulrich Kemloh's avatar
Ulrich Kemloh committed
172
173
174
175
176
        Debug::Error(" signal_stopVisualisationThread not connected ");
    }

    if(!QObject::connect(dataTransferThread,
                         SIGNAL(signal_errorMessage(QString)), this,
Ulrich Kemloh's avatar
Ulrich Kemloh committed
177
                         SLOT(slotErrorOutput(QString)))) {
Ulrich Kemloh's avatar
Ulrich Kemloh committed
178
179
180
        Debug::Error("signal_errorMessage  not connected ");
    }

181
    QObject::connect(_visualisationThread,
Ulrich Kemloh's avatar
Ulrich Kemloh committed
182
183
184
185
186
187
188
189
190
191
192
193
194
                     SIGNAL(signal_controlSequences(const char*)), this,
                     SLOT(slotControlSequence(const char *)));

    QObject::connect(&extern_trajectories_firstSet,
                     SIGNAL(signal_controlSequences(const char*)), this,
                     SLOT(slotControlSequence(const char *)));

    QObject::connect(dataTransferThread, SIGNAL(signal_CurrentAction(QString)),
                     this, SLOT(slotCurrentAction(QString)));

    QObject::connect(this, SIGNAL(signal_controlSequence(QString)),
                     travistoOptions, SLOT(slotControlSequence(QString)));

195

196
    QObject::connect(&_visualisationThread->getGeometry().GetModel(),
197
198
                     SIGNAL(itemChanged(QStandardItem*)),this,SLOT(slotOnGeometryItemChanged(QStandardItem*)));

Ulrich Kemloh's avatar
Ulrich Kemloh committed
199
200
201
202
203
    isPlaying = false;
    isPaused = false;
    numberOfDatasetLoaded=0;
    frameSliderHold=false;

Ulrich Kemloh's avatar
Ulrich Kemloh committed
204
205
    //some hand made stuffs
    ui.BtFullscreen->setVisible(false);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232

    labelCurrentAction = new QLabel();
    labelCurrentAction->setFrameStyle(QFrame::Panel | QFrame::Sunken);
    labelCurrentAction->setText("   Idle   ");
    statusBar()->addPermanentWidget(labelCurrentAction);

    labelFrameNumber = new QLabel();
    labelFrameNumber->setFrameStyle(QFrame::Panel | QFrame::Sunken);
    labelFrameNumber->setText("fps:");
    statusBar()->addPermanentWidget(labelFrameNumber);

    labelRecording = new QLabel();
    labelRecording->setFrameStyle(QFrame::Panel | QFrame::Sunken);
    labelRecording->setText(" rec: off ");
    statusBar()->addPermanentWidget(labelRecording);

    labelMode = new QLabel();
    labelMode->setFrameStyle(QFrame::Panel | QFrame::Sunken);
    labelMode->setText(" Offline ");
    statusBar()->addPermanentWidget(labelMode);


    ui.BtNextFrame->setVisible(false);
    ui.BtPreviousFrame->setVisible(false);
    //TODO: Maybe this is not longer needed
    ui.framePerSecondSliderLabel->setVisible(false);
    ui.contrastSettingSlider->setVisible(false);
233
234
    //restore the settings
    loadAllSettings();
Ulrich Kemloh's avatar
Ulrich Kemloh committed
235
236
237
238
239

    QStringList arguments =QApplication::arguments();
    int group=1; // there are max 3 groups of pedestrians
    bool mayPlay=false;

240
241
    //arguments.append("-online");
    //arguments.append("-caption");
Ulrich Kemloh's avatar
Ulrich Kemloh committed
242
243
244
    arguments.append("-2D");
    // parse arguments list
    if(arguments.size()>1)
Ulrich Kemloh's avatar
Ulrich Kemloh committed
245
        for(int argCount=1; argCount<arguments.size(); argCount++) {
Ulrich Kemloh's avatar
Ulrich Kemloh committed
246
247
248
249

            QString argument=arguments[argCount];

            if(argument.compare("help")==0) {
Mohcine Chraibi's avatar
Mohcine Chraibi committed
250
                Debug::Messages("Usage: jpsvis [file1] [-2D] [-caption] [-online [port]]");
Mohcine Chraibi's avatar
Mohcine Chraibi committed
251
                exit(0);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
252
            } else if(argument.compare("-2D")==0) {
Ulrich Kemloh's avatar
Ulrich Kemloh committed
253
254
                ui.action2_D->setChecked(true);
                slotToogle2D();
Ulrich Kemloh's avatar
Ulrich Kemloh committed
255
            } else if(argument.compare("-caption")==0) {
Ulrich Kemloh's avatar
Ulrich Kemloh committed
256
257
258
                ui.actionShow_Captions->setChecked(true);
                slotShowPedestrianCaption();

Ulrich Kemloh's avatar
Ulrich Kemloh committed
259
            } else if(argument.compare("-online")==0) {
Ulrich Kemloh's avatar
Ulrich Kemloh committed
260
                slotSetOnlineMode(true);
261
                Debug::Messages("Online option parsed");
Ulrich Kemloh's avatar
Ulrich Kemloh committed
262
                // checking for other possible options [ port...]
Ulrich Kemloh's avatar
Ulrich Kemloh committed
263
                if(argCount!=arguments.size()-1) {
Ulrich Kemloh's avatar
Ulrich Kemloh committed
264
265
                    bool ok=false;
                    int port = arguments[++argCount].toInt(&ok);
266
                    Debug::Messages(" listening port: %d",port);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
267
268

                    if (ok) {
269
                        SystemSettings::setListeningPort(port);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
270
                    } else { // maybe that wasnt the port
Ulrich Kemloh's avatar
Ulrich Kemloh committed
271
272
273
                        argCount--; // to ensure that the "option" will be checked
                    }
                }
274
                //mayPlay=true;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
275

Ulrich Kemloh's avatar
Ulrich Kemloh committed
276
            } else if(argument.startsWith("-")) {
Ulrich Kemloh's avatar
Ulrich Kemloh committed
277
                const char* std=argument.toStdString().c_str();
Mohcine Chraibi's avatar
Mohcine Chraibi committed
278
                Debug::Error("unknown options: %s",std);
Mohcine Chraibi's avatar
Mohcine Chraibi committed
279
                Debug::Error("Usage: jpsvis [file1] [-2D] [-caption] [-online [port] ]");
Ulrich Kemloh's avatar
Ulrich Kemloh committed
280
            } else if(addPedestrianGroup(group,argument)) {
Ulrich Kemloh's avatar
Ulrich Kemloh committed
281
                //slotHelpAbout();
282
                 Debug::Messages("group: %d, arg: %s", group, argument.toStdString().c_str());
Ulrich Kemloh's avatar
Ulrich Kemloh committed
283
284
285
286
287
                group++;
                mayPlay=true;
            }

        }
288
    Debug::Messages("MayPlay: %s", mayPlay?"True":"False");
289
290
291
292
293
294
295
    // was call from the command line with a file.
    // disable the online mode if it was enabled
    if(mayPlay)
    {
        slotSetOfflineMode(true);
        slotStartPlaying();
    }
Ulrich Kemloh's avatar
Ulrich Kemloh committed
296
297
}

Ulrich Kemloh's avatar
Ulrich Kemloh committed
298
299
MainWindow::~MainWindow()
{
Ulrich Kemloh's avatar
Ulrich Kemloh committed
300
301
302
    extern_shutdown_visual_thread=true;
    extern_recording_enable=false;

303
    //save all settings for the next session
304
305
306
    if(ui.actionRemember_Settings->isChecked())
    {
        saveAllSettings();
Mohcine Chraibi's avatar
Mohcine Chraibi committed
307
        Debug::Messages("saving all settings");
308
309
310
311
312
313
314
315
    }
    else
    {
        //first clear everyting
        QSettings settings;
        settings.clear();
        //then remember that we do not want any settings saved
        settings.setValue("options/rememberSettings", false);
Mohcine Chraibi's avatar
Mohcine Chraibi committed
316
        Debug::Messages("clearing all settings");
317
    }
318

Ulrich Kemloh's avatar
Ulrich Kemloh committed
319

320
    if (_visualisationThread->isRunning()) {
Ulrich Kemloh's avatar
Ulrich Kemloh committed
321
322
323
324
325
326
327
328
329
330
331
        //std::Debug::Messages("Thread  visualisation is still running" << std::endl;
        waitForVisioThread();
    }

    if (dataTransferThread->isRunning()) {
        //std::Debug::Messages("Thread  visualisation is still running" << std::endl;
        //it is your own fault if you were still recording something
        waitForDataThread();
    }

    delete dataTransferThread;
332
    delete _visualisationThread;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
333
334
335
336
337
338
339
    delete travistoOptions;
    delete labelCurrentAction;
    delete labelFrameNumber;
    delete labelRecording;
    delete labelMode;
}

Ulrich Kemloh's avatar
Ulrich Kemloh committed
340
341
void MainWindow::slotHelpAbout()
{
Mohcine Chraibi's avatar
Mohcine Chraibi committed
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360

     Debug::Messages("About JPSvis");
     QString gittext = QMessageBox::tr(
          "<h1><p style=\"line-height:0.7\">JPSvis</p></h1><p style=\"line-height:1.4\" style=\"color:Gray;\"><small><i>Version %1</i></small></p>"
          "<p style=\"line-height:0.4\" style=\"color:Gray;\"><i>CommHash</i> %2</p>"
          "<p  style=\"line-height:0.4\" style=\"color:Gray;\"><i>CommDate</i> %3</p>"
          "<p  style=\"line-height:0.4\" style=\"color:Gray;\"><i>Branch</i> %4</p><hr>"
          ).arg(JPSVIS_VERSION).arg(GIT_COMMIT_HASH).arg(GIT_COMMIT_DATE).arg(GIT_BRANCH);

     QString text = QMessageBox::tr(
          "<p style=\"color:Gray;\"><small><i> &copy; 2009-2019  Ulrich Kemloh <br><a href=\"http://jupedsim.org\">jupedsim.org</a></i></small></p>"

        );





QMessageBox msg(QMessageBox::Information,"About JPSvis", gittext+text, QMessageBox::Ok);
Mohcine Chraibi's avatar
Mohcine Chraibi committed
361
362
QString logo = ":/new/iconsS/icons/JPSvis.icns" ;
msg.setIconPixmap(QPixmap(logo));
Mohcine Chraibi's avatar
Mohcine Chraibi committed
363
364
365
366
367
368
369

    //Change font
QFont font( "Tokyo" );
font.setPointSize( 10 );
// font.setWeight( QFont::Bold );
//font.setItalic( TRUE );
msg.setFont(font);
Mohcine Chraibi's avatar
Mohcine Chraibi committed
370
//msg.setStandardButtons(0);
Mohcine Chraibi's avatar
Mohcine Chraibi committed
371
msg.exec();
Ulrich Kemloh's avatar
Ulrich Kemloh committed
372
373
374
375
376
377
378
}


/**
 *setup the network port to bind
 *
 */
Ulrich Kemloh's avatar
Ulrich Kemloh committed
379
380
void MainWindow::slotNetworkSettings()
{
Ulrich Kemloh's avatar
Ulrich Kemloh committed
381
382
    bool ok;
    int port = QInputDialog::getInt(this, tr("input a port "), tr(
Ulrich Kemloh's avatar
Ulrich Kemloh committed
383
                                        "port(default to 8989):"), 8989, 5000, 65355, 1, &ok);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
384
    if (ok) {
385
        SystemSettings::setListeningPort(port);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
386
387
388
    }
}

Ulrich Kemloh's avatar
Ulrich Kemloh committed
389
390
void MainWindow::slotStartPlaying()
{
Ulrich Kemloh's avatar
Ulrich Kemloh committed
391
392
393
394
395
396
397
398
399
400

    //first reset this variable. just for the case
    // the thread was shutdown from the reset option
    extern_shutdown_visual_thread = false;

    if (!isPlaying) {
        statusBar()->showMessage(QString::fromUtf8("waiting for data"));
        //only start the visualisation thread if in offline modus
        // otherwise both threads should be started
        if (extern_offline_mode) {
Ulrich Kemloh's avatar
Ulrich Kemloh committed
401
            if(anyDatasetLoaded()) { //at least one dataset was loaded, restarting the playback
Ulrich Kemloh's avatar
Ulrich Kemloh committed
402
403
404
405
406
407
408

                // starting the playback from the beginning
                extern_trajectories_firstSet.resetFrameCursor();

            } else if (!slotAddDataSet()) { //else load a dataset
                return;//could not read any data
            }
409
410
411
412
413
#ifdef __APPLE__
            //std::thread::id main_thread_id = std::this_thread::get_id();
            dispatch_queue_t main_q = dispatch_get_main_queue();
            dispatch_async(main_q, ^(void){
                               //visualisationThread->moveToThread(QApplication::instance()->thread());
414
                               _visualisationThread->run();
415
                           });
Ulrich Kemloh's avatar
Ulrich Kemloh committed
416
#else
417
            _visualisationThread->start();
Ulrich Kemloh's avatar
Ulrich Kemloh committed
418

419
420
#endif

Ulrich Kemloh's avatar
Ulrich Kemloh committed
421
        } else { /*if (extern_online_mode)*/  //live visualisation
422
423
424
425
426

#ifdef __APPLE__
            dispatch_async(main_q, ^(void){
                               dataTransferThread->run();
                           });
Ulrich Kemloh's avatar
Ulrich Kemloh committed
427
#else
Ulrich Kemloh's avatar
Ulrich Kemloh committed
428
            dataTransferThread->start();
429
430
431

#endif
            //dataTransferThread->start();
Ulrich Kemloh's avatar
Ulrich Kemloh committed
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
            //visualisationThread->start();
        }

        //enable recording
        ui.BtRecord->setEnabled(true);
        labelCurrentAction->setText("   playing   ");

        //change Icon to Pause
        QIcon icon1;
        icon1.addPixmap(QPixmap(QString::fromUtf8(
                                    ":/new/iconsS/icons/PauseHot.png")), QIcon::Normal, QIcon::Off);
        ui.BtStart->setToolTip("pause the visualisation");
        ui.BtStart->setIcon(icon1);

        //anyDataLoaded=true;
        isPlaying = true;
        extern_is_pause = false;

    } else if (isPaused) { //resume playing
        isPaused = false;
        extern_is_pause = false;
        //restore the play Icon
        QIcon icon1;
        icon1.addPixmap(QPixmap(QString::fromUtf8(
                                    ":/new/iconsS/icons/PauseHot.png")), QIcon::Normal, QIcon::Off);
        ui.BtStart->setIcon(icon1);
        labelCurrentAction->setText("   playing   ");
        ui.BtStart->setToolTip("pause the visualisation");

    } else { //call pause
        isPaused = true;
        extern_is_pause = true;
        QIcon icon1;
        icon1.addPixmap(QPixmap(QString::fromUtf8(
                                    ":/new/iconsS/icons/Play1Hot.png")), QIcon::Normal, QIcon::Off);
        ui.BtStart->setIcon(icon1);
        ui.BtStart->setToolTip("resume the visualisation");
        labelCurrentAction->setText("   paused   ");
    }

    //no matter what, the stop button should be enabled
    ui.BtStop->setEnabled(true);
}

Ulrich Kemloh's avatar
Ulrich Kemloh committed
476
477
void MainWindow::slotStopPlaying()
{
Ulrich Kemloh's avatar
Ulrich Kemloh committed
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497

    if (extern_recording_enable) {
        int res = QMessageBox::question(this, "action",
                                        "do you wish to stop the recording?", QMessageBox::Discard
                                        | QMessageBox::Yes, QMessageBox::Yes);
        if (res == QMessageBox::Yes) {
            extern_recording_enable = false;
            //currentAction->setText("   Idle   ");
            labelRecording->setText("rec: OFF");

        } else {
            return;
        }
    }

    //shutdown the threads
    //fixme: shut the network thread as well
    // maybe this is a feature !
    extern_shutdown_visual_thread=true;
    waitForVisioThread();
498
    //waitForDataThread();
Ulrich Kemloh's avatar
Ulrich Kemloh committed
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514

    //reset all frames cursors
    resetAllFrameCursor();

    //disable/reset all graphical elements
    isPlaying = false;
    isPaused = false;
    resetGraphicalElements();
    labelCurrentAction->setText(" Idle ");
}

/**
 * open a file containing visualisation data, parse them
 * and initialize the visualization
 *
 */
Ulrich Kemloh's avatar
Ulrich Kemloh committed
515
516
bool MainWindow::slotLoadFile()
{
Ulrich Kemloh's avatar
Ulrich Kemloh committed
517
518
519
520
    return slotAddDataSet();
}

// This function is only used in online Mode
521
void MainWindow::parseGeometry(const QDomNode &geoNode)
Ulrich Kemloh's avatar
Ulrich Kemloh committed
522
{
Mohcine Chraibi's avatar
Mohcine Chraibi committed
523
524

     Debug::Messages("parsing the geo");
525
    if(geoNode.isNull()) return ;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
526
527
528

    //check if there is a tag 'file' there in
    QString fileName = geoNode.toElement().elementsByTagName("file").item(0).toElement().attribute("location");
529
    auto&& geometry = _visualisationThread->getGeometry();
Ulrich Kemloh's avatar
Ulrich Kemloh committed
530

Ulrich Kemloh's avatar
Ulrich Kemloh committed
531
532
    if(!fileName.isEmpty()) {
        if (fileName.endsWith(".xml",Qt::CaseInsensitive)) {
533
            //parsing the file
534
            SaxParser::parseGeometryJPS(fileName,geometry);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
535
        } else if (fileName.endsWith(".trav",Qt::CaseInsensitive)) {
Ulrich Kemloh's avatar
Ulrich Kemloh committed
536
537
538
539
540
541
            //must not be a file name
            SaxParser::parseGeometryTRAV(fileName,geometry);
        }
    }
    // I assume it is a trav format node,
    //which is the only one which can directly be inserted into a file
Ulrich Kemloh's avatar
Ulrich Kemloh committed
542
    else {
543
544
545
546
        //cout<<"online geo: "<<geoNode.toDocument().toString().toStdString()<<endl; exit(0);
        //geoNode.toText().toComment().toDocument().toString()
        QDomDocument doc("");
        QDomNode geoNode;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
547
        if(!geoNode.isNull()) {
Mohcine Chraibi's avatar
Mohcine Chraibi committed
548
             std::cout << "online geo: "<<geoNode.toElement().toDocument().toString().toStdString();
Ulrich Kemloh's avatar
Ulrich Kemloh committed
549
            exit(0);
550
551
        }

Ulrich Kemloh's avatar
Ulrich Kemloh committed
552
553
554
        //must not be a file name
        SaxParser::parseGeometryTRAV(fileName,geometry,geoNode);
    }
555
    //return geometry;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
556
557
}

558
// This function is only used in online Mode
559
void MainWindow::parseGeometry(const QString& geometryString)
560
561
{
    QFile file("_geometry_tmp_file.xml");
562
    if (file.open(QIODevice::ReadWrite | QIODevice::Text)) {
563
564
565
566
567
568
569
570
571
572
573
        QTextStream stream(&file);
        stream << geometryString << endl;
    }

    QString tmpFileName = file.fileName();

    //check if there is a tag 'file' there in
    QString geofileName = SaxParser::extractGeometryFilename(tmpFileName);

    //cout<<"filename: "<<geofileName.toStdString()<<endl;exit(0);

574
    auto&& geometry = _visualisationThread->getGeometry();
575

Ulrich Kemloh's avatar
Ulrich Kemloh committed
576
    if(!geofileName.isEmpty()) {
577
        SystemSettings::CreateLogfile();
Ulrich Kemloh's avatar
Ulrich Kemloh committed
578
        if (geofileName.endsWith(".xml",Qt::CaseInsensitive)) {
579
            //parsing the file
Ulrich Kemloh's avatar
Ulrich Kemloh committed
580
581
            if(!SaxParser::parseGeometryJPS(geofileName,geometry)) {
                SaxParser::parseGeometryXMLV04(geofileName,geometry);
582
            }
Ulrich Kemloh's avatar
Ulrich Kemloh committed
583
        } else if (geofileName.endsWith(".trav",Qt::CaseInsensitive)) {
584
585
586
            //must not be a file name
            SaxParser::parseGeometryTRAV(geofileName,geometry);
        }
587
        SystemSettings::DeleteLogfile();
588
589
590
    }
    // I assume it is a trav format node,
    //which is the only one which can directly be inserted into a file
Ulrich Kemloh's avatar
Ulrich Kemloh committed
591
    else {
592

593
        QDomNode geoNode;
594
595
596
597
        //must not be a file name
        SaxParser::parseGeometryTRAV(geometryString,geometry,geoNode);
    }

598
599
600
601
602
603
604
605
606
607
608
609
610
611
    //workaround. In some cases there are still hlines in the input files which are not parsed.
    if(geofileName.endsWith(".xml",Qt::CaseInsensitive))
    {
        QXmlInputSource source;
        source.setData(geometryString);
        QXmlSimpleReader reader;

        SyncData dummy;
        double fps;
        SaxParser handler(geometry,dummy,&fps);
        reader.setContentHandler(&handler);
        reader.parse(source);
    }

612
613
614
615
    //delete the file
    file.remove();
}

Ulrich Kemloh's avatar
Ulrich Kemloh committed
616
// TODO: still used?
Mohcine Chraibi's avatar
Mohcine Chraibi committed
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
// bool MainWindow::parsePedestrianShapes(QDomNode shapeNode, int groupID)
// {

//     if(shapeNode.isNull()) return false;
//     QStringList heights;
//     QStringList colors;

//     QDomNodeList agents = shapeNode.toElement().elementsByTagName("agentInfo");

//     for (int i = 0; i < agents.length(); i++) {

//         bool ok=false;
//         int id=agents.item(i).toElement().attribute("ID").toInt(&ok);
//         if(!ok) continue; // invalid ID
//         double height=agents.item(i).toElement().attribute("height").toDouble(&ok);
//         if(!ok)height=std::numeric_limits<double>::quiet_NaN();

//         int color=agents.item(i).toElement().attribute("color").toDouble(&ok);
//         if(!ok)color=std::numeric_limits<int>::quiet_NaN();
//         Debug::Messages("id= %d height= %lf color =%d",id,height,color);

//         if(height!=height) {
//             heights.append(QString::number(id));
//             heights.append(QString::number(height));
//         }
//         if(color!=color) {
//             colors.append(QString::number(id));
//             colors.append(QString::number(color));
//         }

//     }
//     switch (groupID) {

//     case 1:
//         extern_trajectories_firstSet.setInitialHeights(heights);
//         extern_trajectories_firstSet.setInitialColors(colors);
//         break;
//     }
//     return true;
// }
Ulrich Kemloh's avatar
Ulrich Kemloh committed
657
658

/// add a new dataset
Ulrich Kemloh's avatar
Ulrich Kemloh committed
659
660
bool MainWindow::slotAddDataSet()
{
Ulrich Kemloh's avatar
Ulrich Kemloh committed
661
662

    // just continue
663
    numberOfDatasetLoaded=1;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
664
    if(addPedestrianGroup(numberOfDatasetLoaded)==false) {
Ulrich Kemloh's avatar
Ulrich Kemloh committed
665
666
667
668
669
670
671
672
673
        numberOfDatasetLoaded--;
        return false;
    }

    QString stre(numberOfDatasetLoaded);
    stre.setNum(numberOfDatasetLoaded);
    stre.append(" dataset loaded");
    statusBar()->showMessage(stre);

674
675
    slotStartPlaying();

Ulrich Kemloh's avatar
Ulrich Kemloh committed
676
677
678
679
    return true;
}

///clear all datasets previously entered.
Ulrich Kemloh's avatar
Ulrich Kemloh committed
680
681
void MainWindow::slotClearAllDataset()
{
Ulrich Kemloh's avatar
Ulrich Kemloh committed
682
683
684
685
    clearDataSet(1);
    numberOfDatasetLoaded=0;
}

686
687
bool MainWindow::addPedestrianGroup(int groupID,QString fileName)
{
Mohcine Chraibi's avatar
Mohcine Chraibi committed
688
689
     Debug::Messages("Enter MainWindow::addPedestrianGroup with filename <%s>", fileName.toStdString().c_str());

Ulrich Kemloh's avatar
Ulrich Kemloh committed
690
    statusBar()->showMessage(tr("Select a file"));
691
     if(fileName.isEmpty())
Ulrich Kemloh's avatar
Ulrich Kemloh committed
692
        fileName = QFileDialog::getOpenFileName(this,
693
                                                "Select the file containing the data to visualize",
Mohcine Chraibi's avatar
Mohcine Chraibi committed
694
                                                QDir::currentPath(),
Mohcine Chraibi's avatar
Mohcine Chraibi committed
695
                                                "JuPedSim Files (*.xml *.txt);;All Files (*.*)");
Ulrich Kemloh's avatar
Ulrich Kemloh committed
696

697
    //the action was cancelled
Ulrich Kemloh's avatar
Ulrich Kemloh committed
698
699
700
    if (fileName.isNull()) {
        return false;
    }
701
702
703
704

    //get and set the working dir
    QFileInfo fileInfo(fileName);
    QString wd=fileInfo.absoluteDir().absolutePath();
Mohcine Chraibi's avatar
Mohcine Chraibi committed
705
    Debug::Messages("MainWindow::addPedestrianGroup: wd:  <%s>", wd.toStdString().c_str());
706
    SystemSettings::setWorkingDirectory(wd);
707
    SystemSettings::setFilenamePrefix(QFileInfo ( fileName ).baseName()+"_");
708

Ulrich Kemloh's avatar
Ulrich Kemloh committed
709
    //the geometry actor
710
    auto&& geometry = _visualisationThread->getGeometry();
Mohcine Chraibi's avatar
Mohcine Chraibi committed
711
    QString geometry_file;
712
    //try to get a geometry filename
Mohcine Chraibi's avatar
Mohcine Chraibi committed
713
714
    if(fileName.endsWith(".xml",Qt::CaseInsensitive))
    {
Mohcine Chraibi's avatar
Mohcine Chraibi committed
715
         Debug::Messages("1. Extract geometry file from <%s>", fileName.toStdString().c_str());
Mohcine Chraibi's avatar
Mohcine Chraibi committed
716
         geometry_file=SaxParser::extractGeometryFilename(fileName);
Mohcine Chraibi's avatar
Mohcine Chraibi committed
717
718
719
    }
    else
    {
Mohcine Chraibi's avatar
Mohcine Chraibi committed
720
         Debug::Messages("Extract geometry file from <%s>", fileName.toStdString().c_str());
Mohcine Chraibi's avatar
Mohcine Chraibi committed
721
         geometry_file=SaxParser::extractGeometryFilenameTXT(fileName);
Mohcine Chraibi's avatar
Mohcine Chraibi committed
722
723
    }

Mohcine Chraibi's avatar
Mohcine Chraibi committed
724
    Debug::Messages("MainWindow::addPedestrianGroup: geometry name: <%s>", geometry_file.toStdString().c_str());
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
    if(geometry_file.isEmpty())
    {
         auto fileDir = fileInfo.path();
         if(fileName.endsWith(".txt",Qt::CaseInsensitive))
         {
              int res = QMessageBox::warning(this, "Did not find geometry name in TXT file",
                                             "Warning: Did not find geometry name in TXT file\nOpen geometry file?"
                                             , QMessageBox::Yes
                                             | QMessageBox::No, QMessageBox::Yes);
              if (res == QMessageBox::No) {
                   exit(EXIT_FAILURE);
                   //return false;
              }
              geometry_file = QFileDialog::getOpenFileName(this,
                                                           "Select a geometry file",
                                                           fileDir,
                                                           "Geometry (*.xml)");
              Debug::Messages("Got geometry file: <%s>", geometry_file.toStdString().c_str());
              QFileInfo check_file(geometry_file);
              if( !(check_file.exists() && check_file.isFile()) )
              {
                   Debug::Error("Geomery file does not exist.");
747
748
749
                   //exit(EXIT_FAILURE);
                   return(false);

750
              }
751
              //geometry_file =  check_file.fileName();
752
753
754
755
756
757
758
         }
         // @todo: check xml file too, although probably xml files
         // always have a geometry tag

    }
    std::cout << "---> geometry " << geometry_file.toStdString().c_str() << "\n" ;

Ulrich Kemloh's avatar
Ulrich Kemloh committed
759
    // if xml is detected, just load and show the geometry then exit
Ulrich Kemloh's avatar
Ulrich Kemloh committed
760
    if(geometry_file.endsWith(".xml",Qt::CaseInsensitive)) {
761
762
763

        //try to parse the correct way
        // fall back to this if it fails
764
        SystemSettings::CreateLogfile();
Mohcine Chraibi's avatar
Mohcine Chraibi committed
765
        Debug::Messages("Calling parseGeometryJPS with <%s>", geometry_file.toStdString().c_str());
Ulrich Kemloh's avatar
Ulrich Kemloh committed
766
        if(! SaxParser::parseGeometryJPS(geometry_file,geometry)) {
767
768
            int res = QMessageBox::warning(this, "Errors in Geometry. Continue Parsing?",
                                           "JuPedSim has detected an error in the supplied geometry.\n"
769
770
771
                                           "<"
                                           +geometry_file+
                                           ">"
772
                                           "The simulation will likely fail using this geometry.\n"
773
774
                                           "More information are provided in the log file:\n"
                                           +SystemSettings::getLogfile()+
775
                                           "\n\nShould I try to parse and display what I can?"
776
777
778
779
780
                                           , QMessageBox::Yes
                                           | QMessageBox::No, QMessageBox::No);
            if (res == QMessageBox::No) {
                return false;
            }
781
782
783
            SaxParser::parseGeometryXMLV04(wd+"/"+geometry_file,geometry);//@todo:
                                                                          //use
                                                                          //qt sep
Ulrich Kemloh's avatar
Ulrich Kemloh committed
784
785
        } else {
            //everything was fine. Delete the log file
Mohcine Chraibi's avatar
Mohcine Chraibi committed
786
787
             //std::cout << "won't delete logfile\n";
            SystemSettings::DeleteLogfile();
788
        }
789

790
        //SaxParser::parseGeometryXMLV04(fileName,geometry);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
791
792
793
794
        //slotLoadParseShowGeometry(fileName);
        //return false;
    }

795
796
797
798
799
800
801
802
    //check if it is vtrk file containinf gradient
    if(fileName.endsWith(".vtk",Qt::CaseInsensitive))
    {
        if (false==SaxParser::ParseGradientFieldVTK(fileName,geometry))
            return false;

    }

Ulrich Kemloh's avatar
Ulrich Kemloh committed
803
804
    QFile file(fileName);
    if (!file.open(QIODevice::ReadOnly)) {
Mohcine Chraibi's avatar
Mohcine Chraibi committed
805
        Debug::Error("parseGeometryJPS:  could not open the File: ",fileName.toStdString().c_str());
Ulrich Kemloh's avatar
Ulrich Kemloh committed
806
        return false;
Mohcine Chraibi's avatar
Mohcine Chraibi committed
807

Ulrich Kemloh's avatar
Ulrich Kemloh committed
808
809
810
    }

    SyncData* dataset=NULL;
811
    extern_trajectories_firstSet.clearFrames();
Ulrich Kemloh's avatar
Ulrich Kemloh committed
812

Ulrich Kemloh's avatar
Ulrich Kemloh committed
813
    switch(groupID) {
Ulrich Kemloh's avatar
Ulrich Kemloh committed
814
815
816
817
818
819
820
821
822
823
824
    case 1:
        Debug::Messages("handling first set");
        dataset=&extern_trajectories_firstSet;
        extern_first_dataset_loaded=true;
        extern_first_dataset_visible=true;
        ui.actionFirst_Group->setEnabled(true);
        ui.actionFirst_Group->setChecked(true);
        slotToggleFirstPedestrianGroup();
        break;

    default:
825
        Debug::Error("Only one dataset can be loaded at a time");
826
        //return false;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
827
828
829
        break;
    }

830
    //no other geometry format was detected
Mohcine Chraibi's avatar
Mohcine Chraibi committed
831
    double frameRate=16; //default frame rate
Ulrich Kemloh's avatar
Ulrich Kemloh committed
832
833
834
    statusBar()->showMessage(tr("parsing the file"));


835

836
837
838
839
840
    //parsing the xml file
    if(fileName.endsWith(".xml",Qt::CaseInsensitive))
    {
        QXmlInputSource source(&file);
        QXmlSimpleReader reader;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
841

842
        SaxParser handler(geometry,*dataset,&frameRate);
843
844
845
846
        reader.setContentHandler(&handler);
        reader.parse(source);
        file.close();
    }
847
848
849
850
851
852
853
    //parsing the vtk file
//    else if(fileName.endsWith(".vtk",Qt::CaseInsensitive))
//    {
//        if (false==SaxParser::ParseGradientFieldVTK(fileName,geometry))
//            return false;

//    }
854
    // try to parse the txt file
855
    else if(fileName.endsWith(".txt",Qt::CaseInsensitive))
856
    {
Mohcine Chraibi's avatar
Mohcine Chraibi committed
857
858
         QString source_file= wd + QDir::separator() + SaxParser::extractSourceFileTXT(fileName);
         QString goal_file=wd + QDir::separator() + SaxParser::extractGoalFileTXT(fileName);
859
860
861
862
863
864
865
866
         QFileInfo check_file(source_file);
         if( !(check_file.exists() && check_file.isFile()) )
        {
             Debug::Messages("WARNING: MainWindow::addPedestrianGroup: source name: <%s> not found!", source_file.toStdString().c_str());
        }
         else
              Debug::Messages("INFO: MainWindow::addPedestrianGroup: source name: <%s>", source_file.toStdString().c_str());

867
868
869
870
871
872
873
874
875
876
         check_file = source_file;
         if( !(check_file.exists() && check_file.isFile()) )
        {
             Debug::Messages("WARNING: MainWindow::addPedestrianGroup: goal name: <%s> not found!", goal_file.toStdString().c_str());
        }
         else
              Debug::Messages("INFO: MainWindow::addPedestrianGroup: goal name: <%s>", goal_file.toStdString().c_str());


        // ------ parsing sources
877
878
879
880
881
882
883
        QFile file(source_file);
        QXmlInputSource source(&file);
        QXmlSimpleReader reader;
        SaxParser handler(geometry,*dataset,&frameRate);
        reader.setContentHandler(&handler);
        reader.parse(source);
        file.close();
884
885
886
887
888
889
890
891
        QFile file2(goal_file);
        QXmlInputSource source2(&file2);
        reader.parse(source2);
        file2.close();
        // -----
        // // ---- parsing goals
        // -----

892

893
894
        if(false==SaxParser::ParseTxtFormat(fileName, dataset,&frameRate))
            return false;
895

896
    }
897

898
899

    QString frameRateStr=QString::number(frameRate);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
900
    // set the visualisation window title
901
902
    _visualisationThread->setWindowTitle(fileName);
    _visualisationThread->slotSetFrameRate(frameRate);
903
    //visualisationThread->setGeometry(geometry);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
904
905
906
907
908
909
910
911
912
913
914
915
    //visualisationThread->setWindowTitle(caption);
    labelFrameNumber->setText("fps: " + frameRateStr+"/"+frameRateStr);

    //shutdown the visio thread
    extern_shutdown_visual_thread=true;
    waitForVisioThread();

    statusBar()->showMessage(tr("file loaded and parsed"));

    return true;
}

Ulrich Kemloh's avatar
Ulrich Kemloh committed
916
917
void MainWindow::slotRecord()
{
Ulrich Kemloh's avatar
Ulrich Kemloh committed
918
919
920
921
922
923
924
925
926
927

    if (extern_recording_enable) {
        int res = QMessageBox::warning(this, "action",
                                       "JuPedSim is already recording a video\n"
                                       "do you wish to stop the recording?", QMessageBox::Yes
                                       | QMessageBox::No, QMessageBox::No);
        if (res == QMessageBox::Yes) {
            extern_recording_enable = false;
            ui.BtRecord->setToolTip("Start Recording");
            labelCurrentAction->setText("   Playing   ");
928
            labelRecording->setText(" rec: off ");
Ulrich Kemloh's avatar
Ulrich Kemloh committed
929
930
931
932
933
            return;
        }
    }
    extern_launch_recording = true;
    ui.BtRecord->setToolTip("Stop Recording");
934
    labelRecording->setText(" rec: on ");
Ulrich Kemloh's avatar
Ulrich Kemloh committed
935
936
937
938
939
    //labelCurrentAction->setText("   recording   ");

}

QString MainWindow::getTagValueFromElement(QDomNode node,
940
                                           const char * tagName)
Ulrich Kemloh's avatar
Ulrich Kemloh committed
941
{
Ulrich Kemloh's avatar
Ulrich Kemloh committed
942
943
944
945
946
947
    if (node.isNull())
        return "";
    return node.toElement().namedItem(tagName).firstChild().nodeValue();

}

Ulrich Kemloh's avatar
Ulrich Kemloh committed
948
949
void MainWindow::slotFullScreen(bool status)
{
Ulrich Kemloh's avatar
Ulrich Kemloh committed
950
951

    Debug::Messages("changing full screen status %d",status);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
952
    extern_fullscreen_enable = true;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
953
954
955
956
    //dont forget this.
    extern_force_system_update=true;
}

Ulrich Kemloh's avatar
Ulrich Kemloh committed
957
958
void MainWindow::slotSetOfflineMode(bool status)
{
Ulrich Kemloh's avatar
Ulrich Kemloh committed
959
960
961
962
963
964
965
966
967
968
969
    if (status) {
        ui.actionOnline->setChecked(false);
        extern_offline_mode = true;
        labelMode->setText(" Offline ");
    } else {
        ui.actionOnline->setChecked(true);
        extern_offline_mode = false;
        labelMode->setText(" Online ");
    }
}

Ulrich Kemloh's avatar
Ulrich Kemloh committed
970
971
void MainWindow::slotSetOnlineMode(bool status)
{
Ulrich Kemloh's avatar
Ulrich Kemloh committed
972
973
974
975
976
977
978
979
980
981
982
983
    if (status) {
        ui.actionOffline->setChecked(false);
        extern_offline_mode = false;
        labelMode->setText(" Online  ");
    } else {
        ui.actionOffline->setChecked(true);
        extern_offline_mode = true;
        labelMode->setText(" Offline ");

    }
}

Ulrich Kemloh's avatar
Ulrich Kemloh committed
984
985
void MainWindow::slotReset()
{
Ulrich Kemloh's avatar
Ulrich Kemloh committed
986
987
988
989
990
991
992
993
994

    //stop any recording
    if (extern_recording_enable) {
        int res = QMessageBox::question(this, "action",
                                        "do you wish to stop the recording?", QMessageBox::Discard
                                        | QMessageBox::Yes, QMessageBox::Yes);
        if (res == QMessageBox::Yes) {
            extern_recording_enable = false;
            labelCurrentAction->setText("   Playing   ");
Ulrich Kemloh's avatar
Ulrich Kemloh committed
995
        } else {
Ulrich Kemloh's avatar
Ulrich Kemloh committed
996
997
998
999
            return;
        }
    }

Ulrich Kemloh's avatar
Ulrich Kemloh committed
1000
    if(anyDatasetLoaded()) {
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
        int res = QMessageBox::question(this, "action",
                                        "This will also clear any dataset if loaded.\n"
                                        "Do you wish to continue?", QMessageBox::Discard
                                        | QMessageBox::Yes, QMessageBox::Yes);
        if (res == QMessageBox::No) {
            return;
        }
    }


    //shutdown the visual thread
    extern_shutdown_visual_thread = true;
    waitForVisioThread();
    waitForDataThread();

    //reset all buttons
    //anyDataLoaded = false;
    slotClearAllDataset();
    isPlaying = false;
    isPaused  = false;
    labelCurrentAction->setText("   Idle   ");
    resetGraphicalElements();

}

Ulrich Kemloh's avatar
Ulrich Kemloh committed
1026
1027
void MainWindow::slotCurrentAction(QString msg)
{
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1028
1029
1030
1031
1032
1033
    msg = " " + msg + " ";
    //labelCurrentAction->setText(msg);
    statusBar()->showMessage(msg);
    //	labelMode->setText(msg);
}

Ulrich Kemloh's avatar
Ulrich Kemloh committed
1034
1035
void MainWindow::slotFrameNumber(unsigned long actualFrameCount)
{
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1036
1037
1038

    //compute the  mamixum framenumber
    int maxFrameCount=1;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1039
    if(extern_first_dataset_loaded) {
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1040
1041
1042
        maxFrameCount=extern_trajectories_firstSet.getFramesNumber();
    }

1043
1044
1045
1046
1047
    if(actualFrameCount>maxFrameCount) actualFrameCount=maxFrameCount;
    QString msg;
    msg.setNum(actualFrameCount);
    ui.frameSliderLabel->setText("Frame: " + msg);

Ulrich Kemloh's avatar
Ulrich Kemloh committed
1048
    if(!frameSliderHold)if(maxFrameCount!=0)//TODO WTF, otherwise an arrymtic exeption arises
1049
        ui.framesIndicatorSlider->setValue((ui.framesIndicatorSlider->maximum()*actualFrameCount)/maxFrameCount);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1050
1051
}

Ulrich Kemloh's avatar
Ulrich Kemloh committed
1052
1053
void MainWindow::slotRunningTime(unsigned long timems)
{
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
    //	QString msg;
    //	msg.setNum(timems);
    //	msg.append(" milsec");
    //labelRunningTime->setText(msg);
    ui.lcdRunningTime->display((int)timems);
}

void MainWindow::slotRenderingTime(int fps)
{
    QString msg =labelFrameNumber->text().replace(QRegExp("[0-9]+/"),QString::number(fps)+"/");
    labelFrameNumber->setText(msg);
}
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1066
1067
void MainWindow::slotExit()
{
Mohcine Chraibi's avatar
Mohcine Chraibi committed
1068
     Debug::Messages("Exit jpsvis");
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1069
1070
1071
1072
    cleanUp();
    qApp->exit();
}

Ulrich Kemloh's avatar
Ulrich Kemloh committed
1073
1074
void MainWindow::closeEvent(QCloseEvent* event)
{
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1075
1076
1077
1078
1079
    hide();
    cleanUp();
    event->accept();
}

Ulrich Kemloh's avatar
Ulrich Kemloh committed
1080
1081
void MainWindow::cleanUp()
{
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
    //stop the recording process
    extern_recording_enable = false;
    extern_shutdown_visual_thread = true;

    if(SystemSettings::getRecordPNGsequence())
        slotRecordPNGsequence();

    waitForVisioThread();
    waitForDataThread();

}

Ulrich Kemloh's avatar
Ulrich Kemloh committed
1094
1095
void MainWindow::slotControlSequence(const char * sex)
{
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
    QString str(sex);

    //todo: check wich stack is empty and disable
    // the correspinding datasets.
    // are all stacks empty? then stop
    // visualisation adn recording if any
    if (str.compare("CONTROL_STACK_EMPTY") == 0) {

        statusBar()->showMessage(tr(" stack empty "));
        extern_recording_enable = false;
        ui.BtRecord->setToolTip("Start Recording");
        labelRecording->setText(" rec: off ");
    } else if (str.compare("CONTROL_RESET") == 0) {
        Debug::Messages("resetting all");
        isPlaying = false;
        isPaused = false;
        labelCurrentAction->setText("   Idle   ");
        extern_update_step=1;
        //extern_screen_contrast=1;

        //reset the frames cursor
        extern_trajectories_firstSet.resetFrameCursor();

        resetGraphicalElements();

Ulrich Kemloh's avatar
Ulrich Kemloh committed
1121
    } else if (str.compare("STACK_REACHS_BEGINNING")==0) {
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1122
        //return to normal speed
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1123
        if(extern_update_step<0) {
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1124
1125
1126
1127
1128
1129
1130
1131
            Debug::Messages("stack reaches beginning, resuming the playback with vel 1");
            extern_update_step=1;
            ui.speedSettingSlider->setValue(1);
        }

    }
}

Ulrich Kemloh's avatar
Ulrich Kemloh committed
1132
1133
void MainWindow::resetGraphicalElements()
{
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
    //restore play button
    QIcon icon1;
    icon1.addPixmap(QPixmap(
                        QString::fromUtf8(":/new/iconsS/icons/Play1Hot.png")),
                    QIcon::Normal, QIcon::Off);
    ui.BtStart->setIcon(icon1);

    //disable record button
    ui.BtRecord->setEnabled(false);

    //disable fullscreen
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1145
    //ui.BtFullscreen->setEnabled(false);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167

    //disable stop button

    ui.BtStop->setEnabled(false);
    //reset the slider
    ui.framesIndicatorSlider->setValue(ui.framesIndicatorSlider->minimum());

    //reset the speed cursor
    ui.speedSettingSlider->setValue(1);
    //reset the contrast

    // bla bla
    ui.actionShow_Legend->setEnabled(true);

    //restore the play Icon

    //
    ui.actionShow_Trajectories->setEnabled(true);

    ui.action3_D->setEnabled(true);
    ui.action2_D->setEnabled(true);

1168
    labelRecording->setText("rec: off");
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1169
1170
1171
1172
1173
1174
1175
1176
1177
    statusBar()->showMessage(tr("select a File"));

    //resetting the start/stop recording action
    //check whether the a png recording sequence was playing, stop if the case
    if(SystemSettings::getRecordPNGsequence())
        slotRecordPNGsequence();

}

Ulrich Kemloh's avatar
Ulrich Kemloh committed
1178
1179
1180
void MainWindow::slotToggleFirstPedestrianGroup()
{
    if(ui.actionFirst_Group->isChecked()) {
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1181
        extern_first_dataset_visible=true;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1182
    } else {
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1183
1184
1185
1186
1187
1188
        extern_first_dataset_visible=false;
    }
    extern_force_system_update=true;

}

Ulrich Kemloh's avatar
Ulrich Kemloh committed
1189
1190
bool MainWindow::anyDatasetLoaded()
{
1191
    return extern_first_dataset_loaded;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1192
1193
}

Ulrich Kemloh's avatar
Ulrich Kemloh committed
1194
1195
void MainWindow::slotShowTrajectoryOnly()
{
1196
    SystemSettings::setShowTrajectories(ui.actionShow_Trajectories->isChecked());
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1197
    extern_force_system_update=true;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1198
1199
}

Ulrich Kemloh's avatar
Ulrich Kemloh committed
1200
1201
1202
void MainWindow::slotShowPedestrianOnly()
{

Ulrich Kemloh's avatar
Ulrich Kemloh committed
1203
    if(ui.actionShow_Agents->isChecked()) {
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1204
        SystemSettings::setShowAgents(true);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1205
    } else {
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1206
1207
1208
        SystemSettings::setShowAgents(false);
    }
    extern_force_system_update=true;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1209
1210
}

Ulrich Kemloh's avatar
Ulrich Kemloh committed
1211
1212
1213
void MainWindow::slotShowGeometry()
{
    if (ui.actionShow_Geometry->isChecked()) {
1214
        _visualisationThread->setGeometryVisibility(true);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1215
1216
        ui.actionShow_Exits->setEnabled(true);
        ui.actionShow_Walls->setEnabled(true);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1217
        ui.actionShow_Geometry_Captions->setEnabled(true);
1218
        ui.actionShow_Navigation_Lines->setEnabled(true);
1219
        ui.actionShow_Floor->setEnabled(true);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1220
        SystemSettings::setShowGeometry(true);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1221
    } else {
1222
        _visualisationThread->setGeometryVisibility(false);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1223
1224
        ui.actionShow_Exits->setEnabled(false);
        ui.actionShow_Walls->setEnabled(false);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1225
        ui.actionShow_Geometry_Captions->setEnabled(false);
1226
        ui.actionShow_Navigation_Lines->setEnabled(false);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1227
        ui.actionShow_Floor->setEnabled(false);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1228
        SystemSettings::setShowGeometry(false);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1229
    }
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1230
    extern_force_system_update=true;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1231
1232
1233
}

/// shows/hide geometry
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1234
1235
void MainWindow::slotShowHideExits()
{
1236
    bool status = ui.actionShow_Exits->isChecked();
1237
    _visualisationThread->showDoors(status);
1238
    SystemSettings::setShowExits(status);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1239
1240
1241
}

/// shows/hide geometry
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1242
1243
void MainWindow::slotShowHideWalls()
{
1244
    bool status = ui.actionShow_Walls->isChecked();
1245
    _visualisationThread->showWalls(status);
1246
    SystemSettings::setShowWalls(status);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1247
1248
}

1249
1250
void MainWindow::slotShowHideNavLines()
{
1251
    bool status = ui.actionShow_Navigation_Lines->isChecked();
1252
    _visualisationThread->showNavLines(status);
1253
    SystemSettings::setShowNavLines(status);
1254
1255
}

1256
1257
1258
1259
//todo: add to the system settings
void MainWindow::slotShowHideFloor()
{
    bool status = ui.actionShow_Floor->isChecked();
1260
    _visualisationThread->showFloor(status);
1261
1262
1263
    SystemSettings::setShowFloor(status);
}

Ulrich Kemloh's avatar
Ulrich Kemloh committed
1264
1265

/// update the playing speed
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1266
1267
void MainWindow::slotUpdateSpeedSlider(int newValue)
{
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1268
1269
1270
1271
1272
1273
1274
1275
    QString msg;
    msg.setNum(newValue);
    extern_update_step=newValue;
    //Debug::Error( " speed updated to: %d", extern_update_step);
    ui.speedSliderLabel->setText("Speed: " + msg + " ");
}

/// update the position slider
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1276
1277
void MainWindow::slotUpdateFrameSlider(int newValue)
{
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1278
1279
1280

    // first get the correct position
    int maxFrameCount=1;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1281
    if(extern_first_dataset_loaded) {
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1282
1283
1284
1285
1286
1287
1288
        int t=extern_trajectories_firstSet.getFramesNumber();
        if(maxFrameCount<t) maxFrameCount=t;
    }

    int update = ((maxFrameCount*newValue)/ui.framesIndicatorSlider->maximum());

    // then set the correct position
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1289
    if(extern_first_dataset_loaded) {
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1290
1291
1292
1293
1294
1295
1296
1297
        extern_trajectories_firstSet.setFrameCursorTo(update);
        //Debug::Error( " first dataset frames update to [1] : " <<update<<endl;

    }
}

/// update the contrast
/// TODO: now known as framePerScond slider
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1298
1299
void MainWindow::slotUpdateContrastSlider(int newValue)
{
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1300

Mohcine Chraibi's avatar
Mohcine Chraibi committed
1301
    //   extern_screen_contrast=ui.contrastSettingSlider->value();
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1302
1303
1304
1305
1306
1307
1308
1309