gui/graph_displayer_canvas-event.cc
author hegyi
Mon, 21 Nov 2005 12:07:05 +0000
changeset 1819 fd82adfbe905
parent 1777 a70cee06ae9c
child 1826 e07a7a8acd77
permissions -rwxr-xr-x
Reorganizing.
ladanyi@1606
     1
#include "graph_displayer_canvas.h"
alpar@1632
     2
#include <cmath>
hegyi@1510
     3
hegyi@1510
     4
hegyi@1510
     5
bool GraphDisplayerCanvas::on_expose_event(GdkEventExpose *event)
hegyi@1510
     6
{
hegyi@1510
     7
  Gnome::Canvas::CanvasAA::on_expose_event(event);
hegyi@1510
     8
  //usleep(10000);
hegyi@1510
     9
  //rezoom();
hegyi@1510
    10
  return true;
hegyi@1510
    11
}
hegyi@1510
    12
hegyi@1510
    13
void GraphDisplayerCanvas::changeEditorialTool(int newtool)
hegyi@1510
    14
{
hegyi@1562
    15
  if(actual_tool!=newtool)
hegyi@1562
    16
    {
hegyi@1510
    17
hegyi@1562
    18
      actual_handler.disconnect();
hegyi@1510
    19
hegyi@1562
    20
      switch(actual_tool)
hegyi@1562
    21
	{
hegyi@1562
    22
	case CREATE_EDGE:
hegyi@1562
    23
	  {
hegyi@1562
    24
	    GdkEvent * generated=new GdkEvent();
hegyi@1562
    25
	    generated->type=GDK_BUTTON_RELEASE;
hegyi@1562
    26
	    generated->button.button=3;
hegyi@1562
    27
	    createEdgeEventHandler(generated);      
hegyi@1562
    28
	    break;
hegyi@1562
    29
	  }
hegyi@1562
    30
	case EDGE_MAP_EDIT:
hegyi@1579
    31
	  //has to do the same thing as in the case of NODE_MAP_EDIT
hegyi@1579
    32
	case NODE_MAP_EDIT:
hegyi@1579
    33
	  {
hegyi@1579
    34
	    break;
hegyi@1579
    35
	  }
hegyi@1562
    36
	default:
hegyi@1562
    37
	  break;
hegyi@1562
    38
	}
hegyi@1510
    39
hegyi@1562
    40
      active_item=NULL; 
hegyi@1562
    41
      target_item=NULL; 
hegyi@1562
    42
      active_edge=INVALID;	
hegyi@1562
    43
      active_node=INVALID;	
hegyi@1551
    44
hegyi@1510
    45
hegyi@1562
    46
      actual_tool=newtool;
hegyi@1562
    47
  
hegyi@1562
    48
      switch(newtool)
hegyi@1562
    49
	{
hegyi@1562
    50
	case MOVE:
hegyi@1562
    51
	  actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::moveEventHandler), false);
hegyi@1562
    52
	  break;
hegyi@1510
    53
hegyi@1562
    54
	case CREATE_NODE:
hegyi@1562
    55
	  actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::createNodeEventHandler), false);
hegyi@1562
    56
	  break;
hegyi@1510
    57
hegyi@1562
    58
	case CREATE_EDGE:
hegyi@1562
    59
	  actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::createEdgeEventHandler), false);
hegyi@1562
    60
	  break;
hegyi@1510
    61
hegyi@1562
    62
	case ERASER:
hegyi@1562
    63
	  actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::eraserEventHandler), false);
hegyi@1562
    64
	  break;
hegyi@1550
    65
hegyi@1562
    66
	case EDGE_MAP_EDIT:
hegyi@1562
    67
	  grab_focus();
hegyi@1562
    68
	  actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::edgeMapEditEventHandler), false);
hegyi@1562
    69
	  break;
hegyi@1550
    70
hegyi@1562
    71
	case NODE_MAP_EDIT:
hegyi@1562
    72
	  actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::nodeMapEditEventHandler), false);
hegyi@1562
    73
	  break;
hegyi@1562
    74
hegyi@1562
    75
	default:
hegyi@1562
    76
	  break;
hegyi@1562
    77
	}
hegyi@1510
    78
    }
hegyi@1510
    79
}
hegyi@1510
    80
hegyi@1524
    81
int GraphDisplayerCanvas::getActualTool()
hegyi@1510
    82
{
hegyi@1510
    83
  return actual_tool;
hegyi@1510
    84
}
hegyi@1510
    85
