Interface_graphic.cpp 32.9 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
/*!
 *\file interface_graphic.cpp
 *\brief This is graphical interface C source code.
 */
47 48
#include <fstream>
#include <string>
Mathieu Faverge's avatar
Mathieu Faverge committed
49
#include <stack>
50 51 52 53 54 55 56 57
#include <map>
#include <list>
/* -- */
#include <QObject>
/* -- */
#include "common/common.hpp"
#include "common/info.hpp"
#include "common/Tools.hpp"
Olivier Lagrasse's avatar
Olivier Lagrasse committed
58
#include "common/Session.hpp"
59 60
/* -- */
#include "trace/values/Values.hpp"
Mathieu Faverge's avatar
Mathieu Faverge committed
61 62 63
#include "trace/EntityTypes.hpp"
#include "trace/Entitys.hpp"
#include "trace/Trace.hpp"
64
/* -- */
65
//#include "render/render.hpp"
Olivier Lagrasse's avatar
Olivier Lagrasse committed
66
//#include "render/render_svg.hpp"
67 68 69 70 71 72
/* -- */
#include "statistics/Stats_window.hpp"
/* -- */
#include "interface/resource.hpp"
//#include "interface/help.hpp"
//#include "interface/interface.hpp"
73 74
#include "core/Core.hpp"
#include "interface/Interface_graphic.hpp"
75
//#include "resource.hpp"
76

77
#include "plugin/Command_window.hpp"
78 79


80
using namespace std;
81

82 83 84 85
/***********************************
 *
 *
 *
86
 * Constructors and destructor.
87 88 89 90 91 92
 *
 *
 *
 **********************************/


93
Interface_graphic::Interface_graphic(Core* core, QWidget *parent):QMainWindow(parent){ 
94 95

    setupUi(this);
96
    
97
    _zoom_box_check_value="";
98
    _core = core;
99
    _is_rendering_trace = false;
100
    _no_warning = false;/* display warnings */
101
     _on_manual_change = false;
102 103 104 105
    
    _x_factor_virtual_to_real = 0.1;
    _y_factor_virtual_to_real = 0.1;
    
106
    load_windows();
107

108
    setMouseTracking (true);/* to catch mouse events */
109
    
Mathieu Faverge's avatar
Mathieu Faverge committed
110
    _stats_window = NULL; // Creation when needed (when it has to be shown)
111 112
    //    _cmd_window = NULL;
    _cmd_window = new Command_window(this,this);
113

114 115
    // For drag and drop operations
    setAcceptDrops(true);
116 117 118 119
}

Interface_graphic::~Interface_graphic(){

Mathieu Faverge's avatar
Mathieu Faverge committed
120 121
    if(_stats_window != NULL)
        delete _stats_window;
122
 
Johnny Jazeix's avatar
Normes.  
Johnny Jazeix committed
123
    /* Qt desallocates this, _ui_info_window and _render_area automatically */
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
}



/***********************************
 *
 *
 *
 * Window function.
 *
 *
 *
 **********************************/

