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