gui/graph_displayer_canvas.cc
author deba
Wed, 01 Mar 2006 10:17:25 +0000
changeset 1990 15fb7a4ea6be
parent 1860 27a9a75b957b
permissions -rwxr-xr-x
Some classes assumed that the GraphMaps should be inherited
from an ObserverBase. These classes parents replaced with
DefaultMap which cause that the graph maps should not be
inherited from the ObserverBase.
ladanyi@1606
     1
#include "graph_displayer_canvas.h"
alpar@1632
     2
#include <cmath>
ladanyi@1442
     3
hegyi@1849
     4
GraphDisplayerCanvas::GraphDisplayerCanvas(NoteBookTab & mainw) :
hegyi@1837
     5
  nodesmap(mainw.mapstorage.graph), edgesmap(mainw.mapstorage.graph), edgetextmap(mainw.mapstorage.graph),
hegyi@1837
     6
  nodetextmap(mainw.mapstorage.graph), displayed_graph(*(root()), 0, 0),
ladanyi@1648
     7
  isbutton(0), active_item(NULL), target_item(NULL), nodemap_to_edit(""),
hegyi@1849
     8
  edgemap_to_edit(""), mytab(mainw)
ladanyi@1442
     9
{
ladanyi@1606
    10
  //base event handler is move tool
ladanyi@1606
    11
  actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::moveEventHandler), false);
ladanyi@1606
    12
  actual_tool=MOVE;
hegyi@1562
    13
ladanyi@1606
    14
  active_node=INVALID;
ladanyi@1606
    15
  active_edge=INVALID;
ladanyi@1606
    16
  forming_edge=INVALID;
ladanyi@1606
    17
}
hegyi@1468
    18
ladanyi@1606
    19
GraphDisplayerCanvas::~GraphDisplayerCanvas()
ladanyi@1606
    20
{
hegyi@1849
    21
  for (NodeIt n((mytab.mapstorage).graph); n != INVALID; ++n)
hegyi@1837
    22
    {
hegyi@1837
    23
      delete nodesmap[n];
hegyi@1837
    24
      delete nodetextmap[n];
hegyi@1837
    25
    }
hegyi@1837
    26
  
hegyi@1849
    27
  for (EdgeIt e((mytab.mapstorage).graph); e != INVALID; ++e)
hegyi@1837
    28
    {
hegyi@1837
    29
      delete edgesmap[e];
hegyi@1837
    30
      delete edgetextmap[e];
hegyi@1837
    31
    }
hegyi@1837
    32
}
ladanyi@1442
    33
hegyi@1837
    34
void GraphDisplayerCanvas::propertyChange(bool itisedge, int prop)
hegyi@1837
    35
{
hegyi@1837
    36
  if(itisedge)
hegyi@1837
    37
    {
hegyi@1837
    38
      propertyUpdate(Edge(INVALID), prop);
hegyi@1837
    39
    }
hegyi@1837
    40
  else
hegyi@1837
    41
    {
hegyi@1837
    42
      propertyUpdate(Node(INVALID), prop);
hegyi@1837
    43
    }
hegyi@1837
    44
}
hegyi@1837
    45
hegyi@1837
    46
void GraphDisplayerCanvas::propertyUpdate(Edge edge)
hegyi@1837
    47
{
hegyi@1837
    48
  for(int i=0;i<EDGE_PROPERTY_NUM;i++)
hegyi@1837
    49
    {
hegyi@1837
    50
      propertyUpdate(edge, i);
hegyi@1837
    51
    }
hegyi@1837
    52
}
hegyi@1837
    53
hegyi@1837
    54
void GraphDisplayerCanvas::propertyUpdate(Node node)
hegyi@1837
    55
{
hegyi@1837
    56
  for(int i=0;i<NODE_PROPERTY_NUM;i++)
hegyi@1837
    57
    {
hegyi@1837
    58
      propertyUpdate(node, i);
hegyi@1837
    59
    }
hegyi@1837
    60
}
hegyi@1837
    61
hegyi@1888
    62
