graph_displayer_canvas.cc
author hegyi
Mon, 13 Jun 2005 10:30:08 +0000
branchgui
changeset 11 09b2a893fc9d
parent 9 0bb1675306cb
child 12 1db7a6dd17f8
permissions -rwxr-xr-x
Edge creation is available.
ladanyi@6
     1
#include <graph_displayer_canvas.h>
ladanyi@6
     2
#include <math.h>
ladanyi@6
     3
hegyi@11
     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@6
     5
{
hegyi@9
     6
  
hegyi@11
     7
  actual_handler=displayed_graph.signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::create_edge_event_handler), false);
hegyi@9
     8
ladanyi@6
     9
  //set_center_scroll_region(true);
ladanyi@6
    10
ladanyi@6
    11
  //first edges are drawn, to hide joining with nodes later
ladanyi@6
    12
ladanyi@6
    13
  for (EdgeIt i(g); i!=INVALID; ++i)
ladanyi@6
    14
  {
ladanyi@6
    15
ladanyi@6
    16
    //drawing green lines, coordinates are from cm
ladanyi@6
    17
ladanyi@6
    18
    Gnome::Canvas::Points coos;
ladanyi@6
    19
    coos.push_back(Gnome::Art::Point(cm[g.source(i)].x,cm[g.source(i)].y));
ladanyi@6
    20
    coos.push_back(Gnome::Art::Point(cm[g.target(i)].x,cm[g.target(i)].y));
ladanyi@6
    21
    
ladanyi@6
    22
    edgesmap[i]=new Gnome::Canvas::Line(displayed_graph, coos);
ladanyi@6
    23
    *(edgesmap[i]) << Gnome::Canvas::Properties::fill_color("green");
ladanyi@6
    24
    edgesmap[i]->property_width_pixels().set_value(10);    
ladanyi@6
    25
    
ladanyi@6
    26
    //initializing edge-text as well, to empty string
ladanyi@6
    27
ladanyi@6
    28
    double x1, x2, y1, y2;
ladanyi@6
    29
    edgesmap[i]->get_bounds(x1, y1, x2, y2);
ladanyi@6
    30
    
ladanyi@6
    31
    edgetextmap[i]=new Gnome::Canvas::Text(displayed_graph,(x1+x2)/2, (y1+y2)/2, "");
ladanyi@6
    32
    edgetextmap[i]->property_fill_color().set_value("black");
ladanyi@6
    33
  }
ladanyi@6
    34
ladanyi@6
    35
  //afterwards nodes come to be drawn
ladanyi@6
    36
ladanyi@6
    37
  NodeIt i(g);
ladanyi@6
    38
  int maxx=0, maxy=0, minx=(int)cm[i].x, miny=(int)cm[i].y;
ladanyi@6
    39
ladanyi@6
    40
  for (; i!=INVALID; ++i)
ladanyi@6
    41
  {
ladanyi@6
    42
    //minimum and maximum is gathered to be able to zoom to the graph correctly (whole figure should be seen)
ladanyi@6
    43
ladanyi@6
    44
    if(cm[i].x>maxx)maxx=(int)cm[i].x;
ladanyi@6
    45
    if(cm[i].y>maxy)maxy=(int)cm[i].y;
ladanyi@6
    46
    if(cm[i].x<minx)minx=(int)cm[i].x;
ladanyi@6
    47
    if(cm[i].y<miny)miny=(int)cm[i].y;
ladanyi@6
    48
ladanyi@6
    49
    //drawing bule nodes, with black line around them
ladanyi@6
    50
ladanyi@6
    51
    nodesmap[i]=new Gnome::Canvas::Ellipse(displayed_graph, cm[i].x-20, cm[i].y-20, cm[i].x+20, cm[i].y+20);
ladanyi@6
    52
    *(nodesmap[i]) << Gnome::Canvas::Properties::fill_color("blue");
ladanyi@6
    53
    *(nodesmap[i]) << Gnome::Canvas::Properties::outline_color("black");
hegyi@9
    54
    //!!!!!!! (nodesmap[i])->signal_event().connect(sigc::bind(sigc::mem_fun(*this, &GraphDisplayerCanvas::event_handler),i));
ladanyi@6
    55
  }
