Core.cpp 27.6 KB
Newer Older
Mathieu Faverge's avatar
Mathieu Faverge committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/*
** This file is part of the ViTE project.
**
** This software is governed by the CeCILL-A license under French law
** and abiding by the rules of distribution of free software. You can
** use, modify and/or redistribute the software under the terms of the
** CeCILL-A license as circulated by CEA, CNRS and INRIA at the following
** URL: "http://www.cecill.info".
** 
** As a counterpart to the access to the source code and rights to copy,
** modify and redistribute granted by the license, users are provided
** only with a limited warranty and the software's author, the holder of
** the economic rights, and the successive licensors have only limited
** liability.
** 
** In this respect, the user's attention is drawn to the risks associated
** with loading, using, modifying and/or developing or reproducing the
** software by the user in light of its specific status of free software,
** that may mean that it is complicated to manipulate, and that also
** therefore means that it is reserved for developers and experienced
** professionals having in-depth computer knowledge. Users are therefore
** encouraged to load and test the software's suitability as regards
** their requirements in conditions enabling the security of their
** systems and/or data to be ensured and, more generally, to use and
** operate it in the same conditions as regards security.
** 
** The fact that you are presently reading this means that you have had
** knowledge of the CeCILL-A license and that you accept its terms.
**
**
Mathieu Faverge's avatar
Mathieu Faverge committed
31
** ViTE developers are (for version 0.* to 1.0):
Mathieu Faverge's avatar
Mathieu Faverge committed
32 33 34 35 36 37 38 39 40 41 42
**
**        - COULOMB Kevin
**        - FAVERGE Mathieu
**        - JAZEIX Johnny
**        - LAGRASSE Olivier
**        - MARCOUEILLE Jule
**        - NOISETTE Pascal
**        - REDONDY Arthur
**        - VUCHENER Clément 
**
*/
43 44 45 46 47 48
/*!
 *\file interface_console.cpp
 *\brief This is the console interface C source code.
 */


49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
#include <queue>
#include <string>
#include <iostream>
#include <fstream>
#include <list>
#include <map>
#include <vector>
#include <stack>
/* -- */
#include <QObject>
/* -- */
#include "common/common.hpp"
#include "common/info.hpp"
#include "common/Errors.hpp"
#include "common/Tools.hpp"
/* -- */
65

66

67 68 69
//#include "render/render.hpp"
#include "render/Render_svg.hpp"
#include "render/Render.hpp"
70 71 72 73 74 75 76 77
/* -- */
#include "trace/values/Values.hpp"
#include "trace/tree/Interval.hpp"
#include "trace/tree/Node.hpp"
#include "trace/tree/BinaryTree.hpp"
#include "trace/EntityValue.hpp"
#include "trace/EntityTypes.hpp"
#include "trace/Entitys.hpp"
78
//#include "trace/tree/Interval.hpp"
79 80 81 82 83 84 85 86
#include "trace/Trace.hpp"
#include "trace/DrawTree.hpp"
#include "trace/DrawTrace.hpp"
/* -- */
#include "parser/File.hpp"
#include "parser/Parser.hpp"
#include "parser/ParserPaje.hpp"
#ifdef WITH_OTF
87 88
#include <otf.h>
#include "parser/ParserOTF.hpp"
89 90 91 92 93 94 95 96 97
#endif //WITH_OTF
#include "parser/ParserVite.hpp"
/* -- */
#include "statistics/Stats_window.hpp"
/* -- */
#include "interface/resource.hpp"
#include "interface/Interface_graphic.hpp"
#include "core/Core.hpp"
#include "interface/parsing_thread.hpp"
98
/* -- */
99 100 101 102 103 104 105
#ifdef WITH_VBO
#include "render/Render_alternate.hpp"
#else
#include "render/Render_opengl.hpp"
#endif


106
using namespace std;
107

108 109
#define message *Message::get_instance() << "(" << __FILE__ << " l." << __LINE__ << "): "

110 111 112 113 114 115 116 117 118 119
/***********************************
 *
 *
 *
 * Constructor and destructor.
 *
 *
 *
 **********************************/

