graph_displayer_canvas-event.cc
author hegyi
Thu, 28 Jul 2005 14:31:32 +0000
branchgui
changeset 47 9a0e6e92d06c
parent 46 121452cc4096
child 48 b8ec84524fa2
permissions -rwxr-xr-x
Redundant functions from main_win disappeared, entry is now bigger, properties refresh upon edition, edgewidth is zoomable.
hegyi@27
     1
#include <graph_displayer_canvas.h>
hegyi@27
     2
#include <broken_edge.h>
hegyi@27
     3
#include <math.h>
hegyi@27
     4
hegyi@27
     5
hegyi@27
     6
bool GraphDisplayerCanvas::on_expose_event(GdkEventExpose *event)
hegyi@27
     7
{
hegyi@27
     8
  Gnome::Canvas::CanvasAA::on_expose_event(event);
hegyi@27
     9
  //usleep(10000);
hegyi@27
    10
  //rezoom();
hegyi@27
    11
  return true;
hegyi@27
    12
}
hegyi@27
    13
hegyi@27
    14
void GraphDisplayerCanvas::changeEditorialTool(int newtool)
hegyi@27
    15
{
hegyi@34
    16
  if(actual_tool!=newtool)
hegyi@34
    17
    {
hegyi@27
    18
hegyi@34
    19
      actual_handler.disconnect();
hegyi@27
    20
hegyi@34
    21
      switch(actual_tool)
hegyi@34
    22
	{
hegyi@34
    23
	case CREATE_EDGE:
hegyi@34
    24
	  {
hegyi@34
    25
	    GdkEvent * generated=new GdkEvent();
hegyi@34
    26
	    generated->type=GDK_BUTTON_RELEASE;
hegyi@34
    27
	    generated->button.button=3;
hegyi@34
    28
	    createEdgeEventHandler(generated);      
hegyi@34
    29
	    break;
hegyi@34
    30
	  }
hegyi@34
    31
	case EDGE_MAP_EDIT:
hegyi@35
    32
	  //has to do the same thing as in the case of NODE_MAP_EDIT
hegyi@35
    33
	case NODE_MAP_EDIT:
hegyi@35
    34
	  {
hegyi@35
    35
	    GdkEvent * generated=new GdkEvent();
hegyi@35
    36
	    generated->type=GDK_KEY_PRESS;
hegyi@35
    37
	    ((GdkEventKey*)generated)->keyval=GDK_KP_Enter;
hegyi@35
    38
	    entryWidgetChangeHandler(generated);
hegyi@35
    39
	    entrywidget.hide();
hegyi@35
    40
	    break;
hegyi@35
    41
	  }
hegyi@34
    42
	default:
hegyi@34
    43
	  break;
hegyi@34
    44
	}
hegyi@27
    45
hegyi@34
    46
      active_item=NULL; 
hegyi@34
    47
      target_item=NULL; 
hegyi@34
    48
      active_edge=INVALID;	
hegyi@34
    49
      active_node=INVALID;	
hegyi@33
    50
hegyi@27
    51
hegyi@34
    52
      actual_tool=newtool;
hegyi@34
    53
  
hegyi@34
    54
      switch(newtool)
hegyi@34
    55
	{
hegyi@34
    56
	case MOVE:
hegyi@34
    57
	  actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::moveEventHandler), false);
hegyi@34
    58
	  break;
hegyi@27
    59
hegyi@34
    60
	case CREATE_NODE:
hegyi@34
    61
	  actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::createNodeEventHandler), false);
hegyi@34
    62
	  break;
hegyi@27
    63
hegyi@34
    64
	case CREATE_EDGE:
hegyi@34
    65
	  actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::createEdgeEventHandler), false);
hegyi@34
    66
	  break;
hegyi@27
    67
hegyi@34
    68
	case ERASER:
hegyi@34
    69
	  actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::eraserEventHandler), false);
hegyi@34
    70
	  break;
hegyi@32
    71
hegyi@34
    72
	case EDGE_MAP_EDIT:
hegyi@34
    73
	  grab_focus();
hegyi@34
    74
	  actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::edgeMapEditEventHandler), false);
hegyi@34
    75
	  break;
hegyi@32
    76
hegyi@34
    77
	case NODE_MAP_EDIT:
hegyi@34
    78
	  actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::nodeMapEditEventHandler), false);
hegyi@34
    79
	  break;
hegyi@34
    80
hegyi@34
    81
	default:
hegyi@34
    82
	  break;
hegyi@34
    83
	}
hegyi@27
    84
    }
hegyi@27
    85
}
hegyi@27
    86
hegyi@30
    87
int GraphDisplayerCanvas::getActualTool()
hegyi@27
    88
{
hegyi@27
    89
  return actual_tool;
hegyi@27
    90
}
hegyi@27
    91
hegyi@30
    92
bool GraphDisplayerCanvas::moveEventHandler(GdkEvent* e)
hegyi@27
    93
{
hegyi@27
    94
  switch(e->type)
hegyi@27
    95
  {
hegyi@27
    96
    case GDK_BUTTON_PRESS:
hegyi@27
    97
      //we mark the location of the event to be able to calculate parameters of dragging
hegyi@31
    98
      window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
hegyi@31
    99
hegyi@31
   100
      active_item=(get_item_at(clicked_x, clicked_y));
hegyi@27
   101
      active_node=INVALID;
hegyi@27
   102
      for (NodeIt i(g); i!=INVALID; ++i)
hegyi@27
   103
	{
hegyi@27
   104
	  if(nodesmap[i]==active_item)
hegyi@27
   105
	    {
hegyi@27
   106
	      active_node=i;
hegyi@27
   107
	    }
hegyi@27
   108
	}
hegyi@27
   109
      switch(e->button.button)
hegyi@27
   110
	{
hegyi@27
   111
	case 3:      
hegyi@27
   112
	  isbutton=3;
hegyi@27
   113
	  break;
hegyi@27
   114
	default:
hegyi@27
   115
	  isbutton=1;
hegyi@27
   116
	  break;
hegyi@27
   117
	}
hegyi@27
   118
      break;
hegyi@27
   119
    case GDK_BUTTON_RELEASE:
hegyi@27
   120
      isbutton=0;
hegyi@27
   121
      active_item=NULL;
hegyi@27
   122
      active_node=INVALID;
hegyi@27
   123
      break;
hegyi@27
   124
    case GDK_MOTION_NOTIFY:
hegyi@27
   125
      //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@27
   126
      if(active_node!=INVALID)
hegyi@27
   127
      {
hegyi@27
   128
	//new coordinates will be the old values,
hegyi@27
   129
	//because the item will be moved to the
hegyi@27
   130
	//new coordinate therefore the new movement
hegyi@27
   131
	//has to be calculated from here
hegyi@27
   132
hegyi@31
   133
	double new_x, new_y;
hegyi@31
   134
hegyi@31
   135
	window_to_world (e->motion.x, e->motion.y, new_x, new_y);
hegyi@31
   136
hegyi@31
   137
        double dx=new_x-clicked_x;
hegyi@31
   138
        double dy=new_y-clicked_y;
hegyi@27
   139
hegyi@28
   140
	//repositioning node and its text
hegyi@27
   141
        active_item->move(dx, dy);
hegyi@28
   142
	nodetextmap[active_node]->move(dx, dy);
hegyi@27
   143
hegyi@31
   144
        clicked_x=new_x;
hegyi@31
   145
        clicked_y=new_y;
hegyi@27
   146
hegyi@27
   147
	//all the edges connected to the moved point has to be redrawn
hegyi@27
   148
        EdgeIt ei;
hegyi@27
   149
hegyi@27
   150
        g.firstOut(ei,active_node);
hegyi@27
   151
hegyi@27
   152
        for(;ei!=INVALID;g.nextOut(ei))
hegyi@27
   153
        {
hegyi@27
   154
            Gnome::Canvas::Points coos;
hegyi@27
   155
            double x1, x2, y1, y2;
hegyi@27
   156
hegyi@27
   157
            nodesmap[g.source(ei)]->get_bounds(x1, y1, x2, y2);
hegyi@27
   158
            coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
hegyi@27
   159
hegyi@27
   160
            nodesmap[g.target(ei)]->get_bounds(x1, y1, x2, y2);
hegyi@27
   161
            coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
hegyi@27
   162
hegyi@27
   163
	    if(isbutton==3)
hegyi@27
   164
	      {
hegyi@30
   165
		edgesmap[ei]->setPoints(coos);
hegyi@27
   166
	      }
hegyi@27
   167
	    else
hegyi@27
   168
	      {
hegyi@30
   169
		edgesmap[ei]->setPoints(coos,true);
hegyi@27
   170
	      }
hegyi@27
   171
hegyi@28
   172
	    //reposition of edgetext
hegyi@30
   173
	    xy<double> text_pos=edgesmap[ei]->getArrowPos();
hegyi@27
   174
	    text_pos+=(xy<double>(10,10));
hegyi@27
   175
	    edgetextmap[ei]->property_x().set_value(text_pos.x);
hegyi@27
   176
	    edgetextmap[ei]->property_y().set_value(text_pos.y);
hegyi@27
   177
        }
hegyi@27
   178
hegyi@27
   179
        g.firstIn(ei,active_node);
hegyi@27
   180
        for(;ei!=INVALID;g.nextIn(ei))
hegyi@27
   181
        {
hegyi@27
   182
            Gnome::Canvas::Points coos;
hegyi@27
   183
            double x1, x2, y1, y2;
hegyi@27
   184
hegyi@27
   185
            nodesmap[g.source(ei)]->get_bounds(x1, y1, x2, y2);
hegyi@27
   186
            coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
hegyi@27
   187
hegyi@27
   188
            nodesmap[g.target(ei)]->get_bounds(x1, y1, x2, y2);
hegyi@27
   189
            coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
hegyi@27
   190
hegyi@27
   191
	    if(isbutton==3)
hegyi@27
   192
	      {
hegyi@30
   193
		edgesmap[ei]->setPoints(coos);
hegyi@27
   194
	      }
hegyi@27
   195
	    else
hegyi@27
   196
	      {
hegyi@30
   197
		edgesmap[ei]->setPoints(coos,true);
hegyi@27
   198
	      }
hegyi@27
   199
hegyi@30
   200
	    xy<double> text_pos=edgesmap[ei]->getArrowPos();
hegyi@27
   201
	    text_pos+=(xy<double>(10,10));
hegyi@27
   202
	    edgetextmap[ei]->property_x().set_value(text_pos.x);
hegyi@27
   203
	    edgetextmap[ei]->property_y().set_value(text_pos.y);
hegyi@27
   204
        }
hegyi@27
   205
      }
hegyi@27
   206
    default: break;
hegyi@27
   207
  }
hegyi@27
   208
hegyi@31
   209
  return false;
hegyi@27
   210
}
hegyi@27
   211
hegyi@30
   212
bool GraphDisplayerCanvas::createNodeEventHandler(GdkEvent* e)
hegyi@27
   213
{
hegyi@27
   214
  switch(e->type)
hegyi@27
   215
    {
hegyi@27
   216
hegyi@27
   217
      //draw the new node in red at the clicked place
hegyi@31
   218
    case GDK_2BUTTON_PRESS:
hegyi@31
   219
      std::cout << "double click" << std::endl;
hegyi@31
   220
      break;
hegyi@27
   221
    case GDK_BUTTON_PRESS:
hegyi@27
   222
      isbutton=1;
hegyi@27
   223
hegyi@27
   224
      active_node=NodeIt(g,g.addNode());
hegyi@27
   225
hegyi@27
   226
      //initiating values corresponding to new node in maps
hegyi@27
   227
hegyi@27
   228
      window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
hegyi@27
   229
hegyi@27
   230
      nodesmap[active_node]=new Gnome::Canvas::Ellipse(displayed_graph, clicked_x-20, clicked_y-20, clicked_x+20, clicked_y+20);
hegyi@27
   231
      active_item=(Gnome::Canvas::Item *)(nodesmap[active_node]);
hegyi@27
   232
      *(nodesmap[active_node]) << Gnome::Canvas::Properties::fill_color("red");
hegyi@27
   233
      *(nodesmap[active_node]) << Gnome::Canvas::Properties::outline_color("black");
hegyi@27
   234
      (nodesmap[active_node])->show();
hegyi@28
   235
hegyi@28
   236
      nodetextmap[active_node]=new Gnome::Canvas::Text(displayed_graph, clicked_x+node_property_defaults[N_RADIUS]+5, clicked_y+node_property_defaults[N_RADIUS]+5, "");
hegyi@28
   237
      nodetextmap[active_node]->property_fill_color().set_value("darkblue");
hegyi@28
   238
hegyi@30
   239
      mapwin->updateNode(active_node);
hegyi@28
   240
hegyi@27
   241
      break;
hegyi@27
   242
hegyi@27
   243
      //move the new node
hegyi@27
   244
    case GDK_MOTION_NOTIFY:
hegyi@27
   245
      {
hegyi@27
   246
	GdkEvent * generated=new GdkEvent();
hegyi@31
   247
	generated->motion.x=e->motion.x;
hegyi@31
   248
	generated->motion.y=e->motion.y;
hegyi@27
   249
	generated->type=GDK_MOTION_NOTIFY;
hegyi@30
   250
	moveEventHandler(generated);      
hegyi@27
   251
	break;
hegyi@27
   252
      }
hegyi@27
   253
hegyi@27
   254
      //finalize the new node
hegyi@27
   255
    case GDK_BUTTON_RELEASE:
hegyi@35
   256
      window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
hegyi@35
   257
hegyi@35
   258
      active_item->lower_to_bottom();
hegyi@35
   259
hegyi@35
   260
      target_item=NULL;
hegyi@35
   261
      target_item=get_item_at(clicked_x, clicked_y);
hegyi@35
   262
hegyi@35
   263
      active_item->raise_to_top();
hegyi@35
   264
hegyi@27
   265
      isbutton=0;
hegyi@35
   266
      if(target_item==active_item)
hegyi@31
   267
	{
hegyi@31
   268
	  //Its appropriate color is given by update.
hegyi@35
   269
	  *active_item << Gnome::Canvas::Properties::fill_color("blue");
hegyi@31
   270
	}
hegyi@31
   271
      else
hegyi@31
   272
	{
hegyi@31
   273
	  //In this case the given color has to be overwritten, because the noe covers an other item.
hegyi@31
   274
	  *active_item << Gnome::Canvas::Properties::fill_color("lightblue");
hegyi@31
   275
	}
hegyi@31
   276
      target_item=NULL;
hegyi@27
   277
      active_item=NULL;
hegyi@27
   278
      active_node=INVALID;
hegyi@27
   279
      break;
hegyi@27
   280
    default:
hegyi@27
   281
      break;
hegyi@27
   282
    }
hegyi@27
   283
  return false;
hegyi@27
   284
}
hegyi@27
   285
hegyi@30
   286
bool GraphDisplayerCanvas::createEdgeEventHandler(GdkEvent* e)
hegyi@27
   287
{
hegyi@27
   288
  switch(e->type)
hegyi@27
   289
    {
hegyi@27
   290
    case GDK_BUTTON_PRESS:
hegyi@27
   291
      //in edge creation right button has special meaning
hegyi@27
   292
      if(e->button.button!=3)
hegyi@27
   293
	{
hegyi@27
   294
	  //there is not yet selected node
hegyi@27
   295
	  if(active_node==INVALID)
hegyi@27
   296
	    {
hegyi@27
   297
	      //we mark the location of the event to be able to calculate parameters of dragging
hegyi@31
   298
hegyi@31
   299
	      window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
hegyi@31
   300
hegyi@31
   301
	      active_item=(get_item_at(clicked_x, clicked_y));
hegyi@27
   302
	      active_node=INVALID;
hegyi@27
   303
	      for (NodeIt i(g); i!=INVALID; ++i)
hegyi@27
   304
		{
hegyi@27
   305
		  if(nodesmap[i]==active_item)
hegyi@27
   306
		    {
hegyi@27
   307
		      active_node=i;
hegyi@27
   308
		    }
hegyi@27
   309
		}
hegyi@27
   310
	      //the clicked item is really a node
hegyi@27
   311
	      if(active_node!=INVALID)
hegyi@27
   312
		{
hegyi@27
   313
		  *(nodesmap[active_node]) << Gnome::Canvas::Properties::fill_color("red");
hegyi@27
   314
		  isbutton=1;
hegyi@27
   315
		}
hegyi@27
   316
	      //clicked item was not a node. It could be e.g. edge.
hegyi@27
   317
	      else
hegyi@27
   318
		{
hegyi@27
   319
		  active_item=NULL;
hegyi@27
   320
		}
hegyi@27
   321
	    }
hegyi@27
   322
	  //we only have to do sg. if the mouse button
hegyi@27
   323
	  // is pressed already once AND the click was
hegyi@27
   324
	  // on a node that was found in the set of 
hegyi@27
   325
	  //nodes, and now we only search for the second 
hegyi@27
   326
	  //node
hegyi@27
   327
	  else
hegyi@27
   328
	    {
hegyi@31
   329
	      window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
hegyi@31
   330
	      target_item=(get_item_at(clicked_x, clicked_y));
hegyi@27
   331
	      Graph::NodeIt target_node=INVALID;
hegyi@27
   332
	      for (NodeIt i(g); i!=INVALID; ++i)
hegyi@27
   333
		{
hegyi@27
   334
		  if(nodesmap[i]==target_item)
hegyi@27
   335
		    {
hegyi@27
   336
		      target_node=i;
hegyi@27
   337
		    }
hegyi@27
   338
		}
hegyi@27
   339
	      //the clicked item is a node, the edge can be drawn
hegyi@27
   340
	      if(target_node!=INVALID)
hegyi@27
   341
		{
hegyi@28
   342
		  if(target_node!=active_node)		
hegyi@28
   343
		    {
hegyi@28
   344
		      *(nodesmap[target_node]) << Gnome::Canvas::Properties::fill_color("red");
hegyi@27
   345
hegyi@28
   346
		      //creating new edge
hegyi@28
   347
		      active_edge=EdgeIt(g,g.addEdge(active_node, target_node));
hegyi@27
   348
hegyi@28
   349
		      //initiating values corresponding to new edge in maps
hegyi@30
   350
		      mapstorage.initMapsForEdge(active_edge);
hegyi@27
   351
	  
hegyi@28
   352
		      //calculating coordinates of new edge
hegyi@28
   353
		      Gnome::Canvas::Points coos;
hegyi@28
   354
		      double x1, x2, y1, y2;
hegyi@27
   355
	  
hegyi@28
   356
		      active_item->get_bounds(x1, y1, x2, y2);
hegyi@28
   357
		      coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
hegyi@27
   358
hegyi@28
   359
		      target_item->get_bounds(x1, y1, x2, y2);
hegyi@28
   360
		      coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
hegyi@27
   361
hegyi@28
   362
		      //drawing new edge
hegyi@28
   363
		      edgesmap[active_edge]=new BrokenEdge(displayed_graph, coos, *this);
hegyi@28
   364
		      *(edgesmap[active_edge]) << Gnome::Canvas::Properties::fill_color("green");
hegyi@28
   365
		      edgesmap[active_edge]->property_width_pixels().set_value(10);
hegyi@27
   366
hegyi@28
   367
		      //redraw nodes to blank terminations of the new edge
hegyi@28
   368
		      target_item->raise_to_top();
hegyi@28
   369
		      active_item->raise_to_top();
hegyi@27
   370
hegyi@28
   371
		      //initializing edge-text as well, to empty string
hegyi@30
   372
		      xy<double> text_pos=edgesmap[active_edge]->getArrowPos();
hegyi@28
   373
		      text_pos+=(xy<double>(10,10));
hegyi@27
   374
hegyi@28
   375
		      edgetextmap[active_edge]=new Gnome::Canvas::Text(displayed_graph, text_pos.x, text_pos.y, "");
hegyi@28
   376
		      edgetextmap[active_edge]->property_fill_color().set_value("darkgreen");
hegyi@28
   377
hegyi@28
   378
		      //updating its properties
hegyi@30
   379
		      mapwin->updateEdge(active_edge);
hegyi@28
   380
		    }
hegyi@28
   381
		  else
hegyi@28
   382
		    {
hegyi@28
   383
		      target_node=INVALID;
hegyi@28
   384
		      std::cout << "Loop edge is not yet implemented!" << std::endl;
hegyi@28
   385
		    }
hegyi@27
   386
		}
hegyi@27
   387
	      //clicked item was not a node. it could be an e.g. edge. we do not deal with it furthermore.
hegyi@27
   388
	      else
hegyi@27
   389
		{
hegyi@27
   390
		  target_item=NULL;
hegyi@27
   391
		}
hegyi@27
   392
	    }
hegyi@27
   393
	}
hegyi@27
   394
      break;
hegyi@27
   395
    case GDK_BUTTON_RELEASE:
hegyi@27
   396
      isbutton=0;
hegyi@27
   397
      //we clear settings in two cases
hegyi@27
   398
      //1: the edge is ready (target_item has valid value)
hegyi@27
   399
      //2: the edge creation is cancelled with right button
hegyi@27
   400
      if((target_item)||(e->button.button==3))
hegyi@27
   401
	{
hegyi@27
   402
	  if(active_item)
hegyi@27
   403
	    {
hegyi@27
   404
	      *active_item << Gnome::Canvas::Properties::fill_color("blue");
hegyi@27
   405
	      active_item=NULL;
hegyi@27
   406
	    }
hegyi@27
   407
	  if(target_item)
hegyi@27
   408
	    {
hegyi@27
   409
	      *target_item << Gnome::Canvas::Properties::fill_color("blue");
hegyi@27
   410
	      target_item=NULL;
hegyi@27
   411
	    }
hegyi@27
   412
	  active_node=INVALID;
hegyi@27
   413
	  active_edge=INVALID;
hegyi@27
   414
	}
hegyi@27
   415
      break;
hegyi@27
   416
    default:
hegyi@27
   417
      break;
hegyi@27
   418
    }
hegyi@27
   419
  return false;
hegyi@27
   420
}
hegyi@27
   421
hegyi@30
   422
bool GraphDisplayerCanvas::eraserEventHandler(GdkEvent* e)
hegyi@27
   423
{
hegyi@27
   424
  switch(e->type)
hegyi@27
   425
    {
hegyi@27
   426
    case GDK_BUTTON_PRESS:
hegyi@43
   427
      //finding the clicked items
hegyi@31
   428
      window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
hegyi@31
   429
      active_item=(get_item_at(clicked_x, clicked_y));
hegyi@27
   430
      active_node=INVALID;
hegyi@27
   431
      active_edge=INVALID;
hegyi@43
   432
hegyi@43
   433
      //was it a node?
hegyi@27
   434
      for (NodeIt i(g); i!=INVALID; ++i)
hegyi@27
   435
	{
hegyi@27
   436
	  if(nodesmap[i]==active_item)
hegyi@27
   437
	    {
hegyi@27
   438
	      active_node=i;
hegyi@27
   439
	    }
hegyi@27
   440
	}
hegyi@43
   441
      //or was it an edge?
hegyi@27
   442
      if(active_node==INVALID)
hegyi@27
   443
	{
hegyi@27
   444
	  for (EdgeIt i(g); i!=INVALID; ++i)
hegyi@27
   445
	    {
hegyi@27
   446
	      if(edgesmap[i]==active_item)
hegyi@27
   447
		{
hegyi@27
   448
		  active_edge=i;
hegyi@27
   449
		}
hegyi@27
   450
	    }
hegyi@27
   451
	}
hegyi@43
   452
hegyi@43
   453
      //recolor activated item
hegyi@31
   454
      if(active_item)
hegyi@31
   455
	{
hegyi@31
   456
	  *active_item << Gnome::Canvas::Properties::fill_color("red");
hegyi@31
   457
	}
hegyi@27
   458
      break;
hegyi@27
   459
hegyi@27
   460
    case GDK_BUTTON_RELEASE:
hegyi@31
   461
      window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
hegyi@31
   462
      if(active_item)
hegyi@27
   463
	{
hegyi@43
   464
	  //the cursor was not moved since pressing it
hegyi@31
   465
	  if( active_item == ( get_item_at (clicked_x, clicked_y) ) )
hegyi@27
   466
	    {
hegyi@43
   467
	      //a node was found
hegyi@31
   468
	      if(active_node!=INVALID)
hegyi@31
   469
		{
hegyi@27
   470
hegyi@31
   471
		  //collecting edges to delete
hegyi@31
   472
		  EdgeIt e;
hegyi@31
   473
		  std::set<Graph::Edge> edges_to_delete;
hegyi@27
   474
hegyi@31
   475
		  g.firstOut(e,active_node);
hegyi@31
   476
		  for(;e!=INVALID;g.nextOut(e))
hegyi@31
   477
		    {
hegyi@31
   478
		      edges_to_delete.insert(e);
hegyi@31
   479
		    }
hegyi@31
   480
hegyi@31
   481
		  g.firstIn(e,active_node);
hegyi@31
   482
		  for(;e!=INVALID;g.nextIn(e))
hegyi@31
   483
		    {
hegyi@31
   484
		      edges_to_delete.insert(e);
hegyi@31
   485
		    }
hegyi@31
   486
hegyi@31
   487
		  //deleting collected edges
hegyi@31
   488
		  for(std::set<Graph::Edge>::iterator edge_set_it=edges_to_delete.begin();edge_set_it!=edges_to_delete.end();edge_set_it++)
hegyi@31
   489
		    {
hegyi@31
   490
		      deleteItem(*edge_set_it);
hegyi@31
   491
		    }
hegyi@31
   492
		  deleteItem(active_node);
hegyi@31
   493
		}
hegyi@31
   494
	      //a simple edge was chosen
hegyi@31
   495
	      else
hegyi@27
   496
		{
hegyi@31
   497
		  deleteItem(active_edge);
hegyi@27
   498
		}
hegyi@27
   499
	    }
hegyi@31
   500
	  //pointer was moved, deletion is cancelled
hegyi@27
   501
	  else
hegyi@27
   502
	    {
hegyi@31
   503
	      if(active_node!=INVALID)
hegyi@31
   504
		{
hegyi@31
   505
		  *active_item << Gnome::Canvas::Properties::fill_color("blue");
hegyi@31
   506
		}
hegyi@31
   507
	      else
hegyi@31
   508
		{
hegyi@31
   509
		  *active_item << Gnome::Canvas::Properties::fill_color("green");
hegyi@31
   510
		}
hegyi@27
   511
	    }
hegyi@27
   512
	}
hegyi@27
   513
      //reseting datas
hegyi@27
   514
      active_item=NULL;
hegyi@27
   515
      active_edge=INVALID;
hegyi@27
   516
      active_node=INVALID;
hegyi@27
   517
      break;
hegyi@27
   518
hegyi@27
   519
    case GDK_MOTION_NOTIFY:
hegyi@27
   520
      break;
hegyi@27
   521
hegyi@27
   522
    default:
hegyi@27
   523
      break;
hegyi@27
   524
    }
hegyi@31
   525
  return false;
hegyi@27
   526
}
hegyi@27
   527
hegyi@32
   528
bool GraphDisplayerCanvas::edgeMapEditEventHandler(GdkEvent* e)
hegyi@32
   529
{
hegyi@32
   530
  switch(e->type)
hegyi@32
   531
    {
hegyi@34
   532
    case GDK_KEY_PRESS:
hegyi@43
   533
      //for Escape or Enter hide the displayed widget
hegyi@34
   534
      {
hegyi@35
   535
	nodeMapEditEventHandler(e);
hegyi@34
   536
	break;
hegyi@34
   537
      }
hegyi@32
   538
    case GDK_BUTTON_PRESS:
hegyi@43
   539
      //If the click happened on an edge we place the entrywidget there and fill in the value of the activated map at that edge.
hegyi@34
   540
      {
hegyi@43
   541
	//find the activated item
hegyi@34
   542
	window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
hegyi@34
   543
	active_item=(get_item_at(clicked_x, clicked_y));
hegyi@43
   544
hegyi@43
   545
	//determine, whether it was an edge
hegyi@34
   546
	Graph::EdgeIt clicked_edge=INVALID;
hegyi@34
   547
	for (EdgeIt i(g); i!=INVALID; ++i)
hegyi@34
   548
	  {
hegyi@34
   549
	    if(edgesmap[i]==active_item)
hegyi@34
   550
	      {
hegyi@34
   551
		clicked_edge=i;
hegyi@34
   552
	      }
hegyi@34
   553
	  }
hegyi@43
   554
	//if it was really an edge...
hegyi@34
   555
	if(clicked_edge!=INVALID)
hegyi@34
   556
	  {
hegyi@43
   557
	    //If there is already edited edge, it has to be saved first
hegyi@43
   558
	    if(entrywidget.is_visible())
hegyi@34
   559
	      {
hegyi@43
   560
		GdkEvent * generated=new GdkEvent();
hegyi@43
   561
		generated->type=GDK_KEY_PRESS;
hegyi@43
   562
		((GdkEventKey*)generated)->keyval=GDK_KP_Enter;
hegyi@43
   563
		entryWidgetChangeHandler(generated);
hegyi@43
   564
	      }
hegyi@43
   565
	    //If the previous value could be saved, we can go further, otherwise not
hegyi@43
   566
	    if(!entrywidget.is_visible())
hegyi@43
   567
	      {
hegyi@43
   568
		//and there is activated map
hegyi@43
   569
		if(edgetextmap[clicked_edge]->property_text().get_value()!="")
hegyi@34
   570
		  {
hegyi@43
   571
		    //activate the general variable for it
hegyi@43
   572
		    active_edge=clicked_edge;
hegyi@43
   573
		    //delete visible widget if there is
hegyi@43
   574
		    if(canvasentrywidget)
hegyi@43
   575
		      {
hegyi@43
   576
			delete(canvasentrywidget);
hegyi@43
   577
		      }
hegyi@43
   578
hegyi@43
   579
		    //initialize the entry
hegyi@43
   580
		    entrywidget.show();
hegyi@43
   581
hegyi@43
   582
		    //fill in the correct value
hegyi@43
   583
		    entrywidget.set_text(edgetextmap[active_edge]->property_text().get_value());
hegyi@43
   584
hegyi@43
   585
		    //replace and resize the entry to the activated edge and put it in a Canvas::Widget to be able to display it on gdc
hegyi@43
   586
		    xy<double> entry_coos;
hegyi@43
   587
		    entry_coos.x=(edgetextmap[active_edge])->property_x().get_value();
hegyi@43
   588
		    entry_coos.x-=edgetextmap[active_edge]->property_text_width().get_value()/2;
hegyi@43
   589
		    entry_coos.y=(edgetextmap[active_edge])->property_y().get_value();
hegyi@43
   590
		    entry_coos.y-=edgetextmap[active_edge]->property_text_height().get_value()*1.5/2;
hegyi@43
   591
		    canvasentrywidget=new Gnome::Canvas::Widget(displayed_graph, entry_coos.x, entry_coos.y, entrywidget);
hegyi@47
   592
		    canvasentrywidget->property_width().set_value(edgetextmap[active_edge]->property_text_width().get_value()*4);
hegyi@43
   593
		    canvasentrywidget->property_height().set_value(edgetextmap[active_edge]->property_text_height().get_value()*1.5);
hegyi@34
   594
		  }
hegyi@34
   595
	      }
hegyi@34
   596
	  }
hegyi@43
   597
	//if it was not an edge...
hegyi@34
   598
	else
hegyi@34
   599
	  {
hegyi@43
   600
	    //In this case the click did not happen on an edge
hegyi@43
   601
	    //if there is visible entry we save the value in it
hegyi@43
   602
	    //we pretend like an Enter was presse din the Entry widget
hegyi@35
   603
	    GdkEvent * generated=new GdkEvent();
hegyi@35
   604
	    generated->type=GDK_KEY_PRESS;
hegyi@35
   605
	    ((GdkEventKey*)generated)->keyval=GDK_KP_Enter;
hegyi@35
   606
	    entryWidgetChangeHandler(generated);
hegyi@34
   607
	  }
hegyi@34
   608
	break;
hegyi@34
   609
      }
hegyi@32
   610
    default:
hegyi@32
   611
      break;
hegyi@32
   612
    }
hegyi@32
   613
  return false;  
hegyi@32
   614
}
hegyi@32
   615
hegyi@32
   616
bool GraphDisplayerCanvas::nodeMapEditEventHandler(GdkEvent* e)
hegyi@32
   617
{
hegyi@35
   618
  switch(e->type)
hegyi@35
   619
    {
hegyi@35
   620
    case GDK_KEY_PRESS:
hegyi@43
   621
      //for Escape or Enter hide the displayed widget
hegyi@35
   622
      {
hegyi@35
   623
	switch(((GdkEventKey*)e)->keyval)
hegyi@35
   624
	  {
hegyi@35
   625
	  case GDK_Escape:
hegyi@35
   626
	    entrywidget.hide();
hegyi@35
   627
	    break;
hegyi@35
   628
	  case GDK_Return:
hegyi@35
   629
	  case GDK_KP_Enter:
hegyi@35
   630
	    entrywidget.hide();
hegyi@35
   631
	    break;
hegyi@35
   632
	  default:
hegyi@35
   633
	    break;
hegyi@35
   634
	  }
hegyi@35
   635
  
hegyi@35
   636
	break;
hegyi@35
   637
      }
hegyi@35
   638
    case GDK_BUTTON_PRESS:
hegyi@43
   639
      //If the click happened on an edge we place the entrywidget there and fill in the value of the activated map at that edge.
hegyi@35
   640
      {
hegyi@43
   641
	//find the activated item
hegyi@35
   642
	window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
hegyi@35
   643
	active_item=(get_item_at(clicked_x, clicked_y));
hegyi@43
   644
hegyi@43
   645
	//determine, whether it was a node
hegyi@35
   646
	Graph::NodeIt clicked_node=INVALID;
hegyi@35
   647
	for (NodeIt i(g); i!=INVALID; ++i)
hegyi@35
   648
	  {
hegyi@35
   649
	    if(nodesmap[i]==active_item)
hegyi@35
   650
	      {
hegyi@35
   651
		clicked_node=i;
hegyi@35
   652
	      }
hegyi@35
   653
	  }
hegyi@43
   654
hegyi@43
   655
	//if it was really an edge...
hegyi@35
   656
	if(clicked_node!=INVALID)
hegyi@35
   657
	  {
hegyi@35
   658
	    //If there is already edited edge, it has to be saved first
hegyi@35
   659
	    if(entrywidget.is_visible())
hegyi@35
   660
	      {
hegyi@35
   661
		GdkEvent * generated=new GdkEvent();
hegyi@35
   662
		generated->type=GDK_KEY_PRESS;
hegyi@35
   663
		((GdkEventKey*)generated)->keyval=GDK_KP_Enter;
hegyi@35
   664
		entryWidgetChangeHandler(generated);
hegyi@35
   665
	      }
hegyi@35
   666
	    //If the previous value could be saved, we can go further, otherwise not
hegyi@35
   667
	    if(!entrywidget.is_visible())
hegyi@35
   668
	      {
hegyi@43
   669
		//and there is activated map
hegyi@35
   670
		if(nodetextmap[clicked_node]->property_text().get_value()!="")
hegyi@35
   671
		  {
hegyi@43
   672
		    //activate the general variable for it
hegyi@35
   673
		    active_node=clicked_node;
hegyi@43
   674
		    //delete visible widget if there is
hegyi@35
   675
		    if(canvasentrywidget)
hegyi@35
   676
		      {
hegyi@35
   677
			delete(canvasentrywidget);
hegyi@35
   678
		      }
hegyi@43
   679
hegyi@43
   680
		    //initialize the entry
hegyi@35
   681
		    entrywidget.show();
hegyi@43
   682
hegyi@43
   683
		    //fill in the correct value
hegyi@35
   684
		    entrywidget.set_text(nodetextmap[active_node]->property_text().get_value());
hegyi@43
   685
hegyi@43
   686
		    //replace and resize the entry to the activated node and put it in a Canvas::Widget to be able to display it on gdc
hegyi@35
   687
		    xy<double> entry_coos;
hegyi@35
   688
		    entry_coos.x=(nodetextmap[active_node])->property_x().get_value();
hegyi@35
   689
		    entry_coos.x-=nodetextmap[active_node]->property_text_width().get_value()/2;
hegyi@35
   690
		    entry_coos.y=(nodetextmap[active_node])->property_y().get_value();
hegyi@35
   691
		    entry_coos.y-=nodetextmap[active_node]->property_text_height().get_value()*1.5/2;
hegyi@35
   692
		    canvasentrywidget=new Gnome::Canvas::Widget(displayed_graph, entry_coos.x, entry_coos.y, entrywidget);
hegyi@35
   693
		    canvasentrywidget->property_width().set_value(nodetextmap[active_node]->property_text_width().get_value()*1.5);
hegyi@35
   694
		    canvasentrywidget->property_height().set_value(nodetextmap[active_node]->property_text_height().get_value()*1.5);
hegyi@35
   695
		  }
hegyi@35
   696
	      }
hegyi@35
   697
	  }
hegyi@43
   698
	//if it was not an edge...
hegyi@35
   699
	else
hegyi@35
   700
	  {
hegyi@43
   701
	    //In this case the click did not happen on an edge
hegyi@43
   702
	    //if there is visible entry we save the value in it
hegyi@43
   703
	    //we pretend like an Enter was presse din the Entry widget
hegyi@35
   704
	    GdkEvent * generated=new GdkEvent();
hegyi@35
   705
	    generated->type=GDK_KEY_PRESS;
hegyi@35
   706
	    ((GdkEventKey*)generated)->keyval=GDK_KP_Enter;
hegyi@35
   707
	    entryWidgetChangeHandler(generated);
hegyi@35
   708
	  }
hegyi@35
   709
	break;
hegyi@35
   710
      }
hegyi@35
   711
    default:
hegyi@35
   712
      break;
hegyi@35
   713
    }
hegyi@35
   714
  return false;  
hegyi@32
   715
}
hegyi@32
   716
hegyi@34
   717
bool GraphDisplayerCanvas::entryWidgetChangeHandler(GdkEvent* e)
hegyi@34
   718
{
hegyi@35
   719
  if(entrywidget.is_visible())
hegyi@35
   720
    {
hegyi@35
   721
      if(e->type==GDK_KEY_PRESS)
hegyi@35
   722
	{
hegyi@35
   723
	  switch(((GdkEventKey*)e)->keyval)
hegyi@35
   724
	    {
hegyi@35
   725
	    case GDK_Escape:
hegyi@35
   726
	      entrywidget.hide();
hegyi@35
   727
	      break;
hegyi@35
   728
	    case GDK_KP_Enter:
hegyi@35
   729
	    case GDK_Return:
hegyi@35
   730
	      {
hegyi@43
   731
		//these variables check whether the text in the entry is valid
hegyi@41
   732
		bool valid_double=true;
hegyi@41
   733
		int point_num=0;
hegyi@43
   734
hegyi@43
   735
		//getting the value from the entry and converting it to double
hegyi@41
   736
		Glib::ustring mapvalue_str = entrywidget.get_text();
hegyi@35
   737
hegyi@41
   738
		char * mapvalue_ch=new char [mapvalue_str.length()];
hegyi@41
   739
		for(int i=0;i<(int)(mapvalue_str.length());i++)
hegyi@35
   740
		  {
hegyi@41
   741
		    if(((mapvalue_str[i]<'0')||(mapvalue_str[i]>'9'))&&(mapvalue_str[i]!='.'))
hegyi@35
   742
		      {
hegyi@41
   743
			valid_double=false;
hegyi@35
   744
		      }
hegyi@35
   745
		    else
hegyi@35
   746
		      {
hegyi@41
   747
			if(mapvalue_str[i]=='.')
hegyi@41
   748
			  {
hegyi@41
   749
			    point_num++;
hegyi@41
   750
			  }
hegyi@35
   751
		      }
hegyi@41
   752
		    mapvalue_ch[i]=mapvalue_str[i];
hegyi@35
   753
		  }
hegyi@45
   754
  	      
hegyi@43
   755
		//if the text in the entry was correct
hegyi@41
   756
		if((point_num<=1)&&(valid_double))
hegyi@35
   757
		  {
hegyi@45
   758
		    double mapvalue_d=atof(mapvalue_ch);
hegyi@45
   759
hegyi@45
   760
		    //reconvert the double to string for the correct format
hegyi@45
   761
		    std::ostringstream ostr;
hegyi@45
   762
		    ostr << mapvalue_d;
hegyi@45
   763
hegyi@43
   764
		    //save the value to the correct place
hegyi@35
   765
		    switch(actual_tool)
hegyi@35
   766
		      {
hegyi@35
   767
		      case EDGE_MAP_EDIT:
hegyi@45
   768
			edgetextmap[active_edge]->property_text().set_value(ostr.str());
hegyi@41
   769
			(*(mapstorage.edgemap_storage)[edgemap_to_edit])[active_edge]=mapvalue_d;
hegyi@47
   770
			mapwin->updateEdge(active_edge);
hegyi@35
   771
			break;
hegyi@35
   772
		      case NODE_MAP_EDIT:
hegyi@45
   773
			nodetextmap[active_node]->property_text().set_value(ostr.str());
hegyi@41
   774
			(*(mapstorage.nodemap_storage)[nodemap_to_edit])[active_node]=mapvalue_d;
hegyi@47
   775
			mapwin->updateNode(active_node);
hegyi@35
   776
			break;
hegyi@35
   777
		      default:
hegyi@35
   778
			break;
hegyi@35
   779
		      }
hegyi@35
   780
		    entrywidget.hide();
hegyi@35
   781
		  }
hegyi@43
   782
		//the text in the entry was not correct for a double
hegyi@35
   783
		else
hegyi@35
   784
		  {
hegyi@35
   785
		    std::cout << "ERROR: only handling of double values is implemented yet!" << std::endl;
hegyi@35
   786
		  }
hegyi@35
   787
hegyi@35
   788
		break;
hegyi@35
   789
	      }
hegyi@35
   790
	    default:
hegyi@35
   791
	      break;
hegyi@35
   792
	    }
hegyi@35
   793
	}
hegyi@35
   794
    }
hegyi@34
   795
  return false;
hegyi@34
   796
}
hegyi@34
   797