void Interface_graphic::load_windows(){

    QUiLoader loader;
141
    QFile file_info( ":/window/info_window.ui");
142
    QFile file_selection_export(":/window/option_export_window.ui");
143
    QFile file_help(":/window/help_window.ui");    
Olivier Lagrasse's avatar
Olivier Lagrasse committed
144
    //  QFile file_kind_of_export(":/window/kind_of_export.ui");
Mathieu Faverge's avatar
Mathieu Faverge committed
145
    QFile file_counter_to_export(":/window/list_of_counter_to_export.ui");
146 147 148 149

    if (!QGLFormat::hasOpenGL())
        qFatal("This system has no OpenGL support");
       
150
    /* Load the informative window from a .ui file */
151
    file_info.open(QFile::ReadOnly);
152
    CKFP(_ui_info_window = loader.load(&file_info, this), "Cannot open the .ui file : " << ":/window/info_window.ui");
153 154
    file_info.close();

155 156
    /* Load the _option_export_window from a .ui file */
    file_selection_export.open(QFile::ReadOnly);
157
    CKFP(_ui_time_selection_export = loader.load(&file_selection_export, this), "Cannot open the .ui file : " << ":/window/option_export_window.ui");
158 159
    file_selection_export.close();

Mathieu Faverge's avatar
Mathieu Faverge committed
160 161 162 163 164 165 166
    /* Load the choice counter for export box */
    file_counter_to_export.open(QFile::ReadOnly);
    CKFP(_ui_counter_choice_to_export = loader.load(&file_counter_to_export, this), "Cannot open the .ui file : " << ":/window/option_export_window.ui");
    CKFP(_counter_list_names = qFindChild<QComboBox*>(_ui_counter_choice_to_export, "combobox"), "Cannot find the svg export button in the .ui file");
    file_counter_to_export.close();


167 168 169 170
    /* Load the help dialog box from a .ui file */
    file_help.open(QFile::ReadOnly);
    CKFP(_ui_help_window = loader.load(&file_help, this), "Cannot open the .ui file : " << ":/window/help_window.ui");
    file_help.close();
171
 
Mathieu Faverge's avatar
Mathieu Faverge committed
172
    /* Load the export combo box choice from a .ui file */
Olivier Lagrasse's avatar
Olivier Lagrasse committed
173 174
    /*    file_kind_of_export.open(QFile::ReadOnly);
        CKFP(_ui_kind_of_export_choice = loader.load(&file_kind_of_export, this), "Cannot open the .ui file : " << ":/window/kind_of_export.ui");
Mathieu Faverge's avatar
Mathieu Faverge committed
175
    CKFP(_ui_svg_export_button = qFindChild<QRadioButton*>(_ui_kind_of_export_choice, "svg_export"), "Cannot find the svg export button in the .ui file");
Olivier Lagrasse's avatar
Olivier Lagrasse committed
176 177 178
    CKFP(_ui_png_export_button = qFindChild<QRadioButton*>(_ui_kind_of_export_choice, "png_export"), "Cannot find the png export button in the .ui file");
     CKFP(_ui_counter_export_button = qFindChild<QRadioButton*>(_ui_kind_of_export_choice, "counter_export"), "Cannot find the svg export button in the .ui file");   
    file_kind_of_export.close();*/
Mathieu Faverge's avatar
Mathieu Faverge committed
179
    
180 181
    /* Set some windows properties */
    _ui_info_window->setWindowFlags(_ui_info_window->windowFlags() | Qt::WindowStaysOnTopHint);/* Always display info_window on top */
182 183
    _ui_time_selection_export->setWindowFlags(_ui_time_selection_export->windowFlags() | Qt::WindowStaysOnTopHint);
   
184 185
    /* Load widget from the .ui file */
    CKFP(_ui_render_area_layout = qFindChild<QVBoxLayout*>(this, "render_area_layout"), "Cannot find the render_area layout in the .ui file");   
186
    
187
    CKFP(_ui_fullscreen_menu = qFindChild<QAction*>(this, "fullscreen"), "Cannot find the fullscreen menu in the .ui file");
188 189
    CKFP(_ui_info_trace_text = qFindChild<QTextEdit*>(_ui_info_window, "info_trace_text"), "Cannot find the info_trace_text QTextEdit widget in the .ui file");
    CKFP(_ui_info_selection_text = qFindChild<QTextEdit*>(_ui_info_window, "info_selection_text"), "Cannot find the info_selection_text QTextEdit widget in the .ui file");
Johnny Jazeix's avatar
Johnny Jazeix committed
190 191
    CKFP(_ui_toolbar_check = qFindChild<QAction*>(this, "toolbar_menu"), "Cannot find the tool bar checker in the .ui file");   
    CKFP(_ui_toolbar = qFindChild<QToolBar*>(this, "toolBar"), "Cannot find the tool bar in the .ui file");   
192

193 194
    CKFP(_ui_x_scroll = qFindChild<QScrollBar*>(this, "x_scroll"), "Cannot find the horizontal scroll bar in the .ui file");
    CKFP(_ui_y_scroll = qFindChild<QScrollBar*>(this, "y_scroll"), "Cannot find the vertical scroll bar in the .ui file");
195 196 197

    CKFP(_ui_zoom_box = qFindChild<QComboBox*>(this, "zoom_box"), "Cannot find the zoom box in the .ui file");   

198 199
    //  CKFP(Info::Render::_ui_render_min_value = qFindChild<QLabel*>(this, "render_min_value"), "Cannot find the render_min_value label in the .ui file");
    // CKFP(Info::Render::_ui_render_max_value = qFindChild<QLabel*>(this, "render_max_value"), "Cannot find the render_max_value label in the .ui file");
200

201
    /* Export window */
202
    CKFP(_ui_export_ok_button = qFindChild<QPushButton*>(_ui_time_selection_export, "option_export_ok"), "Cannot find the ok push button in the export dialog .ui file");   
203 204 205 206 207 208 209 210 211 212 213 214 215
    CKFP(_ui_min_time_export = qFindChild<QLineEdit*>(_ui_time_selection_export, "min_time"), "Cannot find the ok push button in the export dialog .ui file");

    CKFP(_ui_max_time_export = qFindChild<QLineEdit*>(_ui_time_selection_export, "max_time"), "Cannot find the ok push button in the export dialog .ui file");

    CKFP(_ui_CheckBox_time_export = qFindChild<QCheckBox*>(_ui_time_selection_export, "export_all"), "Cannot find the Line text \"export_all\" in the export dialog .ui file");   

    CKFP(_ui_wide_export = qFindChild<QLineEdit*>(_ui_time_selection_export, "wide"), "Cannot find the Line text \"wide\" in the export dialog .ui file");   
    CKFP(_ui_grow_export = qFindChild<QLineEdit*>(_ui_time_selection_export, "grow"), "Cannot find the Line text \"grow\" in the export dialog .ui file");   
    CKFP(_ui_accuracy_export = qFindChild<QLineEdit*>(_ui_time_selection_export, "accuracy"), "Cannot find the Line text \"accuracy\" in the export dialog .ui file");   
    CKFP(_ui_CheckBox_wide_export = qFindChild<QCheckBox*>(_ui_time_selection_export, "checkBox_wide"), "Cannot find the Line text \"wide\" in the export dialog .ui file");   
    CKFP(_ui_CheckBox_grow_export = qFindChild<QCheckBox*>(_ui_time_selection_export, "checkBox_grow"), "Cannot find the Line text \"grow\" in the export dialog .ui file");   
    CKFP(_ui_CheckBox_accuracy_export = qFindChild<QCheckBox*>(_ui_time_selection_export, "checkBox_accuracy"), "Cannot find the Line text \"accuracy\" in the export dialog .ui file");   
    CKFP(_ui_more_options_export = qFindChild<QCommandLinkButton*>(_ui_time_selection_export, "more_options"), "Cannot find the button \"more_options\" in the export dialog .ui file");   
216

217 218 219
    /* Help window */
    CKFP(_ui_help_ok_button = qFindChild<QPushButton*>(_ui_help_window, "help_ok"), "Cannot find the ok push button in the help dialog .ui file");     
 
220

Mathieu Faverge's avatar
Mathieu Faverge committed
221 222 223
    connect(_ui_counter_choice_to_export, SIGNAL(accepted()),
            this, SLOT(counter_choosed_triggered()));

Olivier Lagrasse's avatar
Olivier Lagrasse committed
224
    /* connect(_ui_kind_of_export_choice, SIGNAL(accepted()),
Mathieu Faverge's avatar
Mathieu Faverge committed
225
            this, SLOT(choice_of_the_export_pressed()));
Olivier Lagrasse's avatar
Olivier Lagrasse committed
226
    */
227
    connect(_ui_export_ok_button, SIGNAL(pressed()),
228
            this, SLOT(option_export_ok_pressed()));
229 230 231 232

    connect(_ui_help_ok_button, SIGNAL(clicked()),
            _ui_help_window, SLOT(close()));

233 234
    /* 
       Special function of Qt which allows methods declared as slots and which name are 'on_[widget]_[action]()' to be called when the 'widget' triggered the signal corresponding to 'action'.
235
       /!\ -> use NULL as argument, else messages will be duplicated! 
Johnny Jazeix's avatar
Normes.  
Johnny Jazeix committed
236
    */
237
    QMetaObject::connectSlotsByName(NULL);
238
    // QMetaObject::connectSlotsByName(_ui_time_selection_export);
239
    
240 241 242
    _ui_x_scroll->setMaximum(_REAL_X_SCROLL_LENGTH);
    _ui_y_scroll->setMaximum(_REAL_Y_SCROLL_LENGTH);
    
243 244
    /* Display the main window */
    this->show();
245 246 247 248
}



