Node antigravity and edge elasticity based graph layout redesigner.
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 std::cout << default_value << std::endl;
56 if( nodemap_storage.find(name) == nodemap_storage.end() )
58 nodemap_storage[name]=nodemap;
59 // set the maps default value
60 nodemap_default[name] = default_value;
62 //announce changement in maps
63 signal_node_map.emit(name);
69 void MapStorage::changeActiveMap(bool itisedge, int prop, std::string mapname)
73 active_edgemaps[prop]=mapname;
77 active_nodemaps[prop]=mapname;
79 signal_prop.emit(itisedge, prop);
82 std::string MapStorage::getActiveEdgeMap(int prop)
84 return active_edgemaps[prop];
87 std::string MapStorage::getActiveNodeMap(int prop)
89 return active_nodemaps[prop];
92 std::vector<std::string> MapStorage::getEdgeMapList()
94 std::vector<std::string> eml;
95 eml.resize(edgemap_storage.size());
97 std::map< std::string,Graph::EdgeMap<double> * >::iterator emsi=beginOfEdgeMaps();
98 for(;emsi!=endOfEdgeMaps();emsi++)
100 eml[i]=(emsi->first);
106 std::vector<std::string> MapStorage::getNodeMapList()
108 std::vector<std::string> nml;
109 nml.resize(nodemap_storage.size());
111 std::map< std::string,Graph::NodeMap<double> * >::iterator nmsi=beginOfNodeMaps();
112 for(;nmsi!=endOfNodeMaps();nmsi++)
114 nml[i]=(nmsi->first);
120 MapStorage::Signal_Prop MapStorage::signal_prop_ch()
125 int MapStorage::addEdgeMap(const std::string & name, Graph::EdgeMap<double> *edgemap, double default_value)
127 if( edgemap_storage.find(name) == edgemap_storage.end() )
129 edgemap_storage[name]=edgemap;
130 // set the maps default value
131 edgemap_default[name] = default_value;
133 //announce changement in maps
134 signal_edge_map.emit(name);
140 double MapStorage::maxOfNodeMap(const std::string & name)
143 for (NodeIt j(graph); j!=INVALID; ++j)
145 if( (*nodemap_storage[name])[j]>max )
147 max=(*nodemap_storage[name])[j];
153 double MapStorage::maxOfEdgeMap(const std::string & name)
156 for (EdgeIt j(graph); j!=INVALID; ++j)
158 if( (*edgemap_storage[name])[j]>max )
160 max=(*edgemap_storage[name])[j];
166 double MapStorage::minOfNodeMap(const std::string & name)
172 min=(*nodemap_storage[name])[j];
178 for (; j!=INVALID; ++j)
180 if( (*nodemap_storage[name])[j]<min )
182 min=(*nodemap_storage[name])[j];
188 double MapStorage::minOfEdgeMap(const std::string & name)
194 min=(*edgemap_storage[name])[j];
200 for (EdgeIt j(graph); j!=INVALID; ++j)
202 if( (*edgemap_storage[name])[j]<min )
204 min=(*edgemap_storage[name])[j];
210 int MapStorage::readFromFile(const std::string &filename)
214 bool read_edge_id = false;
217 LemonReader lreader(filename);
218 ContentReader content(lreader);
221 if (content.nodeSetNum() < 1)
223 Gtk::MessageDialog mdialog("No nodeset found in file.");
229 if (content.edgeSetNum() < 1)
231 Gtk::MessageDialog mdialog("No edgeset found in file.");
237 const std::vector<std::string>& nodeMapNames = content.nodeSetMaps(0);
238 const std::vector<std::string>& edgeMapNames = content.edgeSetMaps(0);
240 GraphReader<Graph> greader(filename, graph);
241 for (std::vector<std::string>::const_iterator it = nodeMapNames.begin();
242 it != nodeMapNames.end(); ++it)
244 if (*it == "coordinates_x")
247 //std::cout << "read X nodemap" << std::endl;
249 else if (*it == "coordinates_y")
252 //std::cout << "read Y nodemap" << std::endl;
254 else if (*it == "label")
256 //std::cout << "read id nodemap" << std::endl;
260 nodemap_storage[*it] = new Graph::NodeMap<double>(graph);
261 //std::cout << "read " << *it << " nodemap" << std::endl;
263 greader.readNodeMap(*it, *nodemap_storage[*it]);
265 for (std::vector<std::string>::const_iterator it = edgeMapNames.begin();
266 it != edgeMapNames.end(); ++it)
270 //std::cout << "read id edgemap" << std::endl;
275 edgemap_storage[*it] = new Graph::EdgeMap<double>(graph);
276 //std::cout << "read " << *it << " edgemap" << std::endl;
278 greader.readEdgeMap(*it, *edgemap_storage[*it]);
280 GuiReader gui_reader(greader, this);
282 } catch (Exception& error) {
283 Gtk::MessageDialog mdialog(error.what());
291 edgemap_storage["label"] = new Graph::EdgeMap<double>(graph);
293 for (EdgeIt e(graph); e != INVALID; ++e)
295 (*edgemap_storage["label"])[e] = i++;
299 if (!read_x || !read_y)
302 for (NodeIt n(graph); n != INVALID; ++n)
306 const double pi = 3.142;
307 double step = 2 * pi / (double) node_num;
309 for (NodeIt n(graph); n != INVALID; ++n)
311 nodemap_storage["coordinates_x"]->set(n, 250.0 * std::cos(i * step));
312 nodemap_storage["coordinates_y"]->set(n, 250.0 * std::sin(i * step));
317 if (!arrow_pos_read_ok)
319 arrow_pos_read_ok = false;
320 for (EdgeIt e(graph); e != INVALID; ++e)
322 if (graph.source(e) == graph.target(e))
324 arrow_pos.set(e, coords[graph.source(e)] + XY(0.0, 80.0));
328 arrow_pos.set(e, (coords[graph.source(e)] + coords[graph.target(e)]) / 2.0);
333 // fill in the default values for the maps
334 for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
335 nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
337 if ((it->first != "label") &&
338 (it->first != "coordiantes_x") &&
339 (it->first != "coordinates_y"))
341 nodemap_default[it->first] = 0.0;
343 else if (it->first == "label")
346 double max = (*nodemap_storage["label"])[n];
347 for (; n != INVALID; ++n)
349 if ((*nodemap_storage["label"])[n] > max)
350 max = (*nodemap_storage["label"])[n];
352 nodemap_default["label"] = max + 1.0;
355 for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
356 edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
358 if (it->first != "label")
360 edgemap_default[it->first] = 0.0;
364 double max = std::numeric_limits<double>::min();
365 for (EdgeIt e(graph); e != INVALID; ++e)
367 if ((*edgemap_storage["label"])[e] > max)
368 max = (*edgemap_storage["label"])[e];
370 if (max > std::numeric_limits<double>::min())
371 edgemap_default["label"] = max + 1.0;
373 edgemap_default["label"] = 1.0;
380 void MapStorage::writeToFile(const std::string &filename)
382 GraphWriter<Graph> gwriter(filename, graph);
384 for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
385 nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
387 gwriter.writeNodeMap(it->first, *(it->second));
389 for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
390 edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
392 if ((it->first != "arrow_pos_x") &&
393 (it->first != "arrow_pos_y"))
395 gwriter.writeEdgeMap(it->first, *(it->second));
399 GuiWriter gui_writer(gwriter, this);
404 void MapStorage::clear()
406 for (std::map<std::string, Graph::NodeMap<double>*>::iterator it =
407 nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
409 if ((it->first != "coordinates_x") &&
410 (it->first != "coordinates_y") &&
411 (it->first != "label"))
414 nodemap_storage.erase(it);
417 for (std::map<std::string, Graph::EdgeMap<double>*>::iterator it =
418 edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
420 if ((it->first != "label") &&
421 (it->first != "arrow_pos_x") &&
422 (it->first != "arrow_pos_y"))
425 edgemap_storage.erase(it);
428 for (std::map<std::string, double>::iterator it =
429 nodemap_default.begin(); it != nodemap_default.end(); ++it)
431 if (it->first != "label")
432 nodemap_default.erase(it);
434 for (std::map<std::string, double>::iterator it =
435 edgemap_default.begin(); it != edgemap_default.end(); ++it)
437 if (it->first != "label")
438 edgemap_default.erase(it);
445 void MapStorage::ArrowPosReadOK()
447 arrow_pos_read_ok = true;
450 void MapStorage::mapChanged(bool itisedge, std::string mapname)
454 for(int i=0;i<EDGE_PROPERTY_NUM;i++)
456 if(active_edgemaps[i]==mapname)
458 signal_prop.emit(itisedge, i);
464 for(int i=0;i<NODE_PROPERTY_NUM;i++)
466 if(active_nodemaps[i]==mapname)
468 signal_prop.emit(itisedge, i);