gui/graph_displayer_canvas.cc
author hegyi
Fri, 27 May 2005 10:34:20 +0000
changeset 1440 3d2e3cfb2a6c
parent 1435 8e85e6bbefdf
child 1441 fd4b6f6d592a
permissions -rwxr-xr-x
Small documentation is added to GUI
ladanyi@1412
     1
#include <graph_displayer_canvas.h>
hegyi@1440
     2
#include <math.h>
ladanyi@1412
     3
ladanyi@1412
     4
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
     5
{
ladanyi@1412
     6
hegyi@1440
     7
  //first edges are drawn, to hide joining with nodes later
hegyi@1440
     8
ladanyi@1412
     9
  for (EdgeIt i(g); i!=INVALID; ++i)
ladanyi@1412
    10
  {
hegyi@1440
    11
hegyi@1440
    12
    //drawing green lines, coordinates are from cm
hegyi@1440
    13
ladanyi@1412
    14
    Gnome::Canvas::Points coos;
ladanyi@1412
    15
    coos.push_back(Gnome::Art::Point(cm[g.source(i)].x,cm[g.source(i)].y));
ladanyi@1412
    16
    coos.push_back(Gnome::Art::Point(cm[g.target(i)].x,cm[g.target(i)].y));
ladanyi@1412
    17
    
ladanyi@1412
    18
    edgesmap[i]=new Gnome::Canvas::Line(displayed_graph, coos);
ladanyi@1412
    19
    *(edgesmap[i]) << Gnome::Canvas::Properties::fill_color("green");
hegyi@1440
    20
    edgesmap[i]->property_width_pixels().set_value(10);    
ladanyi@1412
    21
    
hegyi@1440
    22
    //initializing edge-text as well, to empty string
hegyi@1440
    23
ladanyi@1412
    24
    double x1, x2, y1, y2;
ladanyi@1412
    25
    edgesmap[i]->get_bounds(x1, y1, x2, y2);
ladanyi@1412
    26
    
ladanyi@1412
    27
    edgetextmap[i]=new Gnome::Canvas::Text(displayed_graph,(x1+x2)/2, (y1+y2)/2, "");
ladanyi@1412
    28
    edgetextmap[i]->property_fill_color().set_value("black");
ladanyi@1412
    29
  }
ladanyi@1412
    30
hegyi@1440
    31
  //afterwards nodes come to be drawn
hegyi@1440
    32
ladanyi@1412
    33
  NodeIt i(g);
ladanyi@1412
    34
  int maxx=0, maxy=0, minx=(int)cm[i].x, miny=(int)cm[i].y;
ladanyi@1412
    35
ladanyi@1412
    36
  for (; i!=INVALID; ++i)
ladanyi@1412
    37
  {
hegyi@1440
    38
    //minimum and maximum is gathered to be able to zoom to the graph correctly (whole figure should be seen)
hegyi@1440
    39
ladanyi@1412
    40
    if(cm[i].x>maxx)maxx=(int)cm[i].x;
ladanyi@1412
    41
    if(cm[i].y>maxy)maxy=(int)cm[i].y;
ladanyi@1412
    42
    if(cm[i].x<minx)minx=(int)cm[i].x;
ladanyi@1412
    43
    if(cm[i].y<miny)miny=(int)cm[i].y;
ladanyi@1412
    44
hegyi@1440
    45
    //drawing bule nodes, with black line around them
hegyi@1440
    46
ladanyi@1412
    47
    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
    48
    *(nodesmap[i]) << Gnome::Canvas::Properties::fill_color("blue");
ladanyi@1412
    49
    *(nodesmap[i]) << Gnome::Canvas::Properties::outline_color("black");
ladanyi@1412
    50
    (nodesmap[i])->signal_event().connect(sigc::bind(sigc::mem_fun(*this, &GraphDisplayerCanvas::event_handler),i));
ladanyi@1412
    51
  }
ladanyi@1412
    52
hegyi@1440
    53
  //setting zoom to be able to see the whole graph on the canvas
hegyi@1440
    54
ladanyi@1412
    55
  double biggest_x=(abs(maxx)>abs(minx))?(abs(maxx)+80):(abs(minx)+80);
ladanyi@1412
    56
  double biggest_y=(abs(maxy)>abs(miny))?(abs(maxy)+80):(abs(miny)+80);
ladanyi@1412
    57
ladanyi@1412
    58
  set_pixels_per_unit((biggest_x>biggest_y)?(WIN_WIDTH/biggest_x/2):(WIN_HEIGHT/biggest_y/2));
ladanyi@1412
    59
  std::cout<<abs(maxx)<<" "<<abs(minx)<<" big x "<<biggest_x<<" "<<abs(maxy)<<" "<<abs(miny)<<" big y "<<biggest_y<<std::endl;
ladanyi@1412
    60
  std::cout<<maxx<<" "<<minx<<" big x "<<biggest_x<<" "<<maxy<<" "<<miny<<" big y "<<biggest_y<<std::endl;
ladanyi@1412
    61
  std::cout<<"dx "<<(maxx-minx)<<" dy "<<(maxy-miny)<<" xrate "<<((maxx-minx)/WIN_WIDTH)<<" yrate "<<((maxy-miny)/WIN_HEIGHT)<<std::endl;
ladanyi@1412
    62
ladanyi@1412
    63
}
ladanyi@1412
    64
ladanyi@1412
    65
GraphDisplayerCanvas::~GraphDisplayerCanvas()
ladanyi@1412
    66
{
ladanyi@1412
    67
hegyi@1440
    68
  //writing out the end state of the graph
hegyi@1440
    69
  //\todo all the maps has to be write out!
ladanyi@1412
    70
hegyi@1440
    71
  Graph::NodeMap <int> id(g);
hegyi@1440
    72
  Graph::NodeMap <double> xc(g);
hegyi@1440
    73
  Graph::NodeMap <double> yc(g);
hegyi@1440
    74
  
hegyi@1440
    75
  int j=1;
hegyi@1440
    76
  
hegyi@1440
    77
  for (NodeIt i(g); i!=INVALID; ++i)
hegyi@1440
    78
  {
hegyi@1440
    79
    double x1,y1,x2,y2;
hegyi@1440
    80
    nodesmap[i]->get_bounds(x1, y1, x2, y2);
hegyi@1440
    81
    
hegyi@1440
    82
    id[i]=j++;
hegyi@1440
    83
    xc[i]=(x1+x2)/2;
hegyi@1440
    84
    yc[i]=(y1+y2)/2;
hegyi@1440
    85
  }
ladanyi@1412
    86
hegyi@1440
    87
  GraphWriter<Graph> writer(std::cout,g);
hegyi@1440
    88
  
hegyi@1440
    89
  writer.writeNodeMap("id", id);
hegyi@1440
    90
  writer.writeNodeMap("coordinates_x", xc);
hegyi@1440
    91
  writer.writeNodeMap("coordinates_y", yc);
hegyi@1440
    92
  writer.run();
ladanyi@1412
    93
}
ladanyi@1412
    94
ladanyi@1412
    95
int GraphDisplayerCanvas::changeLineWidth (std::string mapname)
ladanyi@1412
    96
{
ladanyi@1412
    97
  for (EdgeIt i(g); i!=INVALID; ++i)
ladanyi@1412
    98
  {
ladanyi@1412
    99
    int w=(int)(*(mapstorage.edgemap_storage)[mapname])[i];
ladanyi@1412
   100
    edgesmap[i]->property_width_pixels().set_value(w);
ladanyi@1412
   101
  }
ladanyi@1412
   102
  return 0;
ladanyi@1412
   103
};
ladanyi@1412
   104
ladanyi@1412
   105
int GraphDisplayerCanvas::changeColor (std::string mapname)
ladanyi@1412
   106
{  
hegyi@1440
   107
hegyi@1440
   108
  //function maps the range of the maximum and
hegyi@1440
   109
  //the minimum of the nodemap to the range of
hegyi@1440
   110
  //green in RGB
hegyi@1440
   111
ladanyi@1412
   112
  for (EdgeIt i(g); i!=INVALID; ++i)
ladanyi@1412
   113
  {
ladanyi@1412
   114
    double w=(*(mapstorage.edgemap_storage)[mapname])[i];
ladanyi@1412
   115
    double max=mapstorage.maxOfEdgeMap(mapname);
ladanyi@1412
   116
    double min=mapstorage.minOfEdgeMap(mapname);
ladanyi@1412
   117
      
ladanyi@1412
   118
    //std::cout<<w<<" "<<max<<" "<<min<<" "<<100*(w-min)/(max-min)<<std::endl;
ladanyi@1412
   119
    Gdk::Color color;
ladanyi@1412
   120
    if(max!=min)
ladanyi@1412
   121
    {
ladanyi@1412
   122
      color.set_rgb_p (0, 100*(w-min)/(max-min), 0);
ladanyi@1412
   123
    }
ladanyi@1412
   124
    else
ladanyi@1412
   125
    {
ladanyi@1412
   126
      color.set_rgb_p (0, 100, 0);
ladanyi@1412
   127
    }
ladanyi@1412
   128
ladanyi@1412
   129
    edgesmap[i]->property_fill_color_gdk().set_value(color);
ladanyi@1412
   130
  }
ladanyi@1412
   131
  return 0;
ladanyi@1412
   132
};
ladanyi@1412
   133
ladanyi@1412
   134
int GraphDisplayerCanvas::changeText (std::string mapname)
ladanyi@1412
   135
{
hegyi@1440
   136
hegyi@1440
   137
  //the number in the map will be written on the edge
hegyi@1440
   138
  //EXCEPT when the name of the map is Text, because
hegyi@1440
   139
  //in that case empty string will be written, because
hegyi@1440
   140
  //that is the deleter map
hegyi@1440
   141
  //\todo isn't it a bit woodcutter?
hegyi@1440
   142
ladanyi@1412
   143
  for (EdgeIt i(g); i!=INVALID; ++i)
ladanyi@1412
   144
  {
ladanyi@1412
   145
    if(mapname!="Text")
ladanyi@1412
   146
    {
ladanyi@1412
   147
      double number=(*(mapstorage.edgemap_storage)[mapname])[i];
ladanyi@1412
   148
      int length=(int)(floor(log(number)/log(10)))+1;
ladanyi@1412
   149
      int maxpos=(int)(pow(10,length-1));
ladanyi@1412
   150
      int strl=length+1+RANGE;
ladanyi@1412
   151
      char * str=new char[strl];
ladanyi@1412
   152
      str[length]='.';
ladanyi@1412
   153
      str[strl]='\0';
ladanyi@1412
   154
      
ladanyi@1412
   155
      for(int j=0;j<strl;j++)
ladanyi@1412
   156
      {
ladanyi@1412
   157
	if(j!=length)
ladanyi@1412
   158
        {
ladanyi@1412
   159
	  int digit=(int)(number/maxpos);
ladanyi@1412
   160
	  str[j]=(digit+'0');
ladanyi@1412
   161
	  number-=digit*maxpos;
ladanyi@1412
   162
	  number*=10;
ladanyi@1412
   163
        }
ladanyi@1412
   164
      }
ladanyi@1412
   165
      
ladanyi@1412
   166
      edgetextmap[i]->property_text().set_value(str);
ladanyi@1412
   167
    }
ladanyi@1412
   168
    else
ladanyi@1412
   169
    {
ladanyi@1412
   170
      edgetextmap[i]->property_text().set_value("");
ladanyi@1412
   171
    }
ladanyi@1412
   172
  }
ladanyi@1412
   173
  return 0;
ladanyi@1412
   174
};
ladanyi@1412
   175
ladanyi@1412
   176
ladanyi@1412
   177
int GraphDisplayerCanvas::rezoom ()
ladanyi@1412
   178
{
hegyi@1440
   179
hegyi@1440
   180
  //searches for the minimum and the maximum
hegyi@1440
   181
  //value of the coordinates of the nodes to
hegyi@1440
   182
  //set the pixel rpo unit to a value to be 
hegyi@1440
   183
  //able to see the whole graph in the canvas
hegyi@1440
   184
  //\todo does not work properly
hegyi@1440
   185
ladanyi@1412
   186
  double x1, x2, y1, y2;
ladanyi@1412
   187
  int x,y;
ladanyi@1412
   188
ladanyi@1412
   189
  NodeIt i(g);
ladanyi@1412
   190
  nodesmap[i]->get_bounds(x1, y1, x2, y2);
ladanyi@1412
   191
ladanyi@1412
   192
  x=(int)((x1+x2)/2);
ladanyi@1412
   193
  y=(int)((y1+y2)/2);
ladanyi@1412
   194
  
ladanyi@1412
   195
  int maxx=0, maxy=0, minx=(int)x, miny=(int)y;
ladanyi@1412
   196
ladanyi@1412
   197
  for (; i!=INVALID; ++i)
ladanyi@1412
   198
  {
ladanyi@1412
   199
    nodesmap[i]->get_bounds(x1, y1, x2, y2);
ladanyi@1412
   200
ladanyi@1412
   201
    x=(int)((x1+x2)/2);
ladanyi@1412
   202
    y=(int)((y1+y2)/2);
ladanyi@1412
   203
ladanyi@1412
   204
    if(x>maxx)maxx=x;
ladanyi@1412
   205
    if(y>maxy)maxy=y;
ladanyi@1412
   206
    if(x<minx)minx=x;
ladanyi@1412
   207
    if(y<miny)miny=y;
ladanyi@1412
   208
  }
ladanyi@1412
   209
ladanyi@1412
   210
  double biggest_x=(abs(maxx)>abs(minx))?(abs(maxx)+80):(abs(minx)+80);
ladanyi@1412
   211
  double biggest_y=(abs(maxy)>abs(miny))?(abs(maxy)+80):(abs(miny)+80);
ladanyi@1412
   212
ladanyi@1412
   213
  set_pixels_per_unit((biggest_x-WIN_WIDTH>biggest_y-WIN_HEIGHT)?(WIN_WIDTH/biggest_x/2):(WIN_HEIGHT/biggest_y/2));
ladanyi@1412
   214
  return 0;
ladanyi@1412
   215
};
ladanyi@1412
   216
ladanyi@1412
   217
ladanyi@1412
   218
