mapstorage.cc
changeset 1 67188bd752db
child 7 f227a74db59d
equal deleted inserted replaced
-1:000000000000 0:63cd2ee8a173
       
     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 cop	ies. 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 "i18n.h"
       
    20 #include <limits>
       
    21 #include <cmath>
       
    22 #include <iostream>
       
    23 #include <fstream>
       
    24 #include <string>
       
    25 #include <algorithm>
       
    26 #include <gtkmm.h>
       
    27 #include "file_import_dialog.h"
       
    28 #include <mapstorage.h>
       
    29 #include <gui_writer.h>
       
    30 #include <gui_reader.h>
       
    31 #include <lemon/graph_to_eps.h>
       
    32 #include <lemon/lgf_reader.h>
       
    33 #include <lemon/lgf_writer.h>
       
    34 
       
    35 const int i_d=20;
       
    36 const double a_d=0.05;
       
    37 const double p_d=40000;
       
    38 
       
    39 MapStorage::MapStorage() :
       
    40   gui_sect_save_dest(LGF_FILE),
       
    41   node_coords_save_dest(SpecMapSaveOpts::GUI_SECT),
       
    42   arrow_coords_save_dest(SpecMapSaveOpts::GUI_SECT),
       
    43   modified(false),
       
    44   file_name(""),
       
    45   max_node_label(0),
       
    46   max_arc_label(0),
       
    47   node_coords_one_map_name("coord"),
       
    48   node_coords_two_maps_1_name("coord_x"),
       
    49   node_coords_two_maps_2_name("coord_y"),
       
    50   arrow_coords_one_map_name("arrow"),
       
    51   arrow_coords_two_maps_1_name("arrow_x"),
       
    52   arrow_coords_two_maps_2_name("arrow_y"),
       
    53   iterations(i_d),
       
    54   attraction(a_d),
       
    55   propulsation(p_d),
       
    56   node_coords_x(digraph),
       
    57   node_coords_y(digraph),
       
    58   arrow_coords_x(digraph),
       
    59   arrow_coords_y(digraph),
       
    60   node_label(digraph),
       
    61   arc_label(digraph),
       
    62   background_set(false)
       
    63 {
       
    64   node_coords.setXMap(node_coords_x);
       
    65   node_coords.setYMap(node_coords_y);
       
    66   arrow_coords.setXMap(arrow_coords_x);
       
    67   arrow_coords.setYMap(arrow_coords_y);
       
    68 
       
    69   active_nodemaps.resize(NODE_PROPERTY_NUM);
       
    70   for(int i=0;i<NODE_PROPERTY_NUM;i++)
       
    71     {
       
    72       active_nodemaps[i]="";
       
    73     }
       
    74 
       
    75   active_arcmaps.resize(EDGE_PROPERTY_NUM);
       
    76   for(int i=0;i<EDGE_PROPERTY_NUM;i++)
       
    77     {
       
    78       active_arcmaps[i]="";
       
    79     }
       
    80 }
       
    81 
       
    82 MapStorage::~MapStorage()
       
    83 {
       
    84   clear();
       
    85 }
       
    86 
       
    87 void MapStorage::createNodeMap(const std::string& name, MapValue::Type type,
       
    88     MapValue def_val)
       
    89 {
       
    90   NodeMapStore::const_iterator it = nodemaps.find(name);
       
    91   if (it != nodemaps.end())
       
    92     throw Error("Node map " + name + " already exists.");
       
    93 
       
    94   switch (type)
       
    95   {
       
    96     case MapValue::NUMERIC:
       
    97       nodemaps[name] = new NumericNodeMapData(digraph, def_val);
       
    98       break;
       
    99     case MapValue::STRING:
       
   100       nodemaps[name] = new StringNodeMapData(digraph, def_val);
       
   101       break;
       
   102   }
       
   103 
       
   104   nodemaps[name]->default_value = def_val;
       
   105 
       
   106   signal_node_map.emit(name, type);
       
   107 }
       
   108 
       
   109 void MapStorage::createArcMap(const std::string& name, MapValue::Type type,
       
   110     MapValue def_val)
       
   111 {
       
   112   ArcMapStore::const_iterator it = arcmaps.find(name);
       
   113   if (it != arcmaps.end())
       
   114     throw Error("Arc map " + name + " already exists.");
       
   115 
       
   116   switch (type)
       
   117   {
       
   118     case MapValue::NUMERIC:
       
   119       arcmaps[name] = new NumericArcMapData(digraph, def_val);
       
   120       break;
       
   121     case MapValue::STRING:
       
   122       arcmaps[name] = new StringArcMapData(digraph, def_val);
       
   123       break;
       
   124   }
       
   125 
       
   126   arcmaps[name]->default_value = def_val;
       
   127 
       
   128   signal_arc_map.emit(name, type);
       
   129 }
       
   130 
       
   131 void MapStorage::changeActiveMap(bool itisarc, int prop, std::string mapname)
       
   132 {
       
   133   if(itisarc)
       
   134     {
       
   135       active_arcmaps[prop]=mapname;
       
   136     }
       
   137   else
       
   138     {
       
   139       active_nodemaps[prop]=mapname;
       
   140     }
       
   141   signal_prop.emit(itisarc, prop);
       
   142 }
       
   143 
       
   144 void MapStorage::broadcastActiveMaps()
       
   145 {
       
   146   for(int i=0;i<NODE_PROPERTY_NUM;i++)
       
   147     {
       
   148       signal_map_win.emit(false, i, active_nodemaps[i]);
       
   149     }
       
   150   
       
   151   for(int i=0;i<EDGE_PROPERTY_NUM;i++)
       
   152     {
       
   153       signal_map_win.emit(true, i, active_arcmaps[i]);
       
   154     }
       
   155 }
       
   156 
       
   157 std::string MapStorage::getActiveArcMap(int prop)
       
   158 {
       
   159   return active_arcmaps[prop];
       
   160 }
       
   161 
       
   162 std::string MapStorage::getActiveNodeMap(int prop)
       
   163 {
       
   164   return active_nodemaps[prop];
       
   165 }
       
   166 
       
   167 std::vector<std::string> MapStorage::getArcMapList(MapType type)
       
   168 {
       
   169   if (type == ALL)
       
   170   {
       
   171     std::vector<std::string> ret;
       
   172     for (ArcMapStore::const_iterator it = arcmaps.begin();
       
   173         it != arcmaps.end(); ++it)
       
   174     {
       
   175       ret.push_back(it->first);
       
   176     }
       
   177     return ret;
       
   178   }
       
   179   else
       
   180   {
       
   181     std::vector<std::string> ret;
       
   182     for (ArcMapStore::const_iterator it = arcmaps.begin();
       
   183         it != arcmaps.end(); ++it)
       
   184     {
       
   185       ArcMapData* data = getArcMapData(it->first);
       
   186       MapValue::Type t = data->type();
       
   187       if ((t == MapValue::NUMERIC && (type & NUM)) ||
       
   188           (t == MapValue::STRING && (type & STR)))
       
   189       {
       
   190         ret.push_back(it->first);
       
   191       }
       
   192     }
       
   193     return ret;
       
   194   }
       
   195 }
       
   196 
       
   197 std::vector<std::string> MapStorage::getNodeMapList(MapType type)
       
   198 {
       
   199   if (type == ALL)
       
   200   {
       
   201     std::vector<std::string> ret;
       
   202     for (NodeMapStore::const_iterator it = nodemaps.begin();
       
   203         it != nodemaps.end(); ++it)
       
   204     {
       
   205       ret.push_back(it->first);
       
   206     }
       
   207     return ret;
       
   208   }
       
   209   else
       
   210   {
       
   211     std::vector<std::string> ret;
       
   212     for (NodeMapStore::const_iterator it = nodemaps.begin();
       
   213         it != nodemaps.end(); ++it)
       
   214     {
       
   215       NodeMapData* data = getNodeMapData(it->first);
       
   216       MapValue::Type t = data->type();
       
   217       if ((t == MapValue::NUMERIC && (type & NUM)) ||
       
   218           (t == MapValue::STRING && (type & STR)))
       
   219       {
       
   220         ret.push_back(it->first);
       
   221       }
       
   222     }
       
   223     return ret;
       
   224   }
       
   225 }
       
   226 
       
   227 sigc::signal<void, bool, int> MapStorage::signal_prop_ch()
       
   228 {
       
   229   return signal_prop;
       
   230 }
       
   231 
       
   232 int MapStorage::readFromFile(const std::string &filename)
       
   233 {
       
   234   using std::vector;
       
   235   using std::map;
       
   236   using std::string;
       
   237 
       
   238   //reading content of file
       
   239   LgfContents content(filename);
       
   240   try
       
   241     {
       
   242       content.run();
       
   243     }
       
   244   catch (Exception& error)
       
   245     {
       
   246       Gtk::MessageDialog mdialog(error.what());
       
   247       mdialog.run();
       
   248       clear();
       
   249       return 1;
       
   250     }
       
   251 
       
   252   // check whether the .conf file exists
       
   253   bool gui_data_in_conf = g_file_test((filename + ".conf").c_str(),
       
   254       (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR));
       
   255 
       
   256   // check whether the .lgf file contains a gui section
       
   257   bool gui_data_in_lgf = false;
       
   258   {
       
   259     for(int i=0;i<content.extraSectionNum();i++)
       
   260       {
       
   261 	if(content.extraSection(i)=="gui")
       
   262 	  {
       
   263 	    gui_data_in_lgf = true;
       
   264 	  }
       
   265       }
       
   266   }
       
   267 
       
   268   bool gui_data_found = gui_data_in_lgf || gui_data_in_conf;
       
   269 
       
   270   // ask for user input if both exist
       
   271   bool use_gui_data_in_lgf = false;
       
   272   if (gui_data_in_conf && gui_data_in_lgf)
       
   273   {
       
   274     Gtk::MessageDialog mdialog(_("<b>Found both ") + filename +
       
   275         _(".conf and a gui section in ") + filename + _(".</b>"), true,
       
   276         Gtk::MESSAGE_QUESTION, Gtk::BUTTONS_NONE);
       
   277 	  mdialog.add_button(_("Use the ._conf file"), 1);
       
   278 	  mdialog.add_button(_("Use the _gui section"), 2);
       
   279     switch (mdialog.run())
       
   280     {
       
   281       case 1:
       
   282         use_gui_data_in_lgf = false;
       
   283         break;
       
   284       case 2:
       
   285         use_gui_data_in_lgf = true;
       
   286         break;
       
   287       case Gtk::RESPONSE_NONE:
       
   288         return 1;
       
   289     }
       
   290   }
       
   291   else
       
   292   {
       
   293     use_gui_data_in_lgf = gui_data_in_lgf;
       
   294   }
       
   295 
       
   296   if (gui_data_found)
       
   297   {
       
   298     GUISectData gui_data;
       
   299     if (use_gui_data_in_lgf)
       
   300     {
       
   301       // read the gui section from the .lgf file
       
   302       try
       
   303       {
       
   304 	sectionReader(filename).sectionStream("gui", GuiReader(this, gui_data)).run();
       
   305         gui_sect_save_dest = LGF_FILE;
       
   306       }
       
   307       catch (Exception& error)
       
   308       {
       
   309         clear();
       
   310         return 1;
       
   311       }
       
   312     }
       
   313     else
       
   314     {
       
   315       // read the gui section from the .conf file
       
   316       try
       
   317       {
       
   318 	sectionReader(filename + ".conf").sectionStream("gui", GuiReader(this, gui_data)).run();
       
   319         gui_sect_save_dest = CONF_FILE;
       
   320       }
       
   321       catch (Exception& error)
       
   322       {
       
   323         clear();
       
   324         return 1;
       
   325       }
       
   326     }
       
   327 
       
   328     // read the digraph and maps form the .lgf file
       
   329     try
       
   330     {
       
   331       std::string node_coord_xmap_name, node_coord_ymap_name;
       
   332       std::string arrow_coord_xmap_name, arrow_coord_ymap_name;
       
   333 
       
   334       if (gui_data.node_coords_save_dest ==
       
   335           MapStorage::SpecMapSaveOpts::NESET_SECT)
       
   336       {
       
   337         switch (gui_data.node_coords_save_map_num)
       
   338         {
       
   339           case SpecMapSaveOpts::ONE_MAP:
       
   340             node_coord_xmap_name = gui_data.node_coords_one_map_name + ":x";
       
   341             node_coord_ymap_name = gui_data.node_coords_one_map_name + ":y";
       
   342             node_coords_one_map_name = gui_data.node_coords_one_map_name;
       
   343             break;
       
   344           case SpecMapSaveOpts::TWO_MAPS:
       
   345             node_coord_xmap_name = gui_data.node_coords_two_maps_1_name;
       
   346             node_coord_ymap_name = gui_data.node_coords_two_maps_2_name;
       
   347             node_coords_two_maps_1_name = gui_data.node_coords_two_maps_1_name;
       
   348             node_coords_two_maps_2_name = gui_data.node_coords_two_maps_2_name;
       
   349             break;
       
   350         }
       
   351         node_coords_save_dest = gui_data.node_coords_save_dest;
       
   352         node_coords_save_map_num = gui_data.node_coords_save_map_num;
       
   353       }
       
   354 
       
   355       if (gui_data.arrow_coords_save_dest ==
       
   356           MapStorage::SpecMapSaveOpts::NESET_SECT)
       
   357       {
       
   358         switch (gui_data.arrow_coords_save_map_num)
       
   359         {
       
   360           case SpecMapSaveOpts::ONE_MAP:
       
   361             arrow_coord_xmap_name = gui_data.arrow_coords_one_map_name + ":x";
       
   362             arrow_coord_ymap_name = gui_data.arrow_coords_one_map_name + ":y";
       
   363             arrow_coords_one_map_name = gui_data.arrow_coords_one_map_name;
       
   364             break;
       
   365           case SpecMapSaveOpts::TWO_MAPS:
       
   366             arrow_coord_xmap_name = gui_data.arrow_coords_two_maps_1_name;
       
   367             arrow_coord_ymap_name = gui_data.arrow_coords_two_maps_2_name;
       
   368             arrow_coords_two_maps_1_name =
       
   369               gui_data.arrow_coords_two_maps_1_name;
       
   370             arrow_coords_two_maps_2_name =
       
   371               gui_data.arrow_coords_two_maps_2_name;
       
   372             break;
       
   373         }
       
   374         arrow_coords_save_dest = gui_data.arrow_coords_save_dest;
       
   375         arrow_coords_save_map_num = gui_data.arrow_coords_save_map_num;
       
   376       }
       
   377       readLGF(filename, true,
       
   378           gui_data.main_node_map_names, gui_data.main_arc_map_names,
       
   379           gui_data.node_map_types, gui_data.arc_map_types,
       
   380           node_coord_xmap_name, node_coord_ymap_name,
       
   381           arrow_coord_xmap_name, arrow_coord_ymap_name);
       
   382     }
       
   383     catch (Exception& error)
       
   384     {
       
   385       clear();
       
   386       return 1;
       
   387     }
       
   388 
       
   389     // add the maps from the gui section
       
   390     for (vector<string>::const_iterator
       
   391         it = gui_data.gui_node_map_names.begin();
       
   392         it != gui_data.gui_node_map_names.end(); ++it)
       
   393     {
       
   394       string map_name = *it;
       
   395       switch (gui_data.node_map_types[map_name])
       
   396       {
       
   397         case MapValue::NUMERIC:
       
   398           {
       
   399             createNodeMap(map_name, MapValue::NUMERIC, double());
       
   400             NumericNodeMap& dmap = getNumericNodeMap(map_name);
       
   401             map<int, double>& smap = *gui_data.numeric_node_maps[map_name];
       
   402             for (NodeIt n(digraph); n != INVALID; ++n)
       
   403             {
       
   404               dmap[n] = smap[node_label[n]];
       
   405             }
       
   406             break;
       
   407           }
       
   408         case MapValue::STRING:
       
   409           {
       
   410             createNodeMap(map_name, MapValue::STRING, string());
       
   411             StringNodeMap& dmap = getStringNodeMap(map_name);
       
   412             map<int, string>& smap = *gui_data.string_node_maps[map_name];
       
   413             for (NodeIt n(digraph); n != INVALID; ++n)
       
   414             {
       
   415               dmap[n] = smap[node_label[n]];
       
   416             }
       
   417             break;
       
   418           }
       
   419       }
       
   420       getNodeMapData(map_name)->save_dest = GUI_SECT;
       
   421     }
       
   422     for (vector<string>::const_iterator
       
   423         it = gui_data.gui_arc_map_names.begin();
       
   424         it != gui_data.gui_arc_map_names.end(); ++it)
       
   425     {
       
   426       string map_name = *it;
       
   427       switch (gui_data.arc_map_types[map_name])
       
   428       {
       
   429         case MapValue::NUMERIC:
       
   430           {
       
   431             createArcMap(map_name, MapValue::NUMERIC, double());
       
   432             NumericArcMap& dmap = getNumericArcMap(map_name);
       
   433             map<int, double>& smap = *gui_data.numeric_arc_maps[map_name];
       
   434             for (ArcIt e(digraph); e != INVALID; ++e)
       
   435             {
       
   436               dmap[e] = smap[arc_label[e]];
       
   437             }
       
   438             break;
       
   439           }
       
   440         case MapValue::STRING:
       
   441           {
       
   442             createArcMap(map_name, MapValue::STRING, string());
       
   443             StringArcMap& dmap = getStringArcMap(map_name);
       
   444             map<int, string>& smap = *gui_data.string_arc_maps[map_name];
       
   445             for (ArcIt e(digraph); e != INVALID; ++e)
       
   446             {
       
   447               dmap[e] = smap[arc_label[e]];
       
   448             }
       
   449             break;
       
   450           }
       
   451       }
       
   452       getArcMapData(map_name)->save_dest = GUI_SECT;
       
   453     }
       
   454 
       
   455     // restore the node coordinate maps
       
   456     if (gui_data.node_coords_save_dest ==
       
   457         MapStorage::SpecMapSaveOpts::GUI_SECT)
       
   458     {
       
   459       for (NodeIt n(digraph); n != INVALID; ++n)
       
   460       {
       
   461         node_coords.set(n, gui_data.node_coord_map[node_label[n]]);
       
   462       }
       
   463       node_coords_save_dest = gui_data.node_coords_save_dest;
       
   464     }
       
   465     // restore the arrow coordinate maps
       
   466     if (gui_data.arrow_coords_save_dest ==
       
   467         MapStorage::SpecMapSaveOpts::GUI_SECT)
       
   468     {
       
   469       for (ArcIt e(digraph); e != INVALID; ++e)
       
   470       {
       
   471         arrow_coords.set(e, gui_data.arrow_coord_map[arc_label[e]]);
       
   472       }
       
   473       arrow_coords_save_dest = gui_data.arrow_coords_save_dest;
       
   474     }
       
   475   }
       
   476   else
       
   477   {
       
   478     // there is no gui section neither in the .lgf file nor in the .conf file
       
   479     {
       
   480       if (content.nodeSectionNum() < 1)
       
   481       {
       
   482         Gtk::MessageDialog mdialog("No nodeset found in file.");
       
   483         mdialog.run();
       
   484         clear();
       
   485         return 1;
       
   486       }
       
   487 
       
   488       if (content.arcSectionNum() < 1)
       
   489       {
       
   490         Gtk::MessageDialog mdialog("No arcset found in file.");
       
   491         mdialog.run();
       
   492         clear();
       
   493         return 1;
       
   494       }
       
   495 
       
   496       std::vector<std::string> nodeMapNames = content.nodeMapNames(0);
       
   497       std::vector<std::string> arcMapNames = content.arcMapNames(0);
       
   498       
       
   499       bool read_arc_label = true;
       
   500       if (std::find(arcMapNames.begin(), arcMapNames.end(), "label") ==
       
   501           arcMapNames.end())
       
   502       {
       
   503         read_arc_label = false;
       
   504       }
       
   505 
       
   506       nodeMapNames.erase(
       
   507           std::remove(nodeMapNames.begin(), nodeMapNames.end(), "label"),
       
   508           nodeMapNames.end());
       
   509 
       
   510       arcMapNames.erase(
       
   511           std::remove(arcMapNames.begin(), arcMapNames.end(), "label"),
       
   512           arcMapNames.end());
       
   513 
       
   514       FileImportDialog::ImportData data(nodeMapNames, arcMapNames);
       
   515       FileImportDialog fidialog(&data);
       
   516       int response = fidialog.run();
       
   517       if (response == Gtk::RESPONSE_OK)
       
   518       {
       
   519         try
       
   520         {
       
   521           std::string node_coord_xmap_name, node_coord_ymap_name;
       
   522           std::string arrow_coord_xmap_name, arrow_coord_ymap_name;
       
   523           bool gen_node_coords = false;
       
   524           bool gen_arrow_coords = false;
       
   525 
       
   526           switch (data.node_coord_load_from)
       
   527           {
       
   528             case FileImportDialog::ImportData::ONE_MAP:
       
   529               node_coord_xmap_name = data.node_coord_one_map_name + ":x";
       
   530               node_coord_ymap_name = data.node_coord_one_map_name + ":y";
       
   531               node_coords_one_map_name = data.node_coord_one_map_name;
       
   532 
       
   533               node_coords_save_dest = SpecMapSaveOpts::NESET_SECT;
       
   534               node_coords_save_map_num = SpecMapSaveOpts::ONE_MAP;
       
   535               break;
       
   536             case FileImportDialog::ImportData::TWO_MAPS:
       
   537               node_coord_xmap_name = data.node_coord_two_maps_1_name;
       
   538               node_coord_ymap_name = data.node_coord_two_maps_2_name;
       
   539               node_coords_two_maps_1_name = data.node_coord_two_maps_1_name;
       
   540               node_coords_two_maps_2_name = data.node_coord_two_maps_2_name;
       
   541 
       
   542               node_coords_save_dest = SpecMapSaveOpts::NESET_SECT;
       
   543               node_coords_save_map_num = SpecMapSaveOpts::TWO_MAPS;
       
   544               break;
       
   545             case FileImportDialog::ImportData::DONT_READ:
       
   546               node_coord_xmap_name = "";
       
   547               node_coord_ymap_name = "";
       
   548 
       
   549               node_coords_save_dest = SpecMapSaveOpts::GUI_SECT;
       
   550               gen_node_coords = true;
       
   551               break;
       
   552           }
       
   553 
       
   554           switch (data.arrow_coord_load_from)
       
   555           {
       
   556             case FileImportDialog::ImportData::ONE_MAP:
       
   557               arrow_coord_xmap_name = data.arrow_coord_one_map_name + ":x";
       
   558               arrow_coord_ymap_name = data.arrow_coord_one_map_name + ":y";
       
   559               arrow_coords_one_map_name = data.arrow_coord_one_map_name;
       
   560 
       
   561               arrow_coords_save_dest = SpecMapSaveOpts::NESET_SECT;
       
   562               arrow_coords_save_map_num = SpecMapSaveOpts::ONE_MAP;
       
   563               break;
       
   564             case FileImportDialog::ImportData::TWO_MAPS:
       
   565               arrow_coord_xmap_name = data.arrow_coord_two_maps_1_name;
       
   566               arrow_coord_ymap_name = data.arrow_coord_two_maps_2_name;
       
   567               arrow_coords_two_maps_1_name = data.arrow_coord_two_maps_1_name;
       
   568               arrow_coords_two_maps_2_name = data.arrow_coord_two_maps_2_name;
       
   569 
       
   570               arrow_coords_save_dest = SpecMapSaveOpts::NESET_SECT;
       
   571               arrow_coords_save_map_num = SpecMapSaveOpts::TWO_MAPS;
       
   572               break;
       
   573             case FileImportDialog::ImportData::DONT_READ:
       
   574               arrow_coord_xmap_name = "";
       
   575               arrow_coord_ymap_name = "";
       
   576 
       
   577               arrow_coords_save_dest = SpecMapSaveOpts::GUI_SECT;
       
   578               gen_arrow_coords = true;
       
   579               break;
       
   580           }
       
   581 
       
   582           // read arc and node maps
       
   583           std::vector<std::string> node_map_names;
       
   584           std::vector<std::string> arc_map_names;
       
   585           std::map<std::string, MapValue::Type> node_map_types;
       
   586           std::map<std::string, MapValue::Type> arc_map_types;
       
   587           for (std::vector<std::string>::const_iterator it =
       
   588               data.numeric_node_map_names.begin();
       
   589               it != data.numeric_node_map_names.end(); ++it)
       
   590           {
       
   591             node_map_names.push_back(*it);
       
   592             node_map_types[*it] = MapValue::NUMERIC;
       
   593           }
       
   594           for (std::vector<std::string>::const_iterator it =
       
   595               data.string_node_map_names.begin();
       
   596               it != data.string_node_map_names.end(); ++it)
       
   597           {
       
   598             node_map_names.push_back(*it);
       
   599             node_map_types[*it] = MapValue::STRING;
       
   600           }
       
   601           for (std::vector<std::string>::const_iterator it =
       
   602               data.numeric_arc_map_names.begin();
       
   603               it != data.numeric_arc_map_names.end(); ++it)
       
   604           {
       
   605             arc_map_names.push_back(*it);
       
   606             arc_map_types[*it] = MapValue::NUMERIC;
       
   607           }
       
   608           for (std::vector<std::string>::const_iterator it =
       
   609               data.string_arc_map_names.begin();
       
   610               it != data.string_arc_map_names.end(); ++it)
       
   611           {
       
   612             arc_map_names.push_back(*it);
       
   613             arc_map_types[*it] = MapValue::STRING;
       
   614           }
       
   615 
       
   616           readLGF(filename, read_arc_label,
       
   617               node_map_names, arc_map_names,
       
   618               node_map_types, arc_map_types,
       
   619               node_coord_xmap_name, node_coord_ymap_name,
       
   620               arrow_coord_xmap_name, arrow_coord_ymap_name);
       
   621 
       
   622           // generate arc labels
       
   623           if (!read_arc_label)
       
   624           {
       
   625             int l = 0;
       
   626             for (ArcIt e(digraph); e != INVALID; ++e)
       
   627             {
       
   628               arc_label[e] = l++;
       
   629             }
       
   630           }
       
   631 
       
   632           if (gen_node_coords)
       
   633           {
       
   634             // generate node coordinates
       
   635             int node_num = 0;
       
   636             for (NodeIt n(digraph); n != INVALID; ++n) { node_num++; }
       
   637             const double pi = 3.142;
       
   638             double step = 2 * pi / (double) node_num;
       
   639             int i = 0;
       
   640             for (NodeIt n(digraph); n != INVALID; ++n)
       
   641             {
       
   642               setNodeCoords(n,
       
   643                   XY(250.0 * std::cos(i * step),
       
   644                     250.0 * std::sin(i * step)));
       
   645               i++;
       
   646             }
       
   647           }
       
   648           if (gen_arrow_coords)
       
   649           {
       
   650             // generate arrow coordinates
       
   651             for (ArcIt e(digraph); e != INVALID; ++e)
       
   652             {
       
   653               if (digraph.source(e) == digraph.target(e))
       
   654               {
       
   655                 setArrowCoords(e,
       
   656                     getNodeCoords(digraph.source(e)) + XY(0.0, 80.0));
       
   657               }
       
   658               else
       
   659               {
       
   660                 setArrowCoords(e,
       
   661                     (getNodeCoords(digraph.source(e)) +
       
   662                      getNodeCoords(digraph.target(e))) / 2.0);
       
   663               }
       
   664             }
       
   665           }
       
   666         }
       
   667         catch (Exception& error)
       
   668         {
       
   669           clear();
       
   670           return 1;
       
   671         }
       
   672       }
       
   673       else
       
   674       {
       
   675         clear();
       
   676         return 1;
       
   677       }
       
   678     }
       
   679   }
       
   680 
       
   681   // set max_node_label
       
   682   {
       
   683     max_node_label = std::numeric_limits<int>::min();
       
   684     for (NodeIt n(digraph); n != INVALID; ++n)
       
   685     {
       
   686       if (node_label[n] > max_node_label)
       
   687       {
       
   688         max_node_label = node_label[n];
       
   689       }
       
   690     }
       
   691   }
       
   692   // set max_arc_label
       
   693   {
       
   694     max_arc_label = std::numeric_limits<int>::min();
       
   695     for (ArcIt e(digraph); e != INVALID; ++e)
       
   696     {
       
   697       if (arc_label[e] > max_arc_label)
       
   698       {
       
   699         max_arc_label = arc_label[e];
       
   700       }
       
   701     }
       
   702   }
       
   703 
       
   704   return 0;
       
   705 }
       
   706 
       
   707 void MapStorage::writeToFile(const std::string &filename)
       
   708 {
       
   709   // relabel nodes and arcs
       
   710   int i = 0;
       
   711   for (NodeIt n(digraph); n != INVALID; ++n)
       
   712   {
       
   713     node_label[n] = i++;
       
   714   }
       
   715   max_node_label = i-1;
       
   716   i = 0;
       
   717   for (ArcIt e(digraph); e != INVALID; ++e)
       
   718   {
       
   719     arc_label[e] = i++;
       
   720   }
       
   721   max_arc_label = i-1;
       
   722 
       
   723   // write .lgf file
       
   724   {
       
   725     DigraphWriter<Digraph> gwriter(filename, digraph);
       
   726 
       
   727     gwriter.nodeMap("label", node_label);
       
   728     gwriter.arcMap("label", arc_label);
       
   729 
       
   730     // write node maps
       
   731     for (NodeMapStore::const_iterator it = nodemaps.begin();
       
   732         it != nodemaps.end(); ++it)
       
   733     {
       
   734       if (it->second->save_dest == NESET_SECT)
       
   735       {
       
   736         switch (it->second->type())
       
   737         {
       
   738           case MapValue::NUMERIC:
       
   739             gwriter.nodeMap(it->first, getNumericNodeMap(it->first));
       
   740             break;
       
   741           case MapValue::STRING:
       
   742             gwriter.nodeMap(it->first, getStringNodeMap(it->first));
       
   743             break;
       
   744         }
       
   745       }
       
   746     }
       
   747 
       
   748     // write arc maps
       
   749     for (ArcMapStore::const_iterator it = arcmaps.begin();
       
   750         it != arcmaps.end(); ++it)
       
   751     {
       
   752       if (it->second->save_dest == NESET_SECT)
       
   753       {
       
   754         switch (it->second->type())
       
   755         {
       
   756           case MapValue::NUMERIC:
       
   757             gwriter.arcMap(it->first, getNumericArcMap(it->first));
       
   758             break;
       
   759           case MapValue::STRING:
       
   760             gwriter.arcMap(it->first, getStringArcMap(it->first));
       
   761             break;
       
   762         }
       
   763       }
       
   764     }
       
   765 
       
   766     // write node coordinates
       
   767     switch (getNodeCoordsSaveDest())
       
   768     {
       
   769       case MapStorage::SpecMapSaveOpts::GUI_SECT:
       
   770         break;
       
   771       case MapStorage::SpecMapSaveOpts::NESET_SECT:
       
   772         switch (getNodeCoordsSaveMapNum())
       
   773         {
       
   774           case MapStorage::SpecMapSaveOpts::ONE_MAP:
       
   775             gwriter.nodeMap(node_coords_one_map_name + ":x",
       
   776                 node_coords_x);
       
   777             gwriter.nodeMap(node_coords_one_map_name + ":y",
       
   778                 node_coords_y);
       
   779             break;
       
   780           case MapStorage::SpecMapSaveOpts::TWO_MAPS:
       
   781             gwriter.nodeMap(node_coords_two_maps_1_name,
       
   782                 node_coords_x);
       
   783             gwriter.nodeMap(node_coords_two_maps_2_name,
       
   784                 node_coords_y);
       
   785             break;
       
   786         }
       
   787         break;
       
   788     }
       
   789 
       
   790     // write arrow coordinates
       
   791     switch (getArrowCoordsSaveDest())
       
   792     {
       
   793       case MapStorage::SpecMapSaveOpts::GUI_SECT:
       
   794         break;
       
   795       case MapStorage::SpecMapSaveOpts::NESET_SECT:
       
   796         switch (getArrowCoordsSaveMapNum())
       
   797         {
       
   798           case MapStorage::SpecMapSaveOpts::ONE_MAP:
       
   799             gwriter.arcMap(arrow_coords_one_map_name + ":x",
       
   800                 arrow_coords_x);
       
   801             gwriter.arcMap(arrow_coords_one_map_name + ":y",
       
   802                 arrow_coords_y);
       
   803             break;
       
   804           case MapStorage::SpecMapSaveOpts::TWO_MAPS:
       
   805             gwriter.arcMap(arrow_coords_two_maps_1_name,
       
   806                 arrow_coords_x);
       
   807             gwriter.arcMap(arrow_coords_two_maps_2_name,
       
   808                 arrow_coords_y);
       
   809             break;
       
   810         }
       
   811         break;
       
   812     }
       
   813 
       
   814     if (gui_sect_save_dest == LGF_FILE)
       
   815     {
       
   816       GuiWriter gui_writer(this);
       
   817       gui_writer.write(gwriter.ostream());
       
   818       gwriter.run();
       
   819     }
       
   820     else
       
   821     {
       
   822       gwriter.run();
       
   823     }
       
   824   }
       
   825 
       
   826   // write .conf file
       
   827   if (gui_sect_save_dest == CONF_FILE)
       
   828   {
       
   829     DigraphWriter<Digraph> lwriter(filename + ".conf", digraph);
       
   830     GuiWriter gui_writer(this);
       
   831     gui_writer.write(lwriter.ostream());
       
   832     lwriter.run();
       
   833   }
       
   834 }
       
   835 
       
   836 void MapStorage::clear()
       
   837 {
       
   838   for (NodeMapStore::iterator it = nodemaps.begin(); it != nodemaps.end(); ++it)
       
   839   {
       
   840     delete it->second;
       
   841     nodemaps.erase(it);
       
   842   }
       
   843   for (ArcMapStore::iterator it = arcmaps.begin(); it != arcmaps.end(); ++it)
       
   844   {
       
   845     delete it->second;
       
   846     arcmaps.erase(it);
       
   847   }
       
   848   digraph.clear();
       
   849   file_name = "";
       
   850   modified = false;
       
   851   max_node_label = 0;
       
   852   max_arc_label = 0;
       
   853   background_set = false;
       
   854 
       
   855   gui_sect_save_dest = LGF_FILE;
       
   856   node_coords_save_dest = SpecMapSaveOpts::GUI_SECT;
       
   857   arrow_coords_save_dest = SpecMapSaveOpts::GUI_SECT;
       
   858   node_coords_one_map_name = "coord";
       
   859   node_coords_two_maps_1_name = "coord_x";
       
   860   node_coords_two_maps_2_name = "coord_y";
       
   861   arrow_coords_one_map_name = "arrow";
       
   862   arrow_coords_two_maps_1_name = "arrow_x";
       
   863   arrow_coords_two_maps_2_name = "arrow_y";
       
   864 
       
   865   for(int i=0;i<NODE_PROPERTY_NUM;i++)
       
   866     {
       
   867       changeActiveMap(false, i, "");
       
   868       signal_map_win.emit(false, i, "");
       
   869     }
       
   870   
       
   871   for(int i=0;i<EDGE_PROPERTY_NUM;i++)
       
   872     {
       
   873       changeActiveMap(true, i, "");
       
   874       signal_map_win.emit(true, i, "");
       
   875     }
       
   876 
       
   877   attraction=a_d;
       
   878   propulsation=p_d;
       
   879   iterations=i_d;
       
   880 
       
   881   signal_design_win.emit(attraction, propulsation, iterations);
       
   882 }
       
   883 
       
   884 void MapStorage::mapChanged(bool itisarc, std::string mapname)
       
   885 {
       
   886   if(itisarc)
       
   887   {
       
   888     for(int i=0;i<EDGE_PROPERTY_NUM;i++)
       
   889     {
       
   890       if(active_arcmaps[i]==mapname)
       
   891       {
       
   892         signal_prop.emit(itisarc, i);
       
   893       }
       
   894     }
       
   895   }
       
   896   else
       
   897   {
       
   898     for(int i=0;i<NODE_PROPERTY_NUM;i++)
       
   899     {
       
   900       if(active_nodemaps[i]==mapname)
       
   901       {
       
   902         signal_prop.emit(itisarc, i);
       
   903       }
       
   904     }
       
   905   }
       
   906 }
       
   907 
       
   908 void MapStorage::get_design_data(double & attraction_p, double & propulsation_p, int & iterations_p)
       
   909 {
       
   910   attraction_p=attraction;
       
   911   propulsation_p=propulsation;
       
   912   iterations_p=iterations;
       
   913 }
       
   914 
       
   915 void MapStorage::set_attraction(double attraction_p)
       
   916 {
       
   917   attraction=attraction_p;
       
   918 }
       
   919 
       
   920 void MapStorage::set_propulsation(double propulsation_p)
       
   921 {
       
   922   propulsation=propulsation_p;
       
   923 }
       
   924 
       
   925 void MapStorage::set_iteration(int iterations_p)
       
   926 {
       
   927   iterations=iterations_p;
       
   928 }
       
   929 
       
   930 void MapStorage::redesign_data_changed()
       
   931 {
       
   932   signal_design_win.emit(attraction, propulsation, iterations);
       
   933 }
       
   934 
       
   935 XY MapStorage::getNodeCoords(Node n) const
       
   936 {
       
   937   return node_coords[n];
       
   938 }
       
   939 
       
   940 void MapStorage::setNodeCoords(Node n, XY c)
       
   941 {
       
   942   node_coords.set(n, c);
       
   943 }
       
   944 
       
   945 XY MapStorage::getArrowCoords(Arc e) const
       
   946 {
       
   947   return arrow_coords[e];
       
   948 }
       
   949 
       
   950 void MapStorage::setArrowCoords(Arc e, XY c)
       
   951 {
       
   952   arrow_coords.set(e, c);
       
   953 }
       
   954 
       
   955 MapValue MapStorage::get(const std::string& name, Node node) const
       
   956 {
       
   957   NodeMapData* data = getNodeMapData(name);
       
   958   return data->get(node);
       
   959 }
       
   960 
       
   961 void MapStorage::set(const std::string& name, Node node, MapValue val)
       
   962 {
       
   963   NodeMapData* data = getNodeMapData(name);
       
   964   data->set(node, val);
       
   965 }
       
   966 
       
   967 MapValue MapStorage::get(const std::string& name, Arc arc) const
       
   968 {
       
   969   ArcMapData* data = getArcMapData(name);
       
   970   return data->get(arc);
       
   971 }
       
   972 
       
   973 void MapStorage::set(const std::string& name, Arc arc, MapValue val)
       
   974 {
       
   975   ArcMapData* data = getArcMapData(name);
       
   976   data->set(arc, val);
       
   977 }
       
   978 
       
   979 const std::string& MapStorage::getFileName() const
       
   980 {
       
   981   return file_name;
       
   982 }
       
   983 
       
   984 void MapStorage::setFileName(const std::string& fn)
       
   985 {
       
   986   file_name = fn;
       
   987 }
       
   988 
       
   989 bool MapStorage::getModified() const
       
   990 {
       
   991   return modified;
       
   992 }
       
   993 
       
   994 void MapStorage::setModified(bool m)
       
   995 {
       
   996   modified = m;
       
   997 }
       
   998 
       
   999 Node MapStorage::addNode(XY coords)
       
  1000 {
       
  1001   Node node = digraph.addNode();
       
  1002 
       
  1003   setNodeCoords(node, coords);
       
  1004 
       
  1005   max_node_label++;
       
  1006 
       
  1007   node_label[node] = max_node_label;
       
  1008 
       
  1009   std::vector<std::string> node_maps = getNodeMapList();
       
  1010   for (std::vector<std::string>::const_iterator it = node_maps.begin();
       
  1011       it != node_maps.end(); ++it)
       
  1012   {
       
  1013     NodeMapData* data = getNodeMapData(*it);
       
  1014     set(*it, node, data->default_value);
       
  1015   }
       
  1016 
       
  1017   return node;
       
  1018 }
       
  1019 
       
  1020 Arc MapStorage::addArc(Node from, Node to)
       
  1021 {
       
  1022   Arc arc = digraph.addArc(from, to);
       
  1023 
       
  1024   if (from == to)
       
  1025   {
       
  1026     setArrowCoords(arc, getNodeCoords(from) + XY(0.0, 80.0));
       
  1027   }
       
  1028   else
       
  1029   {
       
  1030     setArrowCoords(arc, (getNodeCoords(from) + getNodeCoords(to)) / 2.0);
       
  1031   }
       
  1032 
       
  1033   max_arc_label++;
       
  1034 
       
  1035   arc_label[arc] = max_arc_label;
       
  1036 
       
  1037   std::vector<std::string> arc_maps = getArcMapList();
       
  1038   for (std::vector<std::string>::const_iterator it = arc_maps.begin();
       
  1039       it != arc_maps.end(); ++it)
       
  1040   {
       
  1041     ArcMapData* data = getArcMapData(*it);
       
  1042     set(*it, arc, data->default_value);
       
  1043   }
       
  1044   return arc;
       
  1045 }
       
  1046 
       
  1047 MapStorage::NumericNodeMap& MapStorage::getNumericNodeMap(const std::string& name)
       
  1048 {
       
  1049   NodeMapData* data = getNodeMapData(name);
       
  1050   if (data->type() != MapValue::NUMERIC)
       
  1051     throw Error("Numeric node map " + name + " does not exists.");
       
  1052   return static_cast<NumericNodeMapData*>(data)->map;
       
  1053 }
       
  1054 
       
  1055 MapStorage::StringNodeMap& MapStorage::getStringNodeMap(const std::string& name)
       
  1056 {
       
  1057   NodeMapData* data = getNodeMapData(name);
       
  1058   if (data->type() != MapValue::STRING)
       
  1059     throw Error("String node map " + name + " does not exists.");
       
  1060   return static_cast<StringNodeMapData*>(data)->map;
       
  1061 }
       
  1062 
       
  1063 MapStorage::NumericArcMap& MapStorage::getNumericArcMap(const std::string& name)
       
  1064 {
       
  1065   ArcMapData* data = getArcMapData(name);
       
  1066   if (data->type() != MapValue::NUMERIC)
       
  1067     throw Error("Numeric arc map " + name + " does not exists.");
       
  1068   return static_cast<NumericArcMapData*>(data)->map;
       
  1069 }
       
  1070 
       
  1071 MapStorage::StringArcMap& MapStorage::getStringArcMap(const std::string& name)
       
  1072 {
       
  1073   ArcMapData* data = getArcMapData(name);
       
  1074   if (data->type() != MapValue::STRING)
       
  1075     throw Error("String arc map " + name + " does not exists.");
       
  1076   return static_cast<StringArcMapData*>(data)->map;
       
  1077 }
       
  1078 
       
  1079 MapValueArcMap MapStorage::getArcMap(const std::string& name)
       
  1080 {
       
  1081   return MapValueArcMap(name, this);
       
  1082 }
       
  1083 
       
  1084 MapValueNodeMap MapStorage::getNodeMap(const std::string& name)
       
  1085 {
       
  1086   return MapValueNodeMap(name, this);
       
  1087 }
       
  1088 
       
  1089 int MapStorage::getLabel(Node n) const
       
  1090 {
       
  1091   return node_label[n];
       
  1092 }
       
  1093 
       
  1094 int MapStorage::getLabel(Arc e) const
       
  1095 {
       
  1096   return arc_label[e];
       
  1097 }
       
  1098 
       
  1099 MapStorage::GuiSectSaveDest MapStorage::getGUIDataSaveLocation()
       
  1100 {
       
  1101   return gui_sect_save_dest;
       
  1102 }
       
  1103 
       
  1104 void MapStorage::setGUIDataSaveLocation(MapStorage::GuiSectSaveDest dest)
       
  1105 {
       
  1106   gui_sect_save_dest = dest;
       
  1107 }
       
  1108 
       
  1109 MapStorage::MapSaveDest MapStorage::getNodeMapSaveDest(std::string name) const
       
  1110 {
       
  1111   NodeMapData *data = getNodeMapData(name);
       
  1112   return data->save_dest;
       
  1113 }
       
  1114 
       
  1115 MapStorage::MapSaveDest MapStorage::getArcMapSaveDest(std::string name) const
       
  1116 {
       
  1117   ArcMapData *data = getArcMapData(name);
       
  1118   return data->save_dest;
       
  1119 }
       
  1120 
       
  1121 void MapStorage::setNodeMapSaveDest(std::string name, MapStorage::MapSaveDest dest)
       
  1122 {
       
  1123   NodeMapData *data = getNodeMapData(name);
       
  1124   data->save_dest = dest;
       
  1125 }
       
  1126 
       
  1127 void MapStorage::setArcMapSaveDest(std::string name, MapStorage::MapSaveDest dest)
       
  1128 {
       
  1129   ArcMapData *data = getArcMapData(name);
       
  1130   data->save_dest = dest;
       
  1131 }
       
  1132 
       
  1133 MapStorage::ArcMapData* MapStorage::getArcMapData(std::string name) const
       
  1134 {
       
  1135   ArcMapStore::const_iterator it = arcmaps.find(name);
       
  1136   if (it != arcmaps.end())
       
  1137     return it->second;
       
  1138   else
       
  1139     throw Error("Arc map " + name + " does not exists.");
       
  1140 }
       
  1141 
       
  1142 MapStorage::NodeMapData* MapStorage::getNodeMapData(std::string name) const
       
  1143 {
       
  1144   NodeMapStore::const_iterator it = nodemaps.find(name);
       
  1145   if (it != nodemaps.end())
       
  1146     return it->second;
       
  1147   else
       
  1148     throw Error("Node map " + name + " does not exists.");
       
  1149 }
       
  1150 
       
  1151 MapValue::Type MapStorage::getNodeMapElementType(std::string name) const
       
  1152 {
       
  1153   NodeMapData *data = getNodeMapData(name);
       
  1154   return data->type();
       
  1155 }
       
  1156 
       
  1157 MapValue::Type MapStorage::getArcMapElementType(std::string name) const
       
  1158 {
       
  1159   ArcMapData *data = getArcMapData(name);
       
  1160   return data->type();
       
  1161 }
       
  1162 
       
  1163 const MapStorage::NodeLabelMap& MapStorage::getNodeLabelMap()
       
  1164 {
       
  1165   return node_label;
       
  1166 }
       
  1167 
       
  1168 const MapStorage::ArcLabelMap& MapStorage::getArcLabelMap()
       
  1169 {
       
  1170   return arc_label;
       
  1171 }
       
  1172 
       
  1173 const Digraph& MapStorage::getDigraph()
       
  1174 {
       
  1175   return digraph;
       
  1176 }
       
  1177 
       
  1178 bool MapStorage::nodeMapExists(std::string name)
       
  1179 {
       
  1180   NodeMapStore::const_iterator it = nodemaps.find(name);
       
  1181   if (it == nodemaps.end())
       
  1182     return false;
       
  1183   else
       
  1184     return true;
       
  1185 }
       
  1186 
       
  1187 bool MapStorage::arcMapExists(std::string name)
       
  1188 {
       
  1189   ArcMapStore::const_iterator it = arcmaps.find(name);
       
  1190   if (it == arcmaps.end())
       
  1191     return false;
       
  1192   else
       
  1193     return true;
       
  1194 }
       
  1195 
       
  1196 std::vector<std::string> MapStorage::getArcMaps(MapType type)
       
  1197 {
       
  1198   std::vector<std::string> maps;
       
  1199   for (ArcMapStore::const_iterator it = arcmaps.begin(); it != arcmaps.end(); ++it)
       
  1200   {
       
  1201     if (it->second->type() & type)
       
  1202     {
       
  1203       maps.push_back(it->first);
       
  1204     }
       
  1205   }
       
  1206   return maps;
       
  1207 }
       
  1208 
       
  1209 std::vector<std::string> MapStorage::getNodeMaps(MapType type)
       
  1210 {
       
  1211   std::vector<std::string> maps;
       
  1212   for (NodeMapStore::const_iterator it = nodemaps.begin(); it != nodemaps.end(); ++it)
       
  1213   {
       
  1214     if (it->second->type() & type)
       
  1215     {
       
  1216       maps.push_back(it->first);
       
  1217     }
       
  1218   }
       
  1219   return maps;
       
  1220 }
       
  1221 
       
  1222 MapStorage::NodeCoordMap& MapStorage::getNodeCoordMap()
       
  1223 {
       
  1224   return node_coords;
       
  1225 }
       
  1226 
       
  1227 MapStorage::ArrowCoordMap& MapStorage::getArrowCoordMap()
       
  1228 {
       
  1229   return arrow_coords;
       
  1230 }
       
  1231 
       
  1232 MapStorage::SpecMapSaveOpts::Dest MapStorage::getNodeCoordsSaveDest()
       
  1233 {
       
  1234   return node_coords_save_dest;
       
  1235 }
       
  1236 
       
  1237 MapStorage::SpecMapSaveOpts::Dest MapStorage::getArrowCoordsSaveDest()
       
  1238 {
       
  1239   return arrow_coords_save_dest;
       
  1240 }
       
  1241 
       
  1242 void MapStorage::setNodeCoordsSaveDest(MapStorage::SpecMapSaveOpts::Dest dest)
       
  1243 {
       
  1244   node_coords_save_dest = dest;
       
  1245 }
       
  1246 
       
  1247 void MapStorage::setArrowCoordsSaveDest(MapStorage::SpecMapSaveOpts::Dest dest)
       
  1248 {
       
  1249   arrow_coords_save_dest = dest;
       
  1250 }
       
  1251 
       
  1252 MapStorage::SpecMapSaveOpts::MapNum MapStorage::getNodeCoordsSaveMapNum()
       
  1253 {
       
  1254   return node_coords_save_map_num;
       
  1255 }
       
  1256 
       
  1257 MapStorage::SpecMapSaveOpts::MapNum MapStorage::getArrowCoordsSaveMapNum()
       
  1258 {
       
  1259   return arrow_coords_save_map_num;
       
  1260 }
       
  1261 
       
  1262 void MapStorage::setNodeCoordsSaveMapNum(MapStorage::SpecMapSaveOpts::MapNum num)
       
  1263 {
       
  1264   node_coords_save_map_num = num;
       
  1265 }
       
  1266 
       
  1267 void MapStorage::setArrowCoordsSaveMapNum(MapStorage::SpecMapSaveOpts::MapNum num)
       
  1268 {
       
  1269   arrow_coords_save_map_num = num;
       
  1270 }
       
  1271 
       
  1272 const std::string& MapStorage::getNodeCoordsOneMapName()
       
  1273 {
       
  1274   return node_coords_one_map_name;
       
  1275 }
       
  1276 const std::string& MapStorage::getNodeCoordsTwoMaps1Name()
       
  1277 {
       
  1278   return node_coords_two_maps_1_name;
       
  1279 }
       
  1280 const std::string& MapStorage::getNodeCoordsTwoMaps2Name()
       
  1281 {
       
  1282   return node_coords_two_maps_2_name;
       
  1283 }
       
  1284 
       
  1285 void MapStorage::setNodeCoordsOneMapName(const std::string& name)
       
  1286 {
       
  1287   node_coords_one_map_name = name;
       
  1288 }
       
  1289 void MapStorage::setNodeCoordsTwoMaps1Name(const std::string& name)
       
  1290 {
       
  1291   node_coords_two_maps_1_name = name;
       
  1292 }
       
  1293 void MapStorage::setNodeCoordsTwoMaps2Name(const std::string& name)
       
  1294 {
       
  1295   node_coords_two_maps_2_name = name;
       
  1296 }
       
  1297 
       
  1298 const std::string& MapStorage::getArrowCoordsOneMapName()
       
  1299 {
       
  1300   return arrow_coords_one_map_name;
       
  1301 }
       
  1302 const std::string& MapStorage::getArrowCoordsTwoMaps1Name()
       
  1303 {
       
  1304   return arrow_coords_two_maps_1_name;
       
  1305 }
       
  1306 const std::string& MapStorage::getArrowCoordsTwoMaps2Name()
       
  1307 {
       
  1308   return arrow_coords_two_maps_2_name;
       
  1309 }
       
  1310 
       
  1311 void MapStorage::setArrowCoordsOneMapName(const std::string& name)
       
  1312 {
       
  1313   arrow_coords_one_map_name = name;
       
  1314 }
       
  1315 void MapStorage::setArrowCoordsTwoMaps1Name(const std::string& name)
       
  1316 {
       
  1317   arrow_coords_two_maps_1_name = name;
       
  1318 }
       
  1319 void MapStorage::setArrowCoordsTwoMaps2Name(const std::string& name)
       
  1320 {
       
  1321   arrow_coords_two_maps_2_name = name;
       
  1322 }
       
  1323 
       
  1324 void MapStorage::readLGF(
       
  1325     const std::string& filename,
       
  1326     bool read_arc_label,
       
  1327     const std::vector<std::string>& node_map_names,
       
  1328     const std::vector<std::string>& arc_map_names,
       
  1329     const std::map<std::string, MapValue::Type>& node_map_types,
       
  1330     const std::map<std::string, MapValue::Type>& arc_map_types,
       
  1331     const std::string& node_coord_xmap_name,
       
  1332     const std::string& node_coord_ymap_name,
       
  1333     const std::string& arrow_coord_xmap_name,
       
  1334     const std::string& arrow_coord_ymap_name)
       
  1335 {
       
  1336   using std::vector;
       
  1337   using std::map;
       
  1338   using std::string;
       
  1339 
       
  1340   DigraphReader<Digraph> greader(filename, digraph);
       
  1341 
       
  1342   // read the label maps
       
  1343   greader.nodeMap("label", node_label);
       
  1344   if (read_arc_label)
       
  1345     greader.arcMap("label", arc_label);
       
  1346 
       
  1347   // read the node maps
       
  1348   for (vector<string>::const_iterator
       
  1349       it = node_map_names.begin();
       
  1350       it != node_map_names.end(); ++it)
       
  1351   {
       
  1352     switch (node_map_types.find(*it)->second)
       
  1353     {
       
  1354       case MapValue::NUMERIC:
       
  1355         {
       
  1356           createNodeMap(*it, MapValue::NUMERIC, double());
       
  1357           greader.nodeMap(*it, getNumericNodeMap(*it));
       
  1358           break;
       
  1359         }
       
  1360       case MapValue::STRING:
       
  1361         {
       
  1362           createNodeMap(*it, MapValue::STRING, string());
       
  1363           greader.nodeMap(*it, getStringNodeMap(*it));
       
  1364           break;
       
  1365         }
       
  1366     }
       
  1367     getNodeMapData(*it)->save_dest = NESET_SECT;
       
  1368   }
       
  1369 
       
  1370   // read the arc maps
       
  1371   for (vector<string>::const_iterator
       
  1372       it = arc_map_names.begin();
       
  1373       it != arc_map_names.end(); ++it)
       
  1374   {
       
  1375     switch (arc_map_types.find(*it)->second)
       
  1376     {
       
  1377       case MapValue::NUMERIC:
       
  1378         {
       
  1379           createArcMap(*it, MapValue::NUMERIC, double());
       
  1380           greader.arcMap(*it, getNumericArcMap(*it));
       
  1381           break;
       
  1382         }
       
  1383       case MapValue::STRING:
       
  1384         {
       
  1385           createArcMap(*it, MapValue::STRING, string());
       
  1386           greader.arcMap(*it, getStringArcMap(*it));
       
  1387           break;
       
  1388         }
       
  1389     }
       
  1390     getArcMapData(*it)->save_dest = NESET_SECT;
       
  1391   }
       
  1392 
       
  1393   // read the node coordinate maps
       
  1394   if (node_coord_xmap_name != "")
       
  1395     greader.nodeMap(node_coord_xmap_name, node_coords_x);
       
  1396   if (node_coord_ymap_name != "")
       
  1397     greader.nodeMap(node_coord_ymap_name, node_coords_y);
       
  1398 
       
  1399   // read the arrow coordinate maps
       
  1400   if (arrow_coord_xmap_name != "")
       
  1401     greader.arcMap(arrow_coord_xmap_name, arrow_coords_x);
       
  1402   if (arrow_coord_ymap_name != "")
       
  1403     greader.arcMap(arrow_coord_ymap_name, arrow_coords_y);
       
  1404 
       
  1405   greader.run();
       
  1406 }
       
  1407 
       
  1408 void MapStorage::setBackground(const std::string& file_name)
       
  1409 {
       
  1410   if (file_name == background_file_name) return;
       
  1411   if (file_name == "")
       
  1412   {
       
  1413     background_file_name = "";
       
  1414     background_set = false;
       
  1415   }
       
  1416   else
       
  1417   {
       
  1418     background_file_name = file_name;
       
  1419     background_set = true;
       
  1420   }
       
  1421   signal_background.emit();
       
  1422 }
       
  1423 
       
  1424 const std::string& MapStorage::getBackgroundFilename()
       
  1425 {
       
  1426   return background_file_name;
       
  1427 }
       
  1428 
       
  1429 bool MapStorage::isBackgroundSet()
       
  1430 {
       
  1431   return background_set;
       
  1432 }
       
  1433 
       
  1434 double MapStorage::getBackgroundScaling()
       
  1435 {
       
  1436   return background_scaling;
       
  1437 }
       
  1438 
       
  1439 void MapStorage::setBackgroundScaling(double scaling)
       
  1440 {
       
  1441   background_scaling = scaling;
       
  1442 }
       
  1443 
       
  1444 void MapStorage::exportDigraphToEPS(std::vector<bool> options, std::string filename, std::string shapemap)
       
  1445 {
       
  1446   Digraph::NodeMap<int> _shapes(digraph, 0);
       
  1447   Digraph::NodeMap<int> _nodeColors(digraph, 0);
       
  1448   Digraph::ArcMap<int> _arcColors(digraph, 0);
       
  1449   Digraph::NodeMap<double> _nodeSizes(digraph, 6.0);
       
  1450   Digraph::ArcMap<double> _arcWidths(digraph, 1.0);
       
  1451   bool _drawArrows=options[ARROWS];
       
  1452   bool _enableParallel=options[PAR];
       
  1453 
       
  1454   std::string emptyString="";
       
  1455   Digraph::NodeMap<std::string> _nodeTextMap(digraph,emptyString);
       
  1456 
       
  1457   //_nodeTextMap=(Digraph::NodeMap<void> *)&emptyStringMap;
       
  1458 
       
  1459   if(options[N_MAPS])
       
  1460     {
       
  1461       if(active_nodemaps[N_RADIUS]!="")
       
  1462 	{
       
  1463 	  _nodeSizes=getNumericNodeMap(active_nodemaps[N_RADIUS]);
       
  1464 	}
       
  1465       if(active_nodemaps[N_COLOR]!="")
       
  1466 	{
       
  1467 	  for(NodeIt ni(digraph);ni!=INVALID;++ni)
       
  1468 	    {
       
  1469 	      _nodeColors[ni]=(int)get(active_nodemaps[N_COLOR], ni);
       
  1470 	    }
       
  1471 	}
       
  1472       if(active_nodemaps[N_TEXT]!="")
       
  1473 	{
       
  1474 	  for(NodeIt ni(digraph);ni!=INVALID;++ni)
       
  1475 	    {
       
  1476 	      std::ostringstream o;
       
  1477 	      o << get(active_nodemaps[N_TEXT], ni);
       
  1478 	      _nodeTextMap[ni]=o.str();	      
       
  1479 	    }
       
  1480 	}
       
  1481     }
       
  1482   if(options[E_MAPS])
       
  1483     {
       
  1484       if(active_arcmaps[E_WIDTH]!="")
       
  1485 	{
       
  1486 	  _arcWidths=getNumericArcMap(active_arcmaps[E_WIDTH]);
       
  1487 	}
       
  1488       if(active_arcmaps[E_COLOR]!="")
       
  1489 	{
       
  1490 	  for(ArcIt ei(digraph);ei!=INVALID;++ei)
       
  1491 	    {
       
  1492 	      _arcColors[ei]=(int)get(active_arcmaps[E_COLOR], ei);
       
  1493 	    }
       
  1494 	}
       
  1495     }
       
  1496   if(shapemap!="Default values")
       
  1497     {
       
  1498       double min = std::numeric_limits<double>::max();
       
  1499       double max = std::numeric_limits<double>::min();
       
  1500       for (NodeIt n(digraph); n != INVALID; ++n)
       
  1501       {
       
  1502         double v = static_cast<double>(get(shapemap, n));
       
  1503         if (v < min) min = v;
       
  1504         if (v > max) max = v;
       
  1505       }
       
  1506       if((min>=0)&&(max<=4))
       
  1507 	{
       
  1508           NumericNodeMap& map = static_cast<NumericNodeMapData*>(getNodeMapData(shapemap))->map;
       
  1509           for (NodeIt n(digraph); n != INVALID; ++n)
       
  1510           {
       
  1511             _shapes[n] = static_cast<int>(map[n]);
       
  1512           }
       
  1513 	}
       
  1514     }
       
  1515 
       
  1516   Palette palette;
       
  1517   Palette paletteW(true);
       
  1518 
       
  1519   graphToEps(digraph,filename).
       
  1520     title("Sample .eps figure (fits to A4)").
       
  1521     copyright("(C) 2006 LEMON Project").
       
  1522     absoluteNodeSizes().absoluteArcWidths().
       
  1523     nodeScale(2).nodeSizes(_nodeSizes).
       
  1524     coords(node_coords).
       
  1525     nodeShapes(_shapes).
       
  1526     nodeColors(composeMap(paletteW,_nodeColors)).
       
  1527     arcColors(composeMap(palette,_arcColors)).
       
  1528     arcWidthScale(0.3).arcWidths(_arcWidths).
       
  1529     nodeTexts(_nodeTextMap).nodeTextSize(7).
       
  1530     enableParallel(_enableParallel).parArcDist(5).
       
  1531     drawArrows(_drawArrows).arrowWidth(7).arrowLength(7).
       
  1532     run();
       
  1533 
       
  1534 }