graph_displayer_canvas.cc
author hegyi
Fri, 17 Jun 2005 14:53:28 +0000
branchgui
changeset 23 d0e4ac77bafe
parent 20 a3bd39d50930
child 25 c45a34eaa118
permissions -rwxr-xr-x
Little beauty fault is corrected.
ladanyi@6
     1
#include <graph_displayer_canvas.h>
hegyi@17
     2
#include <broken_edge.h>
ladanyi@6
     3
#include <math.h>
ladanyi@6
     4
hegyi@20
     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(0),active_item(NULL),target_item(NULL)
ladanyi@6
     6
{
hegyi@9
     7
  
hegyi@21
     8
  actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::create_node_event_handler), false);
hegyi@21
     9
  actual_tool=CREATE_NODE;
hegyi@9
    10
hegyi@12
    11
  active_node=INVALID;
hegyi@12
    12
  active_edge=INVALID;
hegyi@12
    13
ladanyi@6
    14
  //set_center_scroll_region(true);
ladanyi@6
    15
ladanyi@6
    16
  //first edges are drawn, to hide joining with nodes later
ladanyi@6
    17
ladanyi@6
    18
  for (EdgeIt i(g); i!=INVALID; ++i)
ladanyi@6
    19
  {
ladanyi@6
    20
ladanyi@6
    21
    //drawing green lines, coordinates are from cm
ladanyi@6
    22
ladanyi@6
    23
    Gnome::Canvas::Points coos;
ladanyi@6
    24
    coos.push_back(Gnome::Art::Point(cm[g.source(i)].x,cm[g.source(i)].y));
ladanyi@6
    25
    coos.push_back(Gnome::Art::Point(cm[g.target(i)].x,cm[g.target(i)].y));
ladanyi@6
    26
    
hegyi@21
    27
    edgesmap[i]=new BrokenEdge(displayed_graph, coos, *this);
ladanyi@6
    28
    *(edgesmap[i]) << Gnome::Canvas::Properties::fill_color("green");
ladanyi@6
    29
    edgesmap[i]->property_width_pixels().set_value(10);    
ladanyi@6
    30
    
ladanyi@6
    31
    //initializing edge-text as well, to empty string
ladanyi@6
    32
ladanyi@6
    33
    double x1, x2, y1, y2;
ladanyi@6
    34
    edgesmap[i]->get_bounds(x1, y1, x2, y2);
ladanyi@6
    35
    
ladanyi@6
    36
    edgetextmap[i]=new Gnome::Canvas::Text(displayed_graph,(x1+x2)/2, (y1+y2)/2, "");
ladanyi@6
    37
    edgetextmap[i]->property_fill_color().set_value("black");
ladanyi@6
    38
  }
ladanyi@6
    39
ladanyi@6
    40
  //afterwards nodes come to be drawn
ladanyi@6
    41
ladanyi@6
    42
  NodeIt i(g);
ladanyi@6
    43
  int maxx=0, maxy=0, minx=(int)cm[i].x, miny=(int)cm[i].y;
ladanyi@6
    44
ladanyi@6
    45
  for (; i!=INVALID; ++i)
ladanyi@6
    46
  {
ladanyi@6
    47
    //minimum and maximum is gathered to be able to zoom to the graph correctly (whole figure should be seen)
ladanyi@6
    48
ladanyi@6
    49
    if(cm[i].x>maxx)maxx=(int)cm[i].x;
ladanyi@6
    50
    if(cm[i].y>maxy)maxy=(int)cm[i].y;
ladanyi@6
    51
    if(cm[i].x<minx)minx=(int)cm[i].x;
ladanyi@6
    52
    if(cm[i].y<miny)miny=(int)cm[i].y;
ladanyi@6
    53
ladanyi@6
    54
    //drawing bule nodes, with black line around them
ladanyi@6
    55
ladanyi@6
    56
    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@6
    57
    *(nodesmap[i]) << Gnome::Canvas::Properties::fill_color("blue");
ladanyi@6
    58
    *(nodesmap[i]) << Gnome::Canvas::Properties::outline_color("black");
hegyi@9
    59
    //!!!!!!! (nodesmap[i])->signal_event().connect(sigc::bind(sigc::mem_fun(*this, &GraphDisplayerCanvas::event_handler),i));
ladanyi@6
    60
  }
ladanyi@6
    61
ladanyi@6
    62
  updateScrollRegion();
ladanyi@6
    63
}
ladanyi@6
    64
ladanyi@6
    65
