render_area.hpp 16.6 KB
Newer Older
1 2 3 4 5 6 7
/*!
 *\file render_area.hpp
 */

#ifndef RENDER_AREA_HPP
#define RENDER_AREA_HPP

8

9 10
class Render_area;

11 12 13
typedef double Level;


14 15
#include <QtGui>
#include <QGLWidget>/* for the OpenGL Widget */
16
//#include "interface_graphic.hpp"
17

18
#include "render.hpp"
19
//#include "render_svg.hpp"
20
#include "resource.hpp"
21

22

23 24 25 26 27
struct Event_{
    Element_pos time;
    Element_pos height;
    Element_pos container_height;
};
28

Olivier Lagrasse's avatar
Olivier Lagrasse committed
29 30 31 32 33 34 35 36
struct Arrow_{
    Element_pos start_time;
    Element_pos end_time;
    Element_pos start_height;
    Element_pos end_height;
};


37 38

/*!
39
 * \brief This class redefined the OpenGL widget - QGLWidget - to display the trace.
40
 */
41
class Render_area : public QGLWidget, public Render
42 43
 {
     Q_OBJECT
44 45


46

47
 protected:
48 49 50



51 52
     std::list<Element_pos> _text_pos;
     std::list<std::string> _text_value;
53
     std::vector<Event_> _events;
Olivier Lagrasse's avatar
Olivier Lagrasse committed
54
     std::vector<Arrow_> _arrows;
55 56


57 58 59 60 61 62 63 64 65
     /***********************************
      *
      * Environment attributes.
      *
      **********************************/
     
     /*!
      * \brief Refers to the parent widget of the render area.
      */
66
     // Interface_graphic* _parent;
67
     
68

69

70 71 72 73 74 75
     /***********************************
      *
      * Data structure attributes.
      *
      **********************************/
     
76
   
77 78


79 80 81 82 83 84

     /***********************************
      *
      * Render area state attributes.
      *
      **********************************/
85

86
     /*!
87 88 89 90 91 92 93 94
      * \brief State when there is no file opened.
      */
     static const int DRAWING_STATE_WAINTING = 1;
     
     /*!
      * \brief State when the application is drawing traces.
      */
     static const int DRAWING_STATE_DRAWING = 2;
95

96
     /*!
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
      * \brief Contains the kind of state for the render area (drawing, waiting, etc.).
      */
     int _state;
 


     /***********************************
      *
      * Default QGLWidget functions.
      *
      **********************************/
     
     /*!
      * \brief Call by the system to initialize the OpenGL render area.
      */
     void initializeGL();
     
     /*!
      * \brief Call by the system when the render area was resized (occurs during a window resizement).
      * \arg width : the new width of the render area.
      *      height : the new height of the render area. 
      */
     void resizeGL(int width, int height);
     
     /*!
      * \brief Call by the system each time the render area need to be updated.
123 124
      */
     void paintGL();
125 126 127 128 129 130 131 132 133 134 135 136

     /***********************************
      *
      * The wait screen drawing.
      *
      **********************************/

     
     /***********************************
      * The wait list Attributes.
      **********************************/
     
137
     /*!
138
     * \brief The wait GLu list.
139
     */
140
     GLuint _wait_list;
141 142
     GLuint _list_containers;
     GLuint _list_states;
143
     GLuint _list_counters;
144

145 146 147 148 149 150
     /*!
     * \brief Rotation angle for the wait.
     */
     float _wait_angle;

     /*!
151 152 153 154
      * \brief Time in ms between two frames for the waiting screen.
      */
     static const int DRAWING_TIMER_DEFAULT = 10;
     
155
     /*!
156 157
      * \brief Wait animation seconds per frame.
      */
158
     int _wait_spf;
159 160 161 162 163
     
     /*!
      * \brief Timer to animate the wait. 
      */
     QTimer* _wait_timer;
164

165

166 167 168 169
     /***********************************
      * Drawing function for the wait screen.
      **********************************/
     
170
     /*!
171 172
     * \brief Display a wait on the screen if there is no file opened.
     * \return Asset value of the wait.
173
     */
174 175 176 177
     GLuint draw_wait();



178

179 180 181 182 183 184 185 186 187 188 189
     /***********************************
      *
      * The trace drawing.
      *
      **********************************/


     /***********************************
      * The drawing list Attributes.
      **********************************/
     
190
     /*!
191
      * \brief The trace Glu list.
192
     */
193
     GLuint _drawing_list;
194

Olivier Lagrasse's avatar
Olivier Lagrasse committed
195
 
Olivier Lagrasse's avatar
Olivier Lagrasse committed
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
  

     Element_pos _counter_last_x;
     Element_pos _counter_last_y;


     /*!
      * \brief The opengl render area width in pixels.
      */
     Element_pos _screen_width;

     /*!
      * \brief The opengl render area height in pixels.
      */
     Element_pos _screen_height;


     /*!
      * \brief The opengl visibled scene width in the OpenGL units.
      */
     Element_pos _render_width;

     /*!
      * \brief The opengl visibled scene height in the OpenGL units.
      */
     Element_pos _render_height;



225 226
     Element_pos _container_x_max;
     Element_pos _container_y_max;
Olivier Lagrasse's avatar
Olivier Lagrasse committed
227

228 229 230
     Element_pos _event_x_max;
     Element_pos _event_y_max;

Olivier Lagrasse's avatar
Olivier Lagrasse committed
231 232 233 234 235 236
     Element_pos _state_x_max;
     Element_pos _state_y_max;

     Element_pos _x_scale_container_state;


237 238
     Element_pos _z_container;/* z position for containers */
     Element_pos _z_state;/* z position for states */
Olivier Lagrasse's avatar
Olivier Lagrasse committed
239
     Element_pos _z_event;/* z position for events */
240
     Element_pos _z_arrow;/* z position for arrows */
241 242 243 244

     /***********************************
      * Trace Drawing functions and attributes.
      **********************************/
245 246 247 248
 
     /*!
     * \brief Return the pos container of the list
     */
249
     // Container* get_container(const std::list <Container*> *list_container, const int pos) const;
250 251


Olivier Lagrasse's avatar
Olivier Lagrasse committed
252 253
     int _state_scale;/* temporary */
     int _state_translate;/* temporary */
Olivier Lagrasse's avatar
Olivier Lagrasse committed
254
     // int _container_view_size;/* temporary */
255

256
 public:
257

258 259 260 261 262 263 264 265


     /***********************************
      *
      * Constructor and destructor.
      *
      **********************************/

266 267 268
     /*!
     * \brief The default constructor
     */
269
     Render_area(QWidget *parent);
270 271 272 273 274

     /*!
     * \brief The destructor
     */
     virtual ~Render_area();
275

276 277 278 279 280 281 282 283


     /***********************************
      *
      * Building functions.
      *
      **********************************/

284
     /*!
285
      * \brief This function draws the trace.
286
      * \arg trace the trace which be displayed. 
287
      */
288
     bool build();
289 290

     /*!
291 292
      * \brief This function releases the trace.
      */
293 294
     bool unbuild();

Olivier Lagrasse's avatar
Olivier Lagrasse committed
295 296
     void change_scale(int scale);/* temporary -> to change the scale to view states */
     void change_translate(int translate);/* temporary -> to change the translate to view states */
Olivier Lagrasse's avatar
Olivier Lagrasse committed
297 298
     void change_scale_container_state(int view_size);/* temporary -> to change the size of container view */

299 300
     

301
     void start_draw();
302

303
     void start_draw_containers();
304 305 306 307 308 309 310 311

     /*!
     * \brief Draw a container according to the parameters
     * \param x the x position of the container
     * \param y the y position of the container
     * \param w the width of the container
     * \param h the height of the container
     */
312
     void draw_container(const Element_pos x, const Element_pos y, const Element_pos w, const Element_pos h);
313 314 315 316 317 318 319

     /*!
      * \brief Draw the text of a container.
      * \param x the x position of the text.
      * \param y the y position of the text.
      * \param value the string value of the text.
      */
320
     void draw_container_text(const Element_pos x, const Element_pos y, const std::string value);
321

322
     void end_draw_containers();
Olivier Lagrasse's avatar
Olivier Lagrasse committed
323
     
324
     void start_draw_states();
325 326 327 328 329 330 331 332 333 334 335

     /*!
      * \brief Draw a state of the trace.
      * \param start the beginning time of the state.
      * \param end the ending time of the state.
      * \param base vertical position of the state.
      * \param height the state height.
      * \param r the red color rate of the state.
      * \param g the green color rate of the state.
      * \param b the blue color rate of the state.
      */
336 337 338
     void draw_state(const Element_pos start , const Element_pos end, const Element_pos base, const Element_pos height, const Element_col r, const Element_col g, const Element_col b);

     void end_draw_states();
339

340
     void draw_arrow(const Element_pos start_time, const Element_pos end_time, const Element_pos start_height, const Element_pos end_height);
341

Olivier Lagrasse's avatar
Olivier Lagrasse committed
342 343
     void draw_stored_arrows(std::vector<Arrow_> &arrows);

344 345
     void draw_event(const Element_pos time, const Element_pos height,  const Element_pos container_height);

346 347
     void draw_stored_events(std::vector<Event_> &events);

348 349
     void start_draw_counter();
     
Olivier Lagrasse's avatar
Olivier Lagrasse committed
350 351 352
     void draw_counter(const Element_pos x, const Element_pos y);
     
     void end_draw_counter();
353

354
     void end_draw();
355 356


357
    
358 359
  };