ladanyi@6
    56
ladanyi@6
    57
  updateScrollRegion();
ladanyi@6
    58
}
ladanyi@6
    59
ladanyi@6
    60
GraphDisplayerCanvas::~GraphDisplayerCanvas()
ladanyi@6
    61
{
ladanyi@6
    62
ladanyi@6
    63
  //writing out the end state of the graph
ladanyi@6
    64
  //\todo all the maps has to be write out!
ladanyi@6
    65
ladanyi@6
    66
  Graph::NodeMap <int> id(g);
ladanyi@6
    67
  Graph::NodeMap <double> xc(g);
ladanyi@6
    68
  Graph::NodeMap <double> yc(g);
ladanyi@6
    69
  
ladanyi@6
    70
  int j=1;
ladanyi@6
    71
  
ladanyi@6
    72
  for (NodeIt i(g); i!=INVALID; ++i)
ladanyi@6
    73
  {
ladanyi@6
    74
    double x1,y1,x2,y2;
ladanyi@6
    75
    nodesmap[i]->get_bounds(x1, y1, x2, y2);
ladanyi@6
    76
    
ladanyi@6
    77
    id[i]=j++;
ladanyi@6
    78
    xc[i]=(x1+x2)/2;
ladanyi@6
    79
    yc[i]=(y1+y2)/2;
ladanyi@6
    80
  }
ladanyi@6
    81
ladanyi@6
    82
  GraphWriter<Graph> writer(std::cout,g);
ladanyi@6
    83
  
ladanyi@6
    84
  writer.writeNodeMap("id", id);
ladanyi@6
    85
  writer.writeNodeMap("coordinates_x", xc);
ladanyi@6
    86
  writer.writeNodeMap("coordinates_y", yc);
ladanyi@6
    87
  writer.run();
ladanyi@6
    88
}
ladanyi@6
    89
ladanyi@6
    90
int GraphDisplayerCanvas::changeLineWidth (std::string mapname)
ladanyi@6
    91
{
ladanyi@6
    92
  for (EdgeIt i(g); i!=INVALID; ++i)
ladanyi@6
    93
  {
ladanyi@6
    94
    int w=(int)(*(mapstorage.edgemap_storage)[mapname])[i];
ladanyi@6
    95
    edgesmap[i]->property_width_pixels().set_value(w);
ladanyi@6
    96
  }
ladanyi@6
    97
  return 0;
ladanyi@6
    98
};
ladanyi@6
    99
ladanyi@6
   100
int GraphDisplayerCanvas::changeColor (std::string mapname)
ladanyi@6
   101
{  
ladanyi@6
   102
ladanyi@6
   103
  //function maps the range of the maximum and
ladanyi@6
   104
  //the minimum of the nodemap to the range of
ladanyi@6
   105
  //green in RGB
ladanyi@6
   106
ladanyi@6
   107
  for (EdgeIt i(g); i!=INVALID; ++i)
ladanyi@6
   108
  {
ladanyi@6
   109
    double w=(*(mapstorage.edgemap_storage)[mapname])[i];
ladanyi@6
   110
    double max=mapstorage.maxOfEdgeMap(mapname);
ladanyi@6
   111
    double min=mapstorage.minOfEdgeMap(mapname);
ladanyi@6
   112
      
ladanyi@6
   113
    //std::cout<<w<<" "<<max<<" "<<min<<" "<<100*(w-min)/(max-min)<<std::endl;
ladanyi@6
   114
    Gdk::Color color;
ladanyi@6
   115
    if(max!=min)
ladanyi@6
   116
    {
ladanyi@6
   117
      color.set_rgb_p (0, 100*(w-min)/(max-min), 0);
ladanyi@6
   118
    }
ladanyi@6
   119
    else
ladanyi@6
   120
    {
ladanyi@6
   121
      color.set_rgb_p (0, 100, 0);
ladanyi@6
   122
    }
ladanyi@6
   123
ladanyi@6
   124
    edgesmap[i]->property_fill_color_gdk().set_value(color);
ladanyi@6
   125
  }
ladanyi@6
   126
  return 0;
ladanyi@6
   127
};
ladanyi@6
   128