249 250 251 252 253 254 255 256 257 258
/***********************************
 *
 *
 *
 * Informative message functions.
 *
 *
 *
 **********************************/

259
void Interface_graphic::error(const string &s) const{
260 261
    if ( true==_no_warning ) return;/* do not display error messages */

262
    QString buf = s.c_str();
263
    _ui_info_trace_text->moveCursor(QTextCursor::End);/* Insert the new text on the end */
264
    _ui_info_trace_text->insertHtml("<font color='red'>"+buf+"</font><br /><br />");
Olivier Lagrasse's avatar
Olivier Lagrasse committed
265

266
    _ui_info_window->show();/* show info_window */
267 268 269
}


270
void Interface_graphic::warning(const string &s) const{
271 272
    if ( true==_no_warning ) return;/* do not display warning messages */

273
    QString buf = s.c_str();
274
    _ui_info_trace_text->moveCursor(QTextCursor::End);/* Insert the new text on the end */
275
    _ui_info_trace_text->insertHtml("<font color='orange'>"+buf+"</font><br /><br />");
Olivier Lagrasse's avatar
Olivier Lagrasse committed
276

277
    _ui_info_window->show();/* show info_window */
278
}
279 280


281
void Interface_graphic::information(const string &s) const{
282
    QString buf = s.c_str();
283
    _ui_info_trace_text->moveCursor(QTextCursor::End);/* Insert the new text on the end */
284
    _ui_info_trace_text->insertHtml("<font color='green'>"+buf+"</font><br /><br />");
285 286 287 288
}



289 290 291 292 293 294 295 296 297 298 299 300 301


/***********************************
 *
 *
 *
 * Selected Trace entity informative function.
 *
 *
 *
 **********************************/


302
void Interface_graphic::selection_information(const string &s) const{
303 304
    QString buf = s.c_str();
    _ui_info_selection_text->clear();/* Clear the current text (if exists) */
305 306
    _ui_info_selection_text->insertHtml("<font color='blue'>"+buf+"</font>");
    _ui_info_selection_text->moveCursor(QTextCursor::Start);/* Insert the new text on the begin */
307 308 309 310 311 312 313 314

    _ui_info_window->show();/* show info_window */
}





315 316 317 318 319 320 321 322 323 324
/***********************************
 *
 *
 *
 * Tool functions.
 *
 *
 *
 **********************************/

325
void Interface_graphic::opening_file(const string &path){
326 327 328
    
    information(string("File opened: ")+ path);
    
329
    _trace_path = path;
Olivier Lagrasse's avatar
Olivier Lagrasse committed
330

331
    this->setWindowTitle(QString("ViTE :: ")+_trace_path.c_str());
332
    
333 334
    QApplication::setOverrideCursor(Qt::WaitCursor);
   
335
    /* if (_core->draw_trace(path)==false){
Johnny Jazeix's avatar
Normes.  
Johnny Jazeix committed
336 337
       error("Draw trace failed");
       }*/
338

339
    //  _ui_render_area->build();
340

341
    _is_rendering_trace = true;
342
        
343
    //   _ui_render_area->updateGL();  
344

345
    QApplication::restoreOverrideCursor();   
346

347
}
348 349