360

361 362


363 364 365
/* inline function area */


366 367 368 369 370 371 372 373 374 375
/***********************************
 *
 *
 *
 * Drawing function for the trace.
 *
 *
 *
 **********************************/

376 377 378 379 380

inline void Render_area::start_draw(){
    /* clear lists to store container texts */
    _text_pos.clear();
    _text_value.clear();
Olivier Lagrasse's avatar
Olivier Lagrasse committed
381 382 383 384 385 386

    /* clear the event vector */
    _events.clear();

    /* clear the arrow vector */
    _arrows.clear();
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
}


inline void Render_area::start_draw_containers(){

    _list_containers = glGenLists(1);/* create the list */
    if (_list_containers==0){
        
        //        _parent->warning("Error when creating list");
    }

    glNewList(_list_containers, GL_COMPILE);/* open the list */

}


inline void Render_area::draw_container(const Element_pos x, const Element_pos y, const Element_pos w, const Element_pos h) {

    float j=0.6;

    glBegin(GL_QUADS);/* create a quads */
    {
        glColor3d(0, 0, j);glVertex2d(x, y);
        glColor3d(0, 0, j-0.1);glVertex2d(x, y+h);
        glColor3d(0, 0, j-0.1);glVertex2d(x+w, y+h);
        glColor3d(0, 0, j);glVertex2d(x+w, y);
    }
    glEnd();

416 417
    if ((x+w)>_container_x_max)
        _container_x_max = x+w;
418
    
419 420
    if ((y+h)>_container_y_max)
        _container_y_max = y+h;
421 422 423 424 425 426 427 428 429 430 431 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

}

