gui/graph_displayer_canvas.cc
author hegyi
Tue, 12 Jul 2005 18:21:49 +0000
changeset 1550 4dcbb4ab1d7a
parent 1525 6d94de269ab1
child 1562 73c3f5d0079c
permissions -rwxr-xr-x
Editors are based.
     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, MapWin * mw):g(gr),nodesmap(g),edgesmap(g),edgetextmap(g),nodetextmap(g),displayed_graph(*(root()), 0, 0),canvasentrywidget(NULL),mapstorage(ms),isbutton(0),active_item(NULL),target_item(NULL),mapwin(mw)
     6 {
     7   
     8   actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::moveEventHandler), 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]->getArrowPos();
    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("darkgreen");
    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 
    60     //initializing edge-text as well, to empty string
    61 
    62     xy<double> text_pos((cm[i].x+node_property_defaults[N_RADIUS]+5),(cm[i].y+node_property_defaults[N_RADIUS]+5));
    63 
    64     nodetextmap[i]=new Gnome::Canvas::Text(displayed_graph, text_pos.x, text_pos.y, "");
    65     nodetextmap[i]->property_fill_color().set_value("darkblue");
    66   }
    67 
    68   updateScrollRegion();
    69 }
    70 
    71 GraphDisplayerCanvas::~GraphDisplayerCanvas()
    72 {
    73   if(canvasentrywidget)
    74     {
    75       delete(canvasentrywidget);
    76     }
    77 
    78   //writing out the end state of the graph
    79   //\todo all the maps has to be write out!
    80 
    81   Graph::NodeMap <int> id(g);
    82   Graph::NodeMap <double> xc(g);
    83   Graph::NodeMap <double> yc(g);
    84   
    85   int j=1;
    86   
    87   for (NodeIt i(g); i!=INVALID; ++i)
    88   {
    89     double x1,y1,x2,y2;
    90     nodesmap[i]->get_bounds(x1, y1, x2, y2);
    91     
    92     id[i]=j++;
    93     xc[i]=(x1+x2)/2;
    94     yc[i]=(y1+y2)/2;
    95   }
    96 
    97   GraphWriter<Graph> writer(std::cout,g);
    98   
    99   writer.writeNodeMap("id", id);
   100   writer.writeNodeMap("coordinates_x", xc);
   101   writer.writeNodeMap("coordinates_y", yc);
   102   writer.run();
   103 }