gui/mapstorage.cc
changeset 1606 dc4ea2010dee
parent 1597 15b51d278bf0
child 1617 c17d61f77319
     1.1 --- a/gui/mapstorage.cc	Thu Jul 28 19:09:39 2005 +0000
     1.2 +++ b/gui/mapstorage.cc	Fri Jul 29 12:01:37 2005 +0000
     1.3 @@ -1,8 +1,29 @@
     1.4 -#include <mapstorage.h>
     1.5 +#include "mapstorage.h"
     1.6  
     1.7 -MapStorage::MapStorage(Graph & graph):g(graph)
     1.8 +MapStorage::MapStorage() : modified(false), file_name("")
     1.9  {
    1.10 -};
    1.11 +  nodemap_storage["coordinates_x"] = new Graph::NodeMap<double>(graph);
    1.12 +  coords.setXMap(*nodemap_storage["coordinates_x"]);
    1.13 +  nodemap_storage["coordinates_y"] = new Graph::NodeMap<double>(graph);
    1.14 +  coords.setYMap(*nodemap_storage["coordinates_y"]);
    1.15 +
    1.16 +  nodemap_storage["id"] = new Graph::NodeMap<double>(graph);
    1.17 +  edgemap_storage["id"] = new Graph::EdgeMap<double>(graph);
    1.18 +}
    1.19 +
    1.20 +MapStorage::~MapStorage()
    1.21 +{
    1.22 +  for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
    1.23 +      nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
    1.24 +  {
    1.25 +    delete it->second;
    1.26 +  }
    1.27 +  for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
    1.28 +      edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
    1.29 +  {
    1.30 +    delete it->second;
    1.31 +  }
    1.32 +}
    1.33  
    1.34  int MapStorage::addNodeMap(const std::string & name, Graph::NodeMap<double> *nodemap)
    1.35  {
    1.36 @@ -27,7 +48,7 @@
    1.37  double MapStorage::maxOfNodeMap(const std::string & name)
    1.38  {
    1.39    double max=0;
    1.40 -  for (NodeIt j(g); j!=INVALID; ++j)
    1.41 +  for (NodeIt j(graph); j!=INVALID; ++j)
    1.42    {
    1.43      if( (*nodemap_storage[name])[j]>max )
    1.44      {
    1.45 @@ -40,7 +61,7 @@
    1.46  double MapStorage::maxOfEdgeMap(const std::string & name)
    1.47  {
    1.48    double max=0;
    1.49 -  for (EdgeIt j(g); j!=INVALID; ++j)
    1.50 +  for (EdgeIt j(graph); j!=INVALID; ++j)
    1.51    {
    1.52      if( (*edgemap_storage[name])[j]>max )
    1.53      {
    1.54 @@ -52,7 +73,7 @@
    1.55  
    1.56  double MapStorage::minOfNodeMap(const std::string & name)
    1.57  {
    1.58 -  NodeIt j(g);
    1.59 +  NodeIt j(graph);
    1.60    double min=(*nodemap_storage[name])[j];
    1.61    for (; j!=INVALID; ++j)
    1.62    {
    1.63 @@ -66,9 +87,9 @@
    1.64  
    1.65  double MapStorage::minOfEdgeMap(const std::string & name)
    1.66  {
    1.67 -  EdgeIt j(g);
    1.68 +  EdgeIt j(graph);
    1.69    double min=(*edgemap_storage[name])[j];
    1.70 -  for (EdgeIt j(g); j!=INVALID; ++j)
    1.71 +  for (EdgeIt j(graph); j!=INVALID; ++j)
    1.72    {
    1.73      if( (*edgemap_storage[name])[j]<min )
    1.74      {
    1.75 @@ -86,3 +107,131 @@
    1.76        (*((*ems_it).second))[e]=5;
    1.77      }
    1.78  }
    1.79 +
    1.80 +void MapStorage::readFromFile(const std::string &filename)
    1.81 +{
    1.82 +  bool read_x = false;
    1.83 +  bool read_y = false;
    1.84 +
    1.85 +  try {
    1.86 +    LemonReader lreader(filename);
    1.87 +    ContentReader content(lreader);
    1.88 +    lreader.run();
    1.89 +
    1.90 +    const std::vector<std::string>& nodeMapNames = content.nodeSetMaps(0);
    1.91 +    const std::vector<std::string>& edgeMapNames = content.edgeSetMaps(0);
    1.92 +
    1.93 +    GraphReader<Graph> greader(filename, graph);
    1.94 +    for (std::vector<std::string>::const_iterator it = nodeMapNames.begin();
    1.95 +        it != nodeMapNames.end(); ++it)
    1.96 +    {
    1.97 +      if (*it == "coordinates_x")
    1.98 +      {
    1.99 +        read_x = true;
   1.100 +        //std::cout << "read X nodemap" << std::endl;
   1.101 +      }
   1.102 +      else if (*it == "coordinates_y")
   1.103 +      {
   1.104 +        read_y = true;
   1.105 +        //std::cout << "read Y nodemap" << std::endl;
   1.106 +      }
   1.107 +      else if (*it == "id")
   1.108 +      {
   1.109 +        //std::cout << "read id nodemap" << std::endl;
   1.110 +      }
   1.111 +      else
   1.112 +      {
   1.113 +        nodemap_storage[*it] = new Graph::NodeMap<double>(graph);
   1.114 +        //std::cout << "read " << *it << " nodemap" << std::endl;
   1.115 +      }
   1.116 +      greader.readNodeMap(*it, *nodemap_storage[*it]);
   1.117 +    }
   1.118 +    for (std::vector<std::string>::const_iterator it = edgeMapNames.begin();
   1.119 +        it != edgeMapNames.end(); ++it)
   1.120 +    {
   1.121 +      if (*it == "id")
   1.122 +      {
   1.123 +        //std::cout << "read id edgemap" << std::endl;
   1.124 +      }
   1.125 +      else
   1.126 +      {
   1.127 +        edgemap_storage[*it] = new Graph::EdgeMap<double>(graph);
   1.128 +        //std::cout << "read " << *it << " edgemap" << std::endl;
   1.129 +      }
   1.130 +      greader.readEdgeMap(*it, *edgemap_storage[*it]);
   1.131 +    }
   1.132 +    greader.run();
   1.133 +  } catch (DataFormatError& error) {
   1.134 +    /*
   1.135 +    Gtk::MessageDialog mdialog("Read Error");
   1.136 +    mdialog.set_message(error.what());
   1.137 +    mdialog.run();
   1.138 +    */
   1.139 +    // reset graph and mapstorage ?
   1.140 +    return;
   1.141 +  }
   1.142 +
   1.143 +  if (!read_x || !read_y)
   1.144 +  {
   1.145 +    int node_num = 0;
   1.146 +    for (NodeIt n(graph); n != INVALID; ++n)
   1.147 +    {
   1.148 +      node_num++;
   1.149 +    }
   1.150 +    const double pi = 3.142;
   1.151 +    double step = 2 * pi / (double) node_num;
   1.152 +    int i = 0;
   1.153 +    for (NodeIt n(graph); n != INVALID; ++n)
   1.154 +    {
   1.155 +      nodemap_storage["coordinates_x"]->set(n, 250.0 * cos(i * step));
   1.156 +      nodemap_storage["coordinates_y"]->set(n, 250.0 * sin(i * step));
   1.157 +      i++;
   1.158 +    }
   1.159 +  }
   1.160 +}
   1.161 +
   1.162 +void MapStorage::writeToFile(const std::string &filename)
   1.163 +{
   1.164 +  GraphWriter<Graph> gwriter(filename, graph);
   1.165 +
   1.166 +  for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
   1.167 +      nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
   1.168 +  {
   1.169 +    gwriter.writeNodeMap(it->first, *(it->second));
   1.170 +    //std::cout << "wrote " << it->first << " nodemap" << std::endl;
   1.171 +  }
   1.172 +  for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
   1.173 +      edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
   1.174 +  {
   1.175 +    gwriter.writeEdgeMap(it->first, *(it->second));
   1.176 +    //std::cout << "wrote " << it->first << " edgemap" << std::endl;
   1.177 +  }
   1.178 +  gwriter.run();
   1.179 +}
   1.180 +
   1.181 +void MapStorage::clear()
   1.182 +{
   1.183 +  for (std::map<std::string, Graph::NodeMap<double>*>::iterator it =
   1.184 +      nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
   1.185 +  {
   1.186 +    if ((it->first != "coordinates_x") &&
   1.187 +        (it->first != "coordinates_y") &&
   1.188 +        (it->first != "id"))
   1.189 +    {
   1.190 +      delete it->second;
   1.191 +      nodemap_storage.erase(it);
   1.192 +    }
   1.193 +  }
   1.194 +  for (std::map<std::string, Graph::EdgeMap<double>*>::iterator it =
   1.195 +      edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
   1.196 +  {
   1.197 +    if (it->first != "id")
   1.198 +    {
   1.199 +      delete it->second;
   1.200 +      edgemap_storage.erase(it);
   1.201 +    }
   1.202 +  }
   1.203 +  graph.clear();
   1.204 +  file_name = "";
   1.205 +  modified = false;
   1.206 +}