350 351 352 353 354 355
void Interface_graphic::bind_render_area(QGLWidget* render_area){
    /* Bind the render area to a layout (_ui_render_area_layout) */
    _ui_render_area = render_area;
    _ui_render_area_layout->addWidget(_ui_render_area);
}

356

357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
void Interface_graphic::set_scroll_bars_length(Element_pos new_x_virtual_length, Element_pos new_y_virtual_length){
    _x_factor_virtual_to_real = _REAL_X_SCROLL_LENGTH / new_x_virtual_length;
    _y_factor_virtual_to_real = _REAL_Y_SCROLL_LENGTH / new_y_virtual_length;
}


void Interface_graphic::linking_scroll_bars(Element_pos x_bar_new_value, Element_pos y_bar_new_value){
    _on_manual_change = true;
    _ui_x_scroll->setValue(virtual_to_real_scroll_unit(x_bar_new_value, 'x'));
    _ui_y_scroll->setValue(virtual_to_real_scroll_unit(y_bar_new_value, 'y'));
    _on_manual_change = false;
}

Element_pos Interface_graphic::real_to_virtual_scroll_unit(int scroll_position, const char &axe){
    switch (axe){
    case 'x':
	return (scroll_position/_x_factor_virtual_to_real);
	break;
    case 'y':
	return (scroll_position/_y_factor_virtual_to_real);
	break;
    
    default:
	error("Option not recognized.");
	break;
    }
    return scroll_position;
}

int Interface_graphic::virtual_to_real_scroll_unit(Element_pos scroll_position, const char &axe){
    switch (axe){
    case 'x':
389
      return (int)(scroll_position*_x_factor_virtual_to_real);
390 391
	break;
    case 'y':
392
	return (int)(scroll_position*_y_factor_virtual_to_real);
393 394 395 396 397 398
	break;
    
    default:
	error("Option not recognized.");
	break;
    }
399
    return (int)scroll_position;
400 401
}

402 403 404 405 406
void Interface_graphic::change_zoom_box_value(int new_value){
    QString formated_value;
    ostringstream int_to_string;
    int index;

407
    int max_value = INT_MIN;
408

409
    if (max_value == new_value)/* prevent overflow value */
410
        new_value = INT_MAX;
411

412 413 414 415 416 417
    int_to_string << new_value;
  
    formated_value = QString(int_to_string.str().c_str())+QString("%");
   
    index = _ui_zoom_box->findText( formated_value, Qt::MatchExactly);

418 419
    _zoom_box_check_value = formated_value;

420 421 422 423
    if (-1 == index){/* value does not exist in the list */
        _ui_zoom_box->removeItem(0);
        _ui_zoom_box->insertItem(0,  formated_value);/* add the value */
        _ui_zoom_box->setCurrentIndex(0);
424
       
425 426 427 428 429 430
    }
    else{
        _ui_zoom_box->setCurrentIndex(index);/* value exists, select it */
    }
}

431

432 433 434 435 436 437 438 439 440
/***********************************
 *
 *
 *
 * Widget slot functions.
 *
 *
 *
 **********************************/
441 442 443 444

void Interface_graphic::on_open_triggered(){

    /* 
445 446
       Be careful! Here, this is used instead 'this' pointer because 'this' is not a window, 
       it's an interface class (Interface_graphic*). The real window is this. 
447
       If 'this' is put, the application closes after the getOpenFilename() dialog box closes.
448
    */
449
    QString filename = QFileDialog::getOpenFileName(this);
Olivier Lagrasse's avatar
Olivier Lagrasse committed
450
  
451
    if (!filename.isEmpty()){
452
        open_trace(filename);
453
    }/* end if (!filename.isEmpty()) */
454 455 456 457
    
}


Olivier Lagrasse's avatar
Olivier Lagrasse committed
458 459 460 461
//void Interface_graphic::choice_of_the_export_pressed() {