bool GraphDisplayerCanvas::event_handler(GdkEvent* e, Node n)
ladanyi@1412
   219
{
ladanyi@1412
   220
  switch(e->type)
ladanyi@1412
   221
  {
ladanyi@1412
   222
    case GDK_BUTTON_PRESS:
hegyi@1440
   223
      //we mark the location of the event to be able to calculate parameters of dragging
ladanyi@1412
   224
      clicked_x=e->button.x;
ladanyi@1412
   225
      clicked_y=e->button.y;
ladanyi@1412
   226
      active_item=(get_item_at(e->button.x, e->button.y));
ladanyi@1412
   227
      isbutton=true;
ladanyi@1412
   228
      break;
ladanyi@1412
   229
    case GDK_BUTTON_RELEASE:
ladanyi@1412
   230
      isbutton=false;
ladanyi@1412
   231
      active_item=NULL;
ladanyi@1412
   232
      break;
ladanyi@1412
   233
    case GDK_MOTION_NOTIFY:
hegyi@1440
   234
      //we only have to do sg. if the mouse button is pressed
ladanyi@1412
   235
      if(isbutton)
ladanyi@1412
   236
      {
hegyi@1440
   237
	//new coordinates will be the old values,
hegyi@1440
   238
	//because the item will be moved to the
hegyi@1440
   239
	//new coordinate therefore the new movement
hegyi@1440
   240
	//has to be calculated from here
hegyi@1440
   241
ladanyi@1412
   242
        double dx=e->motion.x-clicked_x;
ladanyi@1412
   243
        double dy=e->motion.y-clicked_y;
ladanyi@1412
   244
        active_item->move(dx, dy);
ladanyi@1412
   245
        clicked_x=e->motion.x;
ladanyi@1412
   246
        clicked_y=e->motion.y;
ladanyi@1412
   247
hegyi@1440
   248
	//all the edges connected to the moved point has to be redrawn
hegyi@1440
   249
ladanyi@1412
   250
        EdgeIt e;
ladanyi@1412
   251
        g.firstOut(e,n);
ladanyi@1412
   252
        for(;e!=INVALID;g.nextOut(e))
ladanyi@1412
   253
        {
ladanyi@1412
   254
            Gnome::Canvas::Points coos;
ladanyi@1412
   255
            double x1, x2, y1, y2;
ladanyi@1412
   256
ladanyi@1412
   257
            nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2);
ladanyi@1412
   258
            coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
ladanyi@1412
   259
ladanyi@1412
   260
            nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2);
ladanyi@1412
   261
            coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
ladanyi@1412
   262
ladanyi@1412
   263
            edgesmap[e]->property_points().set_value(coos);
ladanyi@1412
   264
ladanyi@1412
   265
	    edgesmap[e]->get_bounds(x1, y1, x2, y2);
ladanyi@1412
   266
ladanyi@1412
   267
	    edgetextmap[e]->property_x().set_value((x1+x2)/2);
ladanyi@1412
   268
	    edgetextmap[e]->property_y().set_value((y1+y2)/2);
ladanyi@1412
   269
        }
ladanyi@1412
   270
ladanyi@1412
   271
        g.firstIn(e,n);
ladanyi@1412
   272
        for(;e!=INVALID;g.nextIn(e))
ladanyi@1412
   273
        {
ladanyi@1412
   274
            Gnome::Canvas::Points coos;
ladanyi@1412
   275
            double x1, x2, y1, y2;
ladanyi@1412
   276
ladanyi@1412
   277
            nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2);
ladanyi@1412
   278
            coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
ladanyi@1412
   279
ladanyi@1412
   280
            nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2);
ladanyi@1412
   281
            coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
ladanyi@1412
   282
ladanyi@1412
   283
            edgesmap[e]->property_points().set_value(coos);
ladanyi@1412
   284
ladanyi@1412
   285
	    edgesmap[e]->get_bounds(x1, y1, x2, y2);
ladanyi@1412
   286
ladanyi@1412
   287
	    edgetextmap[e]->property_x().set_value((x1+x2)/2);
ladanyi@1412
   288
	    edgetextmap[e]->property_y().set_value((y1+y2)/2);
ladanyi@1412
   289
        }
ladanyi@1412
   290
      }
ladanyi@1412
   291
    default: break;
ladanyi@1412
   292
  }
ladanyi@1412
   293
  return true;
ladanyi@1412
   294
}
ladanyi@1412
   295
ladanyi@1412
   296
bool GraphDisplayerCanvas::on_expose_event(GdkEventExpose *event)
ladanyi@1412
   297
{
ladanyi@1412
   298
  Gnome::Canvas::CanvasAA::on_expose_event(event);
ladanyi@1412
   299
  //usleep(10000);
ladanyi@1412
   300
  //rezoom();
ladanyi@1412
   301
  return true;
ladanyi@1412
   302
}