hegyi@1524
    86
bool GraphDisplayerCanvas::moveEventHandler(GdkEvent* e)
hegyi@1510
    87
{
ladanyi@1652
    88
  static Gnome::Canvas::Text *coord_text = 0;
hegyi@1510
    89
  switch(e->type)
hegyi@1510
    90
  {
hegyi@1510
    91
    case GDK_BUTTON_PRESS:
hegyi@1510
    92
      //we mark the location of the event to be able to calculate parameters of dragging
hegyi@1525
    93
      window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
hegyi@1525
    94
hegyi@1525
    95
      active_item=(get_item_at(clicked_x, clicked_y));
hegyi@1510
    96
      active_node=INVALID;
ladanyi@1606
    97
      for (NodeIt i(mapstorage.graph); i!=INVALID; ++i)
hegyi@1510
    98
	{
hegyi@1510
    99
	  if(nodesmap[i]==active_item)
hegyi@1510
   100
	    {
hegyi@1510
   101
	      active_node=i;
hegyi@1510
   102
	    }
hegyi@1510
   103
	}
hegyi@1510
   104
      switch(e->button.button)
hegyi@1510
   105
	{
hegyi@1510
   106
	case 3:      
hegyi@1510
   107
	  isbutton=3;
hegyi@1510
   108
	  break;
hegyi@1510
   109
	default:
hegyi@1510
   110
	  isbutton=1;
hegyi@1510
   111
	  break;
hegyi@1510
   112
	}
hegyi@1510
   113
      break;
hegyi@1510
   114
    case GDK_BUTTON_RELEASE:
ladanyi@1652
   115
      if (coord_text)
ladanyi@1652
   116
      {
ladanyi@1652
   117
        delete coord_text;
ladanyi@1652
   118
        coord_text = 0;
ladanyi@1652
   119
      }
hegyi@1510
   120
      isbutton=0;
hegyi@1510
   121
      active_item=NULL;
hegyi@1510
   122
      active_node=INVALID;
hegyi@1510
   123
      break;
hegyi@1510
   124
    case GDK_MOTION_NOTIFY:
hegyi@1510
   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@1510
   126
      if(active_node!=INVALID)
hegyi@1510
   127
      {
ladanyi@1606
   128
        mapstorage.modified = true;
ladanyi@1652
   129
hegyi@1510
   130
	//new coordinates will be the old values,
hegyi@1510
   131
	//because the item will be moved to the
hegyi@1510
   132
	//new coordinate therefore the new movement
hegyi@1510
   133
	//has to be calculated from here
hegyi@1510
   134
hegyi@1525
   135
	double new_x, new_y;
hegyi@1525
   136
hegyi@1525
   137
	window_to_world (e->motion.x, e->motion.y, new_x, new_y);
hegyi@1525
   138
hegyi@1525
   139
        double dx=new_x-clicked_x;
hegyi@1525
   140
        double dy=new_y-clicked_y;
hegyi@1510
   141
hegyi@1512
   142
	//repositioning node and its text
hegyi@1510
   143
        active_item->move(dx, dy);
hegyi@1512
   144
	nodetextmap[active_node]->move(dx, dy);
hegyi@1510
   145
ladanyi@1652
   146
        // the new coordinates of the centre of the node 
ladanyi@1652
   147
        double coord_x = new_x - (clicked_x - mapstorage.coords[active_node].x);
ladanyi@1652
   148
        double coord_y = new_y - (clicked_y - mapstorage.coords[active_node].y);
ladanyi@1652
   149
hegyi@1525
   150
        clicked_x=new_x;
hegyi@1525
   151
        clicked_y=new_y;
hegyi@1510
   152
ladanyi@1652
   153
        // write back the new coordinates to the coords map
ladanyi@1652
   154
        mapstorage.coords.set(active_node, xy<double>(coord_x, coord_y));
hegyi@1510
   155
ladanyi@1652
   156
        // reposition the coordinates text
ladanyi@1652
   157
        std::ostringstream ostr;
ladanyi@1652
   158
        ostr << "(" <<
ladanyi@1652
   159
          mapstorage.coords[active_node].x << ", " <<
ladanyi@1652
   160
          mapstorage.coords[active_node].y << ")";
ladanyi@1654
   161
        double radius =
ladanyi@1654
   162
          (nodesmap[active_node]->property_x2().get_value() -
ladanyi@1654
   163
          nodesmap[active_node]->property_x1().get_value()) / 2.0;
ladanyi@1652
   164
        if (coord_text)
ladanyi@1652
   165
        {
ladanyi@1652
   166
          coord_text->property_text().set_value(ostr.str());
ladanyi@1652
   167
          coord_text->property_x().set_value(mapstorage.coords[active_node].x +
ladanyi@1654
   168
              radius);
ladanyi@1654
   169
          coord_text->property_y().set_value(mapstorage.coords[active_node].y -
ladanyi@1654
   170
              radius);
ladanyi@1652
   171
        }
ladanyi@1652
   172
        else
ladanyi@1652
   173
        {
ladanyi@1652
   174
          coord_text = new Gnome::Canvas::Text(
ladanyi@1652
   175
              displayed_graph,
ladanyi@1654
   176
              mapstorage.coords[active_node].x + radius,
ladanyi@1654
   177
              mapstorage.coords[active_node].y - radius,
ladanyi@1652
   178
              ostr.str());
ladanyi@1652
   179
          coord_text->property_fill_color().set_value("black");
ladanyi@1654
   180
          coord_text->property_anchor().set_value(Gtk::ANCHOR_SOUTH_WEST);
ladanyi@1652
   181
        }
ladanyi@1652
   182
ladanyi@1652
   183
	//all the edges connected to the moved point has to be redrawn
alpar@1651
   184
        for(OutEdgeIt ei(mapstorage.graph,active_node);ei!=INVALID;++ei)
hegyi@1510
   185
        {
hegyi@1510
   186
            Gnome::Canvas::Points coos;
hegyi@1510
   187
            double x1, x2, y1, y2;
hegyi@1510
   188
ladanyi@1606
   189
            nodesmap[mapstorage.graph.source(ei)]->get_bounds(x1, y1, x2, y2);
hegyi@1510
   190
            coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
hegyi@1510
   191
ladanyi@1606
   192
            nodesmap[mapstorage.graph.target(ei)]->get_bounds(x1, y1, x2, y2);
hegyi@1510
   193
            coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
hegyi@1510
   194
hegyi@1510
   195
	    if(isbutton==3)
hegyi@1510
   196
	      {
hegyi@1524
   197
		edgesmap[ei]->setPoints(coos);
hegyi@1510
   198
	      }
hegyi@1510
   199
	    else
hegyi@1510
   200
	      {
hegyi@1524
   201
		edgesmap[ei]->setPoints(coos,true);
hegyi@1510
   202
	      }
hegyi@1510
   203
hegyi@1512
   204
	    //reposition of edgetext
hegyi@1524
   205
	    xy<double> text_pos=edgesmap[ei]->getArrowPos();
hegyi@1510
   206
	    text_pos+=(xy<double>(10,10));
hegyi@1510
   207
	    edgetextmap[ei]->property_x().set_value(text_pos.x);
hegyi@1510
   208
	    edgetextmap[ei]->property_y().set_value(text_pos.y);
hegyi@1510
   209
        }
hegyi@1510
   210
alpar@1651
   211
        for(InEdgeIt ei(mapstorage.graph,active_node);ei!=INVALID;++ei)
hegyi@1510
   212
        {
hegyi@1510
   213
            Gnome::Canvas::Points coos;
hegyi@1510
   214
            double x1, x2, y1, y2;
hegyi@1510
   215
ladanyi@1606
   216
            nodesmap[mapstorage.graph.source(ei)]->get_bounds(x1, y1, x2, y2);
hegyi@1510
   217
            coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
hegyi@1510
   218
ladanyi@1606
   219
            nodesmap[mapstorage.graph.target(ei)]->get_bounds(x1, y1, x2, y2);
hegyi@1510
   220
            coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
hegyi@1510
   221
hegyi@1510
   222
	    if(isbutton==3)
hegyi@1510
   223
	      {
hegyi@1524
   224
		edgesmap[ei]->setPoints(coos);
hegyi@1510
   225
	      }
hegyi@1510
   226
	    else
hegyi@1510
   227
	      {
hegyi@1524
   228
		edgesmap[ei]->setPoints(coos,true);
hegyi@1510
   229
	      }
hegyi@1510
   230
hegyi@1524
   231
	    xy<double> text_pos=edgesmap[ei]->getArrowPos();
hegyi@1510
   232
	    text_pos+=(xy<double>(10,10));
hegyi@1510
   233
	    edgetextmap[ei]->property_x().set_value(text_pos.x);
hegyi@1510
   234
	    edgetextmap[ei]->property_y().set_value(text_pos.y);
hegyi@1510
   235
        }
hegyi@1510
   236
      }
hegyi@1510
   237
    default: break;
hegyi@1510
   238
  }
hegyi@1510
   239
hegyi@1525
   240
  return false;
hegyi@1510
   241
}
hegyi@1510
   242