void GraphDisplayerCanvas::propertyUpdate(Node node, int prop)
hegyi@1837
    63
{
hegyi@1888
    64
  //dummy=dummy;
hegyi@1837
    65
hegyi@1849
    66
  std::string mapname=mytab.getActiveNodeMap(prop);
hegyi@1837
    67
hegyi@1837
    68
  if(mapname!="")
hegyi@1837
    69
    {
hegyi@1849
    70
      if( ( ((mytab.mapstorage).nodemap_storage).find(mapname) != ((mytab.mapstorage).nodemap_storage).end() ) )
hegyi@1837
    71
	{
hegyi@1837
    72
	  switch(prop)
hegyi@1837
    73
	    {
hegyi@1837
    74
	    case N_RADIUS:
hegyi@1837
    75
	      changeNodeRadius(mapname, node);
hegyi@1837
    76
	      break;
hegyi@1837
    77
	    case N_COLOR:
hegyi@1837
    78
	      changeNodeColor(mapname, node);
hegyi@1837
    79
	      break;
hegyi@1837
    80
	    case N_TEXT:
hegyi@1837
    81
	      changeNodeText(mapname, node);
hegyi@1837
    82
	      break;
hegyi@1837
    83
	    default:
hegyi@1837
    84
	      std::cerr<<"Error\n";
hegyi@1837
    85
	    }
hegyi@1837
    86
	}
hegyi@1837
    87
    }
hegyi@1837
    88
  else //mapname==""
hegyi@1837
    89
    {
hegyi@1837
    90
      Node node=INVALID;	
hegyi@1837
    91
      switch(prop)
hegyi@1837
    92
	{
hegyi@1837
    93
	case N_RADIUS:
hegyi@1837
    94
	  resetNodeRadius(node);
hegyi@1837
    95
	  break;
hegyi@1837
    96
	case N_COLOR:
hegyi@1837
    97
	  resetNodeColor(node);
hegyi@1837
    98
	  break;
hegyi@1837
    99
	case N_TEXT:
hegyi@1837
   100
	  resetNodeText(node);
hegyi@1837
   101
	  break;
hegyi@1837
   102
	default:
hegyi@1837
   103
	  std::cerr<<"Error\n";
hegyi@1837
   104
	}
hegyi@1837
   105
    }
hegyi@1837
   106
hegyi@1837
   107
}
hegyi@1837
   108
hegyi@1888
   109
void GraphDisplayerCanvas::propertyUpdate(Edge edge, int prop)
hegyi@1837
   110
{
hegyi@1888
   111
  //dummy=dummy;
hegyi@1837
   112
hegyi@1849
   113
  std::string mapname=mytab.getActiveEdgeMap(prop);
hegyi@1837
   114
hegyi@1837
   115
  if(mapname!="")
hegyi@1837
   116
    {
hegyi@1849
   117
      if( ( ((mytab.mapstorage).edgemap_storage).find(mapname) != ((mytab.mapstorage).edgemap_storage).end() ) )
hegyi@1837
   118
	{
hegyi@1837
   119
	  switch(prop)
hegyi@1837
   120
	    {
hegyi@1837
   121
	    case E_WIDTH:
hegyi@1837
   122
	      changeEdgeWidth(mapname, edge);
hegyi@1837
   123
	      break;
hegyi@1837
   124
	    case E_COLOR:
hegyi@1837
   125
	      changeEdgeColor(mapname, edge);
hegyi@1837
   126
	      break;
hegyi@1837
   127
	    case E_TEXT:
hegyi@1837
   128
	      changeEdgeText(mapname, edge);
hegyi@1837
   129
	      break;
hegyi@1837
   130
	    default:
hegyi@1837
   131
	      std::cerr<<"Error\n";
hegyi@1837
   132
	    }
hegyi@1837
   133
	}
hegyi@1837
   134
    }
hegyi@1837
   135
  else //mapname==""
hegyi@1837
   136
    {
hegyi@1837
   137
      switch(prop)
hegyi@1837
   138
	{
hegyi@1837
   139
	case E_WIDTH:
hegyi@1837
   140
	  resetEdgeWidth(edge);
hegyi@1837
   141
	  break;
hegyi@1837
   142
	case E_COLOR:
hegyi@1837
   143
	  resetEdgeColor(edge);
hegyi@1837
   144
	  break;
hegyi@1837
   145
	case E_TEXT:
hegyi@1837
   146
	  resetEdgeText(edge);
hegyi@1837
   147
	  break;
hegyi@1837
   148
	default:
hegyi@1837
   149
	  std::cerr<<"Error\n";
hegyi@1837
   150
	}
hegyi@1837
   151
    }
ladanyi@1606
   152
}
ladanyi@1606
   153
ladanyi@1606
   154