120
Core::Core(int argc, char ** argv){
121
    
122
    
123
    bool useGUI;/* if window interface can be displayed */
124 125
    //  QString current_path;

126 127

    app = new QApplication(argc, argv);/* create the Qt application */        
128
 
129
    
130

131

Olivier Lagrasse's avatar
Olivier Lagrasse committed
132
    //  glutInit(&argc, argv);/* use for OpenGL text */
133 134 135

    /* Qt uses the default system encoding for QString (used when opening a file) */
    QTextCodec::setCodecForCStrings(QTextCodec::codecForLocale()); 
136 137 138


    useGUI = true;
139
    
140 141 142 143 144
  
    //    cerr << "Core debug: " << QDir::currentPath().toStdString().c_str() << " " << argv[0] << endl;
    


Olivier Lagrasse's avatar
Olivier Lagrasse committed
145
    /*  if (!useGUI){
Mathieu Faverge's avatar
Mathieu Faverge committed
146
        cerr << QObject::tr("Error: Graphical window cannot be displayed.").toStdString() << endl;
147
        exit(EXIT_FAILURE);
Olivier Lagrasse's avatar
Olivier Lagrasse committed
148
        }*/
149
    
150
    _main_window = NULL;
151
    _render_opengl =  NULL;
152
    _trace = NULL;
153
    
154
    /* Init of the times */
155 156
    _time_start = 0;
    _time_end = 0;
157

158 159 160 161 162 163
    _run_env[0] = new QString(QDir::currentPath().toStdString().c_str());
    _run_env[1] = new QString(argv[0]);
    
    Message::set_interface(this);/* define which interface will receive messages */
    

164
    _state = get_state(argc, argv);
165 166 167 168 169

    /*
     * Suppose that no window will be displayed.
     */
    _is_window_displayed = false;
170 171 172 173 174

    /*
     * No trace loaded
     */
    _is_trace_loaded = false;
175
    
176
    launch_action(_state);
177 178 179
}


180
Core::~Core(){
Jule Marcoueille's avatar
Jule Marcoueille committed
181
    
182 183 184 185
    delete app;
    if(_trace != NULL){
        delete _trace;
    }
186 187 188 189 190 191
    if (_run_env[0]!=NULL)
        delete _run_env[0];

    if (_run_env[1]!=NULL)
        delete _run_env[1];

192
    /* Qt desallocates _main_window and _render_opengl automatically */
Jule Marcoueille's avatar
Jule Marcoueille committed
193
    
194
    //free(_run_env[0]);
Olivier Lagrasse's avatar
CODE  
Olivier Lagrasse committed
195
    Message::kill();
Jule Marcoueille's avatar
Jule Marcoueille committed
196

197 198 199 200
}



201 202 203 204 205 206 207 208 209 210
/***********************************
 *
 *
 *
 * Running function.
 *
 *
 *
 **********************************/

