Commit 60c10f08 authored by Mathieu Faverge's avatar Mathieu Faverge
Browse files

Put Original Diagram in a subclass

parent 2e30bf2f
/*!
*\file DrawStats.hpp
*/
#ifndef DRAW_DIAGRAM_HPP
#define DRAW_DIAGRAM_HPP
#include <string>
#include <vector>
#include <map>
#include "../message/Message.hpp"
#include "../trace/Trace.hpp"
#include "../trace/tree/Interval.hpp"
#include "Statistic.hpp"
#include "DrawStats.hpp"
/*!
* \class Drawdiagramm
* \brief Browse the stats and call back T drawing methods
*/
template<class T>
class DrawDiagram : public DrawStats<T> {
public:
/*
* \brief The default constructor
*/
DrawDiagram() {
this->_size_for_one_container = _HEIGHT_FOR_ONE_CONTAINER_DEFAULT;
this->_pos_x_container_name = _POS_X_CONTAINER_NAME;
this->_pos_y_container_name = _POS_Y_CONTAINER_NAME;
this->_percentage_height_default = (this->_size_for_one_container - _START_HISTOGRAM_Y_DEFAULT) / 100.;
/* Size for rectangles in the legend */
this->_width_for_rect_legend = 20.;
this->_height_for_rect_legend = 15.;
}
/*!
* \brief The destructor
*/
virtual ~DrawDiagram() {
}
/*!
* \fn build(T* draw_object, Trace* trace)
* \brief The trace building function.
* \param draw_object the kind of object which will be drawn (OpenGL, SVG...).
* \param trace the trace data.
*/
virtual void build(T* draw_object, std::vector<Container *> containers_to_print) {
draw_object->clear();
this->_size_for_one_container = draw_object->height();
this->_containers_to_print = containers_to_print;
const int number_of_containers = this->_containers_to_print.size();
draw_object->start_draw();
draw_object->set_total_height((number_of_containers-1)*this->_size_for_one_container);
draw_object->set_total_width(draw_object->width());
for(int i = 0 ; i < number_of_containers ; i ++) {
draw_container_name(draw_object, i);
draw_diagram(draw_object, i);
draw_legend(draw_object, i);
}
end_draw(draw_object);
}
virtual void draw_container_name(T* draw_object, const int container_id) const {
// Get the position for the i-th container name
Element_pos pos_x = this->_pos_x_container_name;
Element_pos pos_y = this->_pos_y_container_name - this->_size_for_one_container * container_id;
std::string name = this->_containers_to_print[container_id]->get_name().to_string();
draw_object->draw_text(pos_x, pos_y, name);
}
virtual void draw_diagram(T* draw_object, const int container_id) {
Statistic *stat_temp = new Statistic();
this->_containers_to_print[container_id]->fill_stat(stat_temp, Interval(this->_start_time, this->_end_time));
std::map<const EntityValue*, stats*> temp_states = stat_temp->get_states();
this->_states.push_back(temp_states);
// Printing of the trace
int pos_x = _START_HISTOGRAM_X_DEFAULT;
int pos_y = _START_HISTOGRAM_Y_DEFAULT - container_id*this->_size_for_one_container;
const double max_percentage = get_max_percentage(temp_states);
// Draw axes
draw_object->draw_axis(pos_x, pos_y, _WIDTH_HISTOGRAM_DEFAULT*(this->_states[container_id].size()+1), this->_percentage_height_default*100);
draw_object->draw_text(pos_x-30, pos_y+this->_percentage_height_default*100-5, QString::number(max_percentage*100., 'g', 3).toStdString()+"%");
draw_object->draw_horizontal_line(pos_x, pos_y+this->_percentage_height_default*100, _WIDTH_HISTOGRAM_DEFAULT*(this->_states[container_id].size()+1));
// Draw the stats
for (map<const EntityValue *, stats *>::iterator it = temp_states.begin();
it != temp_states.end();
it ++) {
std::string name = (*it).first->get_name().to_string();
// We have to convert the percentage in a rectangle and print it
const double length = (*it).second->_total_length;
const double height = length*100.*this->_percentage_height_default/(this->_end_time-this->_start_time);
// We search for a color
if((*it).first->get_extra_fields()->find(std::string("Color")) != (*it).first->get_extra_fields()->end()) {
const Color *color = (const Color *)(*it).first->get_extra_fields()->find(std::string("Color"))->second;
draw_object->draw_rect(pos_x, pos_y, _WIDTH_HISTOGRAM_DEFAULT, height/max_percentage, color->get_red(), color->get_green(), color->get_blue());
}
else {
draw_object->draw_rect(pos_x, pos_y, _WIDTH_HISTOGRAM_DEFAULT, height/max_percentage, 0.7, 0.7, 0.75);
}
// We print the percentage above
draw_object->draw_text(pos_x, pos_y+height/max_percentage+1, QString::number(length/(this->_end_time-this->_start_time)*100., 'f', 1).toStdString()+"%");
//std::cout << "State : " << name << " for " << (*it).second->_total_length*100./(this->_end_time-this->_start_time)<< "% between " << this->_start_time << " sec and " << this->_end_time << " sec" << std::endl;
pos_x += _WIDTH_HISTOGRAM_DEFAULT;
}
delete stat_temp;
}
virtual void draw_legend(T* draw_object, const int container_id) {
Element_pos pos_x = _POS_X_CONTAINER_NAME + _POS_X_LEGEND_DEFAULT;
Element_pos pos_y = -this->_size_for_one_container * (container_id) + _POS_Y_LEGEND_DEFAULT;
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;
for (map<const EntityValue *, stats *>::iterator it = this->_states[container_id].begin();
it != this->_states[container_id].end();
it ++, decalage ++) {
std::string name = (*it).first->get_name().to_string();
draw_object->draw_text(pos_x+w+_POS_X_LEGEND_DEFAULT, pos_y, name);
if((*it).first->get_extra_fields()->find(std::string("Color")) != (*it).first->get_extra_fields()->end()) {
const Color *color = (const Color *)(*it).first->get_extra_fields()->find(std::string("Color"))->second;
draw_object->draw_rect(pos_x, pos_y, w, h, color->get_red(), color->get_green(), color->get_blue());
}
else {
draw_object->draw_rect(pos_x, pos_y, w, h, 0.7, 0.7, 0.75);
}
switch(decalage%3) {
case 2:
pos_x += 100;
pos_y += 40;
break;
default:
pos_y -= 20;
break;
}
}
}
/*!
*
* \return a value between 0. and 1.
*
*/
virtual double get_max_percentage(std::map<const EntityValue*, stats*> &temp_states) const {
double value;
double max_length = 0.;
for (map<const EntityValue *, stats *>::iterator it = temp_states.begin();
it != temp_states.end();
it ++) {
if((*it).second->_total_length >= max_length) {
max_length = (*it).second->_total_length;
}
}
value = max_length/(this->_end_time-this->_start_time);
//std::cerr << value << std::endl;
return value;
}
};
#endif
......@@ -83,9 +83,10 @@ protected:
std::vector<std::map<const EntityValue*, stats*> > _states;
// Interval to compute statistics
double _start_time;
double _previous_start_time;
double _end_time;
double _previous_start_time;
double _previous_end_time;
// Geometrical informations about the stats shape.
......@@ -129,131 +130,17 @@ public:
* \param draw_object the kind of object which will be drawn (OpenGL, SVG...).
* \param trace the trace data.
*/
virtual void build(T* draw_object, std::vector<Container *> containers_to_print) {
draw_object->clear();
_size_for_one_container = draw_object->height();
_containers_to_print = containers_to_print;
const int number_of_containers = _containers_to_print.size();
draw_object->start_draw();
draw_object->set_total_height((number_of_containers-1)*_size_for_one_container);
draw_object->set_total_width(draw_object->width());
for(int i = 0 ; i < number_of_containers ; i ++) {
draw_container_name(draw_object, i);
draw_diagram(draw_object, i);
draw_legend(draw_object, i);
}
end_draw(draw_object);
}
virtual void draw_container_name(T* draw_object, const int container_id) const {
// Get the position for the i-th container name
Element_pos pos_x = _pos_x_container_name;
Element_pos pos_y = _pos_y_container_name - _size_for_one_container * container_id;
std::string name = _containers_to_print[container_id]->get_name().to_string();
draw_object->draw_text(pos_x, pos_y, name);
}
virtual void draw_diagram(T* draw_object, const int container_id) {
Statistic *stat_temp = new Statistic();
_containers_to_print[container_id]->fill_stat(stat_temp, Interval(_start_time, _end_time));
std::map<const EntityValue*, stats*> temp_states = stat_temp->get_states();
_states.push_back(temp_states);
// Printing of the trace
int pos_x = _START_HISTOGRAM_X_DEFAULT;
int pos_y = _START_HISTOGRAM_Y_DEFAULT - container_id*_size_for_one_container;
const double max_percentage = get_max_percentage(temp_states);
// Draw axes
draw_object->draw_axis(pos_x, pos_y, _WIDTH_HISTOGRAM_DEFAULT*(_states[container_id].size()+1), _percentage_height_default*100);
draw_object->draw_text(pos_x-30, pos_y+_percentage_height_default*100-5, QString::number(max_percentage*100., 'g', 3).toStdString()+"%");
draw_object->draw_horizontal_line(pos_x, pos_y+_percentage_height_default*100, _WIDTH_HISTOGRAM_DEFAULT*(_states[container_id].size()+1));
// Draw the stats
for (map<const EntityValue *, stats *>::iterator it = temp_states.begin();
it != temp_states.end();
it ++) {
std::string name = (*it).first->get_name().to_string();
// We have to convert the percentage in a rectangle and print it
const double length = (*it).second->_total_length;
const double height = length*100.*_percentage_height_default/(_end_time-_start_time);
// We search for a color
if((*it).first->get_extra_fields()->find(std::string("Color")) != (*it).first->get_extra_fields()->end()) {
const Color *color = (const Color *)(*it).first->get_extra_fields()->find(std::string("Color"))->second;
draw_object->draw_rect(pos_x, pos_y, _WIDTH_HISTOGRAM_DEFAULT, height/max_percentage, color->get_red(), color->get_green(), color->get_blue());
}
else {
draw_object->draw_rect(pos_x, pos_y, _WIDTH_HISTOGRAM_DEFAULT, height/max_percentage, 0.7, 0.7, 0.75);
}
// We print the percentage above
draw_object->draw_text(pos_x, pos_y+height/max_percentage+1, QString::number(length/(_end_time-_start_time)*100., 'f', 1).toStdString()+"%");
//std::cout << "State : " << name << " for " << (*it).second->_total_length*100./(_end_time-_start_time)<< "% between " << _start_time << " sec and " << _end_time << " sec" << std::endl;
pos_x += _WIDTH_HISTOGRAM_DEFAULT;
}
delete stat_temp;
}
virtual void draw_legend(T* draw_object, const int container_id) {
Element_pos pos_x = _POS_X_CONTAINER_NAME + _POS_X_LEGEND_DEFAULT;
Element_pos pos_y = -_size_for_one_container * (container_id) + _POS_Y_LEGEND_DEFAULT;
const double w = _width_for_rect_legend;
const double h = _height_for_rect_legend;
/* used to print legend on 3 rows */
int decalage = 0;
for (map<const EntityValue *, stats *>::iterator it = _states[container_id].begin();
it != _states[container_id].end();
it ++, decalage ++) {
std::string name = (*it).first->get_name().to_string();
draw_object->draw_text(pos_x+w+_POS_X_LEGEND_DEFAULT, pos_y, name);
if((*it).first->get_extra_fields()->find(std::string("Color")) != (*it).first->get_extra_fields()->end()) {
const Color *color = (const Color *)(*it).first->get_extra_fields()->find(std::string("Color"))->second;
draw_object->draw_rect(pos_x, pos_y, w, h, color->get_red(), color->get_green(), color->get_blue());
}
else {
draw_object->draw_rect(pos_x, pos_y, w, h, 0.7, 0.7, 0.75);
}
switch(decalage%3) {
case 2:
pos_x += 100;
pos_y += 40;
break;
default:
pos_y -= 20;
break;
}
}
}
virtual void build(T* draw_object, std::vector<Container *> containers_to_print) = 0;
inline void end_draw(T* draw_object) {
_previous_start_time = _start_time;
_previous_end_time = _end_time;
_previous_end_time = _end_time;
draw_object->end_draw();
}
inline void set_times(const double start, const double end) {
_start_time = start;
_end_time = end;
_end_time = end;
}
/*!
......
......@@ -115,17 +115,17 @@ void Stats_window::on_reload_button_clicked() {
}
else if (kind_of_diagram == "Histogram")
{
drawer = new DrawStats<Render_stats_opengl>();
drawer = new DrawDiagram<Render_stats_opengl>();
}
else if (kind_of_diagram == "Chart")
{
*Message::get_instance() << kind_of_diagram << " not yet implemented" << Message::endw;
drawer = new DrawStats<Render_stats_opengl>();
drawer = new DrawHDiagram<Render_stats_opengl>();
}
else
{
*Message::get_instance() << kind_of_diagram << " not yet implemented" << Message::endw;
drawer = new DrawStats<Render_stats_opengl>();
drawer = new DrawDiagram<Render_stats_opengl>();
}
drawer->set_times(_start_time, _end_time);
drawer->build(_ui_stats_area, _selected_containers);
......
......@@ -20,6 +20,7 @@ class Stats_window;
#include "../message/Message.hpp"
#include "../render/render_stats_opengl.hpp"
#include "DrawStats.hpp"
#include "DrawDiagram.hpp"
#include "DrawHDiagram.hpp"
/*!
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment