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