gui/graph_displayer_canvas-node.cc
author athos
Thu, 30 Jun 2005 16:13:30 +0000
changeset 1526 8c14aa8f27a2
parent 1512 e54392395480
child 1579 ed7da82bbecf
permissions -rwxr-xr-x
Mainly doc review.
     1 #include <graph_displayer_canvas.h>
     2 #include <broken_edge.h>
     3 #include <math.h>
     4 
     5 
     6 int GraphDisplayerCanvas::changeNodeRadius (std::string mapname, Graph::Node node)
     7 {
     8   Graph::NodeMap<double> * actual_map;
     9   if(mapname=="Default")
    10     {
    11       actual_map=new Graph::NodeMap<double>(g,node_property_defaults[N_RADIUS]);
    12     }
    13   else
    14     {
    15       actual_map=(mapstorage.nodemap_storage)[mapname];
    16     }
    17 
    18   if(node==INVALID)
    19     {
    20       for (NodeIt i(g); i!=INVALID; ++i)
    21 	{
    22 	  int w=(int)(*actual_map)[i];
    23 	  if(w>=0)
    24 	    {
    25 	      double x1, y1, x2, y2;
    26 	      x1=nodesmap[i]->property_x1().get_value();
    27 	      x2=nodesmap[i]->property_x2().get_value();
    28 	      y1=nodesmap[i]->property_y1().get_value();
    29 	      y2=nodesmap[i]->property_y2().get_value();
    30 	      nodesmap[i]->property_x1().set_value((x1+x2)/2-w);
    31 	      nodesmap[i]->property_x2().set_value((x1+x2)/2+w);
    32 	      nodesmap[i]->property_y1().set_value((y1+y2)/2-w);
    33 	      nodesmap[i]->property_y2().set_value((y1+y2)/2+w);
    34 	    }
    35 	}
    36     }
    37   else
    38     {
    39       //I think only new nodes use this case
    40 //       int w=(int)(*actual_map)[node];
    41       int w=(int)(node_property_defaults[N_RADIUS]);
    42       if(w>=0)
    43 	{
    44 	  double x1, y1, x2, y2;
    45 	  x1=nodesmap[node]->property_x1().get_value();
    46 	  x2=nodesmap[node]->property_x2().get_value();
    47 	  y1=nodesmap[node]->property_y1().get_value();
    48 	  y2=nodesmap[node]->property_y2().get_value();
    49 	  nodesmap[node]->property_x1().set_value((x1+x2)/2-w);
    50 	  nodesmap[node]->property_x2().set_value((x1+x2)/2+w);
    51 	  nodesmap[node]->property_y1().set_value((y1+y2)/2-w);
    52 	  nodesmap[node]->property_y2().set_value((y1+y2)/2+w);
    53 	}
    54     }
    55   return 0;
    56 };
    57 
    58 int GraphDisplayerCanvas::changeNodeColor (std::string mapname, Graph::Node node)
    59 {  
    60 
    61   //function maps the range of the maximum and
    62   //the minimum of the nodemap to the range of
    63   //green in RGB
    64 
    65   Graph::NodeMap<double> * actual_map;
    66   if(mapname=="Default")
    67     {
    68       actual_map=new Graph::NodeMap<double>(g,node_property_defaults[N_COLOR]);
    69     }
    70   else
    71     {
    72       actual_map=(mapstorage.nodemap_storage)[mapname];
    73     }
    74 
    75   double max, min;
    76 
    77   if(mapname!="Default")
    78     {
    79       max=mapstorage.maxOfNodeMap(mapname);
    80       min=mapstorage.minOfNodeMap(mapname);
    81     }
    82   else
    83     {
    84       max=node_property_defaults[N_COLOR];
    85       min=node_property_defaults[N_COLOR];
    86     }
    87 
    88 
    89   if(node==INVALID)
    90     {
    91 
    92       for (NodeIt i(g); i!=INVALID; ++i)
    93 	{
    94 	  Gdk::Color color;
    95 
    96 	  double w=(*actual_map)[i];
    97 
    98 	  if(max!=min)
    99 	    {
   100 	      color.set_rgb_p (0, 0, 100*(w-min)/(max-min));
   101 	    }
   102 	  else
   103 	    {
   104 	      color.set_rgb_p (0, 0, 100);
   105 	    }
   106 
   107 	  nodesmap[i]->property_fill_color_gdk().set_value(color);
   108 	}
   109     }
   110   else
   111     {
   112       Gdk::Color color;
   113 
   114       double w=(*actual_map)[node];
   115 
   116       if(max!=min)
   117 	{
   118 	  color.set_rgb_p (0, 0, 100*(w-min)/(max-min));
   119 	}
   120       else
   121 	{
   122 	  color.set_rgb_p (0, 0, 100);
   123 	}
   124 
   125       nodesmap[node]->property_fill_color_gdk().set_value(color);
   126     }
   127   return 0;
   128 };
   129 
   130 int GraphDisplayerCanvas::changeNodeText (std::string mapname, Graph::Node node)
   131 {
   132 
   133   //the number in the map will be written on the node
   134   //EXCEPT when the name of the map is Text, because
   135   //in that case empty string will be written, because
   136   //that is the deleter map
   137 
   138   Graph::NodeMap<double> * actual_map;
   139   if(mapname=="Default")
   140     {
   141       actual_map=new Graph::NodeMap<double>(g,node_property_defaults[N_COLOR]);
   142     }
   143   else
   144     {
   145       actual_map=(mapstorage.nodemap_storage)[mapname];
   146     }
   147 
   148   if(node==INVALID)
   149     {
   150       for (NodeIt i(g); i!=INVALID; ++i)
   151 	{
   152 	  if(mapname!="Default")
   153 	    {
   154 	      double number=(*actual_map)[i];
   155 	      int length=1;
   156 	      //if number is smaller than one, length would be negative, or invalid
   157 	      if(number>=1)
   158 		{
   159 		  length=(int)(floor(log(number)/log(10)))+1;
   160 		}
   161 	      int maxpos=(int)(pow(10,length-1));
   162 	      int strl=length+1+RANGE;
   163 	      char * str=new char[strl];
   164 	      str[length]='.';
   165 	      str[strl]='\0';
   166       
   167 	      for(int j=0;j<strl;j++)
   168 		{
   169 		  if(j!=length)
   170 		    {
   171 		      int digit=(int)(number/maxpos);
   172 		      str[j]=(digit+'0');
   173 		      number-=digit*maxpos;
   174 		      number*=10;
   175 		    }
   176 		}
   177       
   178 	      nodetextmap[i]->property_text().set_value(str);
   179 	    }
   180 	  else
   181 	    {
   182 	      nodetextmap[i]->property_text().set_value("");
   183 	    }
   184 	}
   185     }
   186   else
   187     {
   188       if(mapname!="Default")
   189 	{
   190 	  double number=(*actual_map)[node];
   191 	  int length=1;
   192 	  //if number is smaller than one, length would be negative, or invalid
   193 	  if(number>=1)
   194 	    {
   195 	      length=(int)(floor(log(number)/log(10)))+1;
   196 	    }
   197 	  int maxpos=(int)(pow(10,length-1));
   198 	  int strl=length+1+RANGE;
   199 	  char * str=new char[strl];
   200 	  str[length]='.';
   201 	  str[strl]='\0';
   202       
   203 	  for(int j=0;j<strl;j++)
   204 	    {
   205 	      if(j!=length)
   206 		{
   207 		  int digit=(int)(number/maxpos);
   208 		  str[j]=(digit+'0');
   209 		  number-=digit*maxpos;
   210 		  number*=10;
   211 		}
   212 	    }
   213       
   214 	  nodetextmap[node]->property_text().set_value(str);
   215 	}
   216       else
   217 	{
   218 	  nodetextmap[node]->property_text().set_value("");
   219 	}
   220     }
   221   return 0;
   222 };