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