GUI can now export graph to EPS.
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"
26 #include<lemon/graph_to_eps.h>
29 const double a_d=0.05;
30 const double p_d=40000;
32 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)
34 nodemap_storage["coordinates_x"] = new Graph::NodeMap<double>(graph);
35 coords.setXMap(*nodemap_storage["coordinates_x"]);
36 nodemap_storage["coordinates_y"] = new Graph::NodeMap<double>(graph);
37 coords.setYMap(*nodemap_storage["coordinates_y"]);
39 edgemap_storage["arrow_pos_x"] = new Graph::EdgeMap<double>(graph);
40 arrow_pos.setXMap(*edgemap_storage["arrow_pos_x"]);
41 edgemap_storage["arrow_pos_y"] = new Graph::EdgeMap<double>(graph);
42 arrow_pos.setYMap(*edgemap_storage["arrow_pos_y"]);
44 nodemap_storage["label"] = new Graph::NodeMap<double>(graph);
45 edgemap_storage["label"] = new Graph::EdgeMap<double>(graph);
47 nodemap_default["label"] = 1.0;
48 edgemap_default["label"] = 1.0;
50 active_nodemaps.resize(NODE_PROPERTY_NUM);
51 for(int i=0;i<NODE_PROPERTY_NUM;i++)
53 active_nodemaps[i]="";
56 active_edgemaps.resize(EDGE_PROPERTY_NUM);
57 for(int i=0;i<EDGE_PROPERTY_NUM;i++)
59 active_edgemaps[i]="";
63 MapStorage::~MapStorage()
65 for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
66 nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
70 for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
71 edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
77 int MapStorage::addNodeMap(const std::string & name, Graph::NodeMap<double> *nodemap, double default_value)
79 if( nodemap_storage.find(name) == nodemap_storage.end() )
81 nodemap_storage[name]=nodemap;
82 // set the maps default value
83 nodemap_default[name] = default_value;
85 //announce changement in maps
86 signal_node_map.emit(name);
92 void MapStorage::changeActiveMap(bool itisedge, int prop, std::string mapname)
96 active_edgemaps[prop]=mapname;
100 active_nodemaps[prop]=mapname;
102 signal_prop.emit(itisedge, prop);
105 void MapStorage::broadcastActiveMaps()
107 for(int i=0;i<NODE_PROPERTY_NUM;i++)
109 signal_map_win.emit(false, i, active_nodemaps[i]);
112 for(int i=0;i<EDGE_PROPERTY_NUM;i++)
114 signal_map_win.emit(true, i, active_edgemaps[i]);
119 std::string MapStorage::getActiveEdgeMap(int prop)
121 return active_edgemaps[prop];
124 std::string MapStorage::getActiveNodeMap(int prop)
126 return active_nodemaps[prop];
129 std::vector<std::string> MapStorage::getEdgeMapList()
131 std::vector<std::string> eml;
132 eml.resize(edgemap_storage.size());
134 std::map< std::string,Graph::EdgeMap<double> * >::iterator emsi=beginOfEdgeMaps();
135 for(;emsi!=endOfEdgeMaps();emsi++)
137 eml[i]=(emsi->first);
143 std::vector<std::string> MapStorage::getNodeMapList()
145 std::vector<std::string> nml;
146 nml.resize(nodemap_storage.size());
148 std::map< std::string,Graph::NodeMap<double> * >::iterator nmsi=beginOfNodeMaps();
149 for(;nmsi!=endOfNodeMaps();nmsi++)
151 nml[i]=(nmsi->first);
157 sigc::signal<void, bool, int> MapStorage::signal_prop_ch()
162 int MapStorage::addEdgeMap(const std::string & name, Graph::EdgeMap<double> *edgemap, double default_value)
164 if( edgemap_storage.find(name) == edgemap_storage.end() )
166 edgemap_storage[name]=edgemap;
167 // set the maps default value
168 edgemap_default[name] = default_value;
170 //announce changement in maps
171 signal_edge_map.emit(name);
177 double MapStorage::maxOfNodeMap(const std::string & name)
180 for (NodeIt j(graph); j!=INVALID; ++j)
182 if( (*nodemap_storage[name])[j]>max )
184 max=(*nodemap_storage[name])[j];
190 double MapStorage::maxOfEdgeMap(const std::string & name)
193 for (EdgeIt j(graph); j!=INVALID; ++j)
195 if( (*edgemap_storage[name])[j]>max )
197 max=(*edgemap_storage[name])[j];
203 double MapStorage::minOfNodeMap(const std::string & name)
209 min=(*nodemap_storage[name])[j];
215 for (; j!=INVALID; ++j)
217 if( (*nodemap_storage[name])[j]<min )
219 min=(*nodemap_storage[name])[j];
225 double MapStorage::minOfEdgeMap(const std::string & name)
231 min=(*edgemap_storage[name])[j];
237 for (EdgeIt j(graph); j!=INVALID; ++j)
239 if( (*edgemap_storage[name])[j]<min )
241 min=(*edgemap_storage[name])[j];
247 int MapStorage::readFromFile(const std::string &filename)
251 bool read_edge_id = false;
254 LemonReader lreader(filename);
255 ContentReader content(lreader);
258 if (content.nodeSetNum() < 1)
260 Gtk::MessageDialog mdialog("No nodeset found in file.");
266 if (content.edgeSetNum() < 1)
268 Gtk::MessageDialog mdialog("No edgeset found in file.");
274 const std::vector<std::string>& nodeMapNames = content.nodeSetMaps(0);
275 const std::vector<std::string>& edgeMapNames = content.edgeSetMaps(0);
277 GraphReader<Graph> greader(filename, graph);
278 for (std::vector<std::string>::const_iterator it = nodeMapNames.begin();
279 it != nodeMapNames.end(); ++it)
281 if (*it == "coordinates_x")
284 //std::cout << "read X nodemap" << std::endl;
286 else if (*it == "coordinates_y")
289 //std::cout << "read Y nodemap" << std::endl;
291 else if (*it == "label")
293 //std::cout << "read id nodemap" << std::endl;
297 nodemap_storage[*it] = new Graph::NodeMap<double>(graph);
298 //std::cout << "read " << *it << " nodemap" << std::endl;
300 greader.readNodeMap(*it, *nodemap_storage[*it]);
302 for (std::vector<std::string>::const_iterator it = edgeMapNames.begin();
303 it != edgeMapNames.end(); ++it)
307 //std::cout << "read id edgemap" << std::endl;
312 edgemap_storage[*it] = new Graph::EdgeMap<double>(graph);
313 //std::cout << "read " << *it << " edgemap" << std::endl;
315 greader.readEdgeMap(*it, *edgemap_storage[*it]);
317 GuiReader gui_reader(greader, this);
319 } catch (Exception& error) {
320 Gtk::MessageDialog mdialog(error.what());
328 edgemap_storage["label"] = new Graph::EdgeMap<double>(graph);
330 for (EdgeIt e(graph); e != INVALID; ++e)
332 (*edgemap_storage["label"])[e] = i++;
336 if (!read_x || !read_y)
339 for (NodeIt n(graph); n != INVALID; ++n)
343 const double pi = 3.142;
344 double step = 2 * pi / (double) node_num;
346 for (NodeIt n(graph); n != INVALID; ++n)
348 nodemap_storage["coordinates_x"]->set(n, 250.0 * std::cos(i * step));
349 nodemap_storage["coordinates_y"]->set(n, 250.0 * std::sin(i * step));
354 if (!arrow_pos_read_ok)
356 arrow_pos_read_ok = false;
357 for (EdgeIt e(graph); e != INVALID; ++e)
359 if (graph.source(e) == graph.target(e))
361 arrow_pos.set(e, coords[graph.source(e)] + XY(0.0, 80.0));
365 arrow_pos.set(e, (coords[graph.source(e)] + coords[graph.target(e)]) / 2.0);
370 // fill in the default values for the maps
371 for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
372 nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
374 if ((it->first != "label") &&
375 (it->first != "coordiantes_x") &&
376 (it->first != "coordinates_y"))
378 nodemap_default[it->first] = 0.0;
380 else if (it->first == "label")
383 double max = (*nodemap_storage["label"])[n];
384 for (; n != INVALID; ++n)
386 if ((*nodemap_storage["label"])[n] > max)
387 max = (*nodemap_storage["label"])[n];
389 nodemap_default["label"] = max + 1.0;
392 for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
393 edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
395 if (it->first != "label")
397 edgemap_default[it->first] = 0.0;
401 double max = std::numeric_limits<double>::min();
402 for (EdgeIt e(graph); e != INVALID; ++e)
404 if ((*edgemap_storage["label"])[e] > max)
405 max = (*edgemap_storage["label"])[e];
407 if (max > std::numeric_limits<double>::min())
408 edgemap_default["label"] = max + 1.0;
410 edgemap_default["label"] = 1.0;
417 void MapStorage::writeToFile(const std::string &filename)
419 GraphWriter<Graph> gwriter(filename, graph);
421 for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
422 nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
424 gwriter.writeNodeMap(it->first, *(it->second));
426 for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
427 edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
429 if ((it->first != "arrow_pos_x") &&
430 (it->first != "arrow_pos_y"))
432 gwriter.writeEdgeMap(it->first, *(it->second));
436 GuiWriter gui_writer(gwriter, this);
441 void MapStorage::clear()
443 for (std::map<std::string, Graph::NodeMap<double>*>::iterator it =
444 nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
446 if ((it->first != "coordinates_x") &&
447 (it->first != "coordinates_y") &&
448 (it->first != "label"))
451 nodemap_storage.erase(it);
454 for (std::map<std::string, Graph::EdgeMap<double>*>::iterator it =
455 edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
457 if ((it->first != "label") &&
458 (it->first != "arrow_pos_x") &&
459 (it->first != "arrow_pos_y"))
462 edgemap_storage.erase(it);
465 for (std::map<std::string, double>::iterator it =
466 nodemap_default.begin(); it != nodemap_default.end(); ++it)
468 if (it->first != "label")
469 nodemap_default.erase(it);
471 for (std::map<std::string, double>::iterator it =
472 edgemap_default.begin(); it != edgemap_default.end(); ++it)
474 if (it->first != "label")
475 edgemap_default.erase(it);
481 arrow_pos_read_ok = false;
483 for(int i=0;i<NODE_PROPERTY_NUM;i++)
485 changeActiveMap(false, i, "");
486 signal_map_win.emit(false, i, "");
489 for(int i=0;i<EDGE_PROPERTY_NUM;i++)
491 changeActiveMap(true, i, "");
492 signal_map_win.emit(true, i, "");
499 signal_design_win.emit(attraction, propulsation, iterations);
502 void MapStorage::ArrowPosReadOK()
504 arrow_pos_read_ok = true;
507 void MapStorage::mapChanged(bool itisedge, std::string mapname)
511 for(int i=0;i<EDGE_PROPERTY_NUM;i++)
513 if(active_edgemaps[i]==mapname)
515 signal_prop.emit(itisedge, i);
521 for(int i=0;i<NODE_PROPERTY_NUM;i++)
523 if(active_nodemaps[i]==mapname)
525 signal_prop.emit(itisedge, i);
531 void MapStorage::get_design_data(double & attraction_p, double & propulsation_p, int & iterations_p)
533 attraction_p=attraction;
534 propulsation_p=propulsation;
535 iterations_p=iterations;
538 void MapStorage::set_attraction(double attraction_p)
540 attraction=attraction_p;
543 void MapStorage::set_propulsation(double propulsation_p)
545 propulsation=propulsation_p;
548 void MapStorage::set_iteration(int iterations_p)
550 iterations=iterations_p;
553 void MapStorage::redesign_data_changed()
555 signal_design_win.emit(attraction, propulsation, iterations);
558 void MapStorage::setBackground(const std::string& file_name)
560 if (file_name == background_file_name) return;
563 background_file_name = "";
564 background_set = false;
568 background_file_name = file_name;
569 background_set = true;
571 mytab.gd_canvas->setBackground();
574 const std::string& MapStorage::getBackgroundFilename()
576 return background_file_name;
579 bool MapStorage::isBackgroundSet()
581 return background_set;
584 double MapStorage::getBackgroundScaling()
586 return background_scaling;
589 void MapStorage::setBackgroundScaling(double scaling)
591 background_scaling = scaling;
594 void MapStorage::exportGraphToEPS(std::vector<bool> options, std::string filename)
596 Graph::NodeMap<int> _nodeColors(graph, 0);
597 Graph::EdgeMap<int> _edgeColors(graph, 0);
598 Graph::NodeMap<double> _nodeSizes(graph, 6.0);
599 Graph::EdgeMap<double> _edgeWidths(graph, 1.0);
600 bool _drawArrows=options[ARROWS];
601 bool _enableParallel=options[PAR];
603 std::string emptyString="";
604 Graph::NodeMap<std::string> _nodeTextMap(graph,emptyString);
606 //_nodeTextMap=(Graph::NodeMap<void> *)&emptyStringMap;
610 if(active_nodemaps[N_RADIUS]!="")
612 _nodeSizes=*(nodemap_storage[active_nodemaps[N_RADIUS]]);
614 if(active_nodemaps[N_COLOR]!="")
616 for(NodeIt ni(graph);ni!=INVALID;++ni)
618 _nodeColors[ni]=(int)((*(nodemap_storage[active_nodemaps[N_COLOR]]))[ni]);
621 if(active_nodemaps[N_TEXT]!="")
623 for(NodeIt ni(graph);ni!=INVALID;++ni)
625 std::ostringstream o;
626 o << ((*(nodemap_storage[active_nodemaps[N_TEXT]]))[ni]);
627 _nodeTextMap[ni]=o.str();
633 if(active_edgemaps[E_WIDTH]!="")
635 _edgeWidths=*(edgemap_storage[active_edgemaps[E_WIDTH]]);
637 if(active_edgemaps[E_COLOR]!="")
639 for(EdgeIt ei(graph);ei!=INVALID;++ei)
641 _edgeColors[ei]=(int)((*(edgemap_storage[active_edgemaps[E_COLOR]]))[ei]);
647 Palette paletteW(true);
649 graphToEps(graph,filename).
650 title("Sample .eps figure (fits to A4)").
651 copyright("(C) 2006 LEMON Project").
652 absoluteNodeSizes().absoluteEdgeWidths().
653 nodeScale(2).nodeSizes(_nodeSizes).
655 nodeColors(composeMap(paletteW,_nodeColors)).
656 edgeColors(composeMap(palette,_edgeColors)).
657 edgeWidthScale(0.3).edgeWidths(_edgeWidths).
658 nodeTexts(_nodeTextMap).nodeTextSize(7).
659 enableParallel(_enableParallel).parEdgeDist(4).
660 drawArrows(_drawArrows).arrowWidth(7).arrowLength(7).