ladanyi@6
   129
int GraphDisplayerCanvas::changeText (std::string mapname)
ladanyi@6
   130
{
ladanyi@6
   131
ladanyi@6
   132
  //the number in the map will be written on the edge
ladanyi@6
   133
  //EXCEPT when the name of the map is Text, because
ladanyi@6
   134
  //in that case empty string will be written, because
ladanyi@6
   135
  //that is the deleter map
ladanyi@6
   136
  //\todo isn't it a bit woodcutter?
ladanyi@6
   137
ladanyi@6
   138
  for (EdgeIt i(g); i!=INVALID; ++i)
ladanyi@6
   139
  {
ladanyi@6
   140
    if(mapname!="Text")
ladanyi@6
   141
    {
ladanyi@6
   142
      double number=(*(mapstorage.edgemap_storage)[mapname])[i];
ladanyi@6
   143
      int length=(int)(floor(log(number)/log(10)))+1;
ladanyi@6
   144
      int maxpos=(int)(pow(10,length-1));
ladanyi@6
   145
      int strl=length+1+RANGE;
ladanyi@6
   146
      char * str=new char[strl];
ladanyi@6
   147
      str[length]='.';
ladanyi@6
   148
      str[strl]='\0';
ladanyi@6
   149
      
ladanyi@6
   150
      for(int j=0;j<strl;j++)
ladanyi@6
   151
      {
ladanyi@6
   152
	if(j!=length)
ladanyi@6
   153
        {
ladanyi@6
   154
	  int digit=(int)(number/maxpos);
ladanyi@6
   155
	  str[j]=(digit+'0');
ladanyi@6
   156
	  number-=digit*maxpos;
ladanyi@6
   157
	  number*=10;
ladanyi@6
   158
        }
ladanyi@6
   159
      }
ladanyi@6
   160
      
ladanyi@6
   161
      edgetextmap[i]->property_text().set_value(str);
ladanyi@6
   162
    }
ladanyi@6
   163
    else
ladanyi@6
   164
    {
ladanyi@6
   165
      edgetextmap[i]->property_text().set_value("");
ladanyi@6
   166
    }
ladanyi@6
   167
  }
ladanyi@6
   168
  return 0;
ladanyi@6
   169
};
ladanyi@6
   170
ladanyi@6
   171