GraphDisplayerCanvas::~GraphDisplayerCanvas()
ladanyi@6
    66
{
ladanyi@6
    67
ladanyi@6
    68
  //writing out the end state of the graph
ladanyi@6
    69
  //\todo all the maps has to be write out!
ladanyi@6
    70
ladanyi@6
    71
  Graph::NodeMap <int> id(g);
ladanyi@6
    72
  Graph::NodeMap <double> xc(g);
ladanyi@6
    73
  Graph::NodeMap <double> yc(g);
ladanyi@6
    74
  
ladanyi@6
    75
  int j=1;
ladanyi@6
    76
  
ladanyi@6
    77
  for (NodeIt i(g); i!=INVALID; ++i)
ladanyi@6
    78
  {
ladanyi@6
    79
    double x1,y1,x2,y2;
ladanyi@6
    80
    nodesmap[i]->get_bounds(x1, y1, x2, y2);
ladanyi@6
    81
    
ladanyi@6
    82
    id[i]=j++;
ladanyi@6
    83
    xc[i]=(x1+x2)/2;
ladanyi@6
    84
    yc[i]=(y1+y2)/2;
ladanyi@6
    85
  }
ladanyi@6
    86
ladanyi@6
    87
  GraphWriter<Graph> writer(std::cout,g);
ladanyi@6
    88
  
ladanyi@6
    89
  writer.writeNodeMap("id", id);
ladanyi@6
    90
  writer.writeNodeMap("coordinates_x", xc);
ladanyi@6
    91
  writer.writeNodeMap("coordinates_y", yc);
ladanyi@6
    92
  writer.run();
ladanyi@6
    93
}
ladanyi@6
    94
ladanyi@6
    95
int GraphDisplayerCanvas::changeLineWidth (std::string mapname)
ladanyi@6
    96
{
ladanyi@6
    97
  for (EdgeIt i(g); i!=INVALID; ++i)
ladanyi@6
    98
  {
ladanyi@6
    99
    int w=(int)(*(mapstorage.edgemap_storage)[mapname])[i];
ladanyi@6
   100
    edgesmap[i]->property_width_pixels().set_value(w);
ladanyi@6
   101
  }
ladanyi@6
   102
  return 0;
ladanyi@6
   103
};
ladanyi@6
   104
ladanyi@6
   105
int GraphDisplayerCanvas::changeColor (std::string mapname)
ladanyi@6
   106
{  
ladanyi@6
   107
ladanyi@6
   108
  //function maps the range of the maximum and
ladanyi@6
   109
  //the minimum of the nodemap to the range of
ladanyi@6
   110
  //green in RGB
ladanyi@6
   111
ladanyi@6
   112
  for (EdgeIt i(g); i!=INVALID; ++i)
ladanyi@6
   113
  {
ladanyi@6
   114
    double w=(*(mapstorage.edgemap_storage)[mapname])[i];
ladanyi@6
   115
    double max=mapstorage.maxOfEdgeMap(mapname);
ladanyi@6
   116
    double min=mapstorage.minOfEdgeMap(mapname);
ladanyi@6
   117
      
ladanyi@6
   118
    //std::cout<<w<<" "<<max<<" "<<min<<" "<<100*(w-min)/(max-min)<<std::endl;
ladanyi@6
   119
    Gdk::Color color;
ladanyi@6
   120
    if(max!=min)
ladanyi@6
   121
    {
ladanyi@6
   122
      color.set_rgb_p (0, 100*(w-min)/(max-min), 0);
ladanyi@6
   123
    }
ladanyi@6
   124
    else
ladanyi@6
   125
    {
ladanyi@6
   126
      color.set_rgb_p (0, 100, 0);
ladanyi@6
   127
    }
ladanyi@6
   128
ladanyi@6
   129
    edgesmap[i]->property_fill_color_gdk().set_value(color);
ladanyi@6
   130
  }
ladanyi@6
   131
  return 0;
ladanyi@6
   132
};
ladanyi@6
   133
ladanyi@6
   134
int GraphDisplayerCanvas::changeText (std::string mapname)
ladanyi@6
   135
{
ladanyi@6
   136
ladanyi@6
   137
  //the number in the map will be written on the edge
ladanyi@6
   138
  //EXCEPT when the name of the map is Text, because
ladanyi@6
   139
  //in that case empty string will be written, because
ladanyi@6
   140
  //that is the deleter map
ladanyi@6
   141
  //\todo isn't it a bit woodcutter?
ladanyi@6
   142
ladanyi@6
   143
  for (EdgeIt i(g); i!=INVALID; ++i)
ladanyi@6
   144
  {
ladanyi@6
   145
    if(mapname!="Text")
ladanyi@6
   146
    {
ladanyi@6
   147
      double number=(*(mapstorage.edgemap_storage)[mapname])[i];
ladanyi@6
   148
      int length=(int)(floor(log(number)/log(10)))+1;
ladanyi@6
   149
      int maxpos=(int)(pow(10,length-1));
ladanyi@6
   150
      int strl=length+1+RANGE;
ladanyi@6
   151
      char * str=new char[strl];
ladanyi@6
   152
      str[length]='.';
ladanyi@6
   153
      str[strl]='\0';
ladanyi@6
   154
      
ladanyi@6
   155
      for(int j=0;j<strl;j++)
ladanyi@6
   156
      {
ladanyi@6
   157
	if(j!=length)
ladanyi@6
   158
        {
ladanyi@6
   159
	  int digit=(int)(number/maxpos);
ladanyi@6
   160
	  str[j]=(digit+'0');
ladanyi@6
   161
	  number-=digit*maxpos;
ladanyi@6
   162
	  number*=10;
ladanyi@6
   163
        }
ladanyi@6
   164
      }
ladanyi@6
   165
      
ladanyi@6
   166
      edgetextmap[i]->property_text().set_value(str);
ladanyi@6
   167
    }
ladanyi@6
   168
    else
ladanyi@6
   169
    {
ladanyi@6
   170
      edgetextmap[i]->property_text().set_value("");
ladanyi@6
   171
    }
ladanyi@6
   172
  }
ladanyi@6
   173
  return 0;
ladanyi@6
   174
};
ladanyi@6
   175