void Interface_graphic::on_export_file_triggered(){

462 463 464 465 466
    QStringList selected_files;
    QFileDialog file_dialog(this);
    QString filename;
    QString extension;
    signed int filename_size;/* Must be signed to check if size is negative */
Olivier Lagrasse's avatar
Olivier Lagrasse committed
467 468 469



470 471
    if(_is_rendering_trace == false)
        return;
Olivier Lagrasse's avatar
Olivier Lagrasse committed
472

473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
    /* Try to recover the last file dialog state. */
    if (Session::Export::file_dialog_state!=Session::Export::DEFAULT_STATE){
        if (!file_dialog.restoreState(Session::Export::file_dialog_state))
            warning("Cannot restore the export file dialog state");
    }
    
    file_dialog.setLabelText(QFileDialog::FileName, QString(_trace_path.substr(0, _trace_path.find_last_of('.')).c_str()) + ".svg");
    file_dialog.setFileMode(QFileDialog::AnyFile);/* Allow to browse among directories */
    file_dialog.setAcceptMode(QFileDialog::AcceptSave);/* Only save file */
    file_dialog.setNameFilter(tr("Vector (*.svg);;Bitmap PNG (*.png);;Bitmap JPEG (*.jpeg *.jpg);;Counter (*.txt)"));
    
    if (file_dialog.exec()){/* Display the file dialog */
        selected_files = file_dialog.selectedFiles();//"/tmp/test.png";//file_dialog.selectedFile();
        
        if (selected_files.size()==1)
            filename = selected_files[0];/* Get the first filename */
        else{
            
            if (selected_files.size()==0)
                warning("No file was selected.");
            else
                warning("Cannot save: too much files were selected.");
            
            return;
        }
        
        filename_size = filename.size() - 4;/* filename without extension */
Olivier Lagrasse's avatar
Olivier Lagrasse committed
500

501
        if (filename_size <= 0){
Olivier Lagrasse's avatar
Olivier Lagrasse committed
502

503 504
            error("Filename is not valid. Must be {filename}.{extension}");
            return;
Olivier Lagrasse's avatar
Olivier Lagrasse committed
505

506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
        }else if (filename.at(filename_size) != QChar('.')){/* No extension or extension is not correct */

            /* Add it manually */
            const QString filter_selected = file_dialog.selectedNameFilter();
            
            if ("Vector (*.svg)"==filter_selected){
                filename += ".svg";
                extension = "svg";
            }else if ("Bitmap PNG (*.png)"==filter_selected){
                filename += ".png"; 
                extension = "png";
            }else if ("Bitmap JPEG (*.jpeg *.jpg)"==filter_selected){
                filename += ".jpg";
                extension = "jpg";
            }else if ("Counter (*.txt)"==filter_selected){
                filename += ".txt";
                extension = "txt";
            }else
                warning("Cannot recognize the filter: "+filter_selected.toStdString());
            
            /* Update the _export_filename attribute which will be used in SVG or Counter export functions */      
            _export_filename = filename;
            
        }else
            extension = filename.right(3).toLower();/* in lower case */
    }
    
    /* Save the file dialog state (History and current directory) */
    Session::Export::file_dialog_state = file_dialog.saveState();
    
    if (extension=="svg"){
        //    if(_ui_svg_export_button->isChecked()) {
        /*      ostringstream temp;
Mathieu Faverge's avatar
Mathieu Faverge committed
539 540 541 542 543 544 545 546 547 548
        
        temp.str("");
        
        temp << Info::Render::_x_min_visible; 
        _ui_min_time_export->setText(temp.str().c_str());
        
        temp.str("");
        temp << Info::Render::_x_max_visible; 
        _ui_max_time_export->setText(temp.str().c_str());
        _ui_time_selection_export->show();
549 550 551 552
*/
        _core->set_path_to_export(filename.toStdString());
        _core->launch_action(Core::_STATE_EXPORT_FILE_IN_INTERVAL);

Mathieu Faverge's avatar
Mathieu Faverge committed
553
    }
554 555
    else if (extension=="txt"){
        //    else if (_ui_counter_export_button->isChecked()) {
Mathieu Faverge's avatar
Mathieu Faverge committed
556 557 558 559
        // In order to easily retrieve the good variable, we store them with an index representing the container name followed by the variabletype name (because a variable has no name)
        _all_variables.clear();
        _core->get_trace()->get_all_variables(_all_variables);
        map<string, Variable *>::const_iterator it_end = _all_variables.end();
560
        
Mathieu Faverge's avatar
Mathieu Faverge committed
561 562 563 564 565 566
        _counter_list_names->clear();
        for(map<string, Variable *>::const_iterator it = _all_variables.begin();
            it != it_end ;
            it ++) {
            _counter_list_names->addItem(QString::fromStdString((*it).first));
        }
567
        
Mathieu Faverge's avatar
Mathieu Faverge committed
568 569 570 571
        if(_counter_list_names->count() == 0) {
            error("The trace has no counter");
            return;
        }
572
        
Mathieu Faverge's avatar
Mathieu Faverge committed
573 574
        _ui_counter_choice_to_export->show();
    }
575 576 577 578 579 580 581 582 583 584 585 586 587
    else if (extension=="png" || extension=="jpg" || extension=="jpeg"){
        //    else if (_ui_png_export_button->isChecked()) {
        QImage tempImage = _ui_render_area->grabFrameBuffer(true);/* true = with alpha channel */ // new QImage( pPixelData, width, height, QImage::Format_RGB32);
        
        if ( !tempImage.save( filename, extension.toUpper().toStdString().c_str()) ){
            error("The trace cannot be exported into "+filename.toStdString()+" file.");
        }else{
            information("The trace was exported into "+filename.toStdString()+" file.");
        }
    }else{
        error("Cannot recognize the file extension: "+extension.toStdString()+".");
        
    }
Mathieu Faverge's avatar
Mathieu Faverge committed
588 589 590 591
}