bool GraphDisplayerCanvas::event_handler(GdkEvent* e, Node n)
ladanyi@6
   172
{
ladanyi@6
   173
  switch(e->type)
ladanyi@6
   174
  {
ladanyi@6
   175
    case GDK_BUTTON_PRESS:
ladanyi@6
   176
      //we mark the location of the event to be able to calculate parameters of dragging
ladanyi@6
   177
      clicked_x=e->button.x;
ladanyi@6
   178
      clicked_y=e->button.y;
ladanyi@6
   179
      active_item=(get_item_at(e->button.x, e->button.y));
ladanyi@6
   180
      isbutton=true;
ladanyi@6
   181
      break;
ladanyi@6
   182
    case GDK_BUTTON_RELEASE:
ladanyi@6
   183
      isbutton=false;
ladanyi@6
   184
      active_item=NULL;
ladanyi@7
   185
      updateScrollRegion();
ladanyi@6
   186
      break;
ladanyi@6
   187
    case GDK_MOTION_NOTIFY:
ladanyi@6
   188
      //we only have to do sg. if the mouse button is pressed
ladanyi@6
   189
      if(isbutton)
ladanyi@6
   190
      {
ladanyi@6
   191
	//new coordinates will be the old values,
ladanyi@6
   192
	//because the item will be moved to the
ladanyi@6
   193
	//new coordinate therefore the new movement
ladanyi@6
   194
	//has to be calculated from here
ladanyi@6
   195
ladanyi@6
   196
        double dx=e->motion.x-clicked_x;
ladanyi@6
   197
        double dy=e->motion.y-clicked_y;
ladanyi@6
   198
        active_item->move(dx, dy);
ladanyi@6
   199
        clicked_x=e->motion.x;
ladanyi@6
   200
        clicked_y=e->motion.y;
ladanyi@6
   201
ladanyi@6
   202
	//all the edges connected to the moved point has to be redrawn
ladanyi@6
   203
ladanyi@6
   204
        EdgeIt e;
ladanyi@6
   205
        g.firstOut(e,n);
ladanyi@6
   206
        for(;e!=INVALID;g.nextOut(e))
ladanyi@6
   207
        {
ladanyi@6
   208
            Gnome::Canvas::Points coos;
ladanyi@6
   209
            double x1, x2, y1, y2;
ladanyi@6
   210
ladanyi@6
   211
            nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2);
ladanyi@6
   212
            coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
ladanyi@6
   213
ladanyi@6
   214
            nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2);
ladanyi@6
   215
            coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
ladanyi@6
   216
ladanyi@6
   217
            edgesmap[e]->property_points().set_value(coos);
ladanyi@6
   218
ladanyi@6
   219
	    edgesmap[e]->get_bounds(x1, y1, x2, y2);
ladanyi@6
   220
ladanyi@6
   221
	    edgetextmap[e]->property_x().set_value((x1+x2)/2);
ladanyi@6
   222
	    edgetextmap[e]->property_y().set_value((y1+y2)/2);
ladanyi@6
   223
        }
ladanyi@6
   224
ladanyi@6
   225
        g.firstIn(e,n);
ladanyi@6
   226
        for(;e!=INVALID;g.nextIn(e))
ladanyi@6
   227
        {
ladanyi@6
   228
            Gnome::Canvas::Points coos;
ladanyi@6
   229
            double x1, x2, y1, y2;
ladanyi@6
   230
ladanyi@6
   231
            nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2);
ladanyi@6
   232
            coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
ladanyi@6
   233
ladanyi@6
   234
            nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2);
ladanyi@6
   235
            coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
ladanyi@6
   236
ladanyi@6
   237
            edgesmap[e]->property_points().set_value(coos);
ladanyi@6
   238
ladanyi@6
   239
	    edgesmap[e]->get_bounds(x1, y1, x2, y2);
ladanyi@6
   240
ladanyi@6
   241
	    edgetextmap[e]->property_x().set_value((x1+x2)/2);
ladanyi@6
   242
	    edgetextmap[e]->property_y().set_value((y1+y2)/2);
ladanyi@6
   243
        }
ladanyi@6
   244
      }
ladanyi@6
   245
    default: break;
ladanyi@6
   246
  }
ladanyi@6
   247
  return true;
ladanyi@6
   248
}
ladanyi@6
   249
ladanyi@6
   250
bool GraphDisplayerCanvas::on_expose_event(GdkEventExpose *event)
ladanyi@6
   251
{
ladanyi@6
   252
  Gnome::Canvas::CanvasAA::on_expose_event(event);
ladanyi@6
   253
  //usleep(10000);
ladanyi@6
   254
  //rezoom();
ladanyi@6
   255
  return true;
ladanyi@6
   256
}
ladanyi@6
   257
ladanyi@6
   258
