graph_displayer_canvas-node.cc
changeset 4 244d8c60b997
equal deleted inserted replaced
-1:000000000000 0:c2427923dda3
       
     1 /* -*- C++ -*-
       
     2  *
       
     3  * This file is a part of LEMON, a generic C++ optimization library
       
     4  *
       
     5  * Copyright (C) 2003-2006
       
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
       
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
       
     8  *
       
     9  * Permission to use, modify and distribute this software is granted
       
    10  * provided that this copyright notice appears in all copies. For
       
    11  * precise terms see the accompanying LICENSE file.
       
    12  *
       
    13  * This software is provided "AS IS" with no warranty of any kind,
       
    14  * express or implied, and with no claim as to its suitability for any
       
    15  * purpose.
       
    16  *
       
    17  */
       
    18 
       
    19 #include <graph_displayer_canvas.h>
       
    20 #include <mapstorage.h>
       
    21 #include <nbtab.h>
       
    22 #include <cmath>
       
    23 
       
    24 const int minimum_node_radius=5;
       
    25 
       
    26 int DigraphDisplayerCanvas::changeNodeRadius (std::string mapname, Node node)
       
    27 {
       
    28   MapStorage& ms = *mytab.mapstorage;
       
    29 
       
    30   double min, max;
       
    31 
       
    32   {
       
    33     NodeIt n(ms.digraph);
       
    34     min = max = ms.get(mapname, n);
       
    35     for (; n != INVALID; ++n)
       
    36     {
       
    37       if (static_cast<double>(ms.get(mapname, n)) > max)
       
    38         max = ms.get(mapname, n);
       
    39       if (static_cast<double>(ms.get(mapname, n)) < min)
       
    40         min = ms.get(mapname, n);
       
    41     }
       
    42   }
       
    43 
       
    44   if(node==INVALID)
       
    45     {
       
    46       for (NodeIt i(ms.digraph); i!=INVALID; ++i)
       
    47 	{
       
    48 	  double v=fabs(ms.get(mapname,i));
       
    49 	  int w;
       
    50 	  if(autoscale)
       
    51 	    {
       
    52 	      if(min==max)
       
    53 		{
       
    54 		  w=(int)(node_property_defaults[N_RADIUS]);
       
    55 		}
       
    56 	      else
       
    57 		{
       
    58 		  w=(int)(minimum_node_radius+(v-min)/(max-min)*(radius_size-minimum_node_radius));
       
    59 		}
       
    60 	    }
       
    61 	  else
       
    62 	    {
       
    63 	      w=(int)(v*radius_size);
       
    64 	    }
       
    65 
       
    66 	  if(w<minimum_node_radius)
       
    67 	    {
       
    68 	      w=minimum_node_radius;
       
    69 	    }
       
    70 
       
    71 	  if(zoomtrack)
       
    72 	    {
       
    73 	      double actual_ppu=get_pixels_per_unit();
       
    74 	      w=(int)(w/actual_ppu*fixed_zoom_factor);
       
    75 	    }
       
    76 
       
    77 	  if(w>=0)
       
    78 	    {
       
    79 	      double x1, y1, x2, y2;
       
    80 	      x1=nodesmap[i]->property_x1().get_value();
       
    81 	      x2=nodesmap[i]->property_x2().get_value();
       
    82 	      y1=nodesmap[i]->property_y1().get_value();
       
    83 	      y2=nodesmap[i]->property_y2().get_value();
       
    84 	      nodesmap[i]->property_x1().set_value((x1+x2)/2-w);
       
    85 	      nodesmap[i]->property_x2().set_value((x1+x2)/2+w);
       
    86 	      nodesmap[i]->property_y1().set_value((y1+y2)/2-w);
       
    87 	      nodesmap[i]->property_y2().set_value((y1+y2)/2+w);
       
    88 	    }
       
    89 	}
       
    90     }
       
    91   else
       
    92     {
       
    93       //I think only new nodes use this case
       
    94       //that has no own value, only the default one
       
    95       //int w=(int)(*actual_map)[node];
       
    96       int w=(int)(node_property_defaults[N_RADIUS]);
       
    97       if(w>=0)
       
    98 	{
       
    99 	  double x1, y1, x2, y2;
       
   100 	  x1=nodesmap[node]->property_x1().get_value();
       
   101 	  x2=nodesmap[node]->property_x2().get_value();
       
   102 	  y1=nodesmap[node]->property_y1().get_value();
       
   103 	  y2=nodesmap[node]->property_y2().get_value();
       
   104 	  nodesmap[node]->property_x1().set_value((x1+x2)/2-w);
       
   105 	  nodesmap[node]->property_x2().set_value((x1+x2)/2+w);
       
   106 	  nodesmap[node]->property_y1().set_value((y1+y2)/2-w);
       
   107 	  nodesmap[node]->property_y2().set_value((y1+y2)/2+w);
       
   108 	}
       
   109     }
       
   110   return 0;
       
   111 };
       
   112 
       
   113 int DigraphDisplayerCanvas::resetNodeRadius (Node node)
       
   114 {
       
   115   MapStorage& ms = *mytab.mapstorage;
       
   116 
       
   117   double min, max;
       
   118   min=node_property_defaults[N_RADIUS];
       
   119   max=node_property_defaults[N_RADIUS];
       
   120   Digraph::NodeMap<double> actual_map(ms.digraph,node_property_defaults[N_RADIUS]);
       
   121   
       
   122   if(node==INVALID)
       
   123     {
       
   124       for (NodeIt i(ms.digraph); i!=INVALID; ++i)
       
   125 	{
       
   126 	  double v=fabs(actual_map[i]);
       
   127 	  int w;
       
   128 	  if(min==max)
       
   129 	    {
       
   130 	      w=(int)(node_property_defaults[N_RADIUS]);
       
   131 	    }
       
   132 	  else
       
   133 	    {
       
   134 	      w=(int)(MIN_NODE_RADIUS+(v-min)/(max-min)*(MAX_NODE_RADIUS-MIN_NODE_RADIUS));
       
   135 	    }
       
   136 	  if(zoomtrack)
       
   137 	    {
       
   138 	      double actual_ppu=get_pixels_per_unit();
       
   139 	      w=(int)(w/actual_ppu*fixed_zoom_factor);
       
   140 	    }
       
   141 	  if(w>=0)
       
   142 	    {
       
   143 	      double x1, y1, x2, y2;
       
   144 	      x1=nodesmap[i]->property_x1().get_value();
       
   145 	      x2=nodesmap[i]->property_x2().get_value();
       
   146 	      y1=nodesmap[i]->property_y1().get_value();
       
   147 	      y2=nodesmap[i]->property_y2().get_value();
       
   148 	      nodesmap[i]->property_x1().set_value((x1+x2)/2-w);
       
   149 	      nodesmap[i]->property_x2().set_value((x1+x2)/2+w);
       
   150 	      nodesmap[i]->property_y1().set_value((y1+y2)/2-w);
       
   151 	      nodesmap[i]->property_y2().set_value((y1+y2)/2+w);
       
   152 	    }
       
   153 	}
       
   154     }
       
   155   else
       
   156     {
       
   157       //I think only new nodes use this case
       
   158 //       int w=(int)actual_map[node];
       
   159       int w=(int)(node_property_defaults[N_RADIUS]);
       
   160       if(w>=0)
       
   161 	{
       
   162 	  double x1, y1, x2, y2;
       
   163 	  x1=nodesmap[node]->property_x1().get_value();
       
   164 	  x2=nodesmap[node]->property_x2().get_value();
       
   165 	  y1=nodesmap[node]->property_y1().get_value();
       
   166 	  y2=nodesmap[node]->property_y2().get_value();
       
   167 	  nodesmap[node]->property_x1().set_value((x1+x2)/2-w);
       
   168 	  nodesmap[node]->property_x2().set_value((x1+x2)/2+w);
       
   169 	  nodesmap[node]->property_y1().set_value((y1+y2)/2-w);
       
   170 	  nodesmap[node]->property_y2().set_value((y1+y2)/2+w);
       
   171 	}
       
   172     }
       
   173   return 0;
       
   174 };
       
   175 
       
   176 int DigraphDisplayerCanvas::changeNodeColor (std::string mapname, Node node)
       
   177 {  
       
   178   MapStorage& ms = *mytab.mapstorage;
       
   179 
       
   180   //function maps the range of the maximum and
       
   181   //the minimum of the nodemap to the range of
       
   182   //green in RGB
       
   183 
       
   184   double max, min;
       
   185 
       
   186   {
       
   187     NodeIt n(ms.digraph);
       
   188     min = max = ms.get(mapname, n);
       
   189     for (; n != INVALID; ++n)
       
   190     {
       
   191       if (static_cast<double>(ms.get(mapname, n)) > max)
       
   192         max = ms.get(mapname, n);
       
   193       if (static_cast<double>(ms.get(mapname, n)) < min)
       
   194         min = ms.get(mapname, n);
       
   195     }
       
   196   }
       
   197 
       
   198   if(node==INVALID)
       
   199     {
       
   200 
       
   201       for (NodeIt i(ms.digraph); i!=INVALID; ++i)
       
   202 	{
       
   203 	  Gdk::Color color;
       
   204 
       
   205 	  double w=ms.get(mapname, i);
       
   206 
       
   207 	  if(max!=min)
       
   208 	    {
       
   209 	      color=rainbowColorCounter(min, max, w);
       
   210 	    }
       
   211 	  else
       
   212 	    {
       
   213 	      color.set_rgb_p (0, 0, 1);
       
   214 	    }
       
   215 
       
   216 	  nodesmap[i]->property_fill_color_gdk().set_value(color);
       
   217 	}
       
   218     }
       
   219   else
       
   220     {
       
   221       Gdk::Color color;
       
   222 
       
   223       double w=ms.get(mapname, node);
       
   224 
       
   225       if(max!=min)
       
   226 	{
       
   227 	  color=rainbowColorCounter(min, max, w);
       
   228 	}
       
   229       else
       
   230 	{
       
   231 	  color.set_rgb_p (0, 0, 1);
       
   232 	}
       
   233 
       
   234       nodesmap[node]->property_fill_color_gdk().set_value(color);
       
   235     }
       
   236   return 0;
       
   237 };
       
   238 
       
   239 int DigraphDisplayerCanvas::resetNodeColor (Node node)
       
   240 {  
       
   241   MapStorage& ms = *mytab.mapstorage;
       
   242 
       
   243   //function maps the range of the maximum and
       
   244   //the minimum of the nodemap to the range of
       
   245   //green in RGB
       
   246 
       
   247   Digraph::NodeMap<double> actual_map(ms.digraph,node_property_defaults[N_COLOR]);
       
   248 
       
   249   double max, min;
       
   250 
       
   251   max=node_property_defaults[N_COLOR];
       
   252   min=node_property_defaults[N_COLOR];
       
   253 
       
   254   if(node==INVALID)
       
   255     {
       
   256 
       
   257       for (NodeIt i(ms.digraph); i!=INVALID; ++i)
       
   258 	{
       
   259 	  Gdk::Color color;
       
   260 
       
   261 	  double w=actual_map[i];
       
   262 
       
   263 	  if(max!=min)
       
   264 	    {
       
   265 	      color.set_rgb_p (0, 0, 100*(w-min)/(max-min));
       
   266 	    }
       
   267 	  else
       
   268 	    {
       
   269 	      color.set_rgb_p (0, 0, 100);
       
   270 	    }
       
   271 
       
   272 	  nodesmap[i]->property_fill_color_gdk().set_value(color);
       
   273 	}
       
   274     }
       
   275   else
       
   276     {
       
   277       Gdk::Color color;
       
   278 
       
   279       double w=actual_map[node];
       
   280 
       
   281       if(max!=min)
       
   282 	{
       
   283 	  color.set_rgb_p (0, 0, 100*(w-min)/(max-min));
       
   284 	}
       
   285       else
       
   286 	{
       
   287 	  color.set_rgb_p (0, 0, 100);
       
   288 	}
       
   289 
       
   290       nodesmap[node]->property_fill_color_gdk().set_value(color);
       
   291     }
       
   292   return 0;
       
   293 };
       
   294 
       
   295 int DigraphDisplayerCanvas::changeNodeText (std::string mapname, Node node)
       
   296 {
       
   297   MapStorage& ms = *mytab.mapstorage;
       
   298 
       
   299   //the number in the map will be written on the node
       
   300   //EXCEPT when the name of the map is Text, because
       
   301   //in that case empty string will be written, because
       
   302   //that is the deleter map
       
   303 
       
   304   if(node==INVALID)
       
   305     {
       
   306       for (NodeIt i(ms.digraph); i!=INVALID; ++i)
       
   307 	{
       
   308 	  nodemap_to_edit=mapname;
       
   309 
       
   310 	  nodetextmap[i]->property_text().set_value(
       
   311               static_cast<std::string>(ms.get(mapname, i)));
       
   312 	}
       
   313     }
       
   314   else
       
   315     {
       
   316       nodetextmap[node]->property_text().set_value(
       
   317           static_cast<std::string>(ms.get(mapname, node)));
       
   318     }
       
   319   return 0;
       
   320 };
       
   321 
       
   322 int DigraphDisplayerCanvas::resetNodeText (Node node)
       
   323 {
       
   324   MapStorage& ms = *mytab.mapstorage;
       
   325 
       
   326   //the number in the map will be written on the node
       
   327   //EXCEPT when the name of the map is Text, because
       
   328   //in that case empty string will be written, because
       
   329   //that is the deleter map
       
   330 
       
   331   if(node==INVALID)
       
   332     {
       
   333       for (NodeIt i(ms.digraph); i!=INVALID; ++i)
       
   334 	{
       
   335 	  nodemap_to_edit="";
       
   336 	  nodetextmap[i]->property_text().set_value("");
       
   337 	}
       
   338     }
       
   339   else
       
   340     {
       
   341       nodetextmap[node]->property_text().set_value("");
       
   342     }
       
   343   return 0;
       
   344 };