src/gui/graph_displayer_canvas.cc
author deba
Wed, 18 May 2005 13:02:47 +0000
changeset 1427 14c75970840e
permissions -rwxr-xr-x
Two minor changes.

DefaultReader<std::string>
comment lines
ladanyi@1412
     1
#include <graph_displayer_canvas.h>
ladanyi@1412
     2
ladanyi@1412
     3
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)
ladanyi@1412
     4
{
ladanyi@1412
     5
ladanyi@1412
     6
  for (EdgeIt i(g); i!=INVALID; ++i)
ladanyi@1412
     7
  {
ladanyi@1412
     8
    Gnome::Canvas::Points coos;
ladanyi@1412
     9
    coos.push_back(Gnome::Art::Point(cm[g.source(i)].x,cm[g.source(i)].y));
ladanyi@1412
    10
    coos.push_back(Gnome::Art::Point(cm[g.target(i)].x,cm[g.target(i)].y));
ladanyi@1412
    11
    
ladanyi@1412
    12
    edgesmap[i]=new Gnome::Canvas::Line(displayed_graph, coos);
ladanyi@1412
    13
    *(edgesmap[i]) << Gnome::Canvas::Properties::fill_color("green");
ladanyi@1412
    14
    edgesmap[i]->property_width_pixels().set_value(10);
ladanyi@1412
    15
    
ladanyi@1412
    16
    
ladanyi@1412
    17
    double x1, x2, y1, y2;
ladanyi@1412
    18
    edgesmap[i]->get_bounds(x1, y1, x2, y2);
ladanyi@1412
    19
    
ladanyi@1412
    20
    edgetextmap[i]=new Gnome::Canvas::Text(displayed_graph,(x1+x2)/2, (y1+y2)/2, "");
ladanyi@1412
    21
    edgetextmap[i]->property_fill_color().set_value("black");
ladanyi@1412
    22
  }
ladanyi@1412
    23
ladanyi@1412
    24
  NodeIt i(g);
ladanyi@1412
    25
  int maxx=0, maxy=0, minx=(int)cm[i].x, miny=(int)cm[i].y;
ladanyi@1412
    26
ladanyi@1412
    27
  for (; i!=INVALID; ++i)
ladanyi@1412
    28
  {
ladanyi@1412
    29
    if(cm[i].x>maxx)maxx=(int)cm[i].x;
ladanyi@1412
    30
    if(cm[i].y>maxy)maxy=(int)cm[i].y;
ladanyi@1412
    31
    if(cm[i].x<minx)minx=(int)cm[i].x;
ladanyi@1412
    32
    if(cm[i].y<miny)miny=(int)cm[i].y;
ladanyi@1412
    33
ladanyi@1412
    34
    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@1412
    35
    *(nodesmap[i]) << Gnome::Canvas::Properties::fill_color("blue");
ladanyi@1412
    36
    *(nodesmap[i]) << Gnome::Canvas::Properties::outline_color("black");
ladanyi@1412
    37
    (nodesmap[i])->signal_event().connect(sigc::bind(sigc::mem_fun(*this, &GraphDisplayerCanvas::event_handler),i));
ladanyi@1412
    38
  }
ladanyi@1412
    39
ladanyi@1412
    40
  double biggest_x=(abs(maxx)>abs(minx))?(abs(maxx)+80):(abs(minx)+80);
ladanyi@1412
    41
  double biggest_y=(abs(maxy)>abs(miny))?(abs(maxy)+80):(abs(miny)+80);
ladanyi@1412
    42
ladanyi@1412
    43
  set_pixels_per_unit((biggest_x>biggest_y)?(WIN_WIDTH/biggest_x/2):(WIN_HEIGHT/biggest_y/2));
ladanyi@1412
    44
  std::cout<<abs(maxx)<<" "<<abs(minx)<<" big x "<<biggest_x<<" "<<abs(maxy)<<" "<<abs(miny)<<" big y "<<biggest_y<<std::endl;
ladanyi@1412
    45
  std::cout<<maxx<<" "<<minx<<" big x "<<biggest_x<<" "<<maxy<<" "<<miny<<" big y "<<biggest_y<<std::endl;
ladanyi@1412
    46
  std::cout<<"dx "<<(maxx-minx)<<" dy "<<(maxy-miny)<<" xrate "<<((maxx-minx)/WIN_WIDTH)<<" yrate "<<((maxy-miny)/WIN_HEIGHT)<<std::endl;
ladanyi@1412
    47
ladanyi@1412
    48
}
ladanyi@1412
    49