void GraphDisplayerCanvas::zoomIn()
ladanyi@6
   259
{
ladanyi@6
   260
  set_pixels_per_unit(
ladanyi@6
   261
      (1.0 + (double) zoom_step / 100.0) * get_pixels_per_unit());
ladanyi@6
   262
}
ladanyi@6
   263
ladanyi@6
   264
void GraphDisplayerCanvas::zoomOut()
ladanyi@6
   265
{
ladanyi@6
   266
  set_pixels_per_unit(
ladanyi@6
   267
      (1.0 - (double) zoom_step / 100.0) * get_pixels_per_unit());
ladanyi@6
   268
}
ladanyi@6
   269
ladanyi@6
   270
void GraphDisplayerCanvas::zoomFit()
ladanyi@6
   271
{
ladanyi@6
   272
  // get the height and width of the canvas
ladanyi@6
   273
  Gtk::Allocation a = get_allocation();
ladanyi@6
   274
  int aw = a.get_width();
ladanyi@6
   275
  int ah = a.get_height();
ladanyi@6
   276
  // add some space
ladanyi@6
   277
  aw -= 5; if (aw < 0) aw = 0;
ladanyi@6
   278
  ah -= 5; if (ah < 0) ah = 0;
ladanyi@6
   279
ladanyi@6
   280
  // get the bounding box of the graph
ladanyi@6
   281
  double wx1, wy1, wx2, wy2;
ladanyi@6
   282
  Gnome::Canvas::Item* pCanvasItem = root();
ladanyi@6
   283
  pCanvasItem->get_bounds(wx1, wy1, wx2, wy2);
ladanyi@6
   284
ladanyi@6
   285
  // fit the graph to the window
ladanyi@7
   286
  double ppu1 = (double) aw / fabs(wx2 - wx1);
ladanyi@7
   287
  double ppu2 = (double) ah / fabs(wy2 - wy1);
ladanyi@7
   288
  set_pixels_per_unit((ppu1 < ppu2) ? ppu1 : ppu2);
ladanyi@6
   289
}
ladanyi@6
   290
ladanyi@6
   291
void GraphDisplayerCanvas::zoom100()
ladanyi@6
   292
{
ladanyi@6
   293
  set_pixels_per_unit(1.0);
ladanyi@6
   294
}
ladanyi@6
   295
ladanyi@6
   296
void GraphDisplayerCanvas::updateScrollRegion()
ladanyi@6
   297
{
ladanyi@6
   298
  double wx1, wy1, wx2, wy2;
ladanyi@6
   299
  Gnome::Canvas::Item* pCanvasItem = root();
ladanyi@6
   300
  pCanvasItem->get_bounds(wx1, wy1, wx2, wy2);
ladanyi@7
   301
  set_scroll_region(wx1, wy1, wx2, wy2);
ladanyi@6
   302
}
hegyi@9
   303
hegyi@9
   304
void GraphDisplayerCanvas::changeEditorialTool(int newtool)
hegyi@9
   305
{
hegyi@9
   306
  actual_handler.disconnect();
hegyi@9
   307
hegyi@9
   308
  switch(newtool)
hegyi@9
   309
    {
hegyi@9
   310
    case MOVE:
hegyi@9
   311
      actual_handler=displayed_graph.signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::move_event_handler), false);
hegyi@9
   312
      break;
hegyi@9
   313
    case CREATE_NODE:
hegyi@9
   314
      actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::create_node_event_handler), false);
hegyi@9
   315
      break;
hegyi@9
   316
    case CREATE_EDGE:
hegyi@9
   317
      actual_handler=displayed_graph.signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::create_edge_event_handler), false);
hegyi@9
   318
      break;
hegyi@9
   319
    default:
hegyi@9
   320
      break;
hegyi@9
   321
    }
hegyi@9
   322
}
hegyi@9
   323
hegyi@9
   324
