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