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