gui/graph_displayer_canvas.cc
author athos
Fri, 22 Jul 2005 09:41:20 +0000
changeset 1580 a9e4208cf4e3
parent 1562 73c3f5d0079c
child 1581 9bb83c7f479b
permissions -rwxr-xr-x
Some changes to kruskal stuff.
     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),edgemap_to_edit(""),nodemap_to_edit(""),mapwin(mw)
     6 {
     7   
     8   
     9   active_node=INVALID;
    10   active_edge=INVALID;
    11   forming_edge=INVALID;
    12 
    13   //setting event handler for the editor widget
    14   entrywidget.signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::entryWidgetChangeHandler), false);
    15 
    16   //base event handler is move tool
    17   actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::moveEventHandler), false);
    18   actual_tool=MOVE;
    19 
    20   //set_center_scroll_region(true);
    21 
    22   //first edges are drawn, to hide joining with nodes later
    23 
    24   for (EdgeIt i(g); i!=INVALID; ++i)
    25   {
    26 
    27     //drawing green lines, coordinates are from cm
    28 
    29     Gnome::Canvas::Points coos;
    30     coos.push_back(Gnome::Art::Point(cm[g.source(i)].x,cm[g.source(i)].y));
    31     coos.push_back(Gnome::Art::Point(cm[g.target(i)].x,cm[g.target(i)].y));
    32     
    33     edgesmap[i]=new BrokenEdge(displayed_graph, coos, *this);
    34     *(edgesmap[i]) << Gnome::Canvas::Properties::fill_color("green");
    35     edgesmap[i]->property_width_pixels().set_value(10);    
    36     
    37     //initializing edge-text as well, to empty string
    38 
    39     xy<double> text_pos=edgesmap[i]->getArrowPos();
    40     text_pos+=(xy<double>(10,10));
    41 
    42     edgetextmap[i]=new Gnome::Canvas::Text(displayed_graph, text_pos.x, text_pos.y, "");
    43     edgetextmap[i]->property_fill_color().set_value("darkgreen");
    44   }
    45 
    46   //afterwards nodes come to be drawn
    47 
    48   NodeIt i(g);
    49   int maxx=0, maxy=0, minx=(int)cm[i].x, miny=(int)cm[i].y;
    50 
    51   for (; i!=INVALID; ++i)
    52   {
    53     //minimum and maximum is gathered to be able to zoom to the graph correctly (whole figure should be seen)
    54 
    55     if(cm[i].x>maxx)maxx=(int)cm[i].x;
    56     if(cm[i].y>maxy)maxy=(int)cm[i].y;
    57     if(cm[i].x<minx)minx=(int)cm[i].x;
    58     if(cm[i].y<miny)miny=(int)cm[i].y;
    59 
    60     //drawing bule nodes, with black line around them
    61 
    62     nodesmap[i]=new Gnome::Canvas::Ellipse(displayed_graph, cm[i].x-20, cm[i].y-20, cm[i].x+20, cm[i].y+20);
    63     *(nodesmap[i]) << Gnome::Canvas::Properties::fill_color("blue");
    64     *(nodesmap[i]) << Gnome::Canvas::Properties::outline_color("black");
    65 
    66     //initializing edge-text as well, to empty string
    67 
    68     xy<double> text_pos((cm[i].x+node_property_defaults[N_RADIUS]+5),(cm[i].y+node_property_defaults[N_RADIUS]+5));
    69 
    70     nodetextmap[i]=new Gnome::Canvas::Text(displayed_graph, text_pos.x, text_pos.y, "");
    71     nodetextmap[i]->property_fill_color().set_value("darkblue");
    72   }
    73 
    74   updateScrollRegion();
    75 }
    76 
    77 GraphDisplayerCanvas::~GraphDisplayerCanvas()
    78 {
    79   if(canvasentrywidget)
    80     {
    81       delete(canvasentrywidget);
    82     }
    83 
    84   //writing out the end state of the graph
    85   //\todo all the maps has to be write out!
    86 
    87   Graph::NodeMap <int> id(g);
    88   Graph::NodeMap <double> xc(g);
    89   Graph::NodeMap <double> yc(g);
    90   
    91   int j=1;
    92   
    93   for (NodeIt i(g); i!=INVALID; ++i)
    94   {
    95     double x1,y1,x2,y2;
    96     nodesmap[i]->get_bounds(x1, y1, x2, y2);
    97     
    98     id[i]=j++;
    99     xc[i]=(x1+x2)/2;
   100     yc[i]=(y1+y2)/2;
   101   }
   102 
   103   GraphWriter<Graph> writer(std::cout,g);
   104   
   105   writer.writeNodeMap("id", id);
   106   writer.writeNodeMap("coordinates_x", xc);
   107   writer.writeNodeMap("coordinates_y", yc);
   108   writer.run();
   109 }