ladanyi@6
   176
bool GraphDisplayerCanvas::event_handler(GdkEvent* e, Node n)
ladanyi@6
   177
{
ladanyi@6
   178
  switch(e->type)
ladanyi@6
   179
  {
ladanyi@6
   180
    case GDK_BUTTON_PRESS:
ladanyi@6
   181
      //we mark the location of the event to be able to calculate parameters of dragging
ladanyi@6
   182
      clicked_x=e->button.x;
ladanyi@6
   183
      clicked_y=e->button.y;
ladanyi@6
   184
      active_item=(get_item_at(e->button.x, e->button.y));
hegyi@20
   185
      isbutton=1;
ladanyi@6
   186
      break;
ladanyi@6
   187
    case GDK_BUTTON_RELEASE:
hegyi@20
   188
      isbutton=0;
ladanyi@6
   189
      active_item=NULL;
ladanyi@7
   190
      updateScrollRegion();
ladanyi@6
   191
      break;
ladanyi@6
   192
    case GDK_MOTION_NOTIFY:
ladanyi@6
   193
      //we only have to do sg. if the mouse button is pressed
ladanyi@6
   194
      if(isbutton)
ladanyi@6
   195
      {
ladanyi@6
   196
	//new coordinates will be the old values,
ladanyi@6
   197
	//because the item will be moved to the
ladanyi@6
   198
	//new coordinate therefore the new movement
ladanyi@6
   199
	//has to be calculated from here
ladanyi@6
   200
ladanyi@6
   201
        double dx=e->motion.x-clicked_x;
ladanyi@6
   202
        double dy=e->motion.y-clicked_y;
ladanyi@6
   203
        active_item->move(dx, dy);
ladanyi@6
   204
        clicked_x=e->motion.x;
ladanyi@6
   205
        clicked_y=e->motion.y;
ladanyi@6
   206
ladanyi@6
   207
	//all the edges connected to the moved point has to be redrawn
ladanyi@6
   208
ladanyi@6
   209
        EdgeIt e;
ladanyi@6
   210
        g.firstOut(e,n);
ladanyi@6
   211
        for(;e!=INVALID;g.nextOut(e))
ladanyi@6
   212
        {
ladanyi@6
   213
            Gnome::Canvas::Points coos;
ladanyi@6
   214
            double x1, x2, y1, y2;
ladanyi@6
   215
ladanyi@6
   216
            nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2);
ladanyi@6
   217
            coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
ladanyi@6
   218
ladanyi@6
   219
            nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2);
ladanyi@6
   220
            coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
ladanyi@6
   221
ladanyi@6
   222
            edgesmap[e]->property_points().set_value(coos);
ladanyi@6
   223
ladanyi@6
   224
	    edgesmap[e]->get_bounds(x1, y1, x2, y2);
ladanyi@6
   225
ladanyi@6
   226
	    edgetextmap[e]->property_x().set_value((x1+x2)/2);
ladanyi@6
   227
	    edgetextmap[e]->property_y().set_value((y1+y2)/2);
ladanyi@6
   228
        }
ladanyi@6
   229
ladanyi@6
   230
        g.firstIn(e,n);
ladanyi@6
   231
        for(;e!=INVALID;g.nextIn(e))
ladanyi@6
   232
        {
ladanyi@6
   233
            Gnome::Canvas::Points coos;
ladanyi@6
   234
            double x1, x2, y1, y2;
ladanyi@6
   235
ladanyi@6
   236
            nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2);
ladanyi@6
   237
            coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
ladanyi@6
   238
ladanyi@6
   239
            nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2);
ladanyi@6
   240
            coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
ladanyi@6
   241
ladanyi@6
   242
            edgesmap[e]->property_points().set_value(coos);
ladanyi@6
   243
ladanyi@6
   244
	    edgesmap[e]->get_bounds(x1, y1, x2, y2);
ladanyi@6
   245
ladanyi@6
   246
	    edgetextmap[e]->property_x().set_value((x1+x2)/2);
ladanyi@6
   247
	    edgetextmap[e]->property_y().set_value((y1+y2)/2);
ladanyi@6
   248
        }
ladanyi@6
   249
      }
ladanyi@6
   250
    default: break;
ladanyi@6
   251
  }
ladanyi@6
   252
  return true;
ladanyi@6
   253
}
ladanyi@6
   254
ladanyi@6
   255
bool GraphDisplayerCanvas::on_expose_event(GdkEventExpose *event)
ladanyi@6
   256
{
ladanyi@6
   257
  Gnome::Canvas::CanvasAA::on_expose_event(event);
ladanyi@6
   258
  //usleep(10000);
ladanyi@6
   259
  //rezoom();
ladanyi@6
   260
  return true;
ladanyi@6
   261
}
ladanyi@6
   262
ladanyi@6
   263
