Added two new classes.
1 #include "mapstorage.h"
2 #include "gui_writer.h"
3 #include "gui_reader.h"
8 MapStorage::MapStorage() : modified(false), file_name(""), arrow_pos_read_ok(false)
10 nodemap_storage["coordinates_x"] = new Graph::NodeMap<double>(graph);
11 coords.setXMap(*nodemap_storage["coordinates_x"]);
12 nodemap_storage["coordinates_y"] = new Graph::NodeMap<double>(graph);
13 coords.setYMap(*nodemap_storage["coordinates_y"]);
15 edgemap_storage["arrow_pos_x"] = new Graph::EdgeMap<double>(graph);
16 arrow_pos.setXMap(*edgemap_storage["arrow_pos_x"]);
17 edgemap_storage["arrow_pos_y"] = new Graph::EdgeMap<double>(graph);
18 arrow_pos.setYMap(*edgemap_storage["arrow_pos_y"]);
20 nodemap_storage["label"] = new Graph::NodeMap<double>(graph);
21 edgemap_storage["label"] = new Graph::EdgeMap<double>(graph);
23 nodemap_default["label"] = 1.0;
24 edgemap_default["label"] = 1.0;
26 active_nodemaps.resize(NODE_PROPERTY_NUM);
27 for(int i=0;i<NODE_PROPERTY_NUM;i++)
29 active_nodemaps[i]="";
32 active_edgemaps.resize(EDGE_PROPERTY_NUM);
33 for(int i=0;i<EDGE_PROPERTY_NUM;i++)
35 active_edgemaps[i]="";
39 MapStorage::~MapStorage()
41 for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
42 nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
46 for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
47 edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
53 int MapStorage::addNodeMap(const std::string & name, Graph::NodeMap<double> *nodemap, double default_value)
55 if( nodemap_storage.find(name) == nodemap_storage.end() )
57 nodemap_storage[name]=nodemap;
58 // set the maps default value
59 nodemap_default[name] = default_value;
61 //announce changement in maps
62 signal_node_map.emit(name);
68 void MapStorage::changeActiveMap(bool itisedge, int prop, std::string mapname)
72 active_edgemaps[prop]=mapname;
76 active_nodemaps[prop]=mapname;
78 signal_prop.emit(itisedge, prop);
81 void MapStorage::broadcastActiveMaps()
83 for(int i=0;i<NODE_PROPERTY_NUM;i++)
85 signal_map_win.emit(false, i, active_nodemaps[i]);
88 for(int i=0;i<EDGE_PROPERTY_NUM;i++)
90 signal_map_win.emit(true, i, active_edgemaps[i]);
95 std::string MapStorage::getActiveEdgeMap(int prop)
97 return active_edgemaps[prop];
100 std::string MapStorage::getActiveNodeMap(int prop)
102 return active_nodemaps[prop];
105 std::vector<std::string> MapStorage::getEdgeMapList()
107 std::vector<std::string> eml;
108 eml.resize(edgemap_storage.size());
110 std::map< std::string,Graph::EdgeMap<double> * >::iterator emsi=beginOfEdgeMaps();
111 for(;emsi!=endOfEdgeMaps();emsi++)
113 eml[i]=(emsi->first);
119 std::vector<std::string> MapStorage::getNodeMapList()
121 std::vector<std::string> nml;
122 nml.resize(nodemap_storage.size());
124 std::map< std::string,Graph::NodeMap<double> * >::iterator nmsi=beginOfNodeMaps();
125 for(;nmsi!=endOfNodeMaps();nmsi++)
127 nml[i]=(nmsi->first);
133 MapStorage::Signal_Prop MapStorage::signal_prop_ch()
138 int MapStorage::addEdgeMap(const std::string & name, Graph::EdgeMap<double> *edgemap, double default_value)
140 if( edgemap_storage.find(name) == edgemap_storage.end() )
142 edgemap_storage[name]=edgemap;
143 // set the maps default value
144 edgemap_default[name] = default_value;
146 //announce changement in maps
147 signal_edge_map.emit(name);
153 double MapStorage::maxOfNodeMap(const std::string & name)
156 for (NodeIt j(graph); j!=INVALID; ++j)
158 if( (*nodemap_storage[name])[j]>max )
160 max=(*nodemap_storage[name])[j];
166 double MapStorage::maxOfEdgeMap(const std::string & name)
169 for (EdgeIt j(graph); j!=INVALID; ++j)
171 if( (*edgemap_storage[name])[j]>max )
173 max=(*edgemap_storage[name])[j];
179 double MapStorage::minOfNodeMap(const std::string & name)
185 min=(*nodemap_storage[name])[j];
191 for (; j!=INVALID; ++j)
193 if( (*nodemap_storage[name])[j]<min )
195 min=(*nodemap_storage[name])[j];
201 double MapStorage::minOfEdgeMap(const std::string & name)
207 min=(*edgemap_storage[name])[j];
213 for (EdgeIt j(graph); j!=INVALID; ++j)
215 if( (*edgemap_storage[name])[j]<min )
217 min=(*edgemap_storage[name])[j];
223 int MapStorage::readFromFile(const std::string &filename)
227 bool read_edge_id = false;
230 LemonReader lreader(filename);
231 ContentReader content(lreader);
234 if (content.nodeSetNum() < 1)
236 Gtk::MessageDialog mdialog("No nodeset found in file.");
242 if (content.edgeSetNum() < 1)
244 Gtk::MessageDialog mdialog("No edgeset found in file.");
250 const std::vector<std::string>& nodeMapNames = content.nodeSetMaps(0);
251 const std::vector<std::string>& edgeMapNames = content.edgeSetMaps(0);
253 GraphReader<Graph> greader(filename, graph);
254 for (std::vector<std::string>::const_iterator it = nodeMapNames.begin();
255 it != nodeMapNames.end(); ++it)
257 if (*it == "coordinates_x")
260 //std::cout << "read X nodemap" << std::endl;
262 else if (*it == "coordinates_y")
265 //std::cout << "read Y nodemap" << std::endl;
267 else if (*it == "label")
269 //std::cout << "read id nodemap" << std::endl;
273 nodemap_storage[*it] = new Graph::NodeMap<double>(graph);
274 //std::cout << "read " << *it << " nodemap" << std::endl;
276 greader.readNodeMap(*it, *nodemap_storage[*it]);
278 for (std::vector<std::string>::const_iterator it = edgeMapNames.begin();
279 it != edgeMapNames.end(); ++it)
283 //std::cout << "read id edgemap" << std::endl;
288 edgemap_storage[*it] = new Graph::EdgeMap<double>(graph);
289 //std::cout << "read " << *it << " edgemap" << std::endl;
291 greader.readEdgeMap(*it, *edgemap_storage[*it]);
293 GuiReader gui_reader(greader, this);
295 } catch (Exception& error) {
296 Gtk::MessageDialog mdialog(error.what());
304 edgemap_storage["label"] = new Graph::EdgeMap<double>(graph);
306 for (EdgeIt e(graph); e != INVALID; ++e)
308 (*edgemap_storage["label"])[e] = i++;
312 if (!read_x || !read_y)
315 for (NodeIt n(graph); n != INVALID; ++n)
319 const double pi = 3.142;
320 double step = 2 * pi / (double) node_num;
322 for (NodeIt n(graph); n != INVALID; ++n)
324 nodemap_storage["coordinates_x"]->set(n, 250.0 * std::cos(i * step));
325 nodemap_storage["coordinates_y"]->set(n, 250.0 * std::sin(i * step));
330 if (!arrow_pos_read_ok)
332 arrow_pos_read_ok = false;
333 for (EdgeIt e(graph); e != INVALID; ++e)
335 if (graph.source(e) == graph.target(e))
337 arrow_pos.set(e, coords[graph.source(e)] + XY(0.0, 80.0));
341 arrow_pos.set(e, (coords[graph.source(e)] + coords[graph.target(e)]) / 2.0);
346 // fill in the default values for the maps
347 for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
348 nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
350 if ((it->first != "label") &&
351 (it->first != "coordiantes_x") &&
352 (it->first != "coordinates_y"))
354 nodemap_default[it->first] = 0.0;
356 else if (it->first == "label")
359 double max = (*nodemap_storage["label"])[n];
360 for (; n != INVALID; ++n)
362 if ((*nodemap_storage["label"])[n] > max)
363 max = (*nodemap_storage["label"])[n];
365 nodemap_default["label"] = max + 1.0;
368 for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
369 edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
371 if (it->first != "label")
373 edgemap_default[it->first] = 0.0;
377 double max = std::numeric_limits<double>::min();
378 for (EdgeIt e(graph); e != INVALID; ++e)
380 if ((*edgemap_storage["label"])[e] > max)
381 max = (*edgemap_storage["label"])[e];
383 if (max > std::numeric_limits<double>::min())
384 edgemap_default["label"] = max + 1.0;
386 edgemap_default["label"] = 1.0;
393 void MapStorage::writeToFile(const std::string &filename)
395 GraphWriter<Graph> gwriter(filename, graph);
397 for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
398 nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
400 gwriter.writeNodeMap(it->first, *(it->second));
402 for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
403 edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
405 if ((it->first != "arrow_pos_x") &&
406 (it->first != "arrow_pos_y"))
408 gwriter.writeEdgeMap(it->first, *(it->second));
412 GuiWriter gui_writer(gwriter, this);
417 void MapStorage::clear()
419 for (std::map<std::string, Graph::NodeMap<double>*>::iterator it =
420 nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
422 if ((it->first != "coordinates_x") &&
423 (it->first != "coordinates_y") &&
424 (it->first != "label"))
427 nodemap_storage.erase(it);
430 for (std::map<std::string, Graph::EdgeMap<double>*>::iterator it =
431 edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
433 if ((it->first != "label") &&
434 (it->first != "arrow_pos_x") &&
435 (it->first != "arrow_pos_y"))
438 edgemap_storage.erase(it);
441 for (std::map<std::string, double>::iterator it =
442 nodemap_default.begin(); it != nodemap_default.end(); ++it)
444 if (it->first != "label")
445 nodemap_default.erase(it);
447 for (std::map<std::string, double>::iterator it =
448 edgemap_default.begin(); it != edgemap_default.end(); ++it)
450 if (it->first != "label")
451 edgemap_default.erase(it);
457 arrow_pos_read_ok = false;
459 for(int i=0;i<NODE_PROPERTY_NUM;i++)
461 changeActiveMap(false, i, "");
462 signal_map_win.emit(false, i, "");
465 for(int i=0;i<EDGE_PROPERTY_NUM;i++)
467 changeActiveMap(true, i, "");
468 signal_map_win.emit(true, i, "");
472 void MapStorage::ArrowPosReadOK()
474 arrow_pos_read_ok = true;
477 void MapStorage::mapChanged(bool itisedge, std::string mapname)
481 for(int i=0;i<EDGE_PROPERTY_NUM;i++)
483 if(active_edgemaps[i]==mapname)
485 signal_prop.emit(itisedge, i);
491 for(int i=0;i<NODE_PROPERTY_NUM;i++)
493 if(active_nodemaps[i]==mapname)
495 signal_prop.emit(itisedge, i);