[1442] | 1 | // -*- C++ -*- // |
---|
| 2 | |
---|
| 3 | #ifndef GRAPH_DISPLAYER_CANVAS_H |
---|
| 4 | #define GRAPH_DISPLAYER_CANVAS_H |
---|
| 5 | |
---|
[1501] | 6 | class GraphDisplayerCanvas; |
---|
| 7 | |
---|
[1606] | 8 | #include "all_include.h" |
---|
[1849] | 9 | #include "nbtab.h" |
---|
[1442] | 10 | #include <libgnomecanvasmm.h> |
---|
| 11 | #include <libgnomecanvasmm/polygon.h> |
---|
[1819] | 12 | #include <lemon/xy.h> |
---|
[1442] | 13 | |
---|
| 14 | ///This class is the canvas, on which the graph can be drawn. |
---|
| 15 | class GraphDisplayerCanvas : public Gnome::Canvas::CanvasAA |
---|
| 16 | { |
---|
[1860] | 17 | friend class BrokenEdge; |
---|
| 18 | |
---|
[1888] | 19 | ///Edge displayer class |
---|
| 20 | |
---|
| 21 | ///This class is responsible for displaying edges in graph. |
---|
| 22 | ///The displayed edge is broken in the middle. The |
---|
| 23 | ///aim of this is to be able to indicate direction of edges |
---|
| 24 | ///and to be able to display more then one edges between the |
---|
| 25 | ///same source and target |
---|
[1819] | 26 | class BrokenEdge : public Gnome::Canvas::Line |
---|
| 27 | { |
---|
[1888] | 28 | ///The edge that the class displays. |
---|
| 29 | |
---|
| 30 | ///It is needed, because some datas needed from |
---|
| 31 | ///graph can be accessed by this or should be sent |
---|
| 32 | ///as parameter, but it would be complicated |
---|
[1860] | 33 | Edge edge; |
---|
[1888] | 34 | |
---|
| 35 | ///Reference to the canvas, on which the graph is drawn. |
---|
| 36 | |
---|
| 37 | ///It is needed, because some datas needed from |
---|
| 38 | ///graph can be accessed by this or should be sent |
---|
| 39 | ///as parameter, but it would be complicated |
---|
[1819] | 40 | GraphDisplayerCanvas & gdc; |
---|
[1888] | 41 | |
---|
| 42 | ///An arrow that indicates the direction of the edges |
---|
| 43 | |
---|
| 44 | ///in case of directional graph direction can be indicated |
---|
| 45 | ///by this polygon. The polygon formulates a red arrow. |
---|
[1819] | 46 | Gnome::Canvas::Polygon * arrow; |
---|
| 47 | |
---|
[1888] | 48 | ///Indicates whether the button of mouse is pressed or not at the moment. |
---|
[1819] | 49 | bool isbutton; |
---|
| 50 | |
---|
[1888] | 51 | ///At this location was the mousebutton pressed. Horizontal component. |
---|
[1819] | 52 | |
---|
[1888] | 53 | ///It helps to calculate the |
---|
| 54 | ///distance of dragging. |
---|
| 55 | double clicked_x; |
---|
| 56 | |
---|
| 57 | ///At this location was the mousebutton pressed. Vertical component. |
---|
| 58 | |
---|
| 59 | ///It helps to calculate the |
---|
| 60 | ///distance of dragging. |
---|
| 61 | double clicked_y; |
---|
| 62 | |
---|
| 63 | ///event handler for forming broken edges |
---|
| 64 | |
---|
| 65 | ///\param event the |
---|
| 66 | ///event to handle |
---|
| 67 | bool edgeFormerEventHandler(GdkEvent* event); |
---|
[1819] | 68 | public: |
---|
[1888] | 69 | |
---|
| 70 | ///Constructor of broken edge class. |
---|
| 71 | |
---|
| 72 | ///\param g the group to which the edge belongs |
---|
| 73 | ///\param _edge the represented edge |
---|
| 74 | ///\param gc the canvas |
---|
| 75 | BrokenEdge(Gnome::Canvas::Group & g, Edge _edge, GraphDisplayerCanvas & gc); |
---|
| 76 | |
---|
| 77 | ///Destructor of broken edge class |
---|
| 78 | |
---|
| 79 | ///Frees up |
---|
| 80 | ///reserved memory |
---|
[1819] | 81 | ~BrokenEdge(); |
---|
[1888] | 82 | |
---|
| 83 | ///The function that draws the edge based on collected data |
---|
[1860] | 84 | void draw(); |
---|
[1819] | 85 | }; |
---|
[1888] | 86 | |
---|
| 87 | ///Type of canvas, on which the graph is drawn |
---|
[1442] | 88 | typedef Gnome::Canvas::CanvasAA Parent; |
---|
| 89 | |
---|
| 90 | public: |
---|
[1888] | 91 | ///Constructor |
---|
| 92 | |
---|
| 93 | ///\param nbt the tab of the window, in which the graph is displayed |
---|
| 94 | GraphDisplayerCanvas(NoteBookTab & nbt); |
---|
| 95 | |
---|
| 96 | ///destructor of the class |
---|
[1442] | 97 | virtual ~GraphDisplayerCanvas(); |
---|
| 98 | |
---|
[1888] | 99 | ///Changes the width of edge(s) according to the given map. |
---|
| 100 | |
---|
| 101 | ///\param mapname is the name of the map which contains the values to be set |
---|
| 102 | ///\param edge if it is given, only the width of the given edge will be set, instead of all of them. |
---|
| 103 | int changeEdgeWidth (std::string mapname, Edge edge=INVALID); |
---|
| 104 | |
---|
| 105 | ///Resets width of edge(s) to the default value |
---|
| 106 | |
---|
| 107 | ///\param edge if it is given, only the width of the |
---|
| 108 | ///given edge will be reset, instead of all of them. |
---|
| 109 | int resetEdgeWidth (Edge edge=INVALID); |
---|
| 110 | |
---|
| 111 | ///Changes the color of edge(s) according to the given map. |
---|
| 112 | |
---|
[1442] | 113 | ///\param mapname is the name of the map which contains the new values |
---|
[1888] | 114 | ///\param edge if it is given, only the color of the given edge will be set, instead of all of them. |
---|
| 115 | int changeEdgeColor (std::string mapname, Edge edge=INVALID); |
---|
[1442] | 116 | |
---|
[1888] | 117 | ///Resets color of edge(s) to the default value |
---|
| 118 | |
---|
| 119 | ///\param edge if it is given, only the color of the |
---|
| 120 | ///given edge will be reset, instead of all of them. |
---|
| 121 | int resetEdgeColor (Edge edge=INVALID); |
---|
| 122 | |
---|
| 123 | ///Changes the label of edge(s) according to the given map. |
---|
| 124 | |
---|
[1442] | 125 | ///\param mapname is the name of the map which contains the new values |
---|
[1888] | 126 | ///\param edge if it is given, only the label of the given edge will be set, instead of all of them. |
---|
| 127 | int changeEdgeText (std::string mapname, Edge edge=INVALID); |
---|
[1442] | 128 | |
---|
[1888] | 129 | ///Resets label of edge(s) to the default value |
---|
| 130 | |
---|
| 131 | ///\param edge if it is given, only the color of the |
---|
| 132 | ///given edge will be reset, instead of all of them. |
---|
| 133 | int resetEdgeText (Edge edge=INVALID); |
---|
| 134 | |
---|
| 135 | ///Changes the radius of node(s) according to the given map. |
---|
| 136 | |
---|
[1442] | 137 | ///\param mapname is the name of the map which contains the new values |
---|
[1888] | 138 | ///\param node if it is given, only the radius of the given node will be set, instead of all of them. |
---|
| 139 | int changeNodeRadius (std::string mapname, Node node=INVALID); |
---|
[1512] | 140 | |
---|
[1888] | 141 | ///Resets radius of node(s) to the default value |
---|
| 142 | |
---|
| 143 | ///\param node if it is given, only the radius of the |
---|
| 144 | ///given node will be reset, instead of all of them. |
---|
| 145 | int resetNodeRadius (Node node=INVALID); |
---|
| 146 | |
---|
| 147 | ///Changes the color of node(s) according to the given map. |
---|
| 148 | |
---|
[1512] | 149 | ///\param mapname is the name of the map which contains the new values |
---|
[1888] | 150 | ///\param node if it is given, only the color of the given node will be set, instead of all of them. |
---|
| 151 | int changeNodeColor (std::string mapname, Node node=INVALID); |
---|
[1512] | 152 | |
---|
[1888] | 153 | ///Resets color of node(s) to the default value |
---|
| 154 | |
---|
| 155 | ///\param node if it is given, only the color of the |
---|
| 156 | ///given node will be reset, instead of all of them. |
---|
| 157 | int resetNodeColor (Node node=INVALID); |
---|
| 158 | |
---|
| 159 | ///Changes the label of node(s) according to the given map. |
---|
| 160 | |
---|
[1512] | 161 | ///\param mapname is the name of the map which contains the new values |
---|
[1888] | 162 | ///\param node if it is given, only the label of the given node will be set, instead of all of them. |
---|
| 163 | int changeNodeText (std::string mapname, Node node=INVALID); |
---|
[1512] | 164 | |
---|
[1888] | 165 | ///Resets label of node(s) to the default value |
---|
[1442] | 166 | |
---|
[1888] | 167 | ///\param node if it is given, only the label of the |
---|
| 168 | ///given node will be reset, instead of all of them. |
---|
| 169 | int resetNodeText (Node node=INVALID); |
---|
[1837] | 170 | |
---|
[1888] | 171 | ///This function is called, when any of the displayed attributes have to be updated, or changed |
---|
| 172 | |
---|
| 173 | ///\param itisedge if true, edge property has to be changed, else node property |
---|
| 174 | ///\param prop the id of property that has to changed or updated |
---|
| 175 | void propertyChange(bool itisedge, int prop); |
---|
| 176 | |
---|
| 177 | ///updates the given property |
---|
| 178 | |
---|
| 179 | ///\param edge if it is not INVALID, only the property of the given edge will be updated, instead of all of them |
---|
| 180 | ///\param prop the property to update |
---|
| 181 | void propertyUpdate(Edge edge, int prop); |
---|
| 182 | |
---|
| 183 | ///updates the given property |
---|
| 184 | |
---|
| 185 | ///\param node if it is not INVALID, only the property of the given node will be updated, instead of all of them |
---|
| 186 | ///\param prop the property to update |
---|
| 187 | void propertyUpdate(Node node, int prop); |
---|
| 188 | |
---|
| 189 | ///updates all the property for the given edge |
---|
[1837] | 190 | void propertyUpdate(Edge); |
---|
[1888] | 191 | |
---|
| 192 | ///updates all the property for the given node |
---|
[1837] | 193 | void propertyUpdate(Node); |
---|
| 194 | |
---|
[1442] | 195 | ///Callback for 'ViewZoomIn' action. |
---|
| 196 | virtual void zoomIn(); |
---|
| 197 | ///Callback for 'ViewZoomOut' action. |
---|
| 198 | virtual void zoomOut(); |
---|
| 199 | ///Callback for 'ViewZoomFit' action. |
---|
| 200 | virtual void zoomFit(); |
---|
| 201 | ///Callback for 'ViewZoom100' action. |
---|
| 202 | virtual void zoom100(); |
---|
| 203 | ///Sets the scroll region of the convas to the bounding box of the graph. |
---|
| 204 | void updateScrollRegion(); |
---|
| 205 | |
---|
[1468] | 206 | ///This function changes the tool in the graph-editor's hand |
---|
| 207 | void changeEditorialTool(int); |
---|
| 208 | |
---|
[1442] | 209 | protected: |
---|
| 210 | |
---|
[1888] | 211 | //maximizing, minimizing, restoring window, etc. |
---|
[1442] | 212 | virtual bool on_expose_event(GdkEventExpose *); |
---|
| 213 | |
---|
| 214 | private: |
---|
| 215 | |
---|
| 216 | ///This function is responsible for the correct |
---|
| 217 | ///reaction of any action happened in the territory |
---|
| 218 | ///of the canvas |
---|
[1505] | 219 | ///DEPRECATED!!!! |
---|
[1524] | 220 | bool eventHandler(GdkEvent* e, Node n); |
---|
[1442] | 221 | |
---|
[1468] | 222 | ///actual event handler |
---|
| 223 | /// |
---|
| 224 | ///Actual event handler should be stored, to be able to disconnect it and later reconnect it. |
---|
| 225 | sigc::connection actual_handler; |
---|
| 226 | |
---|
| 227 | ///event handler for the case when move-tool is active |
---|
[1524] | 228 | bool moveEventHandler(GdkEvent*); |
---|
[1468] | 229 | ///event handler for the case when create_node-tool is active |
---|
[1524] | 230 | bool createNodeEventHandler(GdkEvent*); |
---|
[1468] | 231 | ///event handler for the case when create_edge-tool is active |
---|
[1524] | 232 | bool createEdgeEventHandler(GdkEvent*); |
---|
[1485] | 233 | ///event handler for the case when eraser-tool is active |
---|
[1524] | 234 | bool eraserEventHandler(GdkEvent*); |
---|
[1550] | 235 | ///event handler for the case when edge map editor tool is active |
---|
[1524] | 236 | bool edgeMapEditEventHandler(GdkEvent*); |
---|
[1550] | 237 | ///event handler for the case when node map editor tool is active |
---|
| 238 | bool nodeMapEditEventHandler(GdkEvent*); |
---|
[1485] | 239 | |
---|
[1501] | 240 | public: |
---|
[1505] | 241 | ///Moves the text to new place |
---|
[1524] | 242 | void textReposition(xy<double>); |
---|
[1888] | 243 | |
---|
[1505] | 244 | ///Activates an edge belonging to a BrokenEdge |
---|
[1888] | 245 | |
---|
[1579] | 246 | ///After we have activated an edge this way, |
---|
| 247 | ///the GDC object will know, which edge is under forming |
---|
[1888] | 248 | ///therefore it can redraw the necessary elements on the canvas, |
---|
[1579] | 249 | ///for example the text belonging to the \ref BrokenEdge can be |
---|
| 250 | ///redrawn (\ref textReposition). |
---|
[1524] | 251 | void toggleEdgeActivity(BrokenEdge*, bool); |
---|
[1505] | 252 | |
---|
| 253 | public: |
---|
[1888] | 254 | |
---|
| 255 | ///Returns the actual tool in hand |
---|
[1524] | 256 | int getActualTool(); |
---|
[1501] | 257 | |
---|
[1888] | 258 | ///draws the graph |
---|
| 259 | |
---|
| 260 | ///Called when opening a file. |
---|
[1606] | 261 | void drawGraph(); |
---|
[1888] | 262 | |
---|
| 263 | ///Clears the canvas |
---|
| 264 | |
---|
| 265 | ///It achieves this by deleting all data |
---|
| 266 | ///structure used to help handle the displayed graph. |
---|
[1606] | 267 | void clear(); |
---|
| 268 | |
---|
[1585] | 269 | ///creates a new Nodemap |
---|
[1888] | 270 | |
---|
| 271 | ///\param init initial value of the map |
---|
| 272 | ///\param mapname name of new map |
---|
| 273 | int addNewNodeMap(double init,std::string mapname); |
---|
[1585] | 274 | ///creates a new Edgemap |
---|
[1888] | 275 | |
---|
| 276 | ///\param init initial value of the map |
---|
| 277 | ///\param mapname name of new map |
---|
| 278 | int addNewEdgeMap(double init,std::string mapname); |
---|
[1585] | 279 | |
---|
[1501] | 280 | private: |
---|
[1486] | 281 | ///Deletes the given element. |
---|
[1643] | 282 | void deleteItem(Node); |
---|
[1486] | 283 | ///Deletes the given element. |
---|
[1643] | 284 | void deleteItem(Edge); |
---|
[1468] | 285 | |
---|
[1501] | 286 | private: |
---|
| 287 | |
---|
[1442] | 288 | ///Map of nodes of graph |
---|
| 289 | Graph::NodeMap<Gnome::Canvas::Ellipse *> nodesmap; |
---|
| 290 | |
---|
| 291 | ///Map of edges of graph |
---|
[1499] | 292 | Graph::EdgeMap<BrokenEdge *> edgesmap; |
---|
[1442] | 293 | |
---|
| 294 | ///Map of texts to write on edges |
---|
| 295 | Graph::EdgeMap<Gnome::Canvas::Text *> edgetextmap; |
---|
| 296 | |
---|
[1512] | 297 | ///Map of texts to write on nodes |
---|
| 298 | Graph::NodeMap<Gnome::Canvas::Text *> nodetextmap; |
---|
| 299 | |
---|
[1442] | 300 | ///Group of graphical elements of displayed_graph |
---|
| 301 | Gnome::Canvas::Group displayed_graph; |
---|
| 302 | |
---|
[1814] | 303 | private: |
---|
[1442] | 304 | ///Indicates whether the button of mouse is pressed or not |
---|
[1500] | 305 | int isbutton; |
---|
[1442] | 306 | |
---|
[1501] | 307 | ///Stores the actual tool in hand |
---|
| 308 | int actual_tool; |
---|
| 309 | |
---|
[1442] | 310 | ///At this location was the mousebutton pressed. |
---|
| 311 | ///It helps to calculate the distance of dragging. |
---|
| 312 | double clicked_x, clicked_y; |
---|
| 313 | |
---|
| 314 | ///Remembers which Gnome::Canvas::Item was pressed. |
---|
[1888] | 315 | |
---|
| 316 | ///this variable is needed, to work on it after selection |
---|
| 317 | Gnome::Canvas::Item * active_item; |
---|
| 318 | |
---|
| 319 | ///Remembers which Gnome::Canvas::Item was pressed. |
---|
| 320 | |
---|
| 321 | ///this variable is used at edge creation, it will |
---|
| 322 | ///be the secondly selected node. No local variable |
---|
| 323 | ///can be used for this purpose inside the function, |
---|
| 324 | ///because the node selected by button press, and |
---|
| 325 | ///the edge is created by button release. Both of |
---|
| 326 | ///them is different function call. |
---|
| 327 | Gnome::Canvas::Item * target_item; |
---|
| 328 | |
---|
| 329 | ///selected node (for any editing) |
---|
[1643] | 330 | Node active_node; |
---|
[1888] | 331 | |
---|
| 332 | ///selected edge (for any editing) |
---|
[1643] | 333 | Edge active_edge; |
---|
[1888] | 334 | |
---|
| 335 | ///the edge that is selected by clicking on the red arrow in the middle of it |
---|
| 336 | |
---|
| 337 | ///This edge is stored only for the purpose of reshape it. |
---|
| 338 | ///That is why it is selected in a different manner. |
---|
[1643] | 339 | Edge forming_edge; |
---|
[1579] | 340 | |
---|
[1888] | 341 | ///Map displayed by label can be edited. |
---|
| 342 | std::string nodemap_to_edit; |
---|
| 343 | |
---|
| 344 | ///Map displayed by label can be edited. |
---|
| 345 | std::string edgemap_to_edit; |
---|
[1442] | 346 | |
---|
| 347 | static const int zoom_step = 5; |
---|
[1499] | 348 | |
---|
[1814] | 349 | private: |
---|
| 350 | |
---|
[1888] | 351 | ///reference to the container, in which the canvas is |
---|
[1849] | 352 | NoteBookTab & mytab; |
---|
[1614] | 353 | |
---|
[1936] | 354 | XY calcArrowPos(XY, XY, XY, XY, bool); |
---|
[1442] | 355 | }; |
---|
| 356 | |
---|
| 357 | #endif //GRAPH_DISPLAYER_CANVAS_H |
---|