void Interface_graphic::counter_choosed_triggered() {
    // Il faut faire choisir le nom du fichier! et enfin on peut lancer l'action !
Olivier Lagrasse's avatar
Olivier Lagrasse committed
592 593 594 595
  // const QString path_by_default = QString(_trace_path.substr(0, _trace_path.find_last_of('.')).c_str()) + ".txt";
  //QString filename = QFileDialog::getSaveFileName(this, QObject::tr("Export File"),
  //                                                path_by_default,
  //                                                QObject::tr("All files"));
Mathieu Faverge's avatar
Mathieu Faverge committed
596 597

    Variable *temp = _all_variables[_counter_list_names->currentText().toStdString()];
Olivier Lagrasse's avatar
Olivier Lagrasse committed
598 599
    //    _core->export_variable(temp, filename.toStdString());
    _core->export_variable(temp, _export_filename.toStdString());
Mathieu Faverge's avatar
Mathieu Faverge committed
600
}
601

Olivier Lagrasse's avatar
Olivier Lagrasse committed
602
/*void Interface_graphic::on_export_file_triggered(){
603 604 605
    if(_is_rendering_trace == false)
        return;

Mathieu Faverge's avatar
Mathieu Faverge committed
606
    _ui_kind_of_export_choice->show();
Olivier Lagrasse's avatar
Olivier Lagrasse committed
607
    }*/
608

609
void Interface_graphic::option_export_ok_pressed(){
610 611
    // We have to save the option from _ui_time_selection_export and hide her if it is not do automatically

612
    _ui_time_selection_export->hide();
Olivier Lagrasse's avatar
Olivier Lagrasse committed
613 614 615 616
    //   const QString path_by_default = QString(_trace_path.substr(0, _trace_path.find_last_of('.')).c_str()) + ".svg";
    //QString filename = QFileDialog::getSaveFileName(this, tr("Export File"),
    //                                              path_by_default,
    //                                              tr("Images (*.svg)"));
617
    
Olivier Lagrasse's avatar
Olivier Lagrasse committed
618 619
    QString filename = _export_filename;

620
    if (!filename.isEmpty()) {
621
        // Adding .svg to the end
Olivier Lagrasse's avatar
Olivier Lagrasse committed
622
      /*   if(!filename.endsWith(".svg")) {
623
            filename += ".svg";
Olivier Lagrasse's avatar
Olivier Lagrasse committed
624
            }*/
625

626
        information(string("Exporting trace to ")+filename.toStdString());
627 628
                     
        _core->set_path_to_export(filename.toStdString());
629
        // _core->draw_trace(_trace_path, Core::_DRAW_SVG);
630

Olivier Lagrasse's avatar
Olivier Lagrasse committed
631 632 633 634
        //  if(_ui_CheckBox_time_export->isChecked()){
        //   _core->set_min_value_for_export(convert_to_double(_ui_min_time_export->text().toStdString()));
        //    _core->set_max_value_for_export(convert_to_double(_ui_max_time_export->text().toStdString()));
            /* }
635 636
        else{
            _core->set_min_value_for_export(0);
637
            _core->set_max_value_for_export(Info::Entity::x_max);//TO DO TODO max_size de la trace?
Olivier Lagrasse's avatar
Olivier Lagrasse committed
638
            }*/
639

640
         _core->launch_action(Core::_STATE_EXPORT_FILE_IN_INTERVAL);
641
    }
642
    else {
643
        error("No file specified for exportation");
644 645
    }
    
646
}
647

648

649 650
void Interface_graphic::on_close_triggered(){

651 652
    if(_is_rendering_trace == false)
        return;
653

654 655 656 657 658 659
    /*
     * Clear the informative window texts and hide it. 
     */
    _ui_info_trace_text->clear();/* Clear the current text (if exists) */
    _ui_info_selection_text->clear();/* Clear the current text (if exists) */
    _ui_info_window->hide();/* Hide the informative window */
660 661

    //_stats_window->close_window();
662

663
    _core->launch_action(Core::_STATE_RELEASE_RENDER_AREA);
664 665

   
666 667 668
    
    _is_rendering_trace = false; 

669 670


671 672
    information(string("File closed."));

673
   
674

675
    this->setWindowTitle(QString("ViTE"));
676

677 678 679 680
}


void Interface_graphic::on_quit_triggered(){
681
    ((QWidget*)_ui_help_window)->close();
682
    ((QWidget*)this)->close();
683 684 685 686 687
}


void Interface_graphic::on_fullscreen_triggered(){

688 689 690 691 692 693 694 695
    /* 
       Notice that some problems can appears under X systems with
       the window decoration. Please refer to the Qt official documentation.
    */

    /* 
       The menu is checked before function call, so if 'fullscreen menu' is checked, 
       the main window is displayed in fullscreen mode 
696
    */
697
    if (_ui_fullscreen_menu->isChecked())
698
        this->showFullScreen();
699
    else
700
        this->showNormal();
701
}
702

Johnny Jazeix's avatar
Johnny Jazeix committed
703
void Interface_graphic::on_toolbar_menu_triggered(){
704 705
    if (_ui_toolbar_check->isChecked()){
        _ui_toolbar_check->setText("Hide toolbar");
Johnny Jazeix's avatar
Johnny Jazeix committed
706
        _ui_toolbar->show();
707 708 709
    }
    else{
        _ui_toolbar_check->setText("Show toolbar");
Johnny Jazeix's avatar
Johnny Jazeix committed
710
        _ui_toolbar->hide();
711
    }
Johnny Jazeix's avatar
Johnny Jazeix committed
712
}
713 714

void Interface_graphic::on_show_info_triggered(){
715 716 717 718
    if (_ui_info_window->isHidden())
        _ui_info_window->show();
    else
        _ui_info_window->hide();
719 720 721 722 723
}