hegyi@30
   798
void GraphDisplayerCanvas::deleteItem(NodeIt node_to_delete)
hegyi@27
   799
{
hegyi@28
   800
  delete(nodetextmap[node_to_delete]);
hegyi@27
   801
  delete(nodesmap[node_to_delete]);
hegyi@27
   802
  g.erase(node_to_delete);
hegyi@27
   803
}
hegyi@27
   804
hegyi@30
   805
void GraphDisplayerCanvas::deleteItem(EdgeIt edge_to_delete)
hegyi@27
   806
{
hegyi@28
   807
  delete(edgetextmap[edge_to_delete]);
hegyi@27
   808
  delete(edgesmap[edge_to_delete]);
hegyi@27
   809
  g.erase(edge_to_delete);
hegyi@27
   810
}
hegyi@27
   811
hegyi@30
   812
void GraphDisplayerCanvas::deleteItem(Graph::Edge edge_to_delete)
hegyi@27
   813
{
hegyi@28
   814
  delete(edgetextmap[edge_to_delete]);
hegyi@27
   815
  delete(edgesmap[edge_to_delete]);
hegyi@27
   816
  g.erase(edge_to_delete);
hegyi@27
   817
}
hegyi@27
   818
hegyi@30
   819
void GraphDisplayerCanvas::textReposition(xy<double> new_place)
hegyi@27
   820
{
hegyi@27
   821
  new_place+=(xy<double>(10,10));
hegyi@35
   822
  edgetextmap[forming_edge]->property_x().set_value(new_place.x);
hegyi@35
   823
  edgetextmap[forming_edge]->property_y().set_value(new_place.y);
hegyi@27
   824
}
hegyi@27
   825
hegyi@30
   826
void GraphDisplayerCanvas::toggleEdgeActivity(BrokenEdge* active_bre, bool on)
hegyi@27
   827
{
hegyi@27
   828
  if(on)
hegyi@27
   829
    {
hegyi@35
   830
      if(forming_edge!=INVALID)
hegyi@27
   831
	{
hegyi@27
   832
	  std::cout << "ERROR!!!! Valid edge found!" << std::endl;
hegyi@27
   833
	}
hegyi@27
   834
      else
hegyi@27
   835
	{
hegyi@27
   836
	  for (EdgeIt i(g); i!=INVALID; ++i)
hegyi@27
   837
	    {
hegyi@27
   838
	      if(edgesmap[i]==active_bre)
hegyi@27
   839
		{
hegyi@35
   840
		  forming_edge=i;
hegyi@27
   841
		}
hegyi@27
   842
	    }
hegyi@27
   843
	}
hegyi@27
   844
    }
hegyi@27
   845
  else
hegyi@27
   846
    {
hegyi@35
   847
      if(forming_edge!=INVALID)
hegyi@27
   848
	{
hegyi@35
   849
	  forming_edge=INVALID;
hegyi@27
   850
	}
hegyi@27
   851
      else
hegyi@27
   852
	{
hegyi@27
   853
	  std::cout << "ERROR!!!! Invalid edge found!" << std::endl;
hegyi@27
   854
	}
hegyi@27
   855
    }
hegyi@27
   856
hegyi@27
   857
}
hegyi@32
   858
hegyi@46
   859
int GraphDisplayerCanvas::addNewEdgeMap(double default_value, std::string mapname)
hegyi@37
   860
{
hegyi@43
   861
  //create the new map
hegyi@41
   862
  Graph::EdgeMap<double> * emptr=new Graph::EdgeMap<double> (g,default_value);
hegyi@46
   863
hegyi@46
   864
  //if addition was not successful addEdgeMap returns one.
hegyi@46
   865
  //cause can be that there is already a map named like the new one
hegyi@46
   866
  if(mapstorage.addEdgeMap(mapname,emptr))
hegyi@46
   867
    {
hegyi@46
   868
      return 1;
hegyi@46
   869
    }
hegyi@46
   870
hegyi@43
   871
hegyi@43
   872
  //add it to the list of the displayable maps
hegyi@41
   873
  mapwin->registerNewEdgeMap(mapname);
hegyi@43
   874
hegyi@43
   875
  //display it
hegyi@41
   876
  changeEdgeText(mapname);
hegyi@46
   877
hegyi@46
   878
  return 0;
hegyi@37
   879
}
hegyi@37
   880
hegyi@46
   881
int GraphDisplayerCanvas::addNewNodeMap(double default_value, std::string mapname)
hegyi@37
   882
{
hegyi@43
   883
  //create the new map
hegyi@41
   884
  Graph::NodeMap<double> * emptr=new Graph::NodeMap<double> (g,default_value);
hegyi@46
   885
hegyi@46
   886
  //if addition was not successful addNodeMap returns one.
hegyi@46
   887
  //cause can be that there is already a map named like the new one
hegyi@46
   888
  if(mapstorage.addNodeMap(mapname,emptr))
hegyi@46
   889
    {
hegyi@46
   890
      return 1;
hegyi@46
   891
    }
hegyi@43
   892
hegyi@43
   893
  //add it to the list of the displayable maps
hegyi@41
   894
  mapwin->registerNewNodeMap(mapname);
hegyi@43
   895
hegyi@43
   896
  //display it
hegyi@41
   897
  changeNodeText(mapname);
hegyi@46
   898
hegyi@46
   899
  return 0;
hegyi@37
   900
}
hegyi@37
   901