211
bool Core::draw_trace(const string & filename, const int format){
212

Mathieu Faverge's avatar
Mathieu Faverge committed
213
    Parser *parser;
214

215
    DrawTrace drawing_ogl;
216 217 218 219
    
#ifdef WITH_VBO
    Render<Render_alternate> render(_render_opengl);
#else
220
    Render<Render_opengl> render(_render_opengl);
221 222
#endif

223

224 225 226 227
    QTime time_elapsed;
    ostringstream buf_txt;
    int time_buf;
    time_buf = 0;
228

229

Mathieu Faverge's avatar
Mathieu Faverge committed
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
    // Get the parser in function of the extension of the file
    const unsigned int position_of_dot = filename.find_last_of('.');
    if (position_of_dot != string::npos) {
        if(filename.substr(position_of_dot) == ".trace") {
            parser = new ParserPaje();
        }
#ifdef WITH_OTF
        else if(filename.substr(position_of_dot) == ".otf") {
            parser = new ParserOTF();
        }
#endif //WITH_OTF
        else if(filename.substr(position_of_dot) == ".ept") {
            parser = new ParserVite();
        }
        else {
            Error::set(Error::_BAD_FILE_EXTENSION, Error::_WARNING);
            parser = new ParserPaje();        
        }
    }
    else {
        Error::set(Error::_BAD_FILE_EXTENSION, Error::_WARNING);
        parser = new ParserPaje();        
    }


    parser->set_file_to_parse(filename);
Johnny Jazeix's avatar
Johnny Jazeix committed
256
    
257
    QApplication::setOverrideCursor(Qt::WaitCursor);
258

259
    if (_DRAW_SVG == format)
260 261
        _render_svg = new Render_svg(&_path_to_export);// svg.init(_path_to_export.c_str());
        
262

263
            
264
    if (NULL == _trace) { /* no trace is loaded, parse the file */
265 266 267 268 269 270 271

        /* Init data */
        Info::Entity::x_min          = 0;
        Info::Entity::x_max          = 0;
        Info::Render::_x_min_visible = 0;
        Info::Render::_x_max_visible = 0;

272 273 274 275
        _trace = new Trace();
        
        // Init of the thread
        parsing_thread thread;
Mathieu Faverge's avatar
Mathieu Faverge committed
276
        thread.init(parser, _trace, filename);
277

278
        int loaded = 0;
279
        float loaded_f = 0.0f;/* floating value of the loading file state. (between 0 and 1) */
280
        
281
        if(_main_window != NULL) { // If we have a window we show a progress bar
Mathieu Faverge's avatar
Mathieu Faverge committed
282 283
            _progress_dialog = new QProgressDialog(QObject::tr("Parsing"), QObject::tr("Cancel"), 0, 100, _main_window);
            _progress_dialog->setWindowTitle(QObject::tr("Loading of ")+QString::fromStdString(filename));
284 285 286 287 288 289 290
            _progress_dialog->show();
            
            _main_window->setDisabled(true);
            _progress_dialog->setDisabled(false); // to be able to cancel while parsing
        }

        thread.start();
291 292
        time_elapsed.start();
        buf_txt.str("");
293
        
Mathieu Faverge's avatar
Mathieu Faverge committed
294 295
        while(!(parser->is_end_of_parsing() && thread.isFinished())) {
            loaded_f = parser->get_size_loaded();/* value between 0 and 1. Should be multiplied by 100 to obtain a percentage */
296 297
            loaded = (int)(loaded_f*100.0f);

298 299 300 301 302
#ifdef WIN32
	  Sleep(1000);
#else
	  sleep(1); // We wait 1 second
#endif
Mathieu Faverge's avatar
Mathieu Faverge committed
303 304
          cout << QObject::tr("Loading of the trace : ").toStdString() << loaded << "%" ;

305 306

          if (loaded_f>0.0f){
Mathieu Faverge's avatar
Mathieu Faverge committed
307
                /* divided by to have in second 1000 since time_elapsed.elapsed() returns ms */
308
              time_buf = (int)(time_elapsed.elapsed() * (1.0 / loaded_f - 1.0) / 1000);
309 310 311

                if (time_buf>=3600){/* convert second in hour and min */
                    buf_txt << "Parsing... Remaining: " <<  time_buf/3600 << " h " << (time_buf%3600)/60 << " min " << time_buf%60 << " s";
312 313
                }
                else if (time_buf>=60){/* convert second in min */
314
                    buf_txt << "Parsing... Remaining: " << time_buf/60 << " min " << time_buf%60 << " s";
315 316
                }
                else{
317 318 319 320 321 322 323 324 325
                    buf_txt << "Parsing... Remaining: " << time_buf << " s";
                }
               
                cout << "  ~  " << buf_txt.str(); 

                if (  (NULL!=_main_window)  &&  (NULL!=_progress_dialog)  )
                    _progress_dialog->setLabelText(QString( buf_txt.str().c_str()));

                buf_txt.str("");
Mathieu Faverge's avatar
Mathieu Faverge committed
326
          }
327 328 329

          cout << endl;

330

331
          if(_main_window != NULL) { // If we have a window we show a progress bar
332 333 334 335
                update_progress_bar(loaded);
                QApplication::processEvents();
            
                if(_progress_dialog->wasCanceled()) {
Mathieu Faverge's avatar
Mathieu Faverge committed
336 337 338
                    cout << QObject::tr("Canceled at ").toStdString() << loaded << "%" << endl;
                    parser->set_canceled();
                    *Message::get_instance() << QObject::tr("The trace opening was canceled by the user at ").toStdString() << loaded << "%" << Message::ende;
339 340 341 342
                   
                    break; // Quit the loop
                }
            }
343 344 345 346 347
        }
        
        Error::print_numbers();
        Error::flush("log.txt");
        
348 349 350 351 352
        if(_main_window != NULL) { // If we have a window we show a progress bar
            delete _progress_dialog;
            _main_window->setDisabled (false);
        }
        // Wait for the end of the thread
Mathieu Faverge's avatar
Mathieu Faverge committed
353
        while(!thread.wait()) {
354
        }
355
                
Mathieu Faverge's avatar
Mathieu Faverge committed
356
        delete parser;       
357
    }
358
    else if ( _file_opened != filename) {/* just check if execution is normal */
359 360
        *Message::get_instance() << "Try to use file: " << filename << " instead of a previous parsed file: " << _file_opened << Message::ende;
    }
361

362 363 364 365 366 367
    if (Info::Render::_x_min_visible == Info::Render::_x_max_visible){// first time
        _trace->set_interval_constrained(new Interval(0,_trace->get_max_date()));
    }else{
        _trace->set_interval_constrained(new Interval(Info::Render::_x_min_visible, Info::Render::_x_max_visible));
    }
         
368

369
    if (_DRAW_OPENGL == format) {
370
        drawing_ogl.build(&render, _trace);
371 372 373
        _render_opengl->build();
        _render_opengl->updateGL();
        _render_opengl->refresh_scroll_bars();
374
        _file_opened = filename;/* store filename for a future export */
375
    }else if (_DRAW_SVG == format) {
376 377 378 379 380 381 382 383 384 385 386 387

        Element_pos buf_min, buf_max;
        
        /* Store current view */
        buf_min = Info::Entity::x_min;
        buf_max = Info::Entity::x_max;

        /* Change current view to fit the user zoom */
        Info::Entity::x_min = Info::Render::_x_min_visible;
        Info::Entity::x_max = Info::Render::_x_max_visible;

        
388 389
        Render<Render_svg> render(_render_svg);
        drawing_ogl.build(&render, _trace);
390
  
391
        delete _render_svg;/* Release the svg render */
392 393 394 395 396

        /* Restore previous view */
        Info::Entity::x_min = buf_min;
        Info::Entity::x_max = buf_max;
        
397
        // svg.end();
398
    }else{/* error */
399
        *Message::get_instance() << "No kind of render recognized" << Message::ende;
400 401
        return false;
    }
402
    //   std::cout << _trace->get_max_date().get_value() << __FILE__ << __LINE__ << std::endl;
403
       
404
    
405
    _is_trace_loaded = true;
406 407
    QApplication::restoreOverrideCursor();  

408 409 410 411
    return true;
}


