Removed this extra widget thing, because it is now developed in my private branch.
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"
21 #include "gui_writer.h"
22 #include "gui_reader.h"
28 const double a_d=0.05;
29 const double p_d=40000;
31 MapStorage::MapStorage(NoteBookTab& tab) : mytab(tab), modified(false), file_name(""), arrow_pos_read_ok(false), iterations(i_d), attraction(a_d), propulsation(p_d), background_set(false)
33 nodemap_storage["coordinates_x"] = new Graph::NodeMap<double>(graph);
34 coords.setXMap(*nodemap_storage["coordinates_x"]);
35 nodemap_storage["coordinates_y"] = new Graph::NodeMap<double>(graph);
36 coords.setYMap(*nodemap_storage["coordinates_y"]);
38 edgemap_storage["arrow_pos_x"] = new Graph::EdgeMap<double>(graph);
39 arrow_pos.setXMap(*edgemap_storage["arrow_pos_x"]);
40 edgemap_storage["arrow_pos_y"] = new Graph::EdgeMap<double>(graph);
41 arrow_pos.setYMap(*edgemap_storage["arrow_pos_y"]);
43 nodemap_storage["label"] = new Graph::NodeMap<double>(graph);
44 edgemap_storage["label"] = new Graph::EdgeMap<double>(graph);
46 nodemap_default["label"] = 1.0;
47 edgemap_default["label"] = 1.0;
49 active_nodemaps.resize(NODE_PROPERTY_NUM);
50 for(int i=0;i<NODE_PROPERTY_NUM;i++)
52 active_nodemaps[i]="";
55 active_edgemaps.resize(EDGE_PROPERTY_NUM);
56 for(int i=0;i<EDGE_PROPERTY_NUM;i++)
58 active_edgemaps[i]="";
62 MapStorage::~MapStorage()
64 for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
65 nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
69 for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
70 edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
76 int MapStorage::addNodeMap(const std::string & name, Graph::NodeMap<double> *nodemap, double default_value)
78 if( nodemap_storage.find(name) == nodemap_storage.end() )
80 nodemap_storage[name]=nodemap;
81 // set the maps default value
82 nodemap_default[name] = default_value;
84 //announce changement in maps
85 signal_node_map.emit(name);
91 void MapStorage::changeActiveMap(bool itisedge, int prop, std::string mapname)
95 active_edgemaps[prop]=mapname;
99 active_nodemaps[prop]=mapname;
101 signal_prop.emit(itisedge, prop);
104 void MapStorage::broadcastActiveMaps()
106 for(int i=0;i<NODE_PROPERTY_NUM;i++)
108 signal_map_win.emit(false, i, active_nodemaps[i]);
111 for(int i=0;i<EDGE_PROPERTY_NUM;i++)
113 signal_map_win.emit(true, i, active_edgemaps[i]);
118 std::string MapStorage::getActiveEdgeMap(int prop)
120 return active_edgemaps[prop];
123 std::string MapStorage::getActiveNodeMap(int prop)
125 return active_nodemaps[prop];
128 std::vector<std::string> MapStorage::getEdgeMapList()
130 std::vector<std::string> eml;
131 eml.resize(edgemap_storage.size());
133 std::map< std::string,Graph::EdgeMap<double> * >::iterator emsi=beginOfEdgeMaps();
134 for(;emsi!=endOfEdgeMaps();emsi++)
136 eml[i]=(emsi->first);
142 std::vector<std::string> MapStorage::getNodeMapList()
144 std::vector<std::string> nml;
145 nml.resize(nodemap_storage.size());
147 std::map< std::string,Graph::NodeMap<double> * >::iterator nmsi=beginOfNodeMaps();
148 for(;nmsi!=endOfNodeMaps();nmsi++)
150 nml[i]=(nmsi->first);
156 sigc::signal<void, bool, int> MapStorage::signal_prop_ch()
161 int MapStorage::addEdgeMap(const std::string & name, Graph::EdgeMap<double> *edgemap, double default_value)
163 if( edgemap_storage.find(name) == edgemap_storage.end() )
165 edgemap_storage[name]=edgemap;
166 // set the maps default value
167 edgemap_default[name] = default_value;
169 //announce changement in maps
170 signal_edge_map.emit(name);
176 double MapStorage::maxOfNodeMap(const std::string & name)
179 for (NodeIt j(graph); j!=INVALID; ++j)
181 if( (*nodemap_storage[name])[j]>max )
183 max=(*nodemap_storage[name])[j];
189 double MapStorage::maxOfEdgeMap(const std::string & name)
192 for (EdgeIt j(graph); j!=INVALID; ++j)
194 if( (*edgemap_storage[name])[j]>max )
196 max=(*edgemap_storage[name])[j];
202 double MapStorage::minOfNodeMap(const std::string & name)
208 min=(*nodemap_storage[name])[j];
214 for (; j!=INVALID; ++j)
216 if( (*nodemap_storage[name])[j]<min )
218 min=(*nodemap_storage[name])[j];
224 double MapStorage::minOfEdgeMap(const std::string & name)
230 min=(*edgemap_storage[name])[j];
236 for (EdgeIt j(graph); j!=INVALID; ++j)
238 if( (*edgemap_storage[name])[j]<min )
240 min=(*edgemap_storage[name])[j];
246 int MapStorage::readFromFile(const std::string &filename)
250 bool read_edge_id = false;
253 LemonReader lreader(filename);
254 ContentReader content(lreader);
257 if (content.nodeSetNum() < 1)
259 Gtk::MessageDialog mdialog("No nodeset found in file.");
265 if (content.edgeSetNum() < 1)
267 Gtk::MessageDialog mdialog("No edgeset found in file.");
273 const std::vector<std::string>& nodeMapNames = content.nodeSetMaps(0);
274 const std::vector<std::string>& edgeMapNames = content.edgeSetMaps(0);
276 GraphReader<Graph> greader(filename, graph);
277 for (std::vector<std::string>::const_iterator it = nodeMapNames.begin();
278 it != nodeMapNames.end(); ++it)
280 if (*it == "coordinates_x")
283 //std::cout << "read X nodemap" << std::endl;
285 else if (*it == "coordinates_y")
288 //std::cout << "read Y nodemap" << std::endl;
290 else if (*it == "label")
292 //std::cout << "read id nodemap" << std::endl;
296 nodemap_storage[*it] = new Graph::NodeMap<double>(graph);
297 //std::cout << "read " << *it << " nodemap" << std::endl;
299 greader.readNodeMap(*it, *nodemap_storage[*it]);
301 for (std::vector<std::string>::const_iterator it = edgeMapNames.begin();
302 it != edgeMapNames.end(); ++it)
306 //std::cout << "read id edgemap" << std::endl;
311 edgemap_storage[*it] = new Graph::EdgeMap<double>(graph);
312 //std::cout << "read " << *it << " edgemap" << std::endl;
314 greader.readEdgeMap(*it, *edgemap_storage[*it]);
316 GuiReader gui_reader(greader, this);
318 } catch (Exception& error) {
319 Gtk::MessageDialog mdialog(error.what());
327 edgemap_storage["label"] = new Graph::EdgeMap<double>(graph);
329 for (EdgeIt e(graph); e != INVALID; ++e)
331 (*edgemap_storage["label"])[e] = i++;
335 if (!read_x || !read_y)
338 for (NodeIt n(graph); n != INVALID; ++n)
342 const double pi = 3.142;
343 double step = 2 * pi / (double) node_num;
345 for (NodeIt n(graph); n != INVALID; ++n)
347 nodemap_storage["coordinates_x"]->set(n, 250.0 * std::cos(i * step));
348 nodemap_storage["coordinates_y"]->set(n, 250.0 * std::sin(i * step));
353 if (!arrow_pos_read_ok)
355 arrow_pos_read_ok = false;
356 for (EdgeIt e(graph); e != INVALID; ++e)
358 if (graph.source(e) == graph.target(e))
360 arrow_pos.set(e, coords[graph.source(e)] + XY(0.0, 80.0));
364 arrow_pos.set(e, (coords[graph.source(e)] + coords[graph.target(e)]) / 2.0);
369 // fill in the default values for the maps
370 for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
371 nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
373 if ((it->first != "label") &&
374 (it->first != "coordiantes_x") &&
375 (it->first != "coordinates_y"))
377 nodemap_default[it->first] = 0.0;
379 else if (it->first == "label")
382 double max = (*nodemap_storage["label"])[n];
383 for (; n != INVALID; ++n)
385 if ((*nodemap_storage["label"])[n] > max)
386 max = (*nodemap_storage["label"])[n];
388 nodemap_default["label"] = max + 1.0;
391 for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
392 edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
394 if (it->first != "label")
396 edgemap_default[it->first] = 0.0;
400 double max = std::numeric_limits<double>::min();
401 for (EdgeIt e(graph); e != INVALID; ++e)
403 if ((*edgemap_storage["label"])[e] > max)
404 max = (*edgemap_storage["label"])[e];
406 if (max > std::numeric_limits<double>::min())
407 edgemap_default["label"] = max + 1.0;
409 edgemap_default["label"] = 1.0;
416 void MapStorage::writeToFile(const std::string &filename)
418 GraphWriter<Graph> gwriter(filename, graph);
420 for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
421 nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
423 gwriter.writeNodeMap(it->first, *(it->second));
425 for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
426 edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
428 if ((it->first != "arrow_pos_x") &&
429 (it->first != "arrow_pos_y"))
431 gwriter.writeEdgeMap(it->first, *(it->second));
435 GuiWriter gui_writer(gwriter, this);
440 void MapStorage::clear()
442 for (std::map<std::string, Graph::NodeMap<double>*>::iterator it =
443 nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
445 if ((it->first != "coordinates_x") &&
446 (it->first != "coordinates_y") &&
447 (it->first != "label"))
450 nodemap_storage.erase(it);
453 for (std::map<std::string, Graph::EdgeMap<double>*>::iterator it =
454 edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
456 if ((it->first != "label") &&
457 (it->first != "arrow_pos_x") &&
458 (it->first != "arrow_pos_y"))
461 edgemap_storage.erase(it);
464 for (std::map<std::string, double>::iterator it =
465 nodemap_default.begin(); it != nodemap_default.end(); ++it)
467 if (it->first != "label")
468 nodemap_default.erase(it);
470 for (std::map<std::string, double>::iterator it =
471 edgemap_default.begin(); it != edgemap_default.end(); ++it)
473 if (it->first != "label")
474 edgemap_default.erase(it);
480 arrow_pos_read_ok = false;
482 for(int i=0;i<NODE_PROPERTY_NUM;i++)
484 changeActiveMap(false, i, "");
485 signal_map_win.emit(false, i, "");
488 for(int i=0;i<EDGE_PROPERTY_NUM;i++)
490 changeActiveMap(true, i, "");
491 signal_map_win.emit(true, i, "");
498 signal_design_win.emit(attraction, propulsation, iterations);
501 void MapStorage::ArrowPosReadOK()
503 arrow_pos_read_ok = true;
506 void MapStorage::mapChanged(bool itisedge, std::string mapname)
510 for(int i=0;i<EDGE_PROPERTY_NUM;i++)
512 if(active_edgemaps[i]==mapname)
514 signal_prop.emit(itisedge, i);
520 for(int i=0;i<NODE_PROPERTY_NUM;i++)
522 if(active_nodemaps[i]==mapname)
524 signal_prop.emit(itisedge, i);
530 void MapStorage::get_design_data(double & attraction_p, double & propulsation_p, int & iterations_p)
532 attraction_p=attraction;
533 propulsation_p=propulsation;
534 iterations_p=iterations;
537 void MapStorage::set_attraction(double attraction_p)
539 attraction=attraction_p;
542 void MapStorage::set_propulsation(double propulsation_p)
544 propulsation=propulsation_p;
547 void MapStorage::set_iteration(int iterations_p)
549 iterations=iterations_p;
552 void MapStorage::redesign_data_changed()
554 signal_design_win.emit(attraction, propulsation, iterations);
557 void MapStorage::setBackground(const std::string& file_name)
559 if (file_name == background_file_name) return;
562 background_file_name = "";
563 background_set = false;
567 background_file_name = file_name;
568 background_set = true;
570 mytab.gd_canvas->setBackground();
573 const std::string& MapStorage::getBackgroundFilename()
575 return background_file_name;
578 bool MapStorage::isBackgroundSet()
580 return background_set;
583 double MapStorage::getBackgroundScaling()
585 return background_scaling;
588 void MapStorage::setBackgroundScaling(double scaling)
590 background_scaling = scaling;