DrawHDiagram.hpp 10.5 KB
Newer Older
Mathieu Faverge's avatar
Mathieu Faverge committed
1
/*
Mathieu Faverge's avatar
Mathieu Faverge committed
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 31 32 33 34 35 36 37 38 39 40 41 42
 ** 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.
 **
 **
 ** ViTE developers are (for version 0.* to 1.0):
 **
 **        - COULOMB Kevin
 **        - FAVERGE Mathieu
 **        - JAZEIX Johnny
 **        - LAGRASSE Olivier
 **        - MARCOUEILLE Jule
 **        - NOISETTE Pascal
 **        - REDONDY Arthur
 **        - VUCHENER Clément
 **
 */
43
/*!
44
 *\file DrawHDiagram.hpp
45 46 47 48 49
 */

#ifndef DRAW_HDIAGRAM_HPP
#define DRAW_HDIAGRAM_HPP

50 51 52 53
/*!
 * \class DrawHDiagram
 * \brief Browse the stats and call back T drawing methods
 */
54 55 56
template<class T>
class DrawHDiagram : public DrawStats<T> {
protected:
Mathieu Faverge's avatar
Mathieu Faverge committed
57 58 59 60 61 62 63 64
    double _startx;
    double _starty;
    double _graph_width;
    double _draw_width;
    double _draw_height;
    double _legend_width;
    double _legend_height;
    std::set<const EntityValue*> _setstates;
65

66 67 68 69 70 71
public:
    /*
     * \brief The default constructor
     */
    DrawHDiagram() {
        _startx = _START_HISTOGRAM_X_DEFAULT;
72
        _starty = 100.0f;
73

74 75 76 77 78
        this->_size_for_one_container = _HEIGHT_FOR_ONE_CONTAINER_DEFAULT;
        this->_percentage_height_default = (this->_size_for_one_container - _START_HISTOGRAM_Y_DEFAULT) / 100.;

        this->_pos_x_container_name   = _POS_X_CONTAINER_NAME;
        this->_pos_y_container_name   = _POS_Y_CONTAINER_NAME;
79

80 81 82 83 84
        /* Size for rectangles in the legend */
        this->_width_for_rect_legend  = 20.;
        this->_height_for_rect_legend = 15.;
    }

85

86 87 88
    /*!
     * \brief The destructor
     */
Mathieu Faverge's avatar
Mathieu Faverge committed
89
    virtual ~DrawHDiagram() = default;
90 91 92


