render_area.hpp 15.2 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 29 30


/*!
31
 * \brief This class redefined the OpenGL widget - QGLWidget - to display the trace.
32
 */
33
class Render_area : public QGLWidget, public Render
34 35
 {
     Q_OBJECT
36 37


38

39
 protected:
40 41 42



43 44
     std::list<Element_pos> _text_pos;
     std::list<std::string> _text_value;
45
     std::vector<Event_> _events;
46 47


48 49 50 51 52 53 54 55 56
     /***********************************
      *
      * Environment attributes.
      *
      **********************************/
     
     /*!
      * \brief Refers to the parent widget of the render area.
      */
57
     // Interface_graphic* _parent;
58
     
59

60

61 62 63 64 65 66
     /***********************************
      *
      * Data structure attributes.
      *
      **********************************/
     
67
   
68 69


70 71 72 73 74 75

     /***********************************
      *
      * Render area state attributes.
      *
      **********************************/
76

77
     /*!
78 79 80 81 82 83 84 85
      * \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;
86

87
     /*!
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
      * \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.
114 115
      */
     void paintGL();
116 117 118 119 120 121 122 123 124 125 126 127

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

     
     /***********************************
      * The wait list Attributes.
      **********************************/
     
128
     /*!
129
     * \brief The wait GLu list.
130
     */
131
     GLuint _wait_list;
132 133
     GLuint _list_containers;
     GLuint _list_states;
134
     GLuint _list_counters;
135

136 137 138 139 140 141
     /*!
     * \brief Rotation angle for the wait.
     */
     float _wait_angle;

     /*!
142 143 144 145
      * \brief Time in ms between two frames for the waiting screen.
      */
     static const int DRAWING_TIMER_DEFAULT = 10;
     
146
     /*!
147 148
      * \brief Wait animation seconds per frame.
      */
149
     int _wait_spf;
150 151 152 153 154
     
     /*!
      * \brief Timer to animate the wait. 
      */
     QTimer* _wait_timer;
155

156

157 158 159 160
     /***********************************
      * Drawing function for the wait screen.
      **********************************/
     
161
     /*!
162 163
     * \brief Display a wait on the screen if there is no file opened.
     * \return Asset value of the wait.
164
     */
165 166 167 168
     GLuint draw_wait();



169

170 171 172 173 174 175 176 177 178 179 180
     /***********************************
      *
      * The trace drawing.
      *
      **********************************/


     /***********************************
      * The drawing list Attributes.
      **********************************/
     
181
     /*!
182
      * \brief The trace Glu list.
183
     */
184
     GLuint _drawing_list;
185

Olivier Lagrasse's avatar
Olivier Lagrasse committed
186
 
Olivier Lagrasse's avatar
Olivier Lagrasse committed
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
  

     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;



216 217
     Element_pos _container_x_max;
     Element_pos _container_y_max;
Olivier Lagrasse's avatar
Olivier Lagrasse committed
218

219 220 221
     Element_pos _event_x_max;
     Element_pos _event_y_max;

Olivier Lagrasse's avatar
Olivier Lagrasse committed
222 223 224 225 226 227
     Element_pos _state_x_max;
     Element_pos _state_y_max;

     Element_pos _x_scale_container_state;


228 229
     Element_pos _z_container;/* z position for containers */
     Element_pos _z_state;/* z position for states */
230
     Element_pos _z_arrow;/* z position for arrows */
231 232 233 234

     /***********************************
      * Trace Drawing functions and attributes.
      **********************************/
235 236 237 238
 
     /*!
     * \brief Return the pos container of the list
     */
239
     // Container* get_container(const std::list <Container*> *list_container, const int pos) const;
240 241


Olivier Lagrasse's avatar
Olivier Lagrasse committed
242 243
     int _state_scale;/* temporary */
     int _state_translate;/* temporary */
Olivier Lagrasse's avatar
Olivier Lagrasse committed
244
     // int _container_view_size;/* temporary */
245

246
 public:
247

248 249 250 251 252 253 254 255


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

256 257 258
     /*!
     * \brief The default constructor
     */
259
     Render_area(QWidget *parent);
260 261 262 263 264

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

266 267 268 269 270 271 272 273


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

274
     /*!
275
      * \brief This function draws the trace.
276
      * \arg trace the trace which be displayed. 
277
      */
278
     bool build();
279 280

     /*!
281 282
      * \brief This function releases the trace.
      */
283 284
     bool unbuild();

Olivier Lagrasse's avatar
Olivier Lagrasse committed
285 286
     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
287 288
     void change_scale_container_state(int view_size);/* temporary -> to change the size of container view */

289 290
     

291
     void start_draw();
292

293
     void start_draw_containers();
294 295 296 297 298 299 300 301

     /*!
     * \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
     */
302
     void draw_container(const Element_pos x, const Element_pos y, const Element_pos w, const Element_pos h);
303 304 305 306 307 308 309

     /*!
      * \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.
      */
310
     void draw_container_text(const Element_pos x, const Element_pos y, const std::string value);
311

312
     void end_draw_containers();
Olivier Lagrasse's avatar
Olivier Lagrasse committed
313
     
314
     void start_draw_states();
315 316 317 318 319 320 321 322 323 324 325

     /*!
      * \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.
      */
326 327 328
     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();
329

330
     void draw_arrow(const Element_pos start_time, const Element_pos end_time, const Element_pos start_height, const Element_pos end_height);
331

332 333
     void draw_event(const Element_pos time, const Element_pos height,  const Element_pos container_height);

334 335
     void draw_stored_events(std::vector<Event_> &events);

336 337
     void start_draw_counter();

338
   void draw_counter(const Element_pos x, const Element_pos y);
339
     
340
   void end_draw_counter();
341

342
     void end_draw();
343 344


345
    
346 347
  };