inline void Render_area::draw_container_text(const Element_pos x, const Element_pos y, const std::string value){

    _text_pos.push_back(x); 
    _text_pos.push_back(y);
    _text_value.push_back(value);

}



inline void Render_area::end_draw_containers(){
    glEndList();/* close the list */
}




inline void Render_area::start_draw_states(){

    _list_states = glGenLists(1);/* create the list */
    if (_list_states==0){
        
        //      _parent->warning("Error when creating list");
    }
    
    glNewList(_list_states, GL_COMPILE);/* open the list */
}


inline void Render_area::draw_state(const Element_pos start, const Element_pos end, const Element_pos base, const Element_pos height, const Element_col r, const Element_col g, const Element_col b){

    

#ifdef DEBUG_MODE_RENDER_AREA
    
459 460
    std::cerr << __FILE__ << " l." << __LINE__ << ":" << std::endl;  
    std::cerr << "States position (for drawing): (x = " <<  start << ", y = " << base << ", w = " << end-start << ", h = " << height << ")" << std::endl << std::endl;  
461 462 463 464 465 466 467 468

#endif

  
    glBegin(GL_QUADS);/* create a quads */
    {
        glColor3d(r, g, b);glVertex2d(start, base);
        glColor3d(r, g, b);glVertex2d(start, base + height);
469 470
        glColor3d(r/1.5, g/1.5, b/1.5);glVertex2d(end, base + height);
        glColor3d(r/1.5, g/1.5, b/1.5);glVertex2d(end, base);
471 472
    }
    glEnd();
Olivier Lagrasse's avatar
Olivier Lagrasse committed
473 474 475 476 477 478

    if (end>_state_x_max)
        _state_x_max = end;
    
    if ((base+height)>_state_y_max)
        _state_y_max = base+height;
479 480 481 482 483 484 485 486 487 488
}


inline void Render_area::end_draw_states(){
    glEndList();/* close the list */
}


inline void Render_area::draw_arrow(const Element_pos start_time, const Element_pos end_time, const Element_pos start_height, const Element_pos end_height){

Olivier Lagrasse's avatar
Olivier Lagrasse committed
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
    Arrow_ buf;

    buf.start_time = start_time;
    buf.end_time = end_time;
    buf.start_height = start_height;
    buf.end_height = end_height;

    _arrows.push_back(buf);
}