412
int Core::run(){
413

414 415 416
    /*
     * If a window is displayed, enter in the Qt event loop.
     */
Johnny Jazeix's avatar
Normes.  
Johnny Jazeix committed
417
    if (_is_window_displayed){
418 419 420 421 422
        return app->exec();
    }
    else{/* else, quit the application */
        return EXIT_SUCCESS;
    }
423 424 425 426
}



427 428 429 430 431 432 433 434 435 436 437

/***********************************
 *
 *
 *
 * The command line parameter processing functions.
 *
 *
 *
 **********************************/

438
int Core::get_state(int argc, char** argv){
439 440
 

441
    /*The following statics must be initialised in case their are not overrided by a command line request*/
442
    /*  Svg::set_height_factor (20);
443
    Svg::set_wide_factor (100);
444
    Svg::set_interval(0, 0);
445
    Svg::set_accuracy(0.1);
446
    Svg::set_scale_frequency(50);*/
447

Johnny Jazeix's avatar
Johnny Jazeix committed
448 449
    if(argc == 1){
        // just the name of the program, launch the window interface
450
        return _STATE_LAUNCH_GRAPHICAL_INTERFACE;
Johnny Jazeix's avatar
Johnny Jazeix committed
451
    }
452

Johnny Jazeix's avatar
Johnny Jazeix committed
453 454
    int state = _STATE_DISPLAY_HELP; 

455

456
    for(int i = 1 ; i < argc ; i ++) {
Johnny Jazeix's avatar
Johnny Jazeix committed
457

458
        if((string)argv[i] == "-h") {
Johnny Jazeix's avatar
Johnny Jazeix committed
459
            // display the help message
Johnny Jazeix's avatar
Johnny Jazeix committed
460 461
            return _STATE_DISPLAY_HELP;
        }
462
        else if((string)argv[i] == "-f" || (string)argv[i] == "-a") {
463
            // We want to open the file which follows this argument
Johnny Jazeix's avatar
Johnny Jazeix committed
464
            i ++;
Johnny Jazeix's avatar
Johnny Jazeix committed
465
            if(i < argc) {
Johnny Jazeix's avatar
Johnny Jazeix committed
466
                _file_opened = argv[i];
467
                state = _STATE_OPEN_FILE;
Johnny Jazeix's avatar
Johnny Jazeix committed
468
            }
Johnny Jazeix's avatar
Johnny Jazeix committed
469
            else {
Mathieu Faverge's avatar
Mathieu Faverge committed
470
                cerr << QObject::tr("no filename to open").toStdString() << endl;
Johnny Jazeix's avatar
Johnny Jazeix committed
471 472
                return _STATE_UNKNOWN;
            }
Johnny Jazeix's avatar
Johnny Jazeix committed
473
        }
Johnny Jazeix's avatar
Johnny Jazeix committed
474
        else if((string)argv[i] == "-e") {
475
            // We want to export the file which follows this argument
Johnny Jazeix's avatar
Johnny Jazeix committed
476
            i ++;
477
            if(i < argc) {
Johnny Jazeix's avatar
Johnny Jazeix committed
478 479
                _path_to_export = argv[i];
                state = _STATE_EXPORT_FILE;
480 481
            }
            else{
Mathieu Faverge's avatar
Mathieu Faverge committed
482
                cerr << QObject::tr("no filename for export").toStdString() << endl;
Johnny Jazeix's avatar
Johnny Jazeix committed
483
                return _STATE_UNKNOWN;
484 485
            }
        }
Johnny Jazeix's avatar
Normes.  
Johnny Jazeix committed
486
        else if((string)argv[i] == "-t") {
487
            // We want to export the file which follows this argument
Johnny Jazeix's avatar
Johnny Jazeix committed
488
            extract_times(argv[++ i]);
489
            if(_time_end == -1 || _time_start == -1){
Mathieu Faverge's avatar
Mathieu Faverge committed
490
                cerr << QObject::tr("One of the time do not exist").toStdString() << endl;
491 492 493
                state = _STATE_LAUNCH_GRAPHICAL_INTERFACE;
            }
            else if (_time_end < _time_start){
Mathieu Faverge's avatar
Mathieu Faverge committed
494
                cerr << QObject::tr("The end time is lower than the start one!").toStdString() << endl;
495 496
                state = _STATE_LAUNCH_GRAPHICAL_INTERFACE;
            }
Johnny Jazeix's avatar
Johnny Jazeix committed
497
            state |= _STATE_IN_AN_INTERVAL;
Johnny Jazeix's avatar
Johnny Jazeix committed
498
        }
Johnny Jazeix's avatar
Johnny Jazeix committed
499
        else if(argv[i][0] == '-') {
500
            // This is here where we put new options !! (for filters for example)
501

Johnny Jazeix's avatar
Normes.  
Johnny Jazeix committed
502
            if((string)argv[i] == "-epsilon") {
Johnny Jazeix's avatar
Johnny Jazeix committed
503
                i ++;
Johnny Jazeix's avatar
Normes.  
Johnny Jazeix committed
504
                if(i > argc) {
Mathieu Faverge's avatar
Mathieu Faverge committed
505
                    cerr << QObject::tr("need an other argument for epsilon").toStdString() << endl;
Johnny Jazeix's avatar
Johnny Jazeix committed
506 507
                    return _STATE_UNKNOWN;
                }
Johnny Jazeix's avatar
Normes.  
Johnny Jazeix committed
508
                else {
Johnny Jazeix's avatar
Johnny Jazeix committed
509 510
                    double accuracy = convert_to_double(argv[i]);
                    if(accuracy < 0.) {
Mathieu Faverge's avatar
Mathieu Faverge committed
511
                        cerr << QObject::tr("need a positive accuracy for epsilon").toStdString() << endl;
Johnny Jazeix's avatar
Johnny Jazeix committed
512 513
                        return _STATE_UNKNOWN;
                    }
Johnny Jazeix's avatar
Normes.  
Johnny Jazeix committed
514
                    else {
515
                        //  Svg::set_accuracy(accuracy);
Johnny Jazeix's avatar
Johnny Jazeix committed
516 517
                    }
                }
518
            }
519 520 521 522 523 524 525 526 527 528 529 530
            else if((string)argv[i] == "-wide") {
                i ++;
                if (i > argc) {
                    cerr << "need an other argument for wide" << endl;
                    return _STATE_UNKNOWN;
                }
                double wide = convert_to_double(argv[i]);
                if(wide < 0.) {
                    cerr << "need a positive width for the export" << endl;
                    return _STATE_UNKNOWN;
                }
                else {
531
                    //  Svg::set_wide_factor(wide);
532 533 534 535 536 537 538 539 540 541 542 543 544 545
                }
            }
            else if((string)argv[i] == "-grow") {
                i ++;
                if (i > argc) {
                    cerr << "need an other argument for grow" << endl;
                    return _STATE_UNKNOWN;
                }
                double grow = convert_to_double(argv[i]);
                if(grow < 0.) {
                    cerr << "need a positive grown for the export" << endl;
                    return _STATE_UNKNOWN;
                }
                else {
546
                    //  Svg::set_height_factor(grow);
547 548 549 550 551 552 553 554 555 556 557 558 559 560
                }
            }
            else if((string)argv[i] == "-scale") {
                i ++;
                if (i > argc) {
                    cerr << "need an other argument for scale" << endl;
                    return _STATE_UNKNOWN;
                }
                double scale = convert_to_double(argv[i]);
                if(scale < 0.) {
                    cerr << "need a positive scale for the export" << endl;
                    return _STATE_UNKNOWN;
                }
                else {
561
                    //  Svg::set_scale_frequency(scale);
562 563
                }
            }
564
            else if((string)argv[i] == "-interval") {
Johnny Jazeix's avatar
Johnny Jazeix committed
565
              
566 567 568 569 570 571 572 573 574 575 576 577
                if (!(i +2 < argc)) {
                    cerr << "interval argc" << endl;
                    return _STATE_UNKNOWN;
                }
                if (argv[i+1][0] == '-') {
                    cerr << "interval -" << endl;
                    return _STATE_UNKNOWN;
                }
                if (!(argv[i+2][0] != '-'))  {
                    cerr << "interval -" << endl;
                    return _STATE_UNKNOWN;
                }
578
	   
579 580
                double t1 = atof(argv[i+1]);
                double t2 = atof(argv[i+2]);
581
	   
582 583 584
                if (t1 >= t2) {
                    cerr << "interval neg" << endl;
                    return _STATE_UNKNOWN;
585
                }
586

587
                // Svg::set_interval(t1, t2);
588
                i += 2;
Johnny Jazeix's avatar
Johnny Jazeix committed
589 590 591 592 593
            }
            else {
                cerr << "the argument "<< argv[i] << " is unknown." << endl;
                return _STATE_DISPLAY_HELP;
            }
594
	  
Johnny Jazeix's avatar
Johnny Jazeix committed
595 596
        }
        else{ // This is a file to open
Johnny Jazeix's avatar
Johnny Jazeix committed
597
            _file_opened = argv[i];
598
            state = _STATE_OPEN_FILE;
Johnny Jazeix's avatar
Johnny Jazeix committed
599
        }
600
    }
Johnny Jazeix's avatar
Johnny Jazeix committed
601
    return state;
602 603
}