348

349 350


351 352 353
/* inline function area */


354 355 356 357 358 359 360 361 362 363
/***********************************
 *
 *
 *
 * Drawing function for the trace.
 *
 *
 *
 **********************************/

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 389 390 391 392 393 394 395 396 397

inline void Render_area::start_draw(){
    /* clear lists to store container texts */
    _text_pos.clear();
    _text_value.clear();
}


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();

398 399
    if ((x+w)>_container_x_max)
        _container_x_max = x+w;
400
    
401 402
    if ((y+h)>_container_y_max)
        _container_y_max = y+h;
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440

}

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
    
441 442
    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;  
443 444 445 446 447 448 449 450

#endif

  
    glBegin(GL_QUADS);/* create a quads */
    {
        glColor3d(r, g, b);glVertex2d(start, base);
        glColor3d(r, g, b);glVertex2d(start, base + height);
451 452
        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);
453 454
    }
    glEnd();
Olivier Lagrasse's avatar
Olivier Lagrasse committed
455 456 457 458 459 460

    if (end>_state_x_max)
        _state_x_max = end;
    
    if ((base+height)>_state_y_max)
        _state_y_max = base+height;
461 462 463 464 465 466 467 468 469 470 471 472
}


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){

    Element_pos angle;

473 474 475
    /* DEBUG */
    // std::cerr << "Arrow draw: (" << start_time << ", " << start_height << ") to (" << end_time << ", " << end_height << ")" << std::endl;

476 477

    glPushMatrix();
478 479 480 481
    
    glTranslated(end_time, end_height, _z_arrow);
    glScalef(2, 2, 0);/* should be set */
    
482 483 484 485 486 487 488 489

    if (start_time != end_time){

        angle = atan2((end_height - start_height), (end_time - start_time))*180.0f/PI;/* arc tangent */

        glRotatef(angle,0, 0, 1);
 
    }/* end if (start_time != end_time) */
490 491 492
    else
        glRotatef(90,0, 0, 1);/* vertical alignment */
   
493
    
494

495 496
    // glBegin(GL_TRIANGLES);/* create an arrow */
    /* {
497 498 499
        glColor3d(1.0, 0.7, 0.7);glVertex2d(0.5, 0.0);
        glColor3d(0.9, 0.6, 0.6);glVertex2d(-0.5, -0.5);
        glColor3d(0.9, 0.6, 0.6);glVertex2d(-0.5, 0.5);
500
    }
501
    glEnd();*/
502 503 504
    
    glPopMatrix();

505
    glPushMatrix();
506

507 508
    glTranslated(0, 0, _z_arrow);
    glLineWidth(2.5f);
509
    glBegin(GL_LINES);
510
    {
511 512
        glColor3d(1.0, 0.7, 0.7);glVertex3d(start_time, start_height, _z_arrow);
        glColor3d(0.9, 0.6, 0.6);glVertex3d(end_time, end_height, _z_arrow);
513
        }
514
    glEnd();
515 516 517
    glLineWidth(1.0f);/* 1 is the default value */

    glPopMatrix();
518 519 520 521

}


