"Fossies" - the Fresh Open Source Software Archive

Member "tcpflow-1.6.1/src/netviz/legend_view.cpp" (19 Feb 2021, 3070 Bytes) of package /linux/misc/tcpflow-1.6.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "legend_view.cpp" see the Fossies "Dox" file reference documentation.

    1 /**
    2  * legend_view.cpp: 
    3  * Show packets received vs port
    4  *
    5  * This source file is public domain, as it is not based on the original tcpflow.
    6  *
    7  * Author: Michael Shick <mike@shick.in>
    8  *
    9  */
   10 
   11 #include "config.h"
   12 
   13 #ifdef HAVE_LIBCAIRO
   14 #include "tcpflow.h"
   15 
   16 #include "legend_view.h"
   17 
   18 using namespace std;
   19 
   20 const string legend_view::empty_legend_label = "No TCP";
   21 const double legend_view::base_font_size = 6.0;
   22 const double legend_view::chip_length = 8.0;
   23 const double legend_view::chip_label_space = 4.0;
   24 const double legend_view::inter_item_space = 12.0;
   25 const double legend_view::padding = 8.0;
   26 const double legend_view::border_width = 0.5;
   27 const plot_view::rgb_t legend_view::border_color(0.67, 0.67, 0.67);
   28 
   29 void legend_view::render(cairo_t *cr, const plot_view::bounds_t &bounds) const
   30 {
   31     double font_size = base_font_size;
   32     if(entries.size() == 0) {
   33         font_size *= 2.0;
   34     }
   35     cairo_set_font_size(cr, font_size);
   36 
   37     double tallest = 0.0;
   38     double total_width = 0.0;
   39     for(entries_t::const_iterator it = entries.begin(); it != entries.end(); ++it) {
   40         cairo_text_extents_t extents;
   41         cairo_text_extents(cr, it->label.c_str(), &extents);
   42         total_width += chip_length + chip_label_space + extents.width;
   43         if(it + 1 != entries.end()) {
   44             total_width += inter_item_space;
   45         }
   46         if(extents.height > tallest) {
   47             tallest = extents.height;
   48         }
   49     }
   50     if(entries.size() == 0) {
   51         cairo_text_extents_t extents;
   52         cairo_text_extents(cr, empty_legend_label.c_str(), &extents);
   53         total_width += extents.width;
   54         tallest = extents.height;
   55     }
   56 
   57     double chip_y = bounds.y + ((bounds.height - chip_length) / 2.0);
   58     double label_y = bounds.y + ((bounds.height + tallest) / 2.0);
   59     double x = bounds.x + ((bounds.width - total_width) / 2.0);
   60 
   61     cairo_set_source_rgb(cr, border_color.r, border_color.g, border_color.b);
   62     cairo_set_line_width(cr, border_width);
   63     cairo_rectangle(cr, x, bounds.y, total_width + (padding * 2.0), bounds.height);
   64     cairo_stroke(cr);
   65 
   66     x += padding;
   67 
   68     for(entries_t::const_iterator it = entries.begin(); it != entries.end(); ++it) {
   69         cairo_text_extents_t extents;
   70         cairo_text_extents(cr, it->label.c_str(), &extents);
   71 
   72         const plot_view::rgb_t &color = it->color;
   73         cairo_set_source_rgb(cr, color.r, color.g, color.b);
   74         cairo_rectangle(cr, x, chip_y, chip_length, chip_length);
   75         cairo_fill(cr);
   76 
   77         x += chip_length + chip_label_space;
   78 
   79         cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
   80         cairo_move_to(cr, x, label_y);
   81         cairo_show_text(cr, it->label.c_str());
   82         x += extents.width + inter_item_space;
   83     }
   84     if(entries.size() == 0) {
   85         cairo_text_extents_t extents;
   86         cairo_text_extents(cr, empty_legend_label.c_str(), &extents);
   87 
   88         cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
   89         cairo_move_to(cr, x, label_y);
   90         cairo_show_text(cr, empty_legend_label.c_str());
   91         x += extents.width + inter_item_space;
   92     }
   93 }
   94 #endif