    /*!
93
     * \fn build(T* draw_object, std::vector<Container *> containers_to_print)
94 95
     * \brief The trace building function.
     * \param draw_object the kind of object which will be drawn (OpenGL, SVG...).
96
     * \param containers_to_print the container's data.
97 98 99 100 101 102 103 104 105 106
     */
    void build(T* draw_object, std::vector<Container *> containers_to_print) {
        int nbcont;

        draw_object->clear();

        _draw_height               = draw_object->height();
        _draw_width                = draw_object->width();
        _legend_height             = draw_object->height();
        _legend_width              = draw_object->width();
107
        _graph_width               = _draw_width - _startx - 30.;
108 109 110 111
        this->_containers_to_print = containers_to_print;
        nbcont = this->_containers_to_print.size();

        draw_object->start_draw();
Mathieu Faverge's avatar
Mathieu Faverge committed
112 113
        draw_object->set_total_height( (nbcont+1) * _WIDTH_HISTOGRAM_DEFAULT + 10. );
        draw_object->set_total_width( draw_object->width() );
114

115 116 117 118 119 120
        draw_object->draw_axis(_startx,
                               _starty - (nbcont-1)*_WIDTH_HISTOGRAM_DEFAULT,
                               _graph_width + 10.,
                               (nbcont+1)*_WIDTH_HISTOGRAM_DEFAULT);

        draw_object->draw_horizontal_line(_startx,
Mathieu Faverge's avatar
Mathieu Faverge committed
121 122
                                          _starty + _WIDTH_HISTOGRAM_DEFAULT,
                                          _graph_width + 10.);
123 124

        for (int i=25; i < 101; i+=25)
Mathieu Faverge's avatar
Mathieu Faverge committed
125
        {
126 127 128 129 130 131 132 133 134 135 136 137 138 139
            // Draw Up Scale
            draw_object->draw_vertical_line(_startx + i*_graph_width/100.,
                                            _starty + _WIDTH_HISTOGRAM_DEFAULT - 2.5,
                                            5.);
            draw_object->draw_text(_startx + i*_graph_width/100 - 10.,
                                   this->_size_for_one_container-(_starty + _WIDTH_HISTOGRAM_DEFAULT + 15.),
                                   QString::number(i, 'd', 1).toStdString()+"%");
            // Draw Down Scale
            draw_object->draw_vertical_line(_startx + i*_graph_width/100.,
                                            _starty - (nbcont-1)*_WIDTH_HISTOGRAM_DEFAULT - 2.5,
                                            5.);
            draw_object->draw_text(_startx + i*_graph_width/100 - 10.,
                                   this->_size_for_one_container-(_starty - (nbcont-1)*_WIDTH_HISTOGRAM_DEFAULT - 15.),
                                   QString::number(i, 'd', 1).toStdString()+"%");
Mathieu Faverge's avatar
Mathieu Faverge committed
140
        }
141 142

        for(int i = 0 ; i < nbcont ; i ++) {
143
            draw_diagram(draw_object, i);
144
        }
145 146 147 148
        draw_legend(draw_object,
                    _POS_X_LEGEND_DEFAULT + _startx,
                    _POS_Y_LEGEND_DEFAULT + _starty - (nbcont+3)*_WIDTH_HISTOGRAM_DEFAULT);
        this->end_draw(draw_object);
149 150
    }

151 152 153 154 155 156
    /*!
     * \fn draw_diagram(T* draw_object, const int container_id)
     * \brief Print the i-th diagram
     * \param draw_object the kind of object which will be drawn (OpenGL, SVG...).
     * \param container_id the id of the container.
     */
157 158 159
    void draw_diagram(T* draw_object, const int container_id) {
        Statistic                           *stat_temp;
        std::map<const EntityValue*, stats*> temp_states;
160 161
        double                               duration;
        int                                  pos_x, pos_y;
162
        std::string                          ctname;
163 164

        stat_temp   = new Statistic();
165
        this->_containers_to_print[container_id]->fill_stat(stat_temp, Interval(this->_start_time, this->_end_time));
166 167
        temp_states = stat_temp->get_states();
        duration = this->_end_time - this->_start_time;
168 169

        // Printing of the trace
170 171
        //std::cout << "nb states " << temp_states.size() << std::endl;

172
        // Depending on the kind of diagram...
173 174 175 176
        pos_x = _startx;
        pos_y = _starty - container_id * _WIDTH_HISTOGRAM_DEFAULT;

        // Draw the container name
Mathieu Faverge's avatar
Mathieu Faverge committed
177
        ctname = this->_containers_to_print[container_id]->get_Name().to_string();
178 179 180 181 182 183 184 185
        draw_object->draw_text(_POS_X_CONTAINER_NAME,
                               this->_size_for_one_container-(pos_y + _WIDTH_HISTOGRAM_DEFAULT / 2. - 5.),
                               ctname);

        // Draw the stats
        for (std::map<const EntityValue *, stats *>::iterator it = temp_states.begin();
             it != temp_states.end();
             it ++)
Mathieu Faverge's avatar
Mathieu Faverge committed
186
        {
187 188 189 190 191 192 193 194 195
            double      length  = (*it).second->_total_length;
            double      percent = length / duration ;
            double      width   = percent * _graph_width;
            double      red     = 0.7;
            double      green   = 0.7;
            double      blue    = 0.75;

            // We search for a color
            if((*it).first->get_extra_fields()->find(std::string("Color")) != (*it).first->get_extra_fields()->end())
Mathieu Faverge's avatar
Mathieu Faverge committed
196
            {
197 198 199 200
                const Color *color = (const Color *)(*it).first->get_extra_fields()->find(std::string("Color"))->second;
                red   = color->get_red();
                green = color->get_green();
                blue  = color->get_blue();
Mathieu Faverge's avatar
Mathieu Faverge committed
201
            }
202 203 204 205 206 207

            draw_object->draw_rect(pos_x, pos_y,
                                   width, _WIDTH_HISTOGRAM_DEFAULT,
                                   red, green, blue);

            if ( width > 30. )
Mathieu Faverge's avatar
Mathieu Faverge committed
208 209 210
                draw_object->draw_text(pos_x + width / 2 - 10.,
                                       this->_size_for_one_container-(pos_y + _WIDTH_HISTOGRAM_DEFAULT / 2. - 5.),
                                       QString::number(percent*100., 'f', 1).toStdString()+"%");
Mathieu Faverge's avatar
Mathieu Faverge committed
211

212 213
            pos_x += width;
            this->_setstates.insert((*it).first);
Mathieu Faverge's avatar
Mathieu Faverge committed
214

Mathieu Faverge's avatar
Mathieu Faverge committed
215
        }
216 217 218 219

        delete stat_temp;
    }

220 221 222 223 224 225 226 227
    /*!
     * \fn draw_legend(T* draw_object, int pos_x, int pos_y)
     * \brief Print the legend for the i-th element (eg the color of each stats)
     * \param draw_object the kind of object which will be drawn (OpenGL, SVG...).
     * \param pos_x the x position of the legend.
     * \param pos_y the y position of the legend.
     */
    void draw_legend(T* draw_object, int pos_x, int pos_y) {
Mathieu Faverge's avatar
Mathieu Faverge committed
228 229
        std::set<const EntityValue *>::const_iterator it;
        std::set<const EntityValue *>::const_iterator end;
230 231 232 233 234 235
        const double w = this->_width_for_rect_legend;
        const double h = this->_height_for_rect_legend;

        /* used to print legend on 3 rows */
        int decalage = 0;

236
        end = this->_setstates.end();
Mathieu Faverge's avatar
Mathieu Faverge committed
237 238
        for (it  = this->_setstates.begin();
             it != end;
239
             it ++, decalage ++) {
240 241
            std::string name = (*it)->get_name();

242 243
            draw_object->draw_text(pos_x+w+_POS_X_LEGEND_DEFAULT,
                                   this->_size_for_one_container-pos_y,
244
                                   name.substr(0, 10));
245 246

            if((*it)->get_extra_fields()->find(std::string("Color")) != (*it)->get_extra_fields()->end())
Mathieu Faverge's avatar
Mathieu Faverge committed
247
            {
Mathieu Faverge's avatar
Mathieu Faverge committed
248
                const Color *color = (const Color *)(*it)->get_extra_fields()->find(std::string("Color"))->second;
249
                draw_object->draw_rect(pos_x, pos_y, w, h, color->get_red(), color->get_green(), color->get_blue());
Mathieu Faverge's avatar
Mathieu Faverge committed
250
            }
251
            else
Mathieu Faverge's avatar
Mathieu Faverge committed
252
            {
253
                draw_object->draw_rect(pos_x, pos_y, w, h, 0.7, 0.7, 0.75);
Mathieu Faverge's avatar
Mathieu Faverge committed
254
            }
255

256 257 258 259 260 261 262 263 264
            switch(decalage%3) {
            case 2:
                pos_x += 100;
                pos_y += 40;
                break;
            default:
                pos_y -= 20;
                break;
            }
265 266 267
        }

        this->_max_width = pos_x + w + _POS_X_LEGEND_DEFAULT;
Johnny Jazeix's avatar
Johnny Jazeix committed
268
        this->set_geometrical_informations_object(draw_object);
269

270 271
    }
};
272

273
#endif