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