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