graph_displayer_canvas.cc
branchgui
changeset 5 b7c36be1e35c
parent 4 e099638ff236
child 6 603b85626bc0
     1.1 --- a/graph_displayer_canvas.cc	Fri May 27 10:34:20 2005 +0000
     1.2 +++ b/graph_displayer_canvas.cc	Wed Jun 01 23:30:13 2005 +0000
     1.3 @@ -3,6 +3,7 @@
     1.4  
     1.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(false),active_item(NULL)
     1.6  {
     1.7 +  //set_center_scroll_region(true);
     1.8  
     1.9    //first edges are drawn, to hide joining with nodes later
    1.10  
    1.11 @@ -50,6 +51,7 @@
    1.12      (nodesmap[i])->signal_event().connect(sigc::bind(sigc::mem_fun(*this, &GraphDisplayerCanvas::event_handler),i));
    1.13    }
    1.14  
    1.15 +/*
    1.16    //setting zoom to be able to see the whole graph on the canvas
    1.17  
    1.18    double biggest_x=(abs(maxx)>abs(minx))?(abs(maxx)+80):(abs(minx)+80);
    1.19 @@ -59,7 +61,8 @@
    1.20    std::cout<<abs(maxx)<<" "<<abs(minx)<<" big x "<<biggest_x<<" "<<abs(maxy)<<" "<<abs(miny)<<" big y "<<biggest_y<<std::endl;
    1.21    std::cout<<maxx<<" "<<minx<<" big x "<<biggest_x<<" "<<maxy<<" "<<miny<<" big y "<<biggest_y<<std::endl;
    1.22    std::cout<<"dx "<<(maxx-minx)<<" dy "<<(maxy-miny)<<" xrate "<<((maxx-minx)/WIN_WIDTH)<<" yrate "<<((maxy-miny)/WIN_HEIGHT)<<std::endl;
    1.23 -
    1.24 +*/
    1.25 +  updateScrollRegion();
    1.26  }
    1.27  
    1.28  GraphDisplayerCanvas::~GraphDisplayerCanvas()
    1.29 @@ -173,48 +176,6 @@
    1.30    return 0;
    1.31  };
    1.32  
    1.33 -
    1.34 -int GraphDisplayerCanvas::rezoom ()
    1.35 -{
    1.36 -
    1.37 -  //searches for the minimum and the maximum
    1.38 -  //value of the coordinates of the nodes to
    1.39 -  //set the pixel rpo unit to a value to be 
    1.40 -  //able to see the whole graph in the canvas
    1.41 -  //\todo does not work properly
    1.42 -
    1.43 -  double x1, x2, y1, y2;
    1.44 -  int x,y;
    1.45 -
    1.46 -  NodeIt i(g);
    1.47 -  nodesmap[i]->get_bounds(x1, y1, x2, y2);
    1.48 -
    1.49 -  x=(int)((x1+x2)/2);
    1.50 -  y=(int)((y1+y2)/2);
    1.51 -  
    1.52 -  int maxx=0, maxy=0, minx=(int)x, miny=(int)y;
    1.53 -
    1.54 -  for (; i!=INVALID; ++i)
    1.55 -  {
    1.56 -    nodesmap[i]->get_bounds(x1, y1, x2, y2);
    1.57 -
    1.58 -    x=(int)((x1+x2)/2);
    1.59 -    y=(int)((y1+y2)/2);
    1.60 -
    1.61 -    if(x>maxx)maxx=x;
    1.62 -    if(y>maxy)maxy=y;
    1.63 -    if(x<minx)minx=x;
    1.64 -    if(y<miny)miny=y;
    1.65 -  }
    1.66 -
    1.67 -  double biggest_x=(abs(maxx)>abs(minx))?(abs(maxx)+80):(abs(minx)+80);
    1.68 -  double biggest_y=(abs(maxy)>abs(miny))?(abs(maxy)+80):(abs(miny)+80);
    1.69 -
    1.70 -  set_pixels_per_unit((biggest_x-WIN_WIDTH>biggest_y-WIN_HEIGHT)?(WIN_WIDTH/biggest_x/2):(WIN_HEIGHT/biggest_y/2));
    1.71 -  return 0;
    1.72 -};
    1.73 -
    1.74 -
    1.75  bool GraphDisplayerCanvas::event_handler(GdkEvent* e, Node n)
    1.76  {
    1.77    switch(e->type)
    1.78 @@ -300,3 +261,61 @@
    1.79    //rezoom();
    1.80    return true;
    1.81  }
    1.82 +
    1.83 +void GraphDisplayerCanvas::zoomIn()
    1.84 +{
    1.85 +  set_pixels_per_unit(
    1.86 +      (1.0 + (double) zoom_step / 100.0) * get_pixels_per_unit());
    1.87 +}
    1.88 +
    1.89 +void GraphDisplayerCanvas::zoomOut()
    1.90 +{
    1.91 +  set_pixels_per_unit(
    1.92 +      (1.0 - (double) zoom_step / 100.0) * get_pixels_per_unit());
    1.93 +}
    1.94 +
    1.95 +void GraphDisplayerCanvas::zoomFit()
    1.96 +{
    1.97 +  // get the height and width of the canvas
    1.98 +  Gtk::Allocation a = get_allocation();
    1.99 +  int aw = a.get_width();
   1.100 +  int ah = a.get_height();
   1.101 +  // add some space
   1.102 +  aw -= 5; if (aw < 0) aw = 0;
   1.103 +  ah -= 5; if (ah < 0) ah = 0;
   1.104 +  //std::cout << "aw=" << aw << " ah=" << ah << std::endl;
   1.105 +
   1.106 +  // get the bounding box of the graph
   1.107 +  set_pixels_per_unit(1.0); // I don't really understand why this is necessary
   1.108 +  double wx1, wy1, wx2, wy2;
   1.109 +  double cx1, cy1, cx2, cy2;
   1.110 +  Gnome::Canvas::Item* pCanvasItem = root();
   1.111 +  pCanvasItem->get_bounds(wx1, wy1, wx2, wy2);
   1.112 +  //std::cout << "root bounds: " << wx1 << " " << wy1 << " " << wx2 << " " << wy2 << std::endl;
   1.113 +  w2c(wx1, wy1, cx1, cy1);
   1.114 +  w2c(wx2, wy2, cx2, cy2);
   1.115 +  //std::cout << "root bounds (c): " << cx1 << " " << cy1 << " " << cx2 << " " << cy2 << std::endl;
   1.116 +  //std::cout << "cx2 - cx1=" << fabs(cx2 - cx1) << " cy2 - cy1=" << fabs(cy2 - cy1) << std::endl;
   1.117 +
   1.118 +  // fit the graph to the window
   1.119 +  double ppu1 = (double) aw / fabs(cx2 - cx1);
   1.120 +  double ppu2 = (double) ah / fabs(cy2 - cy1);
   1.121 +  //std::cout << "ppu1=" << ppu1 << " ppu2=" << ppu2 << std::endl;
   1.122 +  (ppu1 < ppu2) ? set_pixels_per_unit(ppu1) : set_pixels_per_unit(ppu2);
   1.123 +}
   1.124 +
   1.125 +void GraphDisplayerCanvas::zoom100()
   1.126 +{
   1.127 +  set_pixels_per_unit(1.0);
   1.128 +}
   1.129 +
   1.130 +void GraphDisplayerCanvas::updateScrollRegion()
   1.131 +{
   1.132 +  double wx1, wy1, wx2, wy2;
   1.133 +  int cx1, cy1, cx2, cy2;
   1.134 +  Gnome::Canvas::Item* pCanvasItem = root();
   1.135 +  pCanvasItem->get_bounds(wx1, wy1, wx2, wy2);
   1.136 +  w2c(wx1, wy1, cx1, cy1);
   1.137 +  w2c(wx2, wy2, cx2, cy2);
   1.138 +  set_scroll_region(cx1, cy1, cx2, cy2);
   1.139 +}