ladanyi@1412
    50
GraphDisplayerCanvas::~GraphDisplayerCanvas()
ladanyi@1412
    51
{
ladanyi@1412
    52
    Graph::NodeMap <int> id(g);
ladanyi@1412
    53
    Graph::NodeMap <double> xc(g);
ladanyi@1412
    54
    Graph::NodeMap <double> yc(g);
ladanyi@1412
    55
ladanyi@1412
    56
    int j=1;
ladanyi@1412
    57
ladanyi@1412
    58
    for (NodeIt i(g); i!=INVALID; ++i)
ladanyi@1412
    59
    {
ladanyi@1412
    60
        double x1,y1,x2,y2;
ladanyi@1412
    61
        nodesmap[i]->get_bounds(x1, y1, x2, y2);
ladanyi@1412
    62
ladanyi@1412
    63
        id[i]=j++;
ladanyi@1412
    64
        xc[i]=(x1+x2)/2;
ladanyi@1412
    65
        yc[i]=(y1+y2)/2;
ladanyi@1412
    66
    }
ladanyi@1412
    67
ladanyi@1412
    68
    GraphWriter<Graph> writer(std::cout,g);
ladanyi@1412
    69
ladanyi@1412
    70
    writer.writeNodeMap("id", id);
ladanyi@1412
    71
    writer.writeNodeMap("coordinates_x", xc);
ladanyi@1412
    72
    writer.writeNodeMap("coordinates_y", yc);
ladanyi@1412
    73
    writer.run();
ladanyi@1412
    74
}
ladanyi@1412
    75
ladanyi@1412
    76
int GraphDisplayerCanvas::changeLineWidth (std::string mapname)
ladanyi@1412
    77
{
ladanyi@1412
    78
  for (EdgeIt i(g); i!=INVALID; ++i)
ladanyi@1412
    79
  {
ladanyi@1412
    80
    int w=(int)(*(mapstorage.edgemap_storage)[mapname])[i];
ladanyi@1412
    81
    edgesmap[i]->property_width_pixels().set_value(w);
ladanyi@1412
    82
  }
ladanyi@1412
    83
  return 0;
ladanyi@1412
    84
};
ladanyi@1412
    85
ladanyi@1412
    86
int GraphDisplayerCanvas::changeColor (std::string mapname)
ladanyi@1412
    87
{  
ladanyi@1412
    88
  for (EdgeIt i(g); i!=INVALID; ++i)
ladanyi@1412
    89
  {
ladanyi@1412
    90
    double w=(*(mapstorage.edgemap_storage)[mapname])[i];
ladanyi@1412
    91
    double max=mapstorage.maxOfEdgeMap(mapname);
ladanyi@1412
    92
    double min=mapstorage.minOfEdgeMap(mapname);
ladanyi@1412
    93
      
ladanyi@1412
    94
    //std::cout<<w<<" "<<max<<" "<<min<<" "<<100*(w-min)/(max-min)<<std::endl;
ladanyi@1412
    95
    Gdk::Color color;
ladanyi@1412
    96
    if(max!=min)
ladanyi@1412
    97
    {
ladanyi@1412
    98
      color.set_rgb_p (0, 100*(w-min)/(max-min), 0);
ladanyi@1412
    99
    }
ladanyi@1412
   100
    else
ladanyi@1412
   101
    {
ladanyi@1412
   102
      color.set_rgb_p (0, 100, 0);
ladanyi@1412
   103
    }
ladanyi@1412
   104
ladanyi@1412
   105
    edgesmap[i]->property_fill_color_gdk().set_value(color);
ladanyi@1412
   106
  }
ladanyi@1412
   107
  return 0;
ladanyi@1412
   108
};
ladanyi@1412
   109
ladanyi@1412
   110
int GraphDisplayerCanvas::changeText (std::string mapname)
ladanyi@1412
   111
{
ladanyi@1412
   112
  for (EdgeIt i(g); i!=INVALID; ++i)
ladanyi@1412
   113
  {
ladanyi@1412
   114
    if(mapname!="Text")
ladanyi@1412
   115
    {
ladanyi@1412
   116
      double number=(*(mapstorage.edgemap_storage)[mapname])[i];
ladanyi@1412
   117
      int length=(int)(floor(log(number)/log(10)))+1;
ladanyi@1412
   118
      int maxpos=(int)(pow(10,length-1));
ladanyi@1412
   119
      int strl=length+1+RANGE;
ladanyi@1412
   120
      char * str=new char[strl];
ladanyi@1412
   121
      str[length]='.';
ladanyi@1412
   122
      str[strl]='\0';
ladanyi@1412
   123
      
ladanyi@1412
   124
      for(int j=0;j<strl;j++)
ladanyi@1412
   125
      {
ladanyi@1412
   126
	if(j!=length)
ladanyi@1412
   127
        {
ladanyi@1412
   128
	  int digit=(int)(number/maxpos);
ladanyi@1412
   129
	  str[j]=(digit+'0');
ladanyi@1412
   130
	  number-=digit*maxpos;
ladanyi@1412
   131
	  number*=10;
ladanyi@1412
   132
        }
ladanyi@1412
   133
      }
ladanyi@1412
   134
      
ladanyi@1412
   135
      edgetextmap[i]->property_text().set_value(str);
ladanyi@1412
   136
    }
ladanyi@1412
   137
    else
ladanyi@1412
   138
    {
ladanyi@1412
   139
      edgetextmap[i]->property_text().set_value("");
ladanyi@1412
   140
    }
ladanyi@1412
   141
  }
ladanyi@1412
   142
  return 0;
ladanyi@1412
   143
};
ladanyi@1412
   144
ladanyi@1412
   145
ladanyi@1412
   146
int GraphDisplayerCanvas::rezoom ()
ladanyi@1412
   147
{
ladanyi@1412
   148
  double x1, x2, y1, y2;
ladanyi@1412
   149
  int x,y;
ladanyi@1412
   150
ladanyi@1412
   151
  NodeIt i(g);
ladanyi@1412
   152
  nodesmap[i]->get_bounds(x1, y1, x2, y2);
ladanyi@1412
   153
ladanyi@1412
   154
  x=(int)((x1+x2)/2);
ladanyi@1412
   155
  y=(int)((y1+y2)/2);
ladanyi@1412
   156
  
ladanyi@1412
   157
  int maxx=0, maxy=0, minx=(int)x, miny=(int)y;
ladanyi@1412
   158
ladanyi@1412
   159
  for (; i!=INVALID; ++i)
ladanyi@1412
   160
  {
ladanyi@1412
   161
    nodesmap[i]->get_bounds(x1, y1, x2, y2);
ladanyi@1412
   162
ladanyi@1412
   163
    x=(int)((x1+x2)/2);
ladanyi@1412
   164
    y=(int)((y1+y2)/2);
ladanyi@1412
   165
ladanyi@1412
   166
    if(x>maxx)maxx=x;
ladanyi@1412
   167
    if(y>maxy)maxy=y;
ladanyi@1412
   168
    if(x<minx)minx=x;
ladanyi@1412
   169
    if(y<miny)miny=y;
ladanyi@1412
   170
  }
ladanyi@1412
   171
ladanyi@1412
   172
  double biggest_x=(abs(maxx)>abs(minx))?(abs(maxx)+80):(abs(minx)+80);
ladanyi@1412
   173
  double biggest_y=(abs(maxy)>abs(miny))?(abs(maxy)+80):(abs(miny)+80);
ladanyi@1412
   174
ladanyi@1412
   175
  set_pixels_per_unit((biggest_x-WIN_WIDTH>biggest_y-WIN_HEIGHT)?(WIN_WIDTH/biggest_x/2):(WIN_HEIGHT/biggest_y/2));
ladanyi@1412
   176
  return 0;
ladanyi@1412
   177
};
ladanyi@1412
   178
ladanyi@1412
   179
ladanyi@1412
   180
///This function moves only one node of displayed_graph,
ladanyi@1412
   181
///but recalculate the location of weight point,
ladanyi@1412
   182
///and also redraw the sides of the planefigure.
ladanyi@1412
   183