void GraphDisplayerCanvas::zoomIn()
ladanyi@6
   264
{
ladanyi@6
   265
  set_pixels_per_unit(
ladanyi@6
   266
      (1.0 + (double) zoom_step / 100.0) * get_pixels_per_unit());
ladanyi@6
   267
}
ladanyi@6
   268
ladanyi@6
   269
void GraphDisplayerCanvas::zoomOut()
ladanyi@6
   270
{
ladanyi@6
   271
  set_pixels_per_unit(
ladanyi@6
   272
      (1.0 - (double) zoom_step / 100.0) * get_pixels_per_unit());
ladanyi@6
   273
}
ladanyi@6
   274
ladanyi@6
   275
void GraphDisplayerCanvas::zoomFit()
ladanyi@6
   276
{
ladanyi@6
   277
  // get the height and width of the canvas
ladanyi@6
   278
  Gtk::Allocation a = get_allocation();
ladanyi@6
   279
  int aw = a.get_width();
ladanyi@6
   280
  int ah = a.get_height();
ladanyi@6
   281
  // add some space
ladanyi@6
   282
  aw -= 5; if (aw < 0) aw = 0;
ladanyi@6
   283
  ah -= 5; if (ah < 0) ah = 0;
ladanyi@6
   284
ladanyi@6
   285
  // get the bounding box of the graph
ladanyi@6
   286
  double wx1, wy1, wx2, wy2;
ladanyi@6
   287
  Gnome::Canvas::Item* pCanvasItem = root();
ladanyi@6
   288
  pCanvasItem->get_bounds(wx1, wy1, wx2, wy2);
ladanyi@6
   289
ladanyi@6
   290
  // fit the graph to the window
ladanyi@7
   291
  double ppu1 = (double) aw / fabs(wx2 - wx1);
ladanyi@7
   292
  double ppu2 = (double) ah / fabs(wy2 - wy1);
ladanyi@7
   293
  set_pixels_per_unit((ppu1 < ppu2) ? ppu1 : ppu2);
ladanyi@6
   294
}
ladanyi@6
   295
ladanyi@6
   296
void GraphDisplayerCanvas::zoom100()
ladanyi@6
   297
{
ladanyi@6
   298
  set_pixels_per_unit(1.0);
ladanyi@6
   299
}
ladanyi@6
   300
ladanyi@6
   301
void GraphDisplayerCanvas::updateScrollRegion()
ladanyi@6
   302
{
ladanyi@6
   303
  double wx1, wy1, wx2, wy2;
ladanyi@6
   304
  Gnome::Canvas::Item* pCanvasItem = root();
ladanyi@6
   305
  pCanvasItem->get_bounds(wx1, wy1, wx2, wy2);
ladanyi@7
   306
  set_scroll_region(wx1, wy1, wx2, wy2);
ladanyi@6
   307
}
hegyi@9
   308
hegyi@9
   309
void GraphDisplayerCanvas::changeEditorialTool(int newtool)
hegyi@9
   310
{
hegyi@9
   311
  actual_handler.disconnect();
hegyi@9
   312
hegyi@21
   313
  if(actual_tool==CREATE_EDGE)
hegyi@21
   314
    {
hegyi@21
   315
	GdkEvent * generated=new GdkEvent();
hegyi@21
   316
	generated->type=GDK_BUTTON_RELEASE;
hegyi@21
   317
	generated->button.button=3;
hegyi@21
   318
	create_edge_event_handler(generated);      
hegyi@21
   319
    }
hegyi@21
   320
hegyi@21
   321
  actual_tool=newtool;
hegyi@21
   322
hegyi@9
   323
  switch(newtool)
hegyi@9
   324
    {
hegyi@9
   325
    case MOVE:
hegyi@9
   326
      actual_handler=displayed_graph.signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::move_event_handler), false);
hegyi@9
   327
      break;
hegyi@12
   328
hegyi@12
   329
      //it has to assigned to canvas, because all the canvas has to be monitored, not only the elements of the already drawn group
hegyi@9
   330
    case CREATE_NODE:
hegyi@9
   331
      actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::create_node_event_handler), false);
hegyi@9
   332
      break;
hegyi@12
   333
hegyi@9
   334
    case CREATE_EDGE:
hegyi@9
   335
      actual_handler=displayed_graph.signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::create_edge_event_handler), false);
hegyi@9
   336
      break;
hegyi@13
   337
hegyi@13
   338
    case ERASER:
hegyi@13
   339
      actual_handler=displayed_graph.signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::eraser_event_handler), false);
hegyi@13
   340
      break;
hegyi@13
   341
hegyi@9
   342
    default:
hegyi@9
   343
      break;
hegyi@9
   344
    }
hegyi@9
   345
}
hegyi@9
   346
hegyi@21
   347
int GraphDisplayerCanvas::get_actual_tool()
hegyi@21
   348
{
hegyi@21
   349
  return actual_tool;
hegyi@21
   350
}
hegyi@21
   351
hegyi@9
   352