void GraphDisplayerCanvas::drawGraph()
ladanyi@1606
   155
{
ladanyi@1442
   156
  //first edges are drawn, to hide joining with nodes later
ladanyi@1442
   157
hegyi@1849
   158
  for (EdgeIt i((mytab.mapstorage).graph); i!=INVALID; ++i)
ladanyi@1442
   159
  {
ladanyi@1442
   160
ladanyi@1606
   161
    //drawing green lines, coordinates are from mapstorage.coords
ladanyi@1442
   162
ladanyi@1442
   163
    Gnome::Canvas::Points coos;
ladanyi@1606
   164
    coos.push_back(Gnome::Art::Point(
hegyi@1849
   165
          (mytab.mapstorage).coords[(mytab.mapstorage).graph.source(i)].x,
hegyi@1849
   166
          (mytab.mapstorage).coords[(mytab.mapstorage).graph.source(i)].y));
ladanyi@1606
   167
    coos.push_back(Gnome::Art::Point(
hegyi@1849
   168
          (mytab.mapstorage).coords[(mytab.mapstorage).graph.target(i)].x,
hegyi@1849
   169
          (mytab.mapstorage).coords[(mytab.mapstorage).graph.target(i)].y));
ladanyi@1442
   170
    
ladanyi@1860
   171
    edgesmap[i]=new BrokenEdge(displayed_graph, i, *this);
ladanyi@1442
   172
    *(edgesmap[i]) << Gnome::Canvas::Properties::fill_color("green");
hegyi@1598
   173
    edgesmap[i]->property_width_units().set_value(10);    
ladanyi@1645
   174
    edgesmap[i]->lower_to_bottom();
ladanyi@1442
   175
    
ladanyi@1442
   176
    //initializing edge-text as well, to empty string
ladanyi@1442
   177
ladanyi@1860
   178
    XY text_pos=mytab.mapstorage.arrow_pos[i];
ladanyi@1860
   179
    text_pos+=(XY(10,10));
hegyi@1505
   180
hegyi@1505
   181
    edgetextmap[i]=new Gnome::Canvas::Text(displayed_graph, text_pos.x, text_pos.y, "");
hegyi@1512
   182
    edgetextmap[i]->property_fill_color().set_value("darkgreen");
hegyi@1599
   183
    edgetextmap[i]->signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::edgeMapEditEventHandler), false);
ladanyi@1645
   184
    edgetextmap[i]->raise_to_top();
ladanyi@1442
   185
  }
ladanyi@1442
   186
ladanyi@1442
   187
  //afterwards nodes come to be drawn
ladanyi@1442
   188
hegyi@1849
   189
  for (NodeIt i((mytab.mapstorage).graph); i!=INVALID; ++i)
ladanyi@1442
   190
  {
ladanyi@1442
   191
    //drawing bule nodes, with black line around them
ladanyi@1442
   192
ladanyi@1606
   193
    nodesmap[i]=new Gnome::Canvas::Ellipse(
ladanyi@1606
   194
        displayed_graph,
hegyi@1849
   195
        (mytab.mapstorage).coords[i].x-20,
hegyi@1849
   196
        (mytab.mapstorage).coords[i].y-20,
hegyi@1849
   197
        (mytab.mapstorage).coords[i].x+20,
hegyi@1849
   198
        (mytab.mapstorage).coords[i].y+20);
ladanyi@1442
   199
    *(nodesmap[i]) << Gnome::Canvas::Properties::fill_color("blue");
ladanyi@1442
   200
    *(nodesmap[i]) << Gnome::Canvas::Properties::outline_color("black");
ladanyi@1645
   201
    nodesmap[i]->raise_to_top();
hegyi@1512
   202
hegyi@1512
   203
    //initializing edge-text as well, to empty string
hegyi@1512
   204
ladanyi@1606
   205
    xy<double> text_pos(
hegyi@1849
   206
        ((mytab.mapstorage).coords[i].x+node_property_defaults[N_RADIUS]+5),
hegyi@1849
   207
        ((mytab.mapstorage).coords[i].y+node_property_defaults[N_RADIUS]+5));
hegyi@1512
   208
ladanyi@1606
   209
    nodetextmap[i]=new Gnome::Canvas::Text(displayed_graph,
ladanyi@1606
   210
        text_pos.x, text_pos.y, "");
hegyi@1512
   211
    nodetextmap[i]->property_fill_color().set_value("darkblue");
hegyi@1599
   212
    nodetextmap[i]->signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::nodeMapEditEventHandler), false);
ladanyi@1645
   213
    nodetextmap[i]->raise_to_top();
ladanyi@1442
   214
  }
ladanyi@1442
   215
ladanyi@1442
   216
  updateScrollRegion();
ladanyi@1442
   217
}
ladanyi@1442
   218
ladanyi@1606
   219
void GraphDisplayerCanvas::clear()
ladanyi@1442
   220
{
ladanyi@1606
   221
  active_node=INVALID;
ladanyi@1606
   222
  active_edge=INVALID;
ladanyi@1606
   223
  forming_edge=INVALID;
ladanyi@1442
   224
hegyi@1849
   225
  for (NodeIt n((mytab.mapstorage).graph); n != INVALID; ++n)
ladanyi@1442
   226
  {
ladanyi@1606
   227
    delete nodesmap[n];
ladanyi@1606
   228
    delete nodetextmap[n];
ladanyi@1442
   229
  }
ladanyi@1442
   230
hegyi@1849
   231
  for (EdgeIt e((mytab.mapstorage).graph); e != INVALID; ++e)
ladanyi@1606
   232
  {
ladanyi@1606
   233
    delete edgesmap[e];
ladanyi@1606
   234
    delete edgetextmap[e];
ladanyi@1606
   235
  }
ladanyi@1442
   236
}