inline void Render_area::draw_stored_arrows(std::vector<Arrow_> &arrows){

    Element_pos start_time, end_time, start_height, end_height;

    /* Manage the event drawing size from state size and render area dimensions */
    Element_pos arrow_scale_x = _state_scale*(_render_width/_state_x_max);
    Element_pos arrow_scale_y = _render_height/_state_y_max;

509 510
    Element_pos angle;

511

Olivier Lagrasse's avatar
Olivier Lagrasse committed
512 513 514 515 516 517 518
    for (long i=0 ; i<(long)arrows.size() ; i++){
        
        
        start_time = arrows[i].start_time*arrow_scale_x + _render_width*_x_scale_container_state-_state_translate; 
        end_time = arrows[i].end_time*arrow_scale_x + _render_width*_x_scale_container_state-_state_translate; 
        start_height = arrows[i].start_height*arrow_scale_y;
        end_height = arrows[i].end_height*arrow_scale_y;
519

Olivier Lagrasse's avatar
Olivier Lagrasse committed
520 521 522 523 524
        /* DEBUG */
        // std::cerr << "Arrow draw: (" << start_time << ", " << start_height << ") to (" << end_time << ", " << end_height << ")" << std::endl;


        glPushMatrix();
525
    
Olivier Lagrasse's avatar
Olivier Lagrasse committed
526 527
        glTranslated(end_time, end_height, _z_arrow);
        glScalef(2, 2, 0);/* should be set */
528
    
529

Olivier Lagrasse's avatar
Olivier Lagrasse committed
530
        if (start_time != end_time){
531

Olivier Lagrasse's avatar
Olivier Lagrasse committed
532
            angle = atan2((end_height - start_height), (end_time - start_time))*180.0f/PI;/* arc tangent */
533

Olivier Lagrasse's avatar
Olivier Lagrasse committed
534
            glRotatef(angle, 0, 0, 1);
535
 
Olivier Lagrasse's avatar
Olivier Lagrasse committed
536 537
        }/* end if (start_time != end_time) */
        else
Olivier Lagrasse's avatar
Olivier Lagrasse committed
538
            glRotatef(90, 0, 0, 1);/* vertical alignment */
539
   
540
    
541

Olivier Lagrasse's avatar
Olivier Lagrasse committed
542 543
        glBegin(GL_TRIANGLES);/* create an arrow */
        {
Olivier Lagrasse's avatar
Olivier Lagrasse committed
544 545 546
            glColor3d(1.0, 1.0, 1.0);glVertex2d(0.0, 0.0);
            glColor3d(0.9, 0.9, 0.9);glVertex2d(-0.6, -0.2);
            glColor3d(0.9, 0.9, 0.9);glVertex2d(-0.6, 0.2);
Olivier Lagrasse's avatar
Olivier Lagrasse committed
547 548
        }
        glEnd();
549
    
Olivier Lagrasse's avatar
Olivier Lagrasse committed
550
        glPopMatrix();
551

Olivier Lagrasse's avatar
Olivier Lagrasse committed
552
        glPushMatrix();
553

Olivier Lagrasse's avatar
Olivier Lagrasse committed
554
        glTranslated(0, 0, _z_arrow);
Olivier Lagrasse's avatar
Olivier Lagrasse committed
555
        // glLineWidth(1.5f);
Olivier Lagrasse's avatar
Olivier Lagrasse committed
556 557
        glBegin(GL_LINES);
        {
Olivier Lagrasse's avatar
Olivier Lagrasse committed
558 559
            glColor3d(0.8, 0.8, 0.8);glVertex2d(start_time, start_height);
            glColor3d(1.0, 1.0, 1.0);glVertex2d(end_time, end_height);
560
        }
Olivier Lagrasse's avatar
Olivier Lagrasse committed
561
        glEnd();
Olivier Lagrasse's avatar
Olivier Lagrasse committed
562
        // glLineWidth(1.0f);/* 1 is the default value */
563

Olivier Lagrasse's avatar
Olivier Lagrasse committed
564 565
        glPopMatrix();
    }
566 567 568
}


569 570
inline void Render_area::draw_event(const Element_pos time, const Element_pos height,  const Element_pos container_height){

571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
    Event_ buf;

    buf.time = time;
    buf.height = height;
    buf.container_height = container_height;

    _events.push_back(buf);

    if (time>_event_x_max)
        _event_x_max = time;
    
    if ((height+container_height)>_event_y_max)
        _event_y_max = (height+container_height);    
}


