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