522 523 524

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

525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548
    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;

549 550 551 552 553 554
    Element_pos radius;/* the circle radius */
    Element_pos angle;
    Element_pos delta_angle;
    int step;


555
    for (long i=0 ; i<(long)events.size() ; i++){
556

557 558 559
        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;
560 561


562 563 564
        /* DEBUG */
        //  std::cerr << "Event draw: (" << time << ", " << height << ") with height of:" << container_height << " - " << _render_width << " : " << _render_height << std::endl;
    
565

566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
        /* IMPROVEMENT: put the circle into a display list */
        


        /* draw a circle */
        radius = 2.0f;
        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++){
                glVertex3d(time + cos(angle+delta_angle*i)*radius , height + sin(angle+delta_angle*i)*radius , _z_arrow);
            }
585
        }
586 587 588 589
        glEnd();
        
        /* draw line */
        glLineWidth(2.5f);
590

591 592 593 594 595 596 597
        glBegin(GL_LINES);
        {
            glColor3d(0.5, 0.8, 0.5);glVertex3d(time, height, _z_arrow);
            glColor3d(0.4, 0.7, 0.4);glVertex3d(time, height+container_height, _z_arrow);
        }
        glEnd();
        glLineWidth(1.0f);
598

599
    }/* and loop */
600 601 602
}


603 604 605 606 607 608 609 610 611 612 613 614 615 616
/********************
 * Counter
 *******************/

inline void Render_area::start_draw_counter(){

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

Olivier Lagrasse's avatar
Olivier Lagrasse committed
617
   
618 619 620 621
}


inline void Render_area::draw_counter(const Element_pos x, const Element_pos y){
Olivier Lagrasse's avatar
Olivier Lagrasse committed
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
    
    static bool which_color = true;
    float a;

    if (which_color==true){/* yellow color */
        a = 0.0f;
        which_color = false;
    }else{/* white color */
        a = 1.0f;
        which_color = true;
    }
    
    glLineWidth(2.5f);
    glBegin(GL_QUADS);  
    {
        glColor3d(1.0, 1.0, a);glVertex2d(_counter_last_x, 0);
        glColor3d(1.0, 1.0, a);glVertex2d(_counter_last_x, _counter_last_y);
        glColor3d(1.0, 1.0, a);glVertex2d(x, y);
        glColor3d(1.0, 1.0, a);glVertex2d(x, 0);
    }
    glEnd();
    glLineWidth(1.0f);
    _counter_last_x = x;
    _counter_last_y = y;
646 647 648 649
}


inline void Render_area::end_draw_counter(){
Olivier Lagrasse's avatar
Olivier Lagrasse committed
650
   
651 652 653 654
    glEndList();/* close the list */
}


655

656 657 658 659 660
inline void Render_area::end_draw(){

}


661
 #endif