DrawTrace.hpp 11.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
/*!
 *\file DrawTrace.hpp
 */

#ifndef DRAW_TRACE_HPP
#define DRAW_TRACE_HPP

#include "resource.hpp"

10
#include "../interface/render_area.hpp"
11
12
13
14
15
16
17
18
19
20
21
22
23



/*!
 * \brief 
 */
template<class T>
class DrawTrace 
{

protected:

    //  Interface_graphic* _window;
24
    std::stack<const list<State *> *> _stack_states;
25
26
   
    /*!
27
28
29
     * \brief The default height for containers.
     */
    static const float _DRAWING_CONTAINER_HEIGHT_DEFAULT = 1.2f;
30
     
31
32
33
34
    /*!
     * \brief The default width for containers.
     */
    static const float _DRAWING_CONTAINER_WIDTH_DEFAULT = 2.5f;
35

36
37
38
39
    /*!
     * \brief The default horizontal space between containers.
     */
    static const float _DRAWING_CONTAINER_H_SPACE_DEFAULT = 0.1f;
40

41
42
43
44
    /*!
     * \brief The default vertical space between containers.
     */
    static const float _DRAWING_CONTAINER_V_SPACE_DEFAULT = 0.2f;
45
46
47


    /*!
48
49
50
     * \brief The default height for states.
     */
    static const float _DRAWING_STATE_HEIGHT_DEFAULT = 1.2f;
51
 
52
53
54
55
    /*!
     * \brief The default vertical space between states.
     */
    static const float _DRAWING_STATE_V_SPACE_DEFAULT = 0.2f;
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84


    /*!
     * \brief Geometrical informations about the trace shape.
     */
    Element_pos _container_width;
    Element_pos _container_height;
    Element_pos _container_h_space;
    Element_pos _container_v_space;
    
    Element_pos _state_height;
    Element_pos _state_v_space;
 

public:

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

    /*!
     * \brief The default constructor
     */
85
    DrawTrace() {
86
87

        //   _window = NULL;
Olivier Lagrasse's avatar
Olivier Lagrasse committed
88
89
90
91
        _container_width = _DRAWING_CONTAINER_WIDTH_DEFAULT;
        _container_height = _DRAWING_CONTAINER_HEIGHT_DEFAULT;
        _container_h_space = _DRAWING_CONTAINER_H_SPACE_DEFAULT;
        _container_v_space = _DRAWING_CONTAINER_V_SPACE_DEFAULT;
92
        
Olivier Lagrasse's avatar
Olivier Lagrasse committed
93
94
        _state_height = _DRAWING_STATE_HEIGHT_DEFAULT;
        _state_v_space = _DRAWING_STATE_V_SPACE_DEFAULT;
95
96
97
98
99
100
101

    }


    /*!
     * \brief The destructor
     */
102
103
    virtual ~DrawTrace() {
    
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
    }



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


    /*!
     * \brief The trace building function.
     * \param draw_object the kind of object which will be drawn (OpenGL, SVG...).
122
     * \param trace the trace data.
123
     */
124
    void build(T* draw_object, Trace* trace) {
125
126
127
128

        draw_object->start_draw();

        draw_object->start_draw_containers();
129
        browse_container_tree(draw_object, trace);
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
        draw_object->end_draw_containers();

        draw_object->start_draw_states();
        browse_state_tree(draw_object);
        draw_object->end_draw_states();

        draw_object->end_draw();
    }
     



    //protected:




    /***********************************
     *
     *
     *
     * Browsing functions.
     *
     *
     *
     **********************************/
    
157
    inline void browse_container_tree(T* draw_object, Trace* trace){
158
159
160
161
162
163
164
165
166
         
        /*** Drawing containers ***/
        
        /*
         * Start with the root containers.
         * if a container has more than one child, it will be draw
         * vertically, else horizontally. 
         */
         
Olivier Lagrasse's avatar
Olivier Lagrasse committed
167
168
        std::stack<Element_count> position_stack;
        std::stack<Element_pos> height_stack;
169
170
171
172
173
        Element_pos x;
        Element_pos y;
        Element_pos y_buf;/* buffer for the y position */
        Element_pos height_buf;/* buffer for the container height */
        const Container* container;
174
        const std::list<Container *> *trace_container = trace->get_root_containers();
175
         
Olivier Lagrasse's avatar
Olivier Lagrasse committed
176
        std::list<Container *>::iterator it;
177
178
179
180
181
182
183
184
185
186
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
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
         
        bool isAnotherContainer = true;/* use to stop the loop */

 

        position_stack.push(0);
        height_stack.push(0);

        container = get_container(trace_container, 0);

        if (number_of_children(container) <= 1)
            x = _container_width;
        else
            x = _container_height;
        y = 0;
        y_buf = 0;
        height_buf = 0;


        /***********************************
         *
         * Start loop.
         *
         **********************************/
        while (isAnotherContainer == true){
          
            
            Element_pos buf_pos;

            buf_pos = return_last_visited_child(position_stack);
           


            /* If all children are not yet visited */
            if (buf_pos < number_of_children(container)){

                /***********************************
                 *
                 * Browse children.
                 *
                 **********************************/
                
                position_stack.top() ++;/* add a child */
                position_stack.push(0);/* prepare for the next child */
                
                container = get_container(container->get_children(), buf_pos);
                 
                x += _container_width+_container_h_space;/* if there is just one child, container can be draw horizontally */ 
                
                                   
            }else{/* all children have been visited or there is not child, thus it's time to draw ! */
                 
                position_stack.pop();/* remove the information of the container hence it will be drawn */
                     
                if (1 >= number_of_children(container)){/* Should be drawn horizontally */
                     
                    /***********************************
                     *
                     * Draw container with one or less child.
                     *
                     **********************************/

                    height_buf = 0;

                    if (0 == number_of_children(container)){
                        /* y contains the current height, y_buf the new adding height */
                        y_buf = _container_height+_container_v_space;
                        height_stack.push(y_buf);
                        y += y_buf;

                        /* Then, we store the list of entity */
                        _stack_states.push(container->get_states());

                        height_buf =  _container_height;
                   
                    }else
                        height_buf =  height_stack.top()-_container_v_space;/* take the child's height */


256
                    draw_object->draw_container(x-_container_width, y- height_buf, _container_width, height_buf);
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
                    /* draw the container text */  
                    draw_object->draw_container_text(x- _container_width+_container_h_space, y-height_buf/2, container->get_name().to_string());

                    x -= (_container_width + _container_h_space);
                     
                }
                else{ /* Should be drawn vertically */
                    
                    /***********************************
                     *
                     * Draw container with more than one child.
                     *
                     **********************************/

                    height_buf = 0;
                    
                    for (int i = 0 ; i < number_of_children(container) ; i ++){
                        height_buf += height_stack.top();
                        height_stack.pop();
                    }
                    
                    height_stack.push(height_buf);/* push the sum of child heights */
                    height_buf -= _container_v_space;

281
                    draw_object->draw_container(x-_container_width, y- height_buf, _container_width, height_buf);/* Vertically, it is drawn as higher as it has children */
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
                    /* draw the container text */                 
                    draw_object->draw_container_text(x-_container_width+_container_h_space, y- height_buf/2, container->get_name().to_string());  

                    x -= (_container_width + _container_h_space);

                }
                        
                /* Now, container was drawn. It's time to go back up */
                if (container->get_parent() != NULL){
                     
                    /***********************************
                     *
                     * Browse parents.
                     *
                     **********************************/

                    int buf = position_stack.top();
                    position_stack.pop();
                     
                    if (container->get_parent()->get_parent() != NULL){
                        
                        container = get_container( container->get_parent()->get_parent()->get_children(), position_stack.top()-1);/*  '-1' because we continu with the same container, not the following */
                    }
                    else
                        container =  container->get_parent();
                    
                    position_stack.push(buf);
                     
                }
                else/* It is the root container */
                    isAnotherContainer = false;/* end of the loop */
                 
            }/* end "else" of "if (position_stack < trace_container->getChildren().size())" */
             
        }/* end "while (isAnotherContainer==true)"*/
   
    }/* end browse_container_tree */



    inline void browse_state_tree(T* draw_object){


325
        const list<State *> *buf_list;
326
327
328
329
330
331
332
333
        State* buf_entity;
        Element_count i;/* for the level (y axis) of the states */
        
        i=0;
        
        while (!_stack_states.empty()){
            buf_list = _stack_states.top();
	 
334
335
336
            for(list<State *>::const_iterator it = buf_list->begin();
                it != buf_list->end();
                it++) {
337
	               
338
                buf_entity = *it;
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
                Element_pos base = ((Element_pos)i)*_container_height+((Element_pos)i)*_container_v_space+_container_v_space;
                /* Call the object state drawing function */ 
                draw_object->draw_state(buf_entity->get_start_time().get_value(), buf_entity->get_end_time().get_value(), base, _state_height, 0.7, 0.7, 0.75); 
            }/* end for */

            i++;

            /* pop the top of the stack */
            _stack_states.pop();

        }/* end while (!_stack_states.empty()) */

    }/* end list */




    /***********************************
     *
     *
     *
     * Browsing tools.
     *
     *
     *
     **********************************/

    inline Element_count number_of_children(const Container* c){
        
        if (c->get_children()==NULL){
            // _window->warning(string("Container child list is empty"));
            return 0;
        }
     
        return c->get_children()->size();
    }
 

Olivier Lagrasse's avatar
Olivier Lagrasse committed
377
    inline Element_count return_last_visited_child(std::stack<Element_count> const &s){
378
379
380
381
382
383
384
385
386
        if (s.empty()==true){
            //   _window->error("The render area position stack is empty.");
            return -1;
        }
        else
            return s.top();
    }
    

Olivier Lagrasse's avatar
Olivier Lagrasse committed
387
388
    inline Container* get_container(const std::list<Container*>* list_container, const int pos) const{
        std::list<Container *>::const_reverse_iterator it;
389
390
391
392
393
394
395
396
397
398
399
        int i=0;
        
        for (it=list_container->rbegin() ; i<pos; it++, i++ ){ }
        
        return *it;
    }
   

};

#endif