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