bool GraphDisplayerCanvas::move_event_handler(GdkEvent* e)
hegyi@9
   353
{
hegyi@9
   354
  switch(e->type)
hegyi@9
   355
  {
hegyi@9
   356
    case GDK_BUTTON_PRESS:
hegyi@9
   357
      //we mark the location of the event to be able to calculate parameters of dragging
hegyi@9
   358
      clicked_x=e->button.x;
hegyi@9
   359
      clicked_y=e->button.y;
hegyi@9
   360
      active_item=(get_item_at(e->button.x, e->button.y));
hegyi@9
   361
      active_node=INVALID;
hegyi@9
   362
      for (NodeIt i(g); i!=INVALID; ++i)
hegyi@9
   363
	{
hegyi@9
   364
	  if(nodesmap[i]==active_item)
hegyi@9
   365
	    {
hegyi@9
   366
	      active_node=i;
hegyi@9
   367
	    }
hegyi@9
   368
	}
hegyi@20
   369
      switch(e->button.button)
hegyi@20
   370
	{
hegyi@20
   371
	case 3:      
hegyi@20
   372
	  isbutton=3;
hegyi@20
   373
	  break;
hegyi@20
   374
	default:
hegyi@20
   375
	  isbutton=1;
hegyi@20
   376
	  break;
hegyi@20
   377
	}
hegyi@9
   378
      break;
hegyi@9
   379
    case GDK_BUTTON_RELEASE:
hegyi@20
   380
      isbutton=0;
hegyi@9
   381
      active_item=NULL;
hegyi@12
   382
      active_node=INVALID;
hegyi@9
   383
      updateScrollRegion();
hegyi@9
   384
      break;
hegyi@9
   385
    case GDK_MOTION_NOTIFY:
hegyi@12
   386
      //we only have to do sg. if the mouse button is pressed AND the click was on a node that was found in the set of nodes
hegyi@12
   387
      if(active_node!=INVALID)
hegyi@9
   388
      {
hegyi@9
   389
	//new coordinates will be the old values,
hegyi@9
   390
	//because the item will be moved to the
hegyi@9
   391
	//new coordinate therefore the new movement
hegyi@9
   392
	//has to be calculated from here
hegyi@9
   393
hegyi@9
   394
        double dx=e->motion.x-clicked_x;
hegyi@9
   395
        double dy=e->motion.y-clicked_y;
hegyi@9
   396
hegyi@9
   397
        active_item->move(dx, dy);
hegyi@9
   398
hegyi@9
   399
        clicked_x=e->motion.x;
hegyi@9
   400
        clicked_y=e->motion.y;
hegyi@9
   401
hegyi@9
   402
	//all the edges connected to the moved point has to be redrawn
hegyi@20
   403
        EdgeIt ei;
hegyi@9
   404
hegyi@20
   405
        g.firstOut(ei,active_node);
hegyi@9
   406
hegyi@20
   407
        for(;ei!=INVALID;g.nextOut(ei))
hegyi@9
   408
        {
hegyi@9
   409
            Gnome::Canvas::Points coos;
hegyi@9
   410
            double x1, x2, y1, y2;
hegyi@9
   411
hegyi@20
   412
            nodesmap[g.source(ei)]->get_bounds(x1, y1, x2, y2);
hegyi@9
   413
            coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
hegyi@9
   414
hegyi@20
   415
            nodesmap[g.target(ei)]->get_bounds(x1, y1, x2, y2);
hegyi@9
   416
            coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
hegyi@9
   417
hegyi@20
   418
	    if(isbutton==3)
hegyi@20
   419
	      {
hegyi@20
   420
		edgesmap[ei]->set_points(coos);
hegyi@20
   421
	      }
hegyi@20
   422
	    else
hegyi@20
   423
	      {
hegyi@20
   424
		edgesmap[ei]->set_points(coos,true);
hegyi@20
   425
	      }
hegyi@9
   426
hegyi@20
   427
	    edgesmap[ei]->get_bounds(x1, y1, x2, y2);
hegyi@9
   428
hegyi@20
   429
	    edgetextmap[ei]->property_x().set_value((x1+x2)/2);
hegyi@20
   430
	    edgetextmap[ei]->property_y().set_value((y1+y2)/2);
hegyi@9
   431
        }
hegyi@9
   432
hegyi@20
   433
        g.firstIn(ei,active_node);
hegyi@20
   434
        for(;ei!=INVALID;g.nextIn(ei))
hegyi@9
   435
        {
hegyi@9
   436
            Gnome::Canvas::Points coos;
hegyi@9
   437
            double x1, x2, y1, y2;
hegyi@9
   438
hegyi@20
   439
            nodesmap[g.source(ei)]->get_bounds(x1, y1, x2, y2);
hegyi@9
   440
            coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
hegyi@9
   441
hegyi@20
   442
            nodesmap[g.target(ei)]->get_bounds(x1, y1, x2, y2);
hegyi@9
   443
            coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
hegyi@9
   444
hegyi@20
   445
	    if(isbutton==3)
hegyi@20
   446
	      {
hegyi@20
   447
		edgesmap[ei]->set_points(coos);
hegyi@20
   448
	      }
hegyi@20
   449
	    else
hegyi@20
   450
	      {
hegyi@20
   451
		edgesmap[ei]->set_points(coos,true);
hegyi@20
   452
	      }
hegyi@9
   453
hegyi@20
   454
	    edgesmap[ei]->get_bounds(x1, y1, x2, y2);
hegyi@9
   455
hegyi@20
   456
	    edgetextmap[ei]->property_x().set_value((x1+x2)/2);
hegyi@20
   457
	    edgetextmap[ei]->property_y().set_value((y1+y2)/2);
hegyi@9
   458
        }
hegyi@9
   459
      }
hegyi@9
   460
    default: break;
hegyi@9
   461
  }
hegyi@9
   462
hegyi@9
   463
  return true;
hegyi@9
   464
}
hegyi@9
   465
hegyi@9
   466
bool GraphDisplayerCanvas::create_node_event_handler(GdkEvent* e)
hegyi@9
   467
{
hegyi@9
   468
  switch(e->type)
hegyi@9
   469
    {
hegyi@12
   470
hegyi@12
   471
      //draw the new node in red at the clicked place
hegyi@9
   472
    case GDK_BUTTON_PRESS:
hegyi@20
   473
      isbutton=1;
hegyi@9
   474
hegyi@9
   475
      active_node=NodeIt(g,g.addNode());
hegyi@9
   476
hegyi@9
   477
      window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
hegyi@9
   478
hegyi@9
   479
      nodesmap[active_node]=new Gnome::Canvas::Ellipse(displayed_graph, clicked_x-20, clicked_y-20, clicked_x+20, clicked_y+20);
hegyi@9
   480
      active_item=(Gnome::Canvas::Item *)(nodesmap[active_node]);
hegyi@9
   481
      *(nodesmap[active_node]) << Gnome::Canvas::Properties::fill_color("red");
hegyi@9
   482
      *(nodesmap[active_node]) << Gnome::Canvas::Properties::outline_color("black");
hegyi@9
   483
      (nodesmap[active_node])->show();
hegyi@9
   484
      break;
hegyi@12
   485
hegyi@12
   486
      //move the new node
hegyi@9
   487
    case GDK_MOTION_NOTIFY:
hegyi@9
   488
      {
hegyi@9
   489
	double world_motion_x, world_motion_y;
hegyi@9
   490
	GdkEvent * generated=new GdkEvent();
hegyi@9
   491
	window_to_world (e->motion.x, e->motion.y, world_motion_x, world_motion_y);
hegyi@9
   492
	generated->motion.x=world_motion_x;
hegyi@9
   493
	generated->motion.y=world_motion_y;
hegyi@9
   494
	generated->type=GDK_MOTION_NOTIFY;
hegyi@9
   495
	move_event_handler(generated);      
hegyi@9
   496
	break;
hegyi@9
   497
      }
hegyi@12
   498
hegyi@12
   499
      //finalize the new node
hegyi@9
   500
    case GDK_BUTTON_RELEASE:
hegyi@20
   501
      isbutton=0;
hegyi@9
   502
      *active_item << Gnome::Canvas::Properties::fill_color("blue");
hegyi@9
   503
      active_item=NULL;
hegyi@12
   504
      active_node=INVALID;
hegyi@9
   505
      updateScrollRegion();
hegyi@9
   506
      break;
hegyi@9
   507
    default:
hegyi@9
   508
      break;
hegyi@9
   509
    }
hegyi@9
   510
  return false;
hegyi@9
   511
}
hegyi@9
   512
hegyi@9
   513
bool GraphDisplayerCanvas::create_edge_event_handler(GdkEvent* e)
hegyi@9
   514
{
hegyi@11
   515
  switch(e->type)
hegyi@11
   516
    {
hegyi@11
   517
    case GDK_BUTTON_PRESS:
hegyi@12
   518
      //in edge creatino right button has special meaning
hegyi@12
   519
      if(e->button.button!=3)
hegyi@11
   520
	{
hegyi@12
   521
	  //there is not yet selected node
hegyi@12
   522
	  if(active_node==INVALID)
hegyi@11
   523
	    {
hegyi@12
   524
	      //we mark the location of the event to be able to calculate parameters of dragging
hegyi@12
   525
	      clicked_x=e->button.x;
hegyi@12
   526
	      clicked_y=e->button.y;
hegyi@12
   527
	      active_item=(get_item_at(e->button.x, e->button.y));
hegyi@12
   528
	      active_node=INVALID;
hegyi@12
   529
	      for (NodeIt i(g); i!=INVALID; ++i)
hegyi@11
   530
		{
hegyi@12
   531
		  if(nodesmap[i]==active_item)
hegyi@12
   532
		    {
hegyi@12
   533
		      active_node=i;
hegyi@12
   534
		    }
hegyi@12
   535
		}
hegyi@12
   536
	      //the clicked item is really a node
hegyi@12
   537
	      if(active_node!=INVALID)
hegyi@12
   538
		{
hegyi@12
   539
		  *(nodesmap[active_node]) << Gnome::Canvas::Properties::fill_color("red");
hegyi@20
   540
		  isbutton=1;
hegyi@12
   541
		}
hegyi@12
   542
	      //clicked item was not a node. It could be e.g. edge.
hegyi@12
   543
	      else
hegyi@12
   544
		{
hegyi@12
   545
		  active_item=NULL;
hegyi@11
   546
		}
hegyi@11
   547
	    }
hegyi@12
   548
	  //we only have to do sg. if the mouse button
hegyi@12
   549
	  // is pressed already once AND the click was
hegyi@12
   550
	  // on a node that was found in the set of 
hegyi@12
   551
	  //nodes, and now we only search for the second 
hegyi@12
   552
	  //node
hegyi@12
   553
	  else
hegyi@11
   554
	    {
hegyi@12
   555
	      target_item=(get_item_at(e->button.x, e->button.y));
hegyi@12
   556
	      Graph::NodeIt target_node=INVALID;
hegyi@12
   557
	      for (NodeIt i(g); i!=INVALID; ++i)
hegyi@11
   558
		{
hegyi@12
   559
		  if(nodesmap[i]==target_item)
hegyi@12
   560
		    {
hegyi@12
   561
		      target_node=i;
hegyi@12
   562
		    }
hegyi@12
   563
		}
hegyi@12
   564
	      //the clicked item is a node, the edge can be drawn
hegyi@12
   565
	      if(target_node!=INVALID)
hegyi@12
   566
		{
hegyi@12
   567
		  *(nodesmap[target_node]) << Gnome::Canvas::Properties::fill_color("red");
hegyi@12
   568
hegyi@12
   569
		  //creating new edge
hegyi@12
   570
		  active_edge=EdgeIt(g,g.addEdge(active_node, target_node));
hegyi@12
   571
	  
hegyi@12
   572
		  //calculating coordinates of new edge
hegyi@12
   573
		  Gnome::Canvas::Points coos;
hegyi@12
   574
		  double x1, x2, y1, y2;
hegyi@12
   575
	  
hegyi@12
   576
		  active_item->get_bounds(x1, y1, x2, y2);
hegyi@12
   577
		  coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
hegyi@12
   578
hegyi@12
   579
		  target_item->get_bounds(x1, y1, x2, y2);
hegyi@12
   580
		  coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
hegyi@12
   581
hegyi@12
   582
		  //drawing new edge
hegyi@21
   583
		  edgesmap[active_edge]=new BrokenEdge(displayed_graph, coos, *this);
hegyi@12
   584
		  *(edgesmap[active_edge]) << Gnome::Canvas::Properties::fill_color("green");
hegyi@12
   585
		  edgesmap[active_edge]->property_width_pixels().set_value(10);
hegyi@12
   586
hegyi@12
   587
		  //redraw nodes to blank terminations of the new edge
hegyi@12
   588
		  target_item->raise_to_top();
hegyi@12
   589
		  active_item->raise_to_top();
hegyi@12
   590
hegyi@12
   591
		  //initializing edge-text as well, to empty string
hegyi@12
   592
		  edgesmap[active_edge]->get_bounds(x1, y1, x2, y2);
hegyi@12
   593
		  edgetextmap[active_edge]=new Gnome::Canvas::Text(displayed_graph,(x1+x2)/2, (y1+y2)/2, "");
hegyi@12
   594
		  edgetextmap[active_edge]->property_fill_color().set_value("black");
hegyi@12
   595
		}
hegyi@12
   596
	      //clicked item was not a node. it could be an e.g. edge. we do not deal with it furthermore.
hegyi@12
   597
	      else
hegyi@12
   598
		{
hegyi@12
   599
		  target_item=NULL;
hegyi@11
   600
		}
hegyi@11
   601
	    }
hegyi@11
   602
	}
hegyi@11
   603
      break;
hegyi@11
   604
    case GDK_BUTTON_RELEASE:
hegyi@20
   605
      isbutton=0;
hegyi@12
   606
      //we clear settings in two cases
hegyi@12
   607
      //1: the edge is ready (target_item has valid value)
hegyi@12
   608
      //2: the edge creation is cancelled with right button
hegyi@12
   609
      if((target_item)||(e->button.button==3))
hegyi@11
   610
	{
hegyi@12
   611
	  if(active_item)
hegyi@12
   612
	    {
hegyi@12
   613
	      *active_item << Gnome::Canvas::Properties::fill_color("blue");
hegyi@12
   614
	      active_item=NULL;
hegyi@12
   615
	    }
hegyi@12
   616
	  if(target_item)
hegyi@12
   617
	    {
hegyi@12
   618
	      *target_item << Gnome::Canvas::Properties::fill_color("blue");
hegyi@12
   619
	      target_item=NULL;
hegyi@12
   620
	    }
hegyi@12
   621
	  active_node=INVALID;
hegyi@12
   622
	  active_edge=INVALID;
hegyi@11
   623
	}
hegyi@11
   624
      break;
hegyi@11
   625
    default:
hegyi@11
   626
      break;
hegyi@11
   627
    }
hegyi@9
   628
  return false;
hegyi@9
   629
}
hegyi@9
   630
hegyi@13
   631
bool GraphDisplayerCanvas::eraser_event_handler(GdkEvent* e)
hegyi@13
   632
{
hegyi@13
   633
  switch(e->type)
hegyi@13
   634
    {
hegyi@13
   635
    case GDK_BUTTON_PRESS:
hegyi@13
   636
      active_item=(get_item_at(e->button.x, e->button.y));
hegyi@13
   637
      active_node=INVALID;
hegyi@13
   638
      active_edge=INVALID;
hegyi@13
   639
      for (NodeIt i(g); i!=INVALID; ++i)
hegyi@13
   640
	{
hegyi@13
   641
	  if(nodesmap[i]==active_item)
hegyi@13
   642
	    {
hegyi@13
   643
	      active_node=i;
hegyi@13
   644
	    }
hegyi@13
   645
	}
hegyi@13
   646
      if(active_node==INVALID)
hegyi@13
   647
	{
hegyi@13
   648
	  for (EdgeIt i(g); i!=INVALID; ++i)
hegyi@13
   649
	    {
hegyi@13
   650
	      if(edgesmap[i]==active_item)
hegyi@13
   651
		{
hegyi@13
   652
		  active_edge=i;
hegyi@13
   653
		}
hegyi@13
   654
	    }
hegyi@13
   655
	}
hegyi@13
   656
    *active_item << Gnome::Canvas::Properties::fill_color("red");
hegyi@13
   657
      break;
hegyi@13
   658
hegyi@13
   659
    case GDK_BUTTON_RELEASE:
hegyi@13
   660
      if(active_item==(get_item_at(e->button.x, e->button.y)))
hegyi@13
   661
	{
hegyi@13
   662
	  if(active_node!=INVALID)
hegyi@13
   663
	    {
hegyi@14
   664
hegyi@16
   665
	      //collecting edges to delete
hegyi@16
   666
	      EdgeIt e;
hegyi@16
   667
	      std::set<Graph::Edge> edges_to_delete;
hegyi@16
   668
hegyi@16
   669
	      g.firstOut(e,active_node);
hegyi@16
   670
	      for(;e!=INVALID;g.nextOut(e))
hegyi@13
   671
		{
hegyi@16
   672
		      edges_to_delete.insert(e);
hegyi@16
   673
		}
hegyi@16
   674
hegyi@16
   675
	      g.firstIn(e,active_node);
hegyi@16
   676
	      for(;e!=INVALID;g.nextIn(e))
hegyi@16
   677
		{
hegyi@16
   678
		      edges_to_delete.insert(e);
hegyi@16
   679
		}
hegyi@16
   680
hegyi@16
   681
	      //deleting collected edges
hegyi@16
   682
	      for(std::set<Graph::Edge>::iterator edge_set_it=edges_to_delete.begin();edge_set_it!=edges_to_delete.end();edge_set_it++)
hegyi@16
   683
		{
hegyi@16
   684
		  delete_item(*edge_set_it);
hegyi@13
   685
		}
hegyi@14
   686
	      delete_item(active_node);
hegyi@14
   687
	    }
hegyi@16
   688
	  //a simple edge was chosen
hegyi@14
   689
	  else
hegyi@14
   690
	    {
hegyi@14
   691
	      delete_item(active_edge);
hegyi@14
   692
	    }
hegyi@13
   693
hegyi@13
   694
	  
hegyi@13
   695
	}
hegyi@16
   696
      //pointer was moved, deletion is cancelled
hegyi@13
   697
      else
hegyi@13
   698
	{
hegyi@13
   699
	  if(active_node!=INVALID)
hegyi@13
   700
	    {
hegyi@13
   701
	      *active_item << Gnome::Canvas::Properties::fill_color("blue");
hegyi@13
   702
	    }
hegyi@13
   703
	  else
hegyi@13
   704
	    {
hegyi@13
   705
	      *active_item << Gnome::Canvas::Properties::fill_color("green");
hegyi@13
   706
	    }
hegyi@13
   707
	}
hegyi@16
   708
      //reseting datas
hegyi@13
   709
      active_item=NULL;
hegyi@13
   710
      active_edge=INVALID;
hegyi@13
   711
      active_node=INVALID;
hegyi@13
   712
      break;
hegyi@13
   713
hegyi@13
   714
    case GDK_MOTION_NOTIFY:
hegyi@13
   715
      break;
hegyi@13
   716
hegyi@13
   717
    default:
hegyi@13
   718
      break;
hegyi@13
   719
    }
hegyi@13
   720
  return true;
hegyi@13
   721
}
hegyi@13
   722
hegyi@14
   723
void GraphDisplayerCanvas::delete_item(NodeIt node_to_delete)
hegyi@13
   724
{
hegyi@14
   725
  delete(nodesmap[node_to_delete]);
hegyi@14
   726
  g.erase(node_to_delete);
hegyi@13
   727
}
hegyi@13
   728
hegyi@14
   729
void GraphDisplayerCanvas::delete_item(EdgeIt edge_to_delete)
hegyi@14
   730
{
hegyi@14
   731
  delete(edgesmap[edge_to_delete]);
hegyi@14
   732
  g.erase(edge_to_delete);
hegyi@14
   733
}
hegyi@14
   734
hegyi@16
   735
void GraphDisplayerCanvas::delete_item(Graph::Edge edge_to_delete)
hegyi@16
   736
{
hegyi@16
   737
  delete(edgesmap[edge_to_delete]);
hegyi@16
   738
  g.erase(edge_to_delete);
hegyi@16
   739
}
hegyi@16
   740