void Interface_graphic::on_help_triggered(){

724 725
    if(_ui_help_window->isVisible()){
        _ui_help_window->hide();
726 727
    }
    else{
728
        _ui_help_window->show();
729
    }
730 731 732 733 734
}


void Interface_graphic::on_about_triggered(){

735
    QMessageBox::about(this, tr("About ViTE"),
736
                       tr("<h2>ViTE</h2>"
Mathieu Faverge's avatar
Mathieu Faverge committed
737 738 739
                          "the <b>Vi</b><i>sual </i><b>T</b><i>race</i> <b>E</b><i>xplorer</i> - <i>version "
                          VITE_VERSION
                          "</i> - <i>June 2009</i>.<br /><br />"
740
                          "Under the CeCILL A licence. The content can be found <a href=\"http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt\">here</a>."
741
                          "<p><b>Developers:</b><ul><li>Kevin COULOMB</li><li>Mathieu FAVERGE</li><li>Johnny JAZEIX</li><li>Olivier LAGRASSE</li><li>Jule MARCOUEILLE</li><li>Pascal NOISETTE</li><li>Arthur REDONDY</li><li>Cl&eacute;ment VUCHENER</li></ul></p>"
742
                          "The main page project is: <a href=\"http://vite.gforge.inria.fr/\">http://vite.gforge.inria.fr/</a>.<br /><br />"));
743 744
}

745 746
void Interface_graphic::on_show_stats_triggered(){
    if(_core->get_trace() != NULL) {
Mathieu Faverge's avatar
Mathieu Faverge committed
747 748 749 750 751

        if(_stats_window == NULL) { //Creation of the window
            _stats_window = new Stats_window(this);
        }

752
        _stats_window->set_filename(_core->get_filename());
753 754 755 756 757 758 759 760
        _stats_window->set_trace(_core->get_trace());
        _stats_window->init_window();
        _stats_window->show(); 
    }
    else {
        error("Must load a trace before viewing stats");
    }
}
761

762 763 764 765 766 767 768 769 770 771 772 773 774 775
void Interface_graphic::on_actionCommand_triggered(){
    //TODO
    if(_core->get_trace()){
        //        _cmd_window = new Command_window(_core->get_trace(),this);
        _cmd_window->set_trace(_core->get_trace());
        _cmd_window->init_window();
        _cmd_window->show();
    }
    else{
        error("Must load a trace before using an external command on a trace");
    }
}


776 777
void Interface_graphic::on_no_warning_triggered(){
    _no_warning = !_no_warning;
778
}
779

780 781
void Interface_graphic::on_no_arrows_triggered(){
    Info::Render::_no_arrows = !Info::Render::_no_arrows;
782
    _core->launch_action(Core::_STATE_RENDER_UPDATE);
783 784
}

785 786
void Interface_graphic::on_no_events_triggered(){
    Info::Render::_no_events = !Info::Render::_no_events;
787
    _core->launch_action(Core::_STATE_RENDER_UPDATE);
788 789 790
}


791

792
void Interface_graphic::on_zoom_in_triggered(){
793
    Element_pos t = 1;
794 795

    if (true==Info::Render::_key_alt)/* on Y axe */
796
        _core->launch_action(Core::_STATE_RENDER_AREA_CHANGE_SCALE_Y, &t);
797
    else/* on X axe */
798
        _core->launch_action(Core::_STATE_RENDER_AREA_CHANGE_SCALE, &t);
799 800
}

801
void Interface_graphic::on_zoom_out_triggered(){
802
    Element_pos t = -1;
803 804

    if (true==Info::Render::_key_alt)/* on Y axe */
805
        _core->launch_action(Core::_STATE_RENDER_AREA_CHANGE_SCALE_Y, &t);
806
    else/* on X axe */
807
        _core->launch_action(Core::_STATE_RENDER_AREA_CHANGE_SCALE, &t);
808 809 810
}

void Interface_graphic::on_goto_start_triggered(){
811 812 813 814 815 816 817
    int id;

    if (true==Info::Render::_key_alt)/* on Y axe */
        id = Info::Render::Y_TRACE_BEGINNING;
    else/* on X axe */
        id = Info::Render::X_TRACE_BEGINNING;

818
    _core->launch_action(Core::_STATE_RENDER_AREA_REGISTERED_TRANSLATE, &id);
819
}
820

821
void Interface_graphic::on_goto_end_triggered(){
822 823 824 825 826 827 828
    int id;

    if (true==Info::Render::_key_alt)/* on Y axe */
        id = Info::Render::Y_TRACE_ENDING;
    else/* on X axe */
        id = Info::Render::X_TRACE_ENDING;

829
    _core->launch_action(Core::_STATE_RENDER_AREA_REGISTERED_TRANSLATE, &id);
830 831 832
}

void Interface_graphic::on_show_all_trace_triggered(){
833
    int id = Info::Render::X_TRACE_ENTIRE;/* on X axe */
834
    _core->launch_action(Core::_STATE_RENDER_AREA_REGISTERED_TRANSLATE, &id);
835
    id = Info::Render::Y_TRACE_ENTIRE;/* on Y axe */
836
    _core->launch_action(Core::_STATE_RENDER_AREA_REGISTERED_TRANSLATE, &id);
837
}
838

