graph_displayer_canvas.cc
author hegyi
Fri, 24 Jun 2005 07:58:18 +0000
branchgui
changeset 27 e2c86ae158cf
parent 26 b0c76a4d5801
child 28 fa28f1071bd6
permissions -rwxr-xr-x
File graph_displayer is split in functional parts.
     1 #include <graph_displayer_canvas.h>
     2 #include <broken_edge.h>
     3 #include <math.h>
     4 
     5 GraphDisplayerCanvas::GraphDisplayerCanvas(Graph & gr, CoordinatesMap & cm, MapStorage & ms):g(gr),nodesmap(g),edgesmap(g),edgetextmap(g),displayed_graph(*(root()), 0, 0),mapstorage(ms),isbutton(0),active_item(NULL),target_item(NULL)
     6 {
     7   
     8   actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::create_node_event_handler), false);
     9   actual_tool=CREATE_NODE;
    10 
    11   active_node=INVALID;
    12   active_edge=INVALID;
    13 
    14   //set_center_scroll_region(true);
    15 
    16   //first edges are drawn, to hide joining with nodes later
    17 
    18   for (EdgeIt i(g); i!=INVALID; ++i)
    19   {
    20 
    21     //drawing green lines, coordinates are from cm
    22 
    23     Gnome::Canvas::Points coos;
    24     coos.push_back(Gnome::Art::Point(cm[g.source(i)].x,cm[g.source(i)].y));
    25     coos.push_back(Gnome::Art::Point(cm[g.target(i)].x,cm[g.target(i)].y));
    26     
    27     edgesmap[i]=new BrokenEdge(displayed_graph, coos, *this);
    28     *(edgesmap[i]) << Gnome::Canvas::Properties::fill_color("green");
    29     edgesmap[i]->property_width_pixels().set_value(10);    
    30     
    31     //initializing edge-text as well, to empty string
    32 
    33     xy<double> text_pos=edgesmap[i]->get_arrow_pos();
    34     text_pos+=(xy<double>(10,10));
    35 
    36     edgetextmap[i]=new Gnome::Canvas::Text(displayed_graph, text_pos.x, text_pos.y, "");
    37     edgetextmap[i]->property_fill_color().set_value("black");
    38   }
    39 
    40   //afterwards nodes come to be drawn
    41 
    42   NodeIt i(g);
    43   int maxx=0, maxy=0, minx=(int)cm[i].x, miny=(int)cm[i].y;
    44 
    45   for (; i!=INVALID; ++i)
    46   {
    47     //minimum and maximum is gathered to be able to zoom to the graph correctly (whole figure should be seen)
    48 
    49     if(cm[i].x>maxx)maxx=(int)cm[i].x;
    50     if(cm[i].y>maxy)maxy=(int)cm[i].y;
    51     if(cm[i].x<minx)minx=(int)cm[i].x;
    52     if(cm[i].y<miny)miny=(int)cm[i].y;
    53 
    54     //drawing bule nodes, with black line around them
    55 
    56     nodesmap[i]=new Gnome::Canvas::Ellipse(displayed_graph, cm[i].x-20, cm[i].y-20, cm[i].x+20, cm[i].y+20);
    57     *(nodesmap[i]) << Gnome::Canvas::Properties::fill_color("blue");
    58     *(nodesmap[i]) << Gnome::Canvas::Properties::outline_color("black");
    59     //!!!!!!! (nodesmap[i])->signal_event().connect(sigc::bind(sigc::mem_fun(*this, &GraphDisplayerCanvas::event_handler),i));
    60   }
    61 
    62   updateScrollRegion();
    63 }
    64 
    65 GraphDisplayerCanvas::~GraphDisplayerCanvas()
    66 {
    67 
    68   //writing out the end state of the graph
    69   //\todo all the maps has to be write out!
    70 
    71   Graph::NodeMap <int> id(g);
    72   Graph::NodeMap <double> xc(g);
    73   Graph::NodeMap <double> yc(g);
    74   
    75   int j=1;
    76   
    77   for (NodeIt i(g); i!=INVALID; ++i)
    78   {
    79     double x1,y1,x2,y2;
    80     nodesmap[i]->get_bounds(x1, y1, x2, y2);
    81     
    82     id[i]=j++;
    83     xc[i]=(x1+x2)/2;
    84     yc[i]=(y1+y2)/2;
    85   }
    86 
    87   GraphWriter<Graph> writer(std::cout,g);
    88   
    89   writer.writeNodeMap("id", id);
    90   writer.writeNodeMap("coordinates_x", xc);
    91   writer.writeNodeMap("coordinates_y", yc);
    92   writer.run();
    93 }