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