bool GraphDisplayerCanvas::move_event_handler(GdkEvent* e)
hegyi@9
   325
{
hegyi@9
   326
  switch(e->type)
hegyi@9
   327
  {
hegyi@9
   328
    case GDK_BUTTON_PRESS:
hegyi@9
   329
      //we mark the location of the event to be able to calculate parameters of dragging
hegyi@9
   330
      clicked_x=e->button.x;
hegyi@9
   331
      clicked_y=e->button.y;
hegyi@9
   332
      active_item=(get_item_at(e->button.x, e->button.y));
hegyi@9
   333
      active_node=INVALID;
hegyi@9
   334
      for (NodeIt i(g); i!=INVALID; ++i)
hegyi@9
   335
	{
hegyi@9
   336
	  if(nodesmap[i]==active_item)
hegyi@9
   337
	    {
hegyi@9
   338
	      active_node=i;
hegyi@9
   339
	    }
hegyi@9
   340
	}
hegyi@9
   341
      isbutton=true;
hegyi@9
   342
      break;
hegyi@9
   343
    case GDK_BUTTON_RELEASE:
hegyi@9
   344
      isbutton=false;
hegyi@9
   345
      active_item=NULL;
hegyi@9
   346
      updateScrollRegion();
hegyi@9
   347
      break;
hegyi@9
   348
    case GDK_MOTION_NOTIFY:
hegyi@9
   349
      //we only have to do sg. if the mouse button is pressed
hegyi@9
   350
      if(isbutton)
hegyi@9
   351
      {
hegyi@9
   352
	//new coordinates will be the old values,
hegyi@9
   353
	//because the item will be moved to the
hegyi@9
   354
	//new coordinate therefore the new movement
hegyi@9
   355
	//has to be calculated from here
hegyi@9
   356
hegyi@9
   357
        double dx=e->motion.x-clicked_x;
hegyi@9
   358
        double dy=e->motion.y-clicked_y;
hegyi@9
   359
hegyi@9
   360
        active_item->move(dx, dy);
hegyi@9
   361
hegyi@9
   362
        clicked_x=e->motion.x;
hegyi@9
   363
        clicked_y=e->motion.y;
hegyi@9
   364
hegyi@9
   365
	//all the edges connected to the moved point has to be redrawn
hegyi@9
   366
        EdgeIt e;
hegyi@9
   367
hegyi@9
   368
        g.firstOut(e,active_node);
hegyi@9
   369
hegyi@9
   370
        for(;e!=INVALID;g.nextOut(e))
hegyi@9
   371
        {
hegyi@9
   372
            Gnome::Canvas::Points coos;
hegyi@9
   373
            double x1, x2, y1, y2;
hegyi@9
   374
hegyi@9
   375
            nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2);
hegyi@9
   376
            coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
hegyi@9
   377
hegyi@9
   378
            nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2);
hegyi@9
   379
            coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
hegyi@9
   380
hegyi@9
   381
            edgesmap[e]->property_points().set_value(coos);
hegyi@9
   382
hegyi@9
   383
	    edgesmap[e]->get_bounds(x1, y1, x2, y2);
hegyi@9
   384
hegyi@9
   385
	    edgetextmap[e]->property_x().set_value((x1+x2)/2);
hegyi@9
   386
	    edgetextmap[e]->property_y().set_value((y1+y2)/2);
hegyi@9
   387
        }
hegyi@9
   388
hegyi@9
   389
        g.firstIn(e,active_node);
hegyi@9
   390
        for(;e!=INVALID;g.nextIn(e))
hegyi@9
   391
        {
hegyi@9
   392
            Gnome::Canvas::Points coos;
hegyi@9
   393
            double x1, x2, y1, y2;
hegyi@9
   394
hegyi@9
   395
            nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2);
hegyi@9
   396
            coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
hegyi@9
   397
hegyi@9
   398
            nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2);
hegyi@9
   399
            coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
hegyi@9
   400
hegyi@9
   401
            edgesmap[e]->property_points().set_value(coos);
hegyi@9
   402
hegyi@9
   403
	    edgesmap[e]->get_bounds(x1, y1, x2, y2);
hegyi@9
   404
hegyi@9
   405
	    edgetextmap[e]->property_x().set_value((x1+x2)/2);
hegyi@9
   406
	    edgetextmap[e]->property_y().set_value((y1+y2)/2);
hegyi@9
   407
        }
hegyi@9
   408
      }
hegyi@9
   409
    default: break;
hegyi@9
   410
  }
hegyi@9
   411
hegyi@9
   412
  return true;
hegyi@9
   413
}
hegyi@9
   414
hegyi@9
   415
bool GraphDisplayerCanvas::create_node_event_handler(GdkEvent* e)
hegyi@9
   416
{
hegyi@9
   417
  switch(e->type)
hegyi@9
   418
    {
hegyi@9
   419
    case GDK_BUTTON_PRESS:
hegyi@9
   420
      isbutton=true;
hegyi@9
   421
hegyi@9
   422
      active_node=NodeIt(g,g.addNode());
hegyi@9
   423
hegyi@9
   424
      window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
hegyi@9
   425
hegyi@9
   426
      nodesmap[active_node]=new Gnome::Canvas::Ellipse(displayed_graph, clicked_x-20, clicked_y-20, clicked_x+20, clicked_y+20);
hegyi@9
   427
      active_item=(Gnome::Canvas::Item *)(nodesmap[active_node]);
hegyi@9
   428
      *(nodesmap[active_node]) << Gnome::Canvas::Properties::fill_color("red");
hegyi@9
   429
      *(nodesmap[active_node]) << Gnome::Canvas::Properties::outline_color("black");
hegyi@9
   430
      (nodesmap[active_node])->show();
hegyi@9
   431
      break;
hegyi@9
   432
    case GDK_MOTION_NOTIFY:
hegyi@9
   433
      {
hegyi@9
   434
	double world_motion_x, world_motion_y;
hegyi@9
   435
	GdkEvent * generated=new GdkEvent();
hegyi@9
   436
	window_to_world (e->motion.x, e->motion.y, world_motion_x, world_motion_y);
hegyi@9
   437
	generated->motion.x=world_motion_x;
hegyi@9
   438
	generated->motion.y=world_motion_y;
hegyi@9
   439
	generated->type=GDK_MOTION_NOTIFY;
hegyi@9
   440
	move_event_handler(generated);      
hegyi@9
   441
	break;
hegyi@9
   442
      }
hegyi@9
   443
    case GDK_BUTTON_RELEASE:
hegyi@9
   444
      isbutton=false;
hegyi@9
   445
      *active_item << Gnome::Canvas::Properties::fill_color("blue");
hegyi@9
   446
      active_item=NULL;
hegyi@9
   447
      updateScrollRegion();
hegyi@9
   448
      break;
hegyi@9
   449
    default:
hegyi@9
   450
      break;
hegyi@9
   451
    }
hegyi@9
   452
  return false;
hegyi@9
   453
}
hegyi@9
   454
hegyi@9
   455