inline void Render_area::draw_stored_events(std::vector<Event_> &events){

    Element_pos time, height, container_height;

    /* Manage the event drawing size from state size and render area dimensions */
    Element_pos event_scale_x = _state_scale*(_render_width/_state_x_max);
    Element_pos event_scale_y = _render_height/_state_y_max;

595 596 597 598 599 600
    Element_pos radius;/* the circle radius */
    Element_pos angle;
    Element_pos delta_angle;
    int step;


601
    for (long i=0 ; i<(long)events.size() ; i++){
602

603 604 605
        time = events[i].time*event_scale_x + _render_width*_x_scale_container_state-_state_translate; 
        height = events[i].height*event_scale_y;
        container_height = events[i].container_height*event_scale_y;
606 607


608 609 610
        /* DEBUG */
        //  std::cerr << "Event draw: (" << time << ", " << height << ") with height of:" << container_height << " - " << _render_width << " : " << _render_height << std::endl;
    
611

612 613 614 615 616
        /* IMPROVEMENT: put the circle into a display list */
        


        /* draw a circle */
617
        radius = 1.0f;
618 619 620 621 622 623 624 625 626 627 628
        angle = PI/2.0f;
        step = 20;/* 20 polygons for the circle */
        
        if (step!=0)
            delta_angle = 2*PI/step;
        
        
        glColor3d(0.5, 0.8, 0.5);
        glBegin(GL_POLYGON);
        {
            for(int i =0 ; i<step ; i++){
Olivier Lagrasse's avatar
Olivier Lagrasse committed
629
                glVertex3d(time + cos(angle+delta_angle*i)*radius , height + sin(angle+delta_angle*i)*radius , _z_event);
630
            }
631
        }
632 633 634
        glEnd();
        
        /* draw line */
Olivier Lagrasse's avatar
Olivier Lagrasse committed
635
        // glLineWidth(1.5f);
636

637 638
        glBegin(GL_LINES);
        {
Olivier Lagrasse's avatar
Olivier Lagrasse committed
639 640
            glColor3d(0.5, 0.8, 0.5);glVertex3d(time, height, _z_event);
            glColor3d(0.4, 0.7, 0.4);glVertex3d(time, height+container_height, _z_event);
641 642
        }
        glEnd();
Olivier Lagrasse's avatar
Olivier Lagrasse committed
643
        // glLineWidth(1.0f);
644

645
    }/* and loop */
646 647 648
}


649 650 651 652 653 654 655 656
/********************
 * Counter
 *******************/

inline void Render_area::start_draw_counter(){

    _list_counters = glGenLists(1);/* create the list */
    if (_list_counters==0){
Olivier Lagrasse's avatar
Olivier Lagrasse committed
657
        //message_ns::message << "Error when creating list" << message_ns::ende;
658 659 660 661
    }
    
    glNewList(_list_counters, GL_COMPILE);/* open the list */

Olivier Lagrasse's avatar
Olivier Lagrasse committed
662
   
663 664 665 666
}


inline void Render_area::draw_counter(const Element_pos x, const Element_pos y){
Olivier Lagrasse's avatar
Olivier Lagrasse committed
667
    
Olivier Lagrasse's avatar
Olivier Lagrasse committed
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686
    static bool line_already_begun = false;

    if (x <= 0.0f ){
        
        if (line_already_begun==true){/* it is not the first call */
            glEnd();
        }else
            line_already_begun = true;

        glBegin(GL_LINE_STRIP);/* set of connected verteces */
        glColor3d(1.0, 1.0, 1.0);glVertex2d(0.0f, y);/* draw the init point */

        _counter_last_x = 0.0f;

    }else{

        glColor3d(1.0, 1.0, 1.0);glVertex2d(x, y);

        _counter_last_x = x;
Olivier Lagrasse's avatar
Olivier Lagrasse committed
687
    }
Olivier Lagrasse's avatar
Olivier Lagrasse committed
688

Olivier Lagrasse's avatar
Olivier Lagrasse committed
689
    _counter_last_y = y;
690 691 692 693
}


inline void Render_area::end_draw_counter(){
Olivier Lagrasse's avatar
Olivier Lagrasse committed
694
   
Olivier Lagrasse's avatar
Olivier Lagrasse committed
695 696
    glEnd();/* suppose that line is not ended */

697 698 699 700
    glEndList();/* close the list */
}


701

702 703 704 705 706
inline void Render_area::end_draw(){

}


707
 #endif