/* -*- C++ -*- * * This file is a part of LEMON, a generic C++ optimization library * * Copyright (C) 2003-2006 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport * (Egervary Research Group on Combinatorial Optimization, EGRES). * * Permission to use, modify and distribute this software is granted * provided that this copyright notice appears in all copies. For * precise terms see the accompanying LICENSE file. * * This software is provided "AS IS" with no warranty of any kind, * express or implied, and with no claim as to its suitability for any * purpose. * */ #ifndef MAPSTORAGE_H #define MAPSTORAGE_H class Mapstorage; #include #include #include #include "all_include.h" #include "xymap.h" #include #include "map_value.h" #include "map_value_map.h" ///class MapStorage handles NodeMaps and EdgeMaps. ///Class MapStorage is responsible for storing ///NodeMaps and EdgeMaps that can be shown later ///on GUI. Therefore maps can be added to it, ///and datas over the added maps can be queried. ///The maps will be stored in an std::map, ///referenced with their names. Unfortunately at ///the moment it works only with double type maps /// ///\todo too many things are public!! class MapStorage { private: std::string background_file_name; bool background_set; double background_scaling; public: class Error : public std::exception { private: std::string message; public: Error(const std::string& msg) : message(msg) {} virtual const char* what() const throw() { return message.c_str(); } ~Error() throw() {} }; void setBackground(const std::string& file_name); const std::string& getBackgroundFilename(); bool isBackgroundSet(); double getBackgroundScaling(); void setBackgroundScaling(double scaling); enum MapSaveDest { GUI_SECT, NESET_SECT, DONT_SAVE }; enum GuiSectSaveDest { LGF_FILE, CONF_FILE }; struct SpecMapSaveOpts { enum Dest { GUI_SECT, NESET_SECT }; enum MapNum { ONE_MAP, TWO_MAPS }; }; typedef Graph::NodeMap NumericNodeMap; typedef Graph::NodeMap StringNodeMap; typedef Graph::EdgeMap NumericEdgeMap; typedef Graph::EdgeMap StringEdgeMap; typedef Graph::NodeMap NodeLabelMap; typedef Graph::EdgeMap EdgeLabelMap; typedef XYMap > NodeCoordMap; typedef XYMap > ArrowCoordMap; struct EdgeMapData { /// where to save the map MapSaveDest save_dest; /// read-only or read-write bool writeable; /// default value MapValue default_value; virtual MapValue::Type type() = 0; virtual MapValue get(Edge e) = 0; virtual void set(Edge e, MapValue v) = 0; EdgeMapData(MapValue def_val) : save_dest(GUI_SECT), writeable(true), default_value(def_val) {} }; struct NumericEdgeMapData : public EdgeMapData { NumericEdgeMap map; MapValue::Type type() { return MapValue::NUMERIC; } MapValue get(Edge e) { return MapValue(map[e]); } void set(Edge e, MapValue v) { map.set(e, static_cast(v)); } NumericEdgeMapData(Graph& g, double def_val) : EdgeMapData(MapValue(def_val)), map(g, def_val) {} }; struct StringEdgeMapData : public EdgeMapData { StringEdgeMap map; MapValue::Type type() { return MapValue::STRING; } MapValue get(Edge e) { return MapValue(map[e]); } void set(Edge e, MapValue v) { map.set(e, static_cast(v)); } StringEdgeMapData(Graph& g, std::string def_val) : EdgeMapData(MapValue(def_val)), map(g, def_val) {} }; struct NodeMapData { /// where to save the map MapSaveDest save_dest; /// read-only or read-write bool writeable; /// default value MapValue default_value; virtual MapValue::Type type() = 0; virtual MapValue get(Node e) = 0; virtual void set(Node e, MapValue v) = 0; NodeMapData(MapValue def_val) : save_dest(GUI_SECT), writeable(true), default_value(def_val) {} }; struct NumericNodeMapData : public NodeMapData { NumericNodeMap map; MapValue::Type type() { return MapValue::NUMERIC; } MapValue get(Node e) { return MapValue(map[e]); } void set(Node e, MapValue v) { map.set(e, static_cast(v)); } NumericNodeMapData(Graph& g, double def_val) : NodeMapData(MapValue(def_val)), map(g, def_val) {} }; struct StringNodeMapData : public NodeMapData { StringNodeMap map; MapValue::Type type() { return MapValue::STRING; } MapValue get(Node e) { return MapValue(map[e]); } void set(Node e, MapValue v) { map.set(e, static_cast(v)); } StringNodeMapData(Graph& g, std::string def_val) : NodeMapData(MapValue(def_val)), map(g, def_val) {} }; typedef std::map NodeMapStore; typedef std::map EdgeMapStore; struct GUISectData { std::vector main_node_map_names; std::vector main_edge_map_names; std::vector gui_node_map_names; std::vector gui_edge_map_names; std::map node_map_types; std::map edge_map_types; std::map* > numeric_node_maps; std::map* > string_node_maps; std::map* > numeric_edge_maps; std::map* > string_edge_maps; std::map node_coord_map; std::map arrow_coord_map; SpecMapSaveOpts::Dest node_coords_save_dest; SpecMapSaveOpts::MapNum node_coords_save_map_num; std::string node_coords_one_map_name; std::string node_coords_two_maps_1_name; std::string node_coords_two_maps_2_name; SpecMapSaveOpts::Dest arrow_coords_save_dest; SpecMapSaveOpts::MapNum arrow_coords_save_map_num; std::string arrow_coords_one_map_name; std::string arrow_coords_two_maps_1_name; std::string arrow_coords_two_maps_2_name; ~GUISectData() { using std::map; using std::vector; using std::pair; using std::string; for (map* >::iterator it = numeric_node_maps.begin(); it != numeric_node_maps.end(); ++it) { delete it->second; } for (map* >::iterator it = string_node_maps.begin(); it != string_node_maps.end(); ++it) { delete it->second; } for (map* >::iterator it = numeric_edge_maps.begin(); it != numeric_edge_maps.end(); ++it) { delete it->second; } for (map* >::iterator it = string_edge_maps.begin(); it != string_edge_maps.end(); ++it) { delete it->second; } } }; public: ///The graph for which the datas are stored. Graph graph; const Graph& getGraph(); private: GuiSectSaveDest gui_sect_save_dest; SpecMapSaveOpts::Dest node_coords_save_dest; SpecMapSaveOpts::MapNum node_coords_save_map_num; SpecMapSaveOpts::Dest arrow_coords_save_dest; SpecMapSaveOpts::MapNum arrow_coords_save_map_num; NodeMapStore nodemaps; EdgeMapStore edgemaps; NodeLabelMap node_label; EdgeLabelMap edge_label; /// the coordinates of the nodes NodeCoordMap node_coords; Graph::NodeMap node_coords_x; Graph::NodeMap node_coords_y; /// the coordinates of the arrows on the edges ArrowCoordMap arrow_coords; Graph::EdgeMap arrow_coords_x; Graph::EdgeMap arrow_coords_y; ///The content of the object has changed, update is needed. bool modified; ///Name of file loaded in object. std::string file_name; // the largest node label int max_node_label; // the largest edge label int max_edge_label; std::string node_coords_one_map_name; std::string node_coords_two_maps_1_name; std::string node_coords_two_maps_2_name; std::string arrow_coords_one_map_name; std::string arrow_coords_two_maps_1_name; std::string arrow_coords_two_maps_2_name; public: ///Stores the default values for the different visualization node attributes std::vector > default_nodemaps; ///Stores the default values for the different visualization edge attributes std::vector > default_edgemaps; ///Stores the active maps for the different visualization node attributes std::vector< std::string > active_nodemaps; /// Stores the active maps for the different visualization edge attributes std::vector< std::string > active_edgemaps; protected: /// Signal emitted on any change made on map values /// Signal emitted if the visualization of the maps might have to be updated. /// bool shows us whether the changed map is edge or nodemap. /// int tells us the refreshed property sigc::signal signal_prop; /// Signal emitted in the case of nodemap addition /// std::string is the ///name of the new map sigc::signal signal_node_map; /// Signal emitted in the case of edgemap addition /// std::string is the ///name of the new map sigc::signal signal_edge_map; /// Signal emitted, when entry in \ref MapWin should be changed. sigc::signal signal_map_win; /// Signal emitted, when entry in \ref DesignWin should be changed. sigc::signal signal_design_win; ///Signal emitted when background should be set by \ref NoteBookTab sigc::signal signal_background; ///Iteration number during graph design int iterations; ///Attraction factor during graph design double attraction; ///Propulsation factor during graph design double propulsation; public: ///Constructor of MapStorage. ///Its all activity is initializing default values ///for different visualization attributes. MapStorage(); ///Destructor of MapStorage ///Maps stored here are created with new. Destructor ///deletes them to free up the reserved memory. ~MapStorage(); /// Registrates if the shown map by any attribute has changed to another. ///It handles the \ref active_edgemaps and ///\ref active_nodemaps vectors. It also emits \ref signal_prop signal to let ///know the interested objects that the visible map of a certain ///attribute has changed. ///\param itisedge edgemap or nodemap has changed ///\param prop the property of which the map is changed ///\param mapname the visible map void changeActiveMap(bool itisedge , int prop , std::string mapname); ///Emits signals that let change the active maps in \ref MapWin. void broadcastActiveMaps(); /// Returns the active edgemap shown by a visualization property. /// \param prop is the property ///that shows the requested map. std::string getActiveEdgeMap(int prop); /// Returns the active nodemap shown by a visualization property. /// \param prop is the property ///that shows the requested map. std::string getActiveNodeMap(int prop); /// Returns the names of the edgemaps stored here. std::vector getEdgeMapList(MapType type = ALL); /// Returns the names of the nodemaps stored here. std::vector getNodeMapList(MapType type = ALL); ///returns \ref signal_prop to be able to connect functions to it sigc::signal signal_prop_ch(); ///returns \ref signal_node_map to be able to connect functions to it sigc::signal signal_node_map_ch(){return signal_node_map;}; ///returns \ref signal_edge_map to be able to connect functions to it sigc::signal signal_edge_map_ch(){return signal_edge_map;}; ///returns \ref signal_map_win to be able to connect functions to it sigc::signal signal_map_win_ch(){return signal_map_win;}; ///returns \ref signal_design_win to be able to connect functions to it sigc::signal signal_design_win_ch(){return signal_design_win;}; void createNodeMap(const std::string& name, MapValue::Type type, MapValue def_val); void createEdgeMap(const std::string& name, MapValue::Type type, MapValue def_val); ///returns \ref signal_background to be able to connect functions to it sigc::signal signal_background_ch(){return signal_background;}; ///Adds given map to storage. ///Emits \ref signal_prop if mapvalues have changed, and MapStorage gets to know it. ///If values in a map have changed, this function checks, whether it is displayed. ///This check means searching the given mapname between active maps ///(\ref active_nodemaps, \ref active_edgemaps). If it is there at a certain property, ///it emits a signal with the property, where the gotten mapname was found. One signal ///is emitted for each property displaying the given map. ///\param itisedge whether the map an edgemap or nodemap ///\param mapname name of map to visualize void mapChanged(bool itisedge, std::string mapname); ///Read datas from the given filename. int readFromFile(const std::string &); ///Save datas to the given filename. void writeToFile(const std::string &); ///Deletes all datastructures stored here. void clear(); void get_design_data(double &, double &, int &); void set_attraction(double); void set_propulsation(double); void set_iteration(int); void redesign_data_changed(); XY getNodeCoords(Node n) const; void setNodeCoords(Node n, XY c); XY getArrowCoords(Edge e) const; void setArrowCoords(Edge e, XY c); MapValue get(const std::string& name, Node node) const; void set(const std::string& name, Node node, MapValue val); MapValue get(const std::string& name, Edge edge) const; void set(const std::string& name, Edge edge, MapValue val); const std::string& getFileName() const; void setFileName(const std::string& fn); bool getModified() const; void setModified(bool m = true); Node addNode(XY); Edge addEdge(Node, Node); NumericNodeMap& getNumericNodeMap(const std::string& name); StringNodeMap& getStringNodeMap(const std::string& name); NumericEdgeMap& getNumericEdgeMap(const std::string& name); StringEdgeMap& getStringEdgeMap(const std::string& name); MapValueEdgeMap getEdgeMap(const std::string& name); MapValueNodeMap getNodeMap(const std::string& name); int getLabel(Node) const; int getLabel(Edge) const; GuiSectSaveDest getGUIDataSaveLocation(); void setGUIDataSaveLocation(GuiSectSaveDest dest); MapSaveDest getNodeMapSaveDest(std::string name) const; MapSaveDest getEdgeMapSaveDest(std::string name) const; void setNodeMapSaveDest(std::string name, MapSaveDest dest); void setEdgeMapSaveDest(std::string name, MapSaveDest dest); SpecMapSaveOpts::Dest getNodeCoordsSaveDest(); SpecMapSaveOpts::Dest getArrowCoordsSaveDest(); void setNodeCoordsSaveDest(SpecMapSaveOpts::Dest dest); void setArrowCoordsSaveDest(SpecMapSaveOpts::Dest dest); SpecMapSaveOpts::MapNum getNodeCoordsSaveMapNum(); SpecMapSaveOpts::MapNum getArrowCoordsSaveMapNum(); void setNodeCoordsSaveMapNum(SpecMapSaveOpts::MapNum num); void setArrowCoordsSaveMapNum(SpecMapSaveOpts::MapNum num); MapValue::Type getNodeMapElementType(std::string name) const; MapValue::Type getEdgeMapElementType(std::string name) const; const NodeLabelMap& getNodeLabelMap(); const EdgeLabelMap& getEdgeLabelMap(); bool nodeMapExists(std::string name); bool edgeMapExists(std::string name); std::vector getEdgeMaps(MapType type = ALL); std::vector getNodeMaps(MapType type = ALL); NodeCoordMap& getNodeCoordMap(); ArrowCoordMap& getArrowCoordMap(); const std::string& getNodeCoordsOneMapName(); const std::string& getNodeCoordsTwoMaps1Name(); const std::string& getNodeCoordsTwoMaps2Name(); void setNodeCoordsOneMapName(const std::string& name); void setNodeCoordsTwoMaps1Name(const std::string& name); void setNodeCoordsTwoMaps2Name(const std::string& name); const std::string& getArrowCoordsOneMapName(); const std::string& getArrowCoordsTwoMaps1Name(); const std::string& getArrowCoordsTwoMaps2Name(); void setArrowCoordsOneMapName(const std::string& name); void setArrowCoordsTwoMaps1Name(const std::string& name); void setArrowCoordsTwoMaps2Name(const std::string& name); private: EdgeMapData* getEdgeMapData(std::string name) const; NodeMapData* getNodeMapData(std::string name) const; void readLGF( const std::string& filename, bool read_edge_label, const std::vector& node_map_names, const std::vector& edge_map_names, const std::map& node_map_types, const std::map& edge_map_types, const std::string& node_coord_xmap_name, const std::string& node_coord_ymap_name, const std::string& arrow_coord_xmap_name, const std::string& arrow_coord_ymap_name); public: void exportGraphToEPS(std::vector, std::string, std::string); }; #endif //MAPSTORAGE_H