bool GraphDisplayerCanvas::event_handler(GdkEvent* e, Node n)
ladanyi@1412
   184
{
ladanyi@1412
   185
  switch(e->type)
ladanyi@1412
   186
  {
ladanyi@1412
   187
    case GDK_BUTTON_PRESS:
ladanyi@1412
   188
      clicked_x=e->button.x;
ladanyi@1412
   189
      clicked_y=e->button.y;
ladanyi@1412
   190
      active_item=(get_item_at(e->button.x, e->button.y));
ladanyi@1412
   191
      isbutton=true;
ladanyi@1412
   192
      break;
ladanyi@1412
   193
    case GDK_BUTTON_RELEASE:
ladanyi@1412
   194
      isbutton=false;
ladanyi@1412
   195
      active_item=NULL;
ladanyi@1412
   196
      break;
ladanyi@1412
   197
    case GDK_MOTION_NOTIFY:
ladanyi@1412
   198
      if(isbutton)
ladanyi@1412
   199
      {
ladanyi@1412
   200
        double dx=e->motion.x-clicked_x;
ladanyi@1412
   201
        double dy=e->motion.y-clicked_y;
ladanyi@1412
   202
        active_item->move(dx, dy);
ladanyi@1412
   203
        clicked_x=e->motion.x;
ladanyi@1412
   204
        clicked_y=e->motion.y;
ladanyi@1412
   205
ladanyi@1412
   206
        EdgeIt e;
ladanyi@1412
   207
ladanyi@1412
   208
        g.firstOut(e,n);
ladanyi@1412
   209
        for(;e!=INVALID;g.nextOut(e))
ladanyi@1412
   210
        {
ladanyi@1412
   211
            Gnome::Canvas::Points coos;
ladanyi@1412
   212
            double x1, x2, y1, y2;
ladanyi@1412
   213
ladanyi@1412
   214
            nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2);
ladanyi@1412
   215
            coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
ladanyi@1412
   216
ladanyi@1412
   217
            nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2);
ladanyi@1412
   218
            coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
ladanyi@1412
   219
ladanyi@1412
   220
            edgesmap[e]->property_points().set_value(coos);
ladanyi@1412
   221
ladanyi@1412
   222
	    edgesmap[e]->get_bounds(x1, y1, x2, y2);
ladanyi@1412
   223
ladanyi@1412
   224
	    edgetextmap[e]->property_x().set_value((x1+x2)/2);
ladanyi@1412
   225
	    edgetextmap[e]->property_y().set_value((y1+y2)/2);
ladanyi@1412
   226
        }
ladanyi@1412
   227
ladanyi@1412
   228
        g.firstIn(e,n);
ladanyi@1412
   229
        for(;e!=INVALID;g.nextIn(e))
ladanyi@1412
   230
        {
ladanyi@1412
   231
            Gnome::Canvas::Points coos;
ladanyi@1412
   232
            double x1, x2, y1, y2;
ladanyi@1412
   233
ladanyi@1412
   234
            nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2);
ladanyi@1412
   235
            coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
ladanyi@1412
   236
ladanyi@1412
   237
            nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2);
ladanyi@1412
   238
            coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
ladanyi@1412
   239
ladanyi@1412
   240
            edgesmap[e]->property_points().set_value(coos);
ladanyi@1412
   241
ladanyi@1412
   242
	    edgesmap[e]->get_bounds(x1, y1, x2, y2);
ladanyi@1412
   243
ladanyi@1412
   244
	    edgetextmap[e]->property_x().set_value((x1+x2)/2);
ladanyi@1412
   245
	    edgetextmap[e]->property_y().set_value((y1+y2)/2);
ladanyi@1412
   246
        }
ladanyi@1412
   247
      }
ladanyi@1412
   248
    default: break;
ladanyi@1412
   249
  }
ladanyi@1412
   250
  return true;
ladanyi@1412
   251
}
ladanyi@1412
   252
ladanyi@1412
   253
bool GraphDisplayerCanvas::on_expose_event(GdkEventExpose *event)
ladanyi@1412
   254
{
ladanyi@1412
   255
  Gnome::Canvas::CanvasAA::on_expose_event(event);
ladanyi@1412
   256
  //usleep(10000);
ladanyi@1412
   257
  //rezoom();
ladanyi@1412
   258
  return true;
ladanyi@1412
   259
}