Showed map is loaded even if ArrowPos not.
3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2006
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
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.
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
19 #include "mapstorage.h"
20 #include "gui_writer.h"
21 #include "gui_reader.h"
26 MapStorage::MapStorage() : modified(false), file_name(""), arrow_pos_read_ok(false)
28 nodemap_storage["coordinates_x"] = new Graph::NodeMap<double>(graph);
29 coords.setXMap(*nodemap_storage["coordinates_x"]);
30 nodemap_storage["coordinates_y"] = new Graph::NodeMap<double>(graph);
31 coords.setYMap(*nodemap_storage["coordinates_y"]);
33 edgemap_storage["arrow_pos_x"] = new Graph::EdgeMap<double>(graph);
34 arrow_pos.setXMap(*edgemap_storage["arrow_pos_x"]);
35 edgemap_storage["arrow_pos_y"] = new Graph::EdgeMap<double>(graph);
36 arrow_pos.setYMap(*edgemap_storage["arrow_pos_y"]);
38 nodemap_storage["label"] = new Graph::NodeMap<double>(graph);
39 edgemap_storage["label"] = new Graph::EdgeMap<double>(graph);
41 nodemap_default["label"] = 1.0;
42 edgemap_default["label"] = 1.0;
44 active_nodemaps.resize(NODE_PROPERTY_NUM);
45 for(int i=0;i<NODE_PROPERTY_NUM;i++)
47 active_nodemaps[i]="";
50 active_edgemaps.resize(EDGE_PROPERTY_NUM);
51 for(int i=0;i<EDGE_PROPERTY_NUM;i++)
53 active_edgemaps[i]="";
57 MapStorage::~MapStorage()
59 for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
60 nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
64 for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
65 edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
71 int MapStorage::addNodeMap(const std::string & name, Graph::NodeMap<double> *nodemap, double default_value)
73 if( nodemap_storage.find(name) == nodemap_storage.end() )
75 nodemap_storage[name]=nodemap;
76 // set the maps default value
77 nodemap_default[name] = default_value;
79 //announce changement in maps
80 signal_node_map.emit(name);
86 void MapStorage::changeActiveMap(bool itisedge, int prop, std::string mapname)
90 active_edgemaps[prop]=mapname;
94 active_nodemaps[prop]=mapname;
96 signal_prop.emit(itisedge, prop);
99 void MapStorage::broadcastActiveMaps()
101 for(int i=0;i<NODE_PROPERTY_NUM;i++)
103 signal_map_win.emit(false, i, active_nodemaps[i]);
106 for(int i=0;i<EDGE_PROPERTY_NUM;i++)
108 signal_map_win.emit(true, i, active_edgemaps[i]);
113 std::string MapStorage::getActiveEdgeMap(int prop)
115 return active_edgemaps[prop];
118 std::string MapStorage::getActiveNodeMap(int prop)
120 return active_nodemaps[prop];
123 std::vector<std::string> MapStorage::getEdgeMapList()
125 std::vector<std::string> eml;
126 eml.resize(edgemap_storage.size());
128 std::map< std::string,Graph::EdgeMap<double> * >::iterator emsi=beginOfEdgeMaps();
129 for(;emsi!=endOfEdgeMaps();emsi++)
131 eml[i]=(emsi->first);
137 std::vector<std::string> MapStorage::getNodeMapList()
139 std::vector<std::string> nml;
140 nml.resize(nodemap_storage.size());
142 std::map< std::string,Graph::NodeMap<double> * >::iterator nmsi=beginOfNodeMaps();
143 for(;nmsi!=endOfNodeMaps();nmsi++)
145 nml[i]=(nmsi->first);
151 MapStorage::Signal_Prop MapStorage::signal_prop_ch()
156 int MapStorage::addEdgeMap(const std::string & name, Graph::EdgeMap<double> *edgemap, double default_value)
158 if( edgemap_storage.find(name) == edgemap_storage.end() )
160 edgemap_storage[name]=edgemap;
161 // set the maps default value
162 edgemap_default[name] = default_value;
164 //announce changement in maps
165 signal_edge_map.emit(name);
171 double MapStorage::maxOfNodeMap(const std::string & name)
174 for (NodeIt j(graph); j!=INVALID; ++j)
176 if( (*nodemap_storage[name])[j]>max )
178 max=(*nodemap_storage[name])[j];
184 double MapStorage::maxOfEdgeMap(const std::string & name)
187 for (EdgeIt j(graph); j!=INVALID; ++j)
189 if( (*edgemap_storage[name])[j]>max )
191 max=(*edgemap_storage[name])[j];
197 double MapStorage::minOfNodeMap(const std::string & name)
203 min=(*nodemap_storage[name])[j];
209 for (; j!=INVALID; ++j)
211 if( (*nodemap_storage[name])[j]<min )
213 min=(*nodemap_storage[name])[j];
219 double MapStorage::minOfEdgeMap(const std::string & name)
225 min=(*edgemap_storage[name])[j];
231 for (EdgeIt j(graph); j!=INVALID; ++j)
233 if( (*edgemap_storage[name])[j]<min )
235 min=(*edgemap_storage[name])[j];
241 int MapStorage::readFromFile(const std::string &filename)
245 bool read_edge_id = false;
248 LemonReader lreader(filename);
249 ContentReader content(lreader);
252 if (content.nodeSetNum() < 1)
254 Gtk::MessageDialog mdialog("No nodeset found in file.");
260 if (content.edgeSetNum() < 1)
262 Gtk::MessageDialog mdialog("No edgeset found in file.");
268 const std::vector<std::string>& nodeMapNames = content.nodeSetMaps(0);
269 const std::vector<std::string>& edgeMapNames = content.edgeSetMaps(0);
271 GraphReader<Graph> greader(filename, graph);
272 for (std::vector<std::string>::const_iterator it = nodeMapNames.begin();
273 it != nodeMapNames.end(); ++it)
275 if (*it == "coordinates_x")
278 //std::cout << "read X nodemap" << std::endl;
280 else if (*it == "coordinates_y")
283 //std::cout << "read Y nodemap" << std::endl;
285 else if (*it == "label")
287 //std::cout << "read id nodemap" << std::endl;
291 nodemap_storage[*it] = new Graph::NodeMap<double>(graph);
292 //std::cout << "read " << *it << " nodemap" << std::endl;
294 greader.readNodeMap(*it, *nodemap_storage[*it]);
296 for (std::vector<std::string>::const_iterator it = edgeMapNames.begin();
297 it != edgeMapNames.end(); ++it)
301 //std::cout << "read id edgemap" << std::endl;
306 edgemap_storage[*it] = new Graph::EdgeMap<double>(graph);
307 //std::cout << "read " << *it << " edgemap" << std::endl;
309 greader.readEdgeMap(*it, *edgemap_storage[*it]);
311 GuiReader gui_reader(greader, this);
313 } catch (Exception& error) {
314 Gtk::MessageDialog mdialog(error.what());
322 edgemap_storage["label"] = new Graph::EdgeMap<double>(graph);
324 for (EdgeIt e(graph); e != INVALID; ++e)
326 (*edgemap_storage["label"])[e] = i++;
330 if (!read_x || !read_y)
333 for (NodeIt n(graph); n != INVALID; ++n)
337 const double pi = 3.142;
338 double step = 2 * pi / (double) node_num;
340 for (NodeIt n(graph); n != INVALID; ++n)
342 nodemap_storage["coordinates_x"]->set(n, 250.0 * std::cos(i * step));
343 nodemap_storage["coordinates_y"]->set(n, 250.0 * std::sin(i * step));
348 if (!arrow_pos_read_ok)
350 arrow_pos_read_ok = false;
351 for (EdgeIt e(graph); e != INVALID; ++e)
353 if (graph.source(e) == graph.target(e))
355 arrow_pos.set(e, coords[graph.source(e)] + XY(0.0, 80.0));
359 arrow_pos.set(e, (coords[graph.source(e)] + coords[graph.target(e)]) / 2.0);
364 // fill in the default values for the maps
365 for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
366 nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
368 if ((it->first != "label") &&
369 (it->first != "coordiantes_x") &&
370 (it->first != "coordinates_y"))
372 nodemap_default[it->first] = 0.0;
374 else if (it->first == "label")
377 double max = (*nodemap_storage["label"])[n];
378 for (; n != INVALID; ++n)
380 if ((*nodemap_storage["label"])[n] > max)
381 max = (*nodemap_storage["label"])[n];
383 nodemap_default["label"] = max + 1.0;
386 for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
387 edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
389 if (it->first != "label")
391 edgemap_default[it->first] = 0.0;
395 double max = std::numeric_limits<double>::min();
396 for (EdgeIt e(graph); e != INVALID; ++e)
398 if ((*edgemap_storage["label"])[e] > max)
399 max = (*edgemap_storage["label"])[e];
401 if (max > std::numeric_limits<double>::min())
402 edgemap_default["label"] = max + 1.0;
404 edgemap_default["label"] = 1.0;
411 void MapStorage::writeToFile(const std::string &filename)
413 GraphWriter<Graph> gwriter(filename, graph);
415 for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
416 nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
418 gwriter.writeNodeMap(it->first, *(it->second));
420 for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
421 edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
423 if ((it->first != "arrow_pos_x") &&
424 (it->first != "arrow_pos_y"))
426 gwriter.writeEdgeMap(it->first, *(it->second));
430 GuiWriter gui_writer(gwriter, this);
435 void MapStorage::clear()
437 for (std::map<std::string, Graph::NodeMap<double>*>::iterator it =
438 nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
440 if ((it->first != "coordinates_x") &&
441 (it->first != "coordinates_y") &&
442 (it->first != "label"))
445 nodemap_storage.erase(it);
448 for (std::map<std::string, Graph::EdgeMap<double>*>::iterator it =
449 edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
451 if ((it->first != "label") &&
452 (it->first != "arrow_pos_x") &&
453 (it->first != "arrow_pos_y"))
456 edgemap_storage.erase(it);
459 for (std::map<std::string, double>::iterator it =
460 nodemap_default.begin(); it != nodemap_default.end(); ++it)
462 if (it->first != "label")
463 nodemap_default.erase(it);
465 for (std::map<std::string, double>::iterator it =
466 edgemap_default.begin(); it != edgemap_default.end(); ++it)
468 if (it->first != "label")
469 edgemap_default.erase(it);
475 arrow_pos_read_ok = false;
477 for(int i=0;i<NODE_PROPERTY_NUM;i++)
479 changeActiveMap(false, i, "");
480 signal_map_win.emit(false, i, "");
483 for(int i=0;i<EDGE_PROPERTY_NUM;i++)
485 changeActiveMap(true, i, "");
486 signal_map_win.emit(true, i, "");
490 void MapStorage::ArrowPosReadOK()
492 arrow_pos_read_ok = true;
495 void MapStorage::mapChanged(bool itisedge, std::string mapname)
499 for(int i=0;i<EDGE_PROPERTY_NUM;i++)
501 if(active_edgemaps[i]==mapname)
503 signal_prop.emit(itisedge, i);
509 for(int i=0;i<NODE_PROPERTY_NUM;i++)
511 if(active_nodemaps[i]==mapname)
513 signal_prop.emit(itisedge, i);