bool GraphDisplayerCanvas::create_edge_event_handler(GdkEvent* e)
hegyi@9
   456
{
hegyi@11
   457
  switch(e->type)
hegyi@11
   458
    {
hegyi@11
   459
    case GDK_BUTTON_PRESS:
hegyi@11
   460
      if(!active_item)
hegyi@11
   461
	{
hegyi@11
   462
	  //we mark the location of the event to be able to calculate parameters of dragging
hegyi@11
   463
	  clicked_x=e->button.x;
hegyi@11
   464
	  clicked_y=e->button.y;
hegyi@11
   465
	  active_item=(get_item_at(e->button.x, e->button.y));
hegyi@11
   466
	  active_node=INVALID;
hegyi@11
   467
	  for (NodeIt i(g); i!=INVALID; ++i)
hegyi@11
   468
	    {
hegyi@11
   469
	      if(nodesmap[i]==active_item)
hegyi@11
   470
		{
hegyi@11
   471
		  active_node=i;
hegyi@11
   472
		}
hegyi@11
   473
	    }
hegyi@11
   474
	  *(nodesmap[active_node]) << Gnome::Canvas::Properties::fill_color("red");
hegyi@11
   475
	  isbutton=true;
hegyi@11
   476
	}
hegyi@11
   477
      else
hegyi@11
   478
	{
hegyi@11
   479
	  target_item=(get_item_at(e->button.x, e->button.y));
hegyi@11
   480
	  Graph::NodeIt target_node=INVALID;
hegyi@11
   481
	  for (NodeIt i(g); i!=INVALID; ++i)
hegyi@11
   482
	    {
hegyi@11
   483
	      if(nodesmap[i]==target_item)
hegyi@11
   484
		{
hegyi@11
   485
		  target_node=i;
hegyi@11
   486
		}
hegyi@11
   487
	    }
hegyi@11
   488
	  *(nodesmap[target_node]) << Gnome::Canvas::Properties::fill_color("red");
hegyi@11
   489
hegyi@11
   490
	  //creating new edge
hegyi@11
   491
	  //	  Graph::Edge new_edge=g.addEdge(active_node, target_node);
hegyi@11
   492
	  active_edge=EdgeIt(g,g.addEdge(active_node, target_node));
hegyi@11
   493
	  
hegyi@11
   494
	  //calculating coordinates of new edge
hegyi@11
   495
	  Gnome::Canvas::Points coos;
hegyi@11
   496
	  double x1, x2, y1, y2;
hegyi@11
   497
	  
hegyi@11
   498
	  active_item->get_bounds(x1, y1, x2, y2);
hegyi@11
   499
	  coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
hegyi@11
   500
hegyi@11
   501
	  target_item->get_bounds(x1, y1, x2, y2);
hegyi@11
   502
	  coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
hegyi@11
   503
hegyi@11
   504
	  //drawing new edge
hegyi@11
   505
	  edgesmap[active_edge]=new Gnome::Canvas::Line(displayed_graph, coos);
hegyi@11
   506
	  *(edgesmap[active_edge]) << Gnome::Canvas::Properties::fill_color("green");
hegyi@11
   507
	  edgesmap[active_edge]->property_width_pixels().set_value(10);
hegyi@11
   508
hegyi@11
   509
	  //redraw nodes to blank terminations of the new edge
hegyi@11
   510
	  target_item->raise_to_top();
hegyi@11
   511
	  active_item->raise_to_top();
hegyi@11
   512
hegyi@11
   513
	  //initializing edge-text as well, to empty string
hegyi@11
   514
	  edgesmap[active_edge]->get_bounds(x1, y1, x2, y2);
hegyi@11
   515
	  edgetextmap[active_edge]=new Gnome::Canvas::Text(displayed_graph,(x1+x2)/2, (y1+y2)/2, "");
hegyi@11
   516
	  edgetextmap[active_edge]->property_fill_color().set_value("black");
hegyi@11
   517
	}
hegyi@11
   518
      break;
hegyi@11
   519
    case GDK_BUTTON_RELEASE:
hegyi@11
   520
      isbutton=false;
hegyi@11
   521
      if(target_item)
hegyi@11
   522
	{
hegyi@11
   523
	  *active_item << Gnome::Canvas::Properties::fill_color("blue");
hegyi@11
   524
	  *target_item << Gnome::Canvas::Properties::fill_color("blue");
hegyi@11
   525
	  active_item=NULL;
hegyi@11
   526
	  target_item=NULL;
hegyi@11
   527
	}
hegyi@11
   528
      break;
hegyi@11
   529
    default:
hegyi@11
   530
      break;
hegyi@11
   531
    }
hegyi@9
   532
  return false;
hegyi@9
   533
}
hegyi@9
   534