gui/graph_displayer_canvas-event.cc
author deba
Mon, 05 Dec 2005 17:03:58 +0000
changeset 1848 291764ce6d91
parent 1826 e07a7a8acd77
child 1849 a4d1362397fe
permissions -rwxr-xr-x
Doc bug fix
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;
hegyi@1837
    97
      for (NodeIt i((mainwin.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
      {
hegyi@1837
   128
        (mainwin.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 
hegyi@1837
   147
        double coord_x = new_x - (clicked_x - (mainwin.mapstorage).coords[active_node].x);
hegyi@1837
   148
        double coord_y = new_y - (clicked_y - (mainwin.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
hegyi@1837
   154
        (mainwin.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 << "(" <<
hegyi@1837
   159
          (mainwin.mapstorage).coords[active_node].x << ", " <<
hegyi@1837
   160
          (mainwin.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());
hegyi@1837
   167
          coord_text->property_x().set_value((mainwin.mapstorage).coords[active_node].x +
ladanyi@1654
   168
              radius);
hegyi@1837
   169
          coord_text->property_y().set_value((mainwin.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,
hegyi@1837
   176
              (mainwin.mapstorage).coords[active_node].x + radius,
hegyi@1837
   177
              (mainwin.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
hegyi@1837
   184
        for(OutEdgeIt ei((mainwin.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
hegyi@1837
   189
            nodesmap[(mainwin.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
hegyi@1837
   192
            nodesmap[(mainwin.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
hegyi@1837
   211
        for(InEdgeIt ei((mainwin.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
hegyi@1837
   216
            nodesmap[(mainwin.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
hegyi@1837
   219
            nodesmap[(mainwin.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:
hegyi@1837
   259
      (mainwin.mapstorage).modified = true;
ladanyi@1606
   260
hegyi@1510
   261
      isbutton=1;
hegyi@1510
   262
hegyi@1837
   263
      active_node=(mainwin.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
hegyi@1837
   270
      (mainwin.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 =
hegyi@1837
   274
          (mainwin.mapstorage).nodemap_storage.begin(); it !=
hegyi@1837
   275
          (mainwin.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] =
hegyi@1837
   281
            (mainwin.mapstorage).nodemap_default[it->first];
ladanyi@1645
   282
        }
ladanyi@1645
   283
      }
ladanyi@1646
   284
      // increment the id map's default value
hegyi@1837
   285
      (mainwin.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
hegyi@1837
   304
//       mapwin.updateNode(active_node);
hegyi@1837
   305
      propertyUpdate(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;
hegyi@1510
   311
      break;
hegyi@1510
   312
    default:
hegyi@1510
   313
      break;
ladanyi@1645
   314
  }
hegyi@1510
   315
  return false;
hegyi@1510
   316
}
hegyi@1510
   317
hegyi@1524
   318
bool GraphDisplayerCanvas::createEdgeEventHandler(GdkEvent* e)
hegyi@1510
   319
{
hegyi@1510
   320
  switch(e->type)
ladanyi@1645
   321
  {
hegyi@1510
   322
    case GDK_BUTTON_PRESS:
hegyi@1510
   323
      //in edge creation right button has special meaning
hegyi@1510
   324
      if(e->button.button!=3)
ladanyi@1645
   325
      {
ladanyi@1645
   326
        //there is not yet selected node
ladanyi@1645
   327
        if(active_node==INVALID)
ladanyi@1645
   328
        {
ladanyi@1645
   329
          //we mark the location of the event to be able to calculate parameters of dragging
hegyi@1525
   330
ladanyi@1645
   331
          window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
hegyi@1525
   332
ladanyi@1645
   333
          active_item=(get_item_at(clicked_x, clicked_y));
ladanyi@1645
   334
          active_node=INVALID;
hegyi@1837
   335
          for (NodeIt i((mainwin.mapstorage).graph); i!=INVALID; ++i)
ladanyi@1645
   336
          {
ladanyi@1645
   337
            if(nodesmap[i]==active_item)
ladanyi@1645
   338
            {
ladanyi@1645
   339
              active_node=i;
ladanyi@1645
   340
            }
ladanyi@1645
   341
          }
ladanyi@1645
   342
          //the clicked item is really a node
ladanyi@1645
   343
          if(active_node!=INVALID)
ladanyi@1645
   344
          {
ladanyi@1645
   345
            *(nodesmap[active_node]) << Gnome::Canvas::Properties::fill_color("red");
ladanyi@1645
   346
            isbutton=1;
ladanyi@1645
   347
          }
ladanyi@1645
   348
          //clicked item was not a node. It could be e.g. edge.
ladanyi@1645
   349
          else
ladanyi@1645
   350
          {
ladanyi@1645
   351
            active_item=NULL;
ladanyi@1645
   352
          }
ladanyi@1645
   353
        }
ladanyi@1645
   354
        //we only have to do sg. if the mouse button
ladanyi@1645
   355
        // is pressed already once AND the click was
ladanyi@1645
   356
        // on a node that was found in the set of 
ladanyi@1645
   357
        //nodes, and now we only search for the second 
ladanyi@1645
   358
        //node
ladanyi@1645
   359
        else
ladanyi@1645
   360
        {
ladanyi@1645
   361
          window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
ladanyi@1645
   362
          target_item=(get_item_at(clicked_x, clicked_y));
ladanyi@1645
   363
          Node target_node=INVALID;
hegyi@1837
   364
          for (NodeIt i((mainwin.mapstorage).graph); i!=INVALID; ++i)
ladanyi@1645
   365
          {
ladanyi@1645
   366
            if(nodesmap[i]==target_item)
ladanyi@1645
   367
            {
ladanyi@1645
   368
              target_node=i;
ladanyi@1645
   369
            }
ladanyi@1645
   370
          }
ladanyi@1645
   371
          //the clicked item is a node, the edge can be drawn
ladanyi@1645
   372
          if(target_node!=INVALID)
ladanyi@1645
   373
          {
ladanyi@1645
   374
            if(target_node!=active_node)		
ladanyi@1645
   375
            {
hegyi@1837
   376
              (mainwin.mapstorage).modified = true;
ladanyi@1606
   377
ladanyi@1645
   378
              *(nodesmap[target_node]) <<
ladanyi@1645
   379
                Gnome::Canvas::Properties::fill_color("red");
hegyi@1510
   380
ladanyi@1645
   381
              //creating new edge
hegyi@1837
   382
              active_edge=(mainwin.mapstorage).graph.addEdge(active_node,
ladanyi@1645
   383
                  target_node);
hegyi@1510
   384
ladanyi@1646
   385
              // update maps
ladanyi@1645
   386
              for (std::map<std::string,
ladanyi@1645
   387
                  Graph::EdgeMap<double>*>::const_iterator it =
hegyi@1837
   388
                  (mainwin.mapstorage).edgemap_storage.begin(); it !=
hegyi@1837
   389
                  (mainwin.mapstorage).edgemap_storage.end(); ++it)
ladanyi@1645
   390
              {
ladanyi@1646
   391
                (*(it->second))[active_edge] =
hegyi@1837
   392
                  (mainwin.mapstorage).edgemap_default[it->first];
ladanyi@1645
   393
              }
ladanyi@1646
   394
              // increment the id map's default value
hegyi@1837
   395
              (mainwin.mapstorage).edgemap_default["id"] += 1.0;
hegyi@1510
   396
ladanyi@1645
   397
              //calculating coordinates of new edge
ladanyi@1645
   398
              Gnome::Canvas::Points coos;
ladanyi@1645
   399
              double x1, x2, y1, y2;
hegyi@1510
   400
ladanyi@1645
   401
              active_item->get_bounds(x1, y1, x2, y2);
ladanyi@1645
   402
              coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
hegyi@1510
   403
ladanyi@1645
   404
              target_item->get_bounds(x1, y1, x2, y2);
ladanyi@1645
   405
              coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
hegyi@1510
   406
ladanyi@1645
   407
              //drawing new edge
ladanyi@1645
   408
              edgesmap[active_edge]=new BrokenEdge(displayed_graph, coos,
ladanyi@1645
   409
                  *this);
ladanyi@1645
   410
              *(edgesmap[active_edge]) <<
ladanyi@1645
   411
                Gnome::Canvas::Properties::fill_color("green");
ladanyi@1645
   412
              edgesmap[active_edge]->property_width_pixels().set_value(10);
hegyi@1510
   413
ladanyi@1645
   414
              edgesmap[active_edge]->lower_to_bottom();
hegyi@1512
   415
ladanyi@1645
   416
              //initializing edge-text as well, to empty string
ladanyi@1645
   417
              xy<double> text_pos=edgesmap[active_edge]->getArrowPos();
ladanyi@1645
   418
              text_pos+=(xy<double>(10,10));
ladanyi@1645
   419
ladanyi@1645
   420
              edgetextmap[active_edge]=new Gnome::Canvas::Text(displayed_graph,
ladanyi@1645
   421
                  text_pos.x, text_pos.y, "");
ladanyi@1645
   422
              edgetextmap[active_edge]->property_fill_color().set_value(
ladanyi@1645
   423
                  "darkgreen");
ladanyi@1645
   424
              edgetextmap[active_edge]->raise_to_top();
ladanyi@1645
   425
ladanyi@1645
   426
              //updating its properties
hegyi@1837
   427
//               mapwin.updateEdge(active_edge);
hegyi@1837
   428
              propertyUpdate(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?
hegyi@1837
   483
      for (NodeIt i((mainwin.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
	{
hegyi@1837
   493
	  for (EdgeIt i((mainwin.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
		{
hegyi@1837
   519
                  (mainwin.mapstorage).modified = true;
hegyi@1510
   520
hegyi@1525
   521
		  std::set<Graph::Edge> edges_to_delete;
hegyi@1510
   522
hegyi@1837
   523
		  for(OutEdgeIt e((mainwin.mapstorage).graph,active_node);e!=INVALID;++e)
hegyi@1525
   524
		    {
hegyi@1525
   525
		      edges_to_delete.insert(e);
hegyi@1525
   526
		    }
alpar@1651
   527
		  
hegyi@1837
   528
		  for(InEdgeIt e((mainwin.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
hegyi@1837
   592
          for (EdgeIt i((mainwin.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
          {
hegyi@1837
   604
            for (EdgeIt i((mainwin.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
hegyi@1837
   627
              Gtk::Dialog dialog("Edit value", mainwin, 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@1826
   631
              Gtk::SpinButton spin(0.0, 4);
ladanyi@1826
   632
              spin.set_increments(1.0, 10.0);
ladanyi@1826
   633
              spin.set_range(-1000000.0, 1000000.0);
ladanyi@1648
   634
              spin.set_numeric(true);
ladanyi@1648
   635
              vbox->add(spin);
ladanyi@1648
   636
              spin.show();
ladanyi@1648
   637
              switch (dialog.run())
ladanyi@1648
   638
              {
ladanyi@1648
   639
                case Gtk::RESPONSE_NONE:
ladanyi@1648
   640
                case Gtk::RESPONSE_CANCEL:
ladanyi@1648
   641
                  break;
ladanyi@1648
   642
                case Gtk::RESPONSE_ACCEPT:
ladanyi@1648
   643
                  double new_value = spin.get_value();
hegyi@1837
   644
                  (*(mainwin.mapstorage).edgemap_storage[edgemap_to_edit])[active_edge] =
ladanyi@1648
   645
                    new_value;
ladanyi@1648
   646
                  std::ostringstream ostr;
ladanyi@1648
   647
                  ostr << new_value;
ladanyi@1648
   648
                  edgetextmap[active_edge]->property_text().set_value(
ladanyi@1648
   649
                      ostr.str());
ladanyi@1648
   650
                  //mapwin.updateEdge(active_edge);
hegyi@1837
   651
//                   mapwin.updateEdge(Edge(INVALID));
hegyi@1837
   652
                  propertyUpdate(Edge(INVALID));
ladanyi@1648
   653
              }
ladanyi@1648
   654
            }
ladanyi@1648
   655
          }
ladanyi@1648
   656
          break;
ladanyi@1648
   657
        }
ladanyi@1648
   658
      default:
ladanyi@1648
   659
        break;
hegyi@1550
   660
    }
ladanyi@1648
   661
  }
hegyi@1550
   662
  return false;  
hegyi@1550
   663
}
hegyi@1550
   664
hegyi@1550
   665
bool GraphDisplayerCanvas::nodeMapEditEventHandler(GdkEvent* e)
hegyi@1550
   666
{
hegyi@1599
   667
  if(actual_tool==NODE_MAP_EDIT)
ladanyi@1648
   668
  {
ladanyi@1648
   669
    switch(e->type)
hegyi@1579
   670
    {
ladanyi@1648
   671
      case GDK_BUTTON_PRESS:
ladanyi@1648
   672
        {
ladanyi@1648
   673
          //for determine, whether it was a node
ladanyi@1648
   674
          Node clicked_node=INVALID;
hegyi@1594
   675
ladanyi@1648
   676
          window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
ladanyi@1648
   677
          active_item=(get_item_at(clicked_x, clicked_y));
hegyi@1594
   678
ladanyi@1648
   679
          //find the activated item between texts
hegyi@1837
   680
          for (NodeIt i((mainwin.mapstorage).graph); i!=INVALID; ++i)
ladanyi@1648
   681
          {
ladanyi@1648
   682
            //at the same time only one can be active
ladanyi@1648
   683
            if(nodetextmap[i]==active_item)
ladanyi@1648
   684
            {
ladanyi@1648
   685
              clicked_node=i;
ladanyi@1648
   686
            }
ladanyi@1648
   687
          }
hegyi@1599
   688
ladanyi@1648
   689
          //if there was not, search for it between nodes
ladanyi@1648
   690
          if(clicked_node==INVALID)
ladanyi@1648
   691
          {
hegyi@1837
   692
            for (NodeIt i((mainwin.mapstorage).graph); i!=INVALID; ++i)
ladanyi@1648
   693
            {
ladanyi@1648
   694
              //at the same time only one can be active
ladanyi@1648
   695
              if(nodesmap[i]==active_item)
ladanyi@1648
   696
              {
ladanyi@1648
   697
                clicked_node=i;
ladanyi@1648
   698
              }
ladanyi@1648
   699
            }
ladanyi@1648
   700
          }
hegyi@1599
   701
ladanyi@1648
   702
          //if it was really a node...
ladanyi@1648
   703
          if(clicked_node!=INVALID)
ladanyi@1648
   704
          {
ladanyi@1648
   705
            // the id map is not editable
ladanyi@1648
   706
            if (nodemap_to_edit == "id") return 0;
hegyi@1599
   707
ladanyi@1648
   708
            //and there is activated map
ladanyi@1648
   709
            if(nodetextmap[clicked_node]->property_text().get_value()!="")
ladanyi@1648
   710
            {
ladanyi@1648
   711
              //activate the general variable for it
ladanyi@1648
   712
              active_node=clicked_node;
hegyi@1599
   713
ladanyi@1648
   714
              //create a dialog
hegyi@1837
   715
              Gtk::Dialog dialog("Edit value", mainwin, true);
ladanyi@1648
   716
              dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
ladanyi@1648
   717
              dialog.add_button(Gtk::Stock::OK, Gtk::RESPONSE_ACCEPT);
ladanyi@1648
   718
              Gtk::VBox* vbox = dialog.get_vbox();
ladanyi@1826
   719
              Gtk::SpinButton spin(0.0, 4);
ladanyi@1826
   720
              spin.set_increments(1.0, 10.0);
ladanyi@1826
   721
              spin.set_range(-1000000.0, 1000000.0);
ladanyi@1648
   722
              spin.set_numeric(true);
ladanyi@1648
   723
              vbox->add(spin);
ladanyi@1648
   724
              spin.show();
ladanyi@1648
   725
              switch (dialog.run())
ladanyi@1648
   726
              {
ladanyi@1648
   727
                case Gtk::RESPONSE_NONE:
ladanyi@1648
   728
                case Gtk::RESPONSE_CANCEL:
ladanyi@1648
   729
                  break;
ladanyi@1648
   730
                case Gtk::RESPONSE_ACCEPT:
ladanyi@1648
   731
                  double new_value = spin.get_value();
hegyi@1837
   732
                  (*(mainwin.mapstorage).nodemap_storage[nodemap_to_edit])[active_node] =
ladanyi@1648
   733
                    new_value;
ladanyi@1648
   734
                  std::ostringstream ostr;
ladanyi@1648
   735
                  ostr << new_value;
ladanyi@1648
   736
                  nodetextmap[active_node]->property_text().set_value(
ladanyi@1648
   737
                      ostr.str());
ladanyi@1648
   738
                  //mapwin.updateNode(active_node);
hegyi@1837
   739
//                   mapwin.updateNode(Node(INVALID));
hegyi@1837
   740
                  propertyUpdate(Node(INVALID));
ladanyi@1648
   741
              }
ladanyi@1648
   742
            }
ladanyi@1648
   743
          }
ladanyi@1648
   744
          break;
ladanyi@1648
   745
        }
ladanyi@1648
   746
      default:
ladanyi@1648
   747
        break;
hegyi@1579
   748
    }
ladanyi@1648
   749
  }
hegyi@1579
   750
  return false;  
hegyi@1550
   751
}
hegyi@1550
   752
alpar@1643
   753
void GraphDisplayerCanvas::deleteItem(Node node_to_delete)
hegyi@1510
   754
{
hegyi@1512
   755
  delete(nodetextmap[node_to_delete]);
hegyi@1510
   756
  delete(nodesmap[node_to_delete]);
hegyi@1837
   757
  (mainwin.mapstorage).graph.erase(node_to_delete);
hegyi@1510
   758
}
hegyi@1510
   759
alpar@1643
   760
void GraphDisplayerCanvas::deleteItem(Edge edge_to_delete)
hegyi@1510
   761
{
hegyi@1512
   762
  delete(edgetextmap[edge_to_delete]);
hegyi@1510
   763
  delete(edgesmap[edge_to_delete]);
hegyi@1837
   764
  (mainwin.mapstorage).graph.erase(edge_to_delete);
hegyi@1510
   765
}
hegyi@1510
   766
hegyi@1524
   767
void GraphDisplayerCanvas::textReposition(xy<double> new_place)
hegyi@1510
   768
{
hegyi@1510
   769
  new_place+=(xy<double>(10,10));
hegyi@1579
   770
  edgetextmap[forming_edge]->property_x().set_value(new_place.x);
hegyi@1579
   771
  edgetextmap[forming_edge]->property_y().set_value(new_place.y);
hegyi@1510
   772
}
hegyi@1510
   773
hegyi@1524
   774
void GraphDisplayerCanvas::toggleEdgeActivity(BrokenEdge* active_bre, bool on)
hegyi@1510
   775
{
hegyi@1510
   776
  if(on)
hegyi@1510
   777
    {
hegyi@1579
   778
      if(forming_edge!=INVALID)
hegyi@1510
   779
	{
hegyi@1599
   780
	  std::cerr << "ERROR!!!! Valid edge found!" << std::endl;
hegyi@1510
   781
	}
hegyi@1510
   782
      else
hegyi@1510
   783
	{
hegyi@1837
   784
	  for (EdgeIt i((mainwin.mapstorage).graph); i!=INVALID; ++i)
hegyi@1510
   785
	    {
hegyi@1510
   786
	      if(edgesmap[i]==active_bre)
hegyi@1510
   787
		{
hegyi@1579
   788
		  forming_edge=i;
hegyi@1510
   789
		}
hegyi@1510
   790
	    }
hegyi@1510
   791
	}
hegyi@1510
   792
    }
hegyi@1510
   793
  else
hegyi@1510
   794
    {
hegyi@1579
   795
      if(forming_edge!=INVALID)
hegyi@1510
   796
	{
hegyi@1579
   797
	  forming_edge=INVALID;
hegyi@1510
   798
	}
hegyi@1510
   799
      else
hegyi@1510
   800
	{
hegyi@1599
   801
	  std::cerr << "ERROR!!!! Invalid edge found!" << std::endl;
hegyi@1510
   802
	}
hegyi@1510
   803
    }
hegyi@1510
   804
hegyi@1510
   805
}
hegyi@1550
   806
hegyi@1597
   807
int GraphDisplayerCanvas::addNewEdgeMap(double default_value, std::string mapname)
hegyi@1585
   808
{
hegyi@1594
   809
  //create the new map
hegyi@1837
   810
  Graph::EdgeMap<double> * emptr=new Graph::EdgeMap<double> ((mainwin.mapstorage).graph, default_value);
hegyi@1597
   811
hegyi@1597
   812
  //if addition was not successful addEdgeMap returns one.
hegyi@1597
   813
  //cause can be that there is already a map named like the new one
hegyi@1837
   814
  if((mainwin.mapstorage).addEdgeMap(mapname,emptr, default_value))
hegyi@1597
   815
    {
hegyi@1597
   816
      return 1;
hegyi@1597
   817
    }
hegyi@1597
   818
hegyi@1594
   819
hegyi@1594
   820
  //add it to the list of the displayable maps
hegyi@1837
   821
  mainwin.registerNewEdgeMap(mapname);
hegyi@1594
   822
hegyi@1594
   823
  //display it
hegyi@1592
   824
  changeEdgeText(mapname);
hegyi@1597
   825
hegyi@1597
   826
  return 0;
hegyi@1585
   827
}
hegyi@1585
   828
hegyi@1597
   829
int GraphDisplayerCanvas::addNewNodeMap(double default_value, std::string mapname)
hegyi@1585
   830
{
hegyi@1594
   831
  //create the new map
hegyi@1837
   832
  Graph::NodeMap<double> * emptr=new Graph::NodeMap<double> ((mainwin.mapstorage).graph,default_value);
hegyi@1597
   833
hegyi@1597
   834
  //if addition was not successful addNodeMap returns one.
hegyi@1597
   835
  //cause can be that there is already a map named like the new one
hegyi@1837
   836
  if((mainwin.mapstorage).addNodeMap(mapname,emptr, default_value))
hegyi@1597
   837
    {
hegyi@1597
   838
      return 1;
hegyi@1597
   839
    }
hegyi@1594
   840
hegyi@1594
   841
  //add it to the list of the displayable maps
hegyi@1837
   842
  mainwin.registerNewNodeMap(mapname);
hegyi@1594
   843
hegyi@1594
   844
  //display it
hegyi@1592
   845
  changeNodeText(mapname);
hegyi@1597
   846
hegyi@1597
   847
  return 0;
hegyi@1585
   848
}
hegyi@1585
   849