839 840 841 842 843
void Interface_graphic::on_zoom_box_textChanged(QString s){
    QRegExp reg_exp_number("^(\\d+)");
    QString result;
    Element_pos d;

844 845 846 847 848 849 850 851
    if (s=="")/* if there is not value, go out */
       return;

    if (_zoom_box_check_value == s)/* We have just change the zoom box value (in  change_zoom_box_value()) what emits the signal.
                                      So ignore this function call */
        return;
    
   
852 853 854
    if (-1 != reg_exp_number.indexIn(s)){/* match a number */
        result = reg_exp_number.cap(1);/* capture number inside parenthesis in the RegExp */
        d = 0.01*result.toDouble();
Olivier Lagrasse's avatar
Olivier Lagrasse committed
855 856
        if (0 == d)/* if zoom is null ignore it */
            return;
857
        
858
        _core->launch_action(Core::_STATE_RENDER_AREA_REPLACE_SCALE, &d);
859
    }
860

Olivier Lagrasse's avatar
Olivier Lagrasse committed
861 862
}

863
void Interface_graphic::on_x_scroll_valueChanged(int new_value){
864 865
    if (!_on_manual_change){
	Element_pos converted_new_value = real_to_virtual_scroll_unit(new_value, 'x');/* change type of new_value */
866
	_core->launch_action(Core::_STATE_RENDER_AREA_REPLACE_TRANSLATE, &converted_new_value);
867
    }
Olivier Lagrasse's avatar
Olivier Lagrasse committed
868 869
}

870
void Interface_graphic::on_y_scroll_valueChanged(int new_value){
871 872
    if (!_on_manual_change){
	Element_pos converted_new_value = real_to_virtual_scroll_unit(new_value, 'y');/* change type of new_value */
873
	_core->launch_action(Core::_STATE_RENDER_AREA_REPLACE_TRANSLATE_Y, &converted_new_value);
874
    }
875 876
}

Olivier Lagrasse's avatar
Olivier Lagrasse committed
877

Olivier Lagrasse's avatar
Olivier Lagrasse committed
878
void Interface_graphic::on_scale_container_state_valueChanged(int new_value){
879
    _core->launch_action(Core::_STATE_RENDER_AREA_CHANGE_CONTAINER_SCALE, &new_value);
Olivier Lagrasse's avatar
Olivier Lagrasse committed
880 881
}

882

883
void Interface_graphic::closeEvent(QCloseEvent *event){
Olivier Lagrasse's avatar
Olivier Lagrasse committed
884

885
    //   _ui_help_widget->close();
886 887 888 889 890 891 892

    if(isEnabled()){
        event->accept();/* accept to hide the window for a further destruction */
    }
    else{
        event->ignore();
    }
Olivier Lagrasse's avatar
Olivier Lagrasse committed
893
}
894 895 896 897

const std::string Interface_graphic::get_filename() const{
    return _trace_path;
}
898 899


900
Core * Interface_graphic::get_console(){
901 902 903 904
    return _core;
}


905 906 907 908 909 910 911 912 913 914 915 916 917 918
void Interface_graphic::dragEnterEvent(QDragEnterEvent *event) {
    setBackgroundRole(QPalette::Highlight);
        event->acceptProposedAction();
}

void Interface_graphic::dragMoveEvent(QDragMoveEvent *event) {
        event->acceptProposedAction();
}

void Interface_graphic::dragLeaveEvent(QDragLeaveEvent *event) {
    event->accept();

}

919 920 921 922 923 924 925 926
void Interface_graphic::dropEvent(QDropEvent *event) {
    const QMimeData *mimeData = event->mimeData();

    if(!mimeData->hasUrls()) {
        return ;
    }
    
    foreach (QUrl url, event->mimeData()->urls()) {
927 928 929
#ifdef WIN32
        const QString filename = url.toString().right(url.toString().size()-8);
#else
930 931
        // We remove file:// from the filename
        const QString filename = url.toString().right(url.toString().size()-7);
932
#endif
933 934 935 936 937
        open_trace(filename);
    }
}


938 939 940 941 942
void Interface_graphic::open_trace(const QString &filename) {
    if (_is_rendering_trace == true){ /* open a new process */
        
        QStringList arguments = (QStringList() << filename);
        QString program;
943 944 945
        const QString** run_env = _core->get_runenv();
        QDir::setCurrent(*run_env[0]);
        QString run_cmd = *run_env[1];
946
	
947
#ifdef VITE_DEBUG
948
        cout << __FILE__ << " " << __LINE__ << " : " << run_env[0]->toStdString() << " " << run_env[1]->toStdString() << endl;
949
#endif
950

951
        if (run_cmd.startsWith("."))
952
            program = *run_env[0]+(run_cmd.remove(0,1));
953 954 955 956 957 958 959 960 961 962 963 964 965
        else
            program = run_cmd;
        
        QProcess new_process;
        new_process.startDetached(program, arguments);
    }
    else{
        opening_file(filename.toStdString());
        
        if(false==_core->draw_trace(filename.toStdString(), Core::_DRAW_OPENGL))
            error("Draw trace failed");
    }/* end else of if (_is_rendering_trace == true) */   
}