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