hegyi@1524
   243
bool GraphDisplayerCanvas::createNodeEventHandler(GdkEvent* e)
hegyi@1510
   244
{
hegyi@1510
   245
  switch(e->type)
ladanyi@1645
   246
  {
ladanyi@1645
   247
    //move the new node
ladanyi@1645
   248
    case GDK_MOTION_NOTIFY:
ladanyi@1645
   249
      {
ladanyi@1645
   250
        GdkEvent * generated=new GdkEvent();
ladanyi@1645
   251
        generated->motion.x=e->motion.x;
ladanyi@1645
   252
        generated->motion.y=e->motion.y;
ladanyi@1645
   253
        generated->type=GDK_MOTION_NOTIFY;
ladanyi@1645
   254
        moveEventHandler(generated);      
ladanyi@1645
   255
        break;
ladanyi@1645
   256
      }
hegyi@1510
   257
ladanyi@1645
   258
    case GDK_BUTTON_RELEASE:
ladanyi@1606
   259
      mapstorage.modified = true;
ladanyi@1606
   260
hegyi@1510
   261
      isbutton=1;
hegyi@1510
   262
alpar@1643
   263
      active_node=mapstorage.graph.addNode();
hegyi@1510
   264
hegyi@1510
   265
      //initiating values corresponding to new node in maps
hegyi@1510
   266
hegyi@1510
   267
      window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
hegyi@1510
   268
ladanyi@1645
   269
      // update coordinates
ladanyi@1606
   270
      mapstorage.coords.set(active_node, xy<double>(clicked_x, clicked_y));
ladanyi@1645
   271
ladanyi@1645
   272
      // update all other maps
ladanyi@1645
   273
      for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
ladanyi@1645
   274
          mapstorage.nodemap_storage.begin(); it !=
ladanyi@1645
   275
          mapstorage.nodemap_storage.end(); ++it)
ladanyi@1645
   276
      {
ladanyi@1646
   277
        if ((it->first != "coordinates_x") &&
ladanyi@1646
   278
            (it->first != "coordinates_y"))
ladanyi@1645
   279
        {
ladanyi@1645
   280
          (*(it->second))[active_node] =
ladanyi@1645
   281
            mapstorage.nodemap_default[it->first];
ladanyi@1645
   282
        }
ladanyi@1645
   283
      }
ladanyi@1646
   284
      // increment the id map's default value
ladanyi@1646
   285
      mapstorage.nodemap_default["id"] += 1.0;
ladanyi@1645
   286
ladanyi@1606
   287
      nodesmap[active_node]=new Gnome::Canvas::Ellipse(displayed_graph,
ladanyi@1645
   288
          clicked_x-20, clicked_y-20, clicked_x+20, clicked_y+20);
hegyi@1510
   289
      active_item=(Gnome::Canvas::Item *)(nodesmap[active_node]);
ladanyi@1645
   290
      *(nodesmap[active_node]) <<
ladanyi@1645
   291
        Gnome::Canvas::Properties::fill_color("blue");
ladanyi@1645
   292
      *(nodesmap[active_node]) <<
ladanyi@1645
   293
        Gnome::Canvas::Properties::outline_color("black");
ladanyi@1645
   294
      active_item->raise_to_top();
ladanyi@1645
   295
hegyi@1510
   296
      (nodesmap[active_node])->show();
hegyi@1512
   297
ladanyi@1606
   298
      nodetextmap[active_node]=new Gnome::Canvas::Text(displayed_graph,
ladanyi@1645
   299
          clicked_x+node_property_defaults[N_RADIUS]+5,
ladanyi@1645
   300
          clicked_y+node_property_defaults[N_RADIUS]+5, "");
hegyi@1512
   301
      nodetextmap[active_node]->property_fill_color().set_value("darkblue");
ladanyi@1645
   302
      nodetextmap[active_node]->raise_to_top();
hegyi@1512
   303
ladanyi@1606
   304
      mapwin.updateNode(active_node);
hegyi@1512
   305
hegyi@1510
   306
      isbutton=0;
hegyi@1525
   307
      target_item=NULL;
hegyi@1510
   308
      active_item=NULL;
hegyi@1510
   309
      active_node=INVALID;
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