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
27 #include "all_include.h"
29 #include <libgnomecanvasmm.h>
30 #include "map_value.h"
31 #include "map_value_map.h"
33 ///class MapStorage handles NodeMaps and ArcMaps.
35 ///Class MapStorage is responsible for storing
36 ///NodeMaps and ArcMaps that can be shown later
37 ///on GUI. Therefore maps can be added to it,
38 ///and datas over the added maps can be queried.
39 ///The maps will be stored in an std::map,
40 ///referenced with their names. Unfortunately at
41 ///the moment it works only with double type maps
43 ///\todo too many things are public!!
47 std::string background_file_name;
49 double background_scaling;
51 class Error : public std::exception
56 Error(const std::string& msg) : message(msg) {}
57 virtual const char* what() const throw()
59 return message.c_str();
64 void setBackground(const std::string& file_name);
65 const std::string& getBackgroundFilename();
66 bool isBackgroundSet();
67 double getBackgroundScaling();
68 void setBackgroundScaling(double scaling);
70 enum MapSaveDest { GUI_SECT, NESET_SECT, DONT_SAVE };
71 enum GuiSectSaveDest { LGF_FILE, CONF_FILE };
72 struct SpecMapSaveOpts
74 enum Dest { GUI_SECT, NESET_SECT };
75 enum MapNum { ONE_MAP, TWO_MAPS };
78 typedef Digraph::NodeMap<double> NumericNodeMap;
79 typedef Digraph::NodeMap<std::string> StringNodeMap;
80 typedef Digraph::ArcMap<double> NumericArcMap;
81 typedef Digraph::ArcMap<std::string> StringArcMap;
82 typedef Digraph::NodeMap<int> NodeLabelMap;
83 typedef Digraph::ArcMap<int> ArcLabelMap;
84 typedef XYMap<Digraph::NodeMap<double> > NodeCoordMap;
85 typedef XYMap<Digraph::ArcMap<double> > ArrowCoordMap;
89 /// where to save the map
90 MapSaveDest save_dest;
91 /// read-only or read-write
94 MapValue default_value;
95 virtual MapValue::Type type() = 0;
96 virtual MapValue get(Arc e) = 0;
97 virtual void set(Arc e, MapValue v) = 0;
98 ArcMapData(MapValue def_val) :
101 default_value(def_val)
105 struct NumericArcMapData : public ArcMapData
108 MapValue::Type type() { return MapValue::NUMERIC; }
109 MapValue get(Arc e) { return MapValue(map[e]); }
110 void set(Arc e, MapValue v) { map.set(e, static_cast<double>(v)); }
111 NumericArcMapData(Digraph& g, double def_val) :
112 ArcMapData(MapValue(def_val)),
117 struct StringArcMapData : public ArcMapData
120 MapValue::Type type() { return MapValue::STRING; }
121 MapValue get(Arc e) { return MapValue(map[e]); }
122 void set(Arc e, MapValue v) { map.set(e, static_cast<std::string>(v)); }
123 StringArcMapData(Digraph& g, std::string def_val) :
124 ArcMapData(MapValue(def_val)),
131 /// where to save the map
132 MapSaveDest save_dest;
133 /// read-only or read-write
136 MapValue default_value;
137 virtual MapValue::Type type() = 0;
138 virtual MapValue get(Node e) = 0;
139 virtual void set(Node e, MapValue v) = 0;
140 NodeMapData(MapValue def_val) :
143 default_value(def_val)
147 struct NumericNodeMapData : public NodeMapData
150 MapValue::Type type() { return MapValue::NUMERIC; }
151 MapValue get(Node e) { return MapValue(map[e]); }
152 void set(Node e, MapValue v) { map.set(e, static_cast<double>(v)); }
153 NumericNodeMapData(Digraph& g, double def_val) :
154 NodeMapData(MapValue(def_val)),
159 struct StringNodeMapData : public NodeMapData
162 MapValue::Type type() { return MapValue::STRING; }
163 MapValue get(Node e) { return MapValue(map[e]); }
164 void set(Node e, MapValue v) { map.set(e, static_cast<std::string>(v)); }
165 StringNodeMapData(Digraph& g, std::string def_val) :
166 NodeMapData(MapValue(def_val)),
171 typedef std::map<std::string, NodeMapData*> NodeMapStore;
172 typedef std::map<std::string, ArcMapData*> ArcMapStore;
176 std::vector<std::string> main_node_map_names;
177 std::vector<std::string> main_arc_map_names;
179 std::vector<std::string> gui_node_map_names;
180 std::vector<std::string> gui_arc_map_names;
182 std::map<std::string, MapValue::Type> node_map_types;
183 std::map<std::string, MapValue::Type> arc_map_types;
185 std::map<std::string, std::map<int, double>* > numeric_node_maps;
186 std::map<std::string, std::map<int, std::string>* > string_node_maps;
188 std::map<std::string, std::map<int, double>* > numeric_arc_maps;
189 std::map<std::string, std::map<int, std::string>* > string_arc_maps;
191 std::map<int, XY> node_coord_map;
192 std::map<int, XY> arrow_coord_map;
194 SpecMapSaveOpts::Dest node_coords_save_dest;
195 SpecMapSaveOpts::MapNum node_coords_save_map_num;
196 std::string node_coords_one_map_name;
197 std::string node_coords_two_maps_1_name;
198 std::string node_coords_two_maps_2_name;
200 SpecMapSaveOpts::Dest arrow_coords_save_dest;
201 SpecMapSaveOpts::MapNum arrow_coords_save_map_num;
202 std::string arrow_coords_one_map_name;
203 std::string arrow_coords_two_maps_1_name;
204 std::string arrow_coords_two_maps_2_name;
213 for (map<string, map<int, double>* >::iterator it =
214 numeric_node_maps.begin(); it != numeric_node_maps.end(); ++it)
218 for (map<string, map<int, string>* >::iterator it =
219 string_node_maps.begin(); it != string_node_maps.end(); ++it)
223 for (map<string, map<int, double>* >::iterator it =
224 numeric_arc_maps.begin(); it != numeric_arc_maps.end(); ++it)
228 for (map<string, map<int, string>* >::iterator it =
229 string_arc_maps.begin(); it != string_arc_maps.end(); ++it)
236 ///The digraph for which the datas are stored.
238 const Digraph& getDigraph();
241 GuiSectSaveDest gui_sect_save_dest;
243 SpecMapSaveOpts::Dest node_coords_save_dest;
244 SpecMapSaveOpts::MapNum node_coords_save_map_num;
245 SpecMapSaveOpts::Dest arrow_coords_save_dest;
246 SpecMapSaveOpts::MapNum arrow_coords_save_map_num;
248 NodeMapStore nodemaps;
251 NodeLabelMap node_label;
252 ArcLabelMap arc_label;
254 /// the coordinates of the nodes
255 NodeCoordMap node_coords;
256 Digraph::NodeMap<double> node_coords_x;
257 Digraph::NodeMap<double> node_coords_y;
259 /// the coordinates of the arrows on the arcs
260 ArrowCoordMap arrow_coords;
261 Digraph::ArcMap<double> arrow_coords_x;
262 Digraph::ArcMap<double> arrow_coords_y;
264 ///The content of the object has changed, update is needed.
267 ///Name of file loaded in object.
268 std::string file_name;
270 // the largest node label
273 // the largest arc label
276 std::string node_coords_one_map_name;
277 std::string node_coords_two_maps_1_name;
278 std::string node_coords_two_maps_2_name;
280 std::string arrow_coords_one_map_name;
281 std::string arrow_coords_two_maps_1_name;
282 std::string arrow_coords_two_maps_2_name;
285 ///Stores the default values for the different visualization node attributes
286 std::vector<Digraph::NodeMap<double> > default_nodemaps;
288 ///Stores the default values for the different visualization arc attributes
289 std::vector<Digraph::ArcMap<double> > default_arcmaps;
291 ///Stores the active maps for the different visualization node attributes
292 std::vector< std::string > active_nodemaps;
294 /// Stores the active maps for the different visualization arc attributes
295 std::vector< std::string > active_arcmaps;
299 /// Signal emitted on any change made on map values
301 /// Signal emitted if the visualization of the maps might have to be updated.
302 /// bool shows us whether the changed map is arc or nodemap.
303 /// int tells us the refreshed property
304 sigc::signal<void, bool, int> signal_prop;
306 /// Signal emitted in the case of nodemap addition
308 /// std::string is the
309 ///name of the new map
310 sigc::signal<void, std::string, MapValue::Type> signal_node_map;
312 /// Signal emitted in the case of arcmap addition
314 /// std::string is the
315 ///name of the new map
316 sigc::signal<void, std::string, MapValue::Type> signal_arc_map;
318 /// Signal emitted, when entry in \ref MapWin should be changed.
319 sigc::signal<void, bool, int, std::string> signal_map_win;
321 /// Signal emitted, when entry in \ref DesignWin should be changed.
322 sigc::signal<void, double, double, int> signal_design_win;
324 ///Signal emitted when background should be set by \ref NoteBookTab
325 sigc::signal<void> signal_background;
327 ///Iteration number during digraph design
330 ///Attraction factor during digraph design
333 ///Propulsation factor during digraph design
337 ///Constructor of MapStorage.
339 ///Its all activity is initializing default values
340 ///for different visualization attributes.
343 ///Destructor of MapStorage
345 ///Maps stored here are created with new. Destructor
346 ///deletes them to free up the reserved memory.
349 /// Registrates if the shown map by any attribute has changed to another.
351 ///It handles the \ref active_arcmaps and
352 ///\ref active_nodemaps vectors. It also emits \ref signal_prop signal to let
353 ///know the interested objects that the visible map of a certain
354 ///attribute has changed.
355 ///\param itisarc arcmap or nodemap has changed
356 ///\param prop the property of which the map is changed
357 ///\param mapname the visible map
358 void changeActiveMap(bool itisarc , int prop , std::string mapname);
360 ///Emits signals that let change the active maps in \ref MapWin.
361 void broadcastActiveMaps();
363 /// Returns the active arcmap shown by a visualization property.
365 /// \param prop is the property
366 ///that shows the requested map.
367 std::string getActiveArcMap(int prop);
369 /// Returns the active nodemap shown by a visualization property.
371 /// \param prop is the property
372 ///that shows the requested map.
373 std::string getActiveNodeMap(int prop);
375 /// Returns the names of the arcmaps stored here.
376 std::vector<std::string> getArcMapList(MapType type = ALL);
378 /// Returns the names of the nodemaps stored here.
379 std::vector<std::string> getNodeMapList(MapType type = ALL);
381 ///returns \ref signal_prop to be able to connect functions to it
382 sigc::signal<void, bool, int> signal_prop_ch();
384 ///returns \ref signal_node_map to be able to connect functions to it
385 sigc::signal<void, std::string, MapValue::Type> signal_node_map_ch(){return signal_node_map;};
387 ///returns \ref signal_arc_map to be able to connect functions to it
388 sigc::signal<void, std::string, MapValue::Type> signal_arc_map_ch(){return signal_arc_map;};
390 ///returns \ref signal_map_win to be able to connect functions to it
391 sigc::signal<void, bool, int, std::string> signal_map_win_ch(){return signal_map_win;};
393 ///returns \ref signal_design_win to be able to connect functions to it
394 sigc::signal<void, double, double, int> signal_design_win_ch(){return signal_design_win;};
396 void createNodeMap(const std::string& name, MapValue::Type type,
398 void createArcMap(const std::string& name, MapValue::Type type,
401 ///returns \ref signal_background to be able to connect functions to it
402 sigc::signal<void> signal_background_ch(){return signal_background;};
405 ///Adds given map to storage.
407 ///Emits \ref signal_prop if mapvalues have changed, and MapStorage gets to know it.
409 ///If values in a map have changed, this function checks, whether it is displayed.
410 ///This check means searching the given mapname between active maps
411 ///(\ref active_nodemaps, \ref active_arcmaps). If it is there at a certain property,
412 ///it emits a signal with the property, where the gotten mapname was found. One signal
413 ///is emitted for each property displaying the given map.
414 ///\param itisarc whether the map an arcmap or nodemap
415 ///\param mapname name of map to visualize
416 void mapChanged(bool itisarc, std::string mapname);
418 ///Read datas from the given filename.
419 int readFromFile(const std::string &);
421 ///Save datas to the given filename.
422 void writeToFile(const std::string &);
424 ///Deletes all datastructures stored here.
427 void get_design_data(double &, double &, int &);
428 void set_attraction(double);
429 void set_propulsation(double);
430 void set_iteration(int);
432 void redesign_data_changed();
434 XY getNodeCoords(Node n) const;
435 void setNodeCoords(Node n, XY c);
436 XY getArrowCoords(Arc e) const;
437 void setArrowCoords(Arc e, XY c);
439 MapValue get(const std::string& name, Node node) const;
440 void set(const std::string& name, Node node, MapValue val);
441 MapValue get(const std::string& name, Arc arc) const;
442 void set(const std::string& name, Arc arc, MapValue val);
444 const std::string& getFileName() const;
445 void setFileName(const std::string& fn);
447 bool getModified() const;
448 void setModified(bool m = true);
451 Arc addArc(Node, Node);
453 NumericNodeMap& getNumericNodeMap(const std::string& name);
454 StringNodeMap& getStringNodeMap(const std::string& name);
455 NumericArcMap& getNumericArcMap(const std::string& name);
456 StringArcMap& getStringArcMap(const std::string& name);
458 MapValueArcMap getArcMap(const std::string& name);
459 MapValueNodeMap getNodeMap(const std::string& name);
461 int getLabel(Node) const;
462 int getLabel(Arc) const;
464 GuiSectSaveDest getGUIDataSaveLocation();
465 void setGUIDataSaveLocation(GuiSectSaveDest dest);
467 MapSaveDest getNodeMapSaveDest(std::string name) const;
468 MapSaveDest getArcMapSaveDest(std::string name) const;
469 void setNodeMapSaveDest(std::string name, MapSaveDest dest);
470 void setArcMapSaveDest(std::string name, MapSaveDest dest);
472 SpecMapSaveOpts::Dest getNodeCoordsSaveDest();
473 SpecMapSaveOpts::Dest getArrowCoordsSaveDest();
474 void setNodeCoordsSaveDest(SpecMapSaveOpts::Dest dest);
475 void setArrowCoordsSaveDest(SpecMapSaveOpts::Dest dest);
477 SpecMapSaveOpts::MapNum getNodeCoordsSaveMapNum();
478 SpecMapSaveOpts::MapNum getArrowCoordsSaveMapNum();
479 void setNodeCoordsSaveMapNum(SpecMapSaveOpts::MapNum num);
480 void setArrowCoordsSaveMapNum(SpecMapSaveOpts::MapNum num);
482 MapValue::Type getNodeMapElementType(std::string name) const;
483 MapValue::Type getArcMapElementType(std::string name) const;
485 const NodeLabelMap& getNodeLabelMap();
486 const ArcLabelMap& getArcLabelMap();
488 bool nodeMapExists(std::string name);
489 bool arcMapExists(std::string name);
491 std::vector<std::string> getArcMaps(MapType type = ALL);
492 std::vector<std::string> getNodeMaps(MapType type = ALL);
494 NodeCoordMap& getNodeCoordMap();
495 ArrowCoordMap& getArrowCoordMap();
497 const std::string& getNodeCoordsOneMapName();
498 const std::string& getNodeCoordsTwoMaps1Name();
499 const std::string& getNodeCoordsTwoMaps2Name();
500 void setNodeCoordsOneMapName(const std::string& name);
501 void setNodeCoordsTwoMaps1Name(const std::string& name);
502 void setNodeCoordsTwoMaps2Name(const std::string& name);
504 const std::string& getArrowCoordsOneMapName();
505 const std::string& getArrowCoordsTwoMaps1Name();
506 const std::string& getArrowCoordsTwoMaps2Name();
507 void setArrowCoordsOneMapName(const std::string& name);
508 void setArrowCoordsTwoMaps1Name(const std::string& name);
509 void setArrowCoordsTwoMaps2Name(const std::string& name);
512 ArcMapData* getArcMapData(std::string name) const;
513 NodeMapData* getNodeMapData(std::string name) const;
515 const std::string& filename,
517 const std::vector<std::string>& node_map_names,
518 const std::vector<std::string>& arc_map_names,
519 const std::map<std::string, MapValue::Type>& node_map_types,
520 const std::map<std::string, MapValue::Type>& arc_map_types,
521 const std::string& node_coord_xmap_name,
522 const std::string& node_coord_ymap_name,
523 const std::string& arrow_coord_xmap_name,
524 const std::string& arrow_coord_ymap_name);
527 void exportDigraphToEPS(std::vector<bool>, std::string, std::string);
530 #endif //MAPSTORAGE_H