gui/graph_displayer_canvas-node.cc
author hegyi
Thu, 21 Jul 2005 19:28:29 +0000
changeset 1579 ed7da82bbecf
parent 1525 6d94de269ab1
child 1581 9bb83c7f479b
permissions -rwxr-xr-x
Edge and nodemap edition is done.
     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=(mapstorage.nodemap_storage)[mapname];
   142     }
   143 
   144   if(node==INVALID)
   145     {
   146       for (NodeIt i(g); i!=INVALID; ++i)
   147 	{
   148 	  if(mapname!="Default")
   149 	    {
   150 	      nodemap_to_edit=mapname;
   151 	      double number=(*actual_map)[i];
   152 	      int length=1;
   153 	      //if number is smaller than one, length would be negative, or invalid
   154 	      if(number>=1)
   155 		{
   156 		  length=(int)(floor(log(number)/log(10)))+1;
   157 		}
   158 	      int maxpos=(int)(pow(10,length-1));
   159 	      int strl=length+1+RANGE;
   160 	      char * str=new char[strl];
   161 	      str[length]='.';
   162 	      str[strl]='\0';
   163       
   164 	      for(int j=0;j<strl;j++)
   165 		{
   166 		  if(j!=length)
   167 		    {
   168  		      int digit=(int)round(number/maxpos);
   169 		      str[j]=(digit+'0');
   170 		      number-=digit*maxpos;
   171 		      number*=10;
   172 		    }
   173 		}
   174       
   175 	      nodetextmap[i]->property_text().set_value(str);
   176 	    }
   177 	  else
   178 	    {
   179 	      nodemap_to_edit="";
   180 	      nodetextmap[i]->property_text().set_value("");
   181 	    }
   182 	}
   183     }
   184   else
   185     {
   186       if(mapname!="Default")
   187 	{
   188 	  double number=(*actual_map)[node];
   189 	  int length=1;
   190 	  //if number is smaller than one, length would be negative, or invalid
   191 	  if(number>=1)
   192 	    {
   193 	      length=(int)(floor(log(number)/log(10)))+1;
   194 	    }
   195 	  int maxpos=(int)(pow(10,length-1));
   196 	  int strl=length+1+RANGE;
   197 	  char * str=new char[strl];
   198 	  str[length]='.';
   199 	  str[strl]='\0';
   200       
   201 	  for(int j=0;j<strl;j++)
   202 	    {
   203 	      if(j!=length)
   204 		{
   205 		  int digit=(int)(number/maxpos);
   206 		  str[j]=(digit+'0');
   207 		  number-=digit*maxpos;
   208 		  number*=10;
   209 		}
   210 	    }
   211       
   212 	  nodetextmap[node]->property_text().set_value(str);
   213 	}
   214       else
   215 	{
   216 	  nodetextmap[node]->property_text().set_value("");
   217 	}
   218     }
   219   return 0;
   220 };