604
void Core::extract_times(const char *name) {
Johnny Jazeix's avatar
Johnny Jazeix committed
605 606
    bool has_time_start = false;
    bool has_time_end = false;
Johnny Jazeix's avatar
Normes.  
Johnny Jazeix committed
607
    string temp = name;
Johnny Jazeix's avatar
Johnny Jazeix committed
608
    if(name[0] == '['){
609 610
        has_time_start = true;
    }
611
    if(name[strlen(name)-1] == ']') {
612 613
        has_time_end = true;
    }
614
            
Johnny Jazeix's avatar
Normes.  
Johnny Jazeix committed
615 616 617 618 619 620
    if(has_time_start && has_time_end) {
        string start = temp.substr(temp.find('[')+1, temp.find(']')-1);
        temp = temp.substr(temp.find(']')+1);
        string end = temp.substr(temp.find('[')+1, temp.find(']')-2);
        _time_start = convert_to_double(start);
        _time_end = convert_to_double(end);
621
    }
Johnny Jazeix's avatar
Normes.  
Johnny Jazeix committed
622 623
    else if(has_time_start) {
        _time_start = convert_to_double(temp.substr(temp.find('[')+1, temp.find(']')-1));
624
    }
Johnny Jazeix's avatar
Normes.  
Johnny Jazeix committed
625 626
    else if(has_time_end) {
        _time_end = convert_to_double(temp.substr(temp.find('[')+1, temp.find(']')-2));
627 628 629 630 631
    }
    else{
        _time_start = 0;
        _time_end = 0;
    }
632
}
633

634
void Core::launch_action(int state, void* arg) {
635

Olivier Lagrasse's avatar
Olivier Lagrasse committed
636 637
    DrawTrace buf;
    QGLFormat format(QGL::HasOverlay);
638

639
    switch(state) {
640 641

    case _STATE_DISPLAY_HELP :
642 643 644
        display_help();
        break;
        
645
    case _STATE_LAUNCH_GRAPHICAL_INTERFACE :
Olivier Lagrasse's avatar
Olivier Lagrasse committed
646
    case _STATE_OPEN_FILE:
647

648

Olivier Lagrasse's avatar
Olivier Lagrasse committed
649 650
        if ( _STATE_OPEN_FILE == state)
            message << QObject::tr("Opening the file: ").toStdString ()+_file_opened << Message::endi;
651
        
Olivier Lagrasse's avatar
Olivier Lagrasse committed
652 653
        _main_window = new Interface_graphic (this);/* launch the window interface */
        Message::set_interface (_main_window);/* define which interface will receive messages */
654

Olivier Lagrasse's avatar
Olivier Lagrasse committed
655
        //   format.setOverlay(true);
656 657 658
#ifdef WITH_VBO
        _render_opengl = new Render_alternate (this, _main_window, format);
#else
Olivier Lagrasse's avatar
Olivier Lagrasse committed
659
        _render_opengl = new Render_opengl (this, _main_window, format);
660
#endif
661

Olivier Lagrasse's avatar
Olivier Lagrasse committed
662 663 664 665
        if ( NULL == _render_opengl){
            message <<  QObject::tr("Cannot allocate memory for an OpengGL instance").toStdString () << Message::ende;
            break;
        }
666

Olivier Lagrasse's avatar
Olivier Lagrasse committed
667 668 669 670 671 672 673 674
        if (NULL == _render_opengl->context()){
            message <<  QObject::tr("Cannot allocate an OpengGL context").toStdString () << Message::ende;
            break;
        }
        
        if (false == _render_opengl->isValid()){
            message <<  QObject::tr("Invalid context: OpenGL is not supported on your system").toStdString () << Message::ende;
         }
675 676


Olivier Lagrasse's avatar
Olivier Lagrasse committed
677 678 679 680 681 682 683 684 685
        // if (false == _render_opengl->format().hasOverlay ()){
        //     /* If no overlay was created, check the original format */
 
        //     if (false == _render_opengl->context()->requestedFormat().hasOverlay())
        //         message <<  QObject::tr("No overlay format originally created").toStdString () << Message::endw;
        //     else
        //         message <<  QObject::tr("An overlay format was originally asked, but was not created").toStdString () << Message::endw;
        // }

686 687
        _main_window->bind_render_area((QGLWidget*)_render_opengl);

Olivier Lagrasse's avatar
Olivier Lagrasse committed
688 689 690 691 692 693
        if ( _STATE_OPEN_FILE == state){
            _main_window->opening_file(_file_opened);/* Must be called after binding the render area to the main window */
            
            if(false==draw_trace(_file_opened, _DRAW_OPENGL))
                message <<  QObject::tr("Draw trace failed").toStdString () << Message::ende;
        }
694

695
        _is_window_displayed = true;
696
        break;
697

698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
    case _STATE_OPEN_FILE_IN_AN_INTERVAL:
        
        launch_action(_STATE_OPEN_FILE, NULL); 
        
        break;

    case _STATE_RELEASE_RENDER_AREA:
        if (_render_opengl->unbuild()==false)
            message << "Close file : an error occured with trace releasing." << Message::ende;
        _file_opened.clear();

        if (false == _is_trace_loaded){
            *Message::get_instance() << "Try to release a render area whereas no file was loaded" << Message::ende;
        }else{
             _is_trace_loaded = false;
        }

        if (NULL == _trace){
            *Message::get_instance() << "Try to release a render area whereas no trace is loaded" << Message::ende;
        }else{
            delete _trace;
            _trace = NULL;
        }

       
        _render_opengl->updateGL();
        break;

726
        
727
    case _STATE_EXPORT_FILE:
Johnny Jazeix's avatar
Normes.  
Johnny Jazeix committed
728
        if (_file_opened.empty()){
729 730 731 732
            *Message::get_instance() << "Please to previously open a trace." << Message::endw;
            return;
        }

733
        *Message::get_instance() << "export of " << _file_opened << " to " << _path_to_export << Message::endi;
Johnny Jazeix's avatar
Normes.  
Johnny Jazeix committed
734 735
        
        draw_trace(_file_opened, _DRAW_SVG);
736
       
737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752
        break;

    case _STATE_EXPORT_FILE_IN_INTERVAL:
        cout << "export of " << _file_opened << " to " << _path_to_export << " between ";
        if(_time_start == 0){
            cout << "the beginning of the trace to ";
        }
        else{
            cout << _time_start << " seconds to ";
        }
        if(_time_end != 0){
            cout << _time_end << " seconds." << endl;
        }
        else{
            cout << "the end of the trace.";
        }
Johnny Jazeix's avatar
Normes.  
Johnny Jazeix committed
753
        
754
        // Svg::set_interval(_time_start, _time_end);
Johnny Jazeix's avatar
Normes.  
Johnny Jazeix committed
755 756
        draw_trace(_file_opened, _DRAW_SVG);
        
757
        break;
Olivier Lagrasse's avatar
CODE  
Olivier Lagrasse committed
758

759 760 761
    case _STATE_RENDER_AREA_CHANGE_TRANSLATE:
        _render_opengl->change_translate( *((int*)arg) );
        break;
762
	
763
    case _STATE_RENDER_AREA_CHANGE_SCALE:
764
        _render_opengl->change_scale( *((Element_pos*)arg) );
765 766
        break;

767 768 769 770
    case _STATE_RENDER_AREA_CHANGE_SCALE_Y:
        _render_opengl->change_scale_y( *((Element_pos*)arg) );
        break;

771 772 773 774
    case _STATE_RENDER_AREA_CHANGE_CONTAINER_SCALE:
        _render_opengl->change_scale_container_state( *((int*)arg) );
        break;

775 776
    case _STATE_RENDER_AREA_REPLACE_SCALE:
        _render_opengl->replace_scale( *((Element_pos*)arg) );
777
        _render_opengl->replace_scale_y( *((Element_pos*)arg) );
778 779 780 781 782 783
        break;

    case _STATE_RENDER_AREA_REPLACE_TRANSLATE:
        _render_opengl->replace_translate( *((Element_pos*)arg) );
        break;

784 785 786 787
    case _STATE_RENDER_AREA_REPLACE_TRANSLATE_Y:
        _render_opengl->replace_translate_y( *((Element_pos*)arg) );
        break;

788 789 790
    case _STATE_RENDER_AREA_REGISTERED_TRANSLATE:
        _render_opengl->registered_translate( *((int*)arg) );
        break;
791 792 793 794 795 796 797 798 799 800
	
    case _STATE_AJUST_SCROLL_BARS:
	//	cerr << "console: x_max " << ((Element_pos*)arg)[0] << " | y_max " << ((Element_pos*)arg)[1] << endl;
	_main_window->set_scroll_bars_length( ((Element_pos*)arg)[0], ((Element_pos*)arg)[1] );
	break;
	
    case _STATE_REFRESH_SCROLL_BARS:
	_main_window->linking_scroll_bars( ((Element_pos*)arg)[0], ((Element_pos*)arg)[1] );
	break;
	
801 802 803 804
    case _STATE_ZOOM_BOX_VALUE:
        _main_window->change_zoom_box_value( *((int*)arg) );
        break;

805 806
    case _STATE_RENDER_DISPLAY_INFORMATION:
        
807 808 809
        if(_trace != NULL){
            buf.display_information(_trace, Info::Render::_info_x, Info::Render::_info_y, Info::Render::_info_accurate);
        }
810
        break;
811

812
    case _STATE_RENDER_UPDATE:
813
        _render_opengl->updateGL();
814 815
        break;

816
    default:/* like _STATE_UNKNOWN */
Johnny Jazeix's avatar
Normes.  
Johnny Jazeix committed
817 818
        display_help();
        warning(string("Cannot determine the arguments past. Please check the correct syntax."));
819
       
820 821 822 823
    }
}


824
void Core::display_help(){
Mathieu Faverge's avatar
Mathieu Faverge committed
825 826 827 828 829 830 831 832 833 834 835 836
    cout << endl 
         << "Usage: vite [OPTION...] [[-f] inputfile] [-e outputfile] " << endl
         << endl
         << "  -h                 display this help                  " << endl
         << "  -f inputfile       open the inputfile                 " << endl
         << "  -a                 display all the inputfile (Default)" << endl
         << "  -t [T0]:[T1]       display the interval [T0:T1] (Default: T0 = 0 and T1 = Tmax)" << endl 
         << "  -e outputfile      export the trace file in the svg format to outpufile"        << endl
	 << "  -epsilon eps       specify the epsilon value for export" << endl
         << "  -wide w            specify the witdh factor for export"  << endl
         << "  -grow g            specify the height factor for export" << endl 
	 << endl;
837 838
}

839

840 841


842 843 844 845 846 847 848 849 850 851
/***********************************
 *
 *
 *
 * Informative message functions.
 *
 *
 *
 **********************************/
 
852
void Core::error(const string &s) const{
Olivier Lagrasse's avatar
CODE  
Olivier Lagrasse committed
853
    cerr << "ERROR: " << s <<endl;
854 855 856
}


857
void Core::warning(const string &s) const{
Olivier Lagrasse's avatar
CODE  
Olivier Lagrasse committed
858
    cerr << "Warning: "<< s <<endl;
859 860
}

861
void Core::information(const string &s) const{
862
    cerr << s <<endl;
863
}
864

865
const string Core::get_filename() const{
866 867
    return _file_opened;
}
Jule Marcoueille's avatar
Jule Marcoueille committed
868

869
void Core::set_path_to_export(const string& path){
870 871 872
    _path_to_export = path;
}

873 874
const QString** Core::get_runenv() const{
    return (const QString**)_run_env;
Jule Marcoueille's avatar
Jule Marcoueille committed
875
}
876

877
void Core::update_progress_bar(const int loaded){
878 879 880
    _progress_dialog->setValue(loaded);
    _progress_dialog->update();
}
881

882
void Core::set_min_value_for_export(const double d) {
883 884 885
    _time_start = d;
}

886
void Core::set_max_value_for_export(const double d) {
887 888
    _time_end = d;
}
889

890
Trace *Core::get_trace() const {
891 892
    return _trace;
}
893

Mathieu Faverge's avatar
Mathieu Faverge committed
894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920

void Core::export_variable(Variable *var, string filename) {
    const list<pair<Date, Double> > *variable_values = var->get_values();

    ofstream file(filename.c_str(), ios::out | ios::trunc);

    if(file) {
        double first_value = 0.;
        double second_value = 0.;
        const double min = var->get_min().get_value();
        const double max = var->get_max().get_value();
        
        for(list<pair<Date, Double> >::const_iterator value = variable_values->begin();
            value != variable_values->end();
            value++) {
            first_value = (*value).first.get_value();
            second_value =((*value).second.get_value()-min)/(max-min) ;
            file << first_value << "\t" << second_value << endl;
        }
    
        file.close();
    }
    else { 
        // Error
        _main_window->error("Unable to open " + filename + " in order to export the counter");
    }
}