graph_displayer_canvas.h
author ladanyi
Wed, 02 Jan 2008 21:03:09 +0000
changeset 201 879e47e5b731
parent 187 b465e2c34f23
permissions -rw-r--r--
Merge branches/akos to trunk.
     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 NoteBookTab;
    23 
    24 #include "all_include.h"
    25 #include <libgnomecanvasmm.h>
    26 #include <libgnomecanvasmm/polygon.h>
    27 
    28 ///This class is the canvas, on which the graph can be drawn.
    29 class GraphDisplayerCanvas : public Gnome::Canvas::CanvasAA
    30 {
    31   friend class BrokenEdge;
    32   friend class LoopEdge;
    33 
    34   class EdgeBase : public Gnome::Canvas::Group
    35   {
    36     protected:
    37       ///Reference to the canvas, on which the graph is drawn.
    38 
    39       ///It is needed, because some datas needed from
    40       ///graph can be accessed by this or should be sent
    41       ///as parameter, but it would be complicated
    42       GraphDisplayerCanvas& canvas;
    43 
    44       ///The edge that the class displays.
    45 
    46       ///It is needed, because some datas needed from
    47       ///graph can be accessed by this or should be sent
    48       ///as parameter, but it would be complicated
    49       Edge edge;
    50 
    51       Gnome::Canvas::Polygon arrow;
    52 
    53       void drawArrow(XY);
    54     public:
    55       EdgeBase(Gnome::Canvas::Group&, Edge, GraphDisplayerCanvas&);
    56       virtual ~EdgeBase();
    57       virtual void draw() = 0;
    58       virtual void setLineWidth(int) = 0;
    59       virtual void setFillColor(Gdk::Color) = 0;
    60       virtual Gnome::Canvas::Item * getLine() = 0;
    61   };
    62 
    63   ///Edge displayer class
    64 
    65   ///This class is responsible for displaying edges in graph.
    66   ///The displayed edge is broken in the middle. The
    67   ///aim of this is to be able to indicate direction of edges
    68   ///and to be able to display more then one edges between the
    69   ///same source and target
    70   class BrokenEdge : public EdgeBase
    71   {
    72     private:
    73       Gnome::Canvas::Line line;
    74 
    75       ///Indicates whether the button of mouse is pressed or not at the moment.
    76       bool isbutton;
    77 
    78       ///At this location was the mousebutton pressed. Horizontal component.
    79 
    80       ///It helps to calculate the
    81       ///distance of dragging.
    82       double clicked_x;
    83 
    84       ///At this location was the mousebutton pressed. Vertical component.
    85 
    86       ///It helps to calculate the
    87       ///distance of dragging.
    88       double clicked_y;
    89 
    90       ///event handler for forming broken edges
    91 
    92       ///\param event the
    93       ///event to handle
    94       bool edgeFormerEventHandler(GdkEvent* event);
    95 
    96     public:
    97       ///Constructor of broken edge class.
    98 
    99       ///\param g the group to which the edge belongs
   100       ///\param _edge the represented edge
   101       ///\param gc the canvas
   102       BrokenEdge(Gnome::Canvas::Group&, Edge, GraphDisplayerCanvas&);
   103 
   104       ///Destructor of broken edge class
   105 
   106       ///Frees up
   107       ///reserved memory
   108       ~BrokenEdge();
   109 
   110       ///The function that draws the edge based on collected data
   111       void draw();
   112 
   113       void setLineWidth(int);
   114       void setFillColor(Gdk::Color);
   115 
   116       Gnome::Canvas::Item * getLine() { return (Gnome::Canvas::Item *)(&line); };
   117   };
   118 
   119   class LoopEdge : public EdgeBase
   120   {
   121     private:
   122       Gnome::Canvas::Ellipse line;
   123       bool edgeFormerEventHandler(GdkEvent* e);
   124       bool isbutton;
   125     public:
   126       LoopEdge(Gnome::Canvas::Group&, Edge, GraphDisplayerCanvas&);
   127       ~LoopEdge();
   128       void draw();
   129       void setLineWidth(int);
   130       void setFillColor(Gdk::Color);
   131       Gnome::Canvas::Item * getLine() { return (Gnome::Canvas::Item *)(&line); };
   132   };
   133 
   134   ///Type of canvas, on which the graph is drawn
   135   typedef Gnome::Canvas::CanvasAA Parent;
   136 
   137 public:
   138   ///Constructor
   139 
   140   ///\param nbt the tab of the window, in which the graph is displayed
   141   GraphDisplayerCanvas(NoteBookTab & nbt);
   142 
   143   ///destructor of the class
   144   virtual ~GraphDisplayerCanvas();
   145 
   146   ///Returns a color of the rainbow based on a map value and the min and max value of the given map
   147 
   148   ///min and max is purple, between them there is a linear assign
   149   Gdk::Color rainbowColorCounter(double, double, double);
   150 
   151   ///Changes the width of edge(s) according to the given map.
   152 
   153   ///\param mapname is the name of the map which contains the values to be set
   154   ///\param edge if it is given, only the width of the given edge will be set, instead of all of them.
   155   int changeEdgeWidth (std::string mapname, Edge edge=INVALID);
   156 
   157   ///Resets width of edge(s) to the default value
   158 
   159   ///\param edge if it is given, only the width of the
   160   ///given edge will be reset, instead of all of them.
   161   int resetEdgeWidth (Edge edge=INVALID);
   162 
   163   ///Changes the color of edge(s) according to the given map.
   164 
   165   ///\param mapname is the name of the map which contains the new values
   166   ///\param edge if it is given, only the color of the given edge will be set, instead of all of them.
   167   int changeEdgeColor (std::string mapname, Edge edge=INVALID);
   168 
   169   ///Resets color of edge(s) to the default value
   170 
   171   ///\param edge if it is given, only the color of the
   172   ///given edge will be reset, instead of all of them.
   173   int resetEdgeColor (Edge edge=INVALID);
   174 
   175   ///Changes the label of edge(s) according to the given map.
   176 
   177   ///\param mapname is the name of the map which contains the new values
   178   ///\param edge if it is given, only the label of the given edge will be set, instead of all of them.
   179   int changeEdgeText (std::string mapname, Edge edge=INVALID);
   180 
   181   ///Resets label of edge(s) to the default value
   182 
   183   ///\param edge if it is given, only the color of the
   184   ///given edge will be reset, instead of all of them.
   185   int resetEdgeText (Edge edge=INVALID);
   186 
   187   ///Changes the radius of node(s) according to the given map.
   188 
   189   ///\param mapname is the name of the map which contains the new values
   190   ///\param node if it is given, only the radius of the given node will be set, instead of all of them.
   191   int changeNodeRadius (std::string mapname, Node node=INVALID);
   192 
   193   ///Resets radius of node(s) to the default value
   194 
   195   ///\param node if it is given, only the radius of the
   196   ///given node will be reset, instead of all of them.
   197   int resetNodeRadius (Node node=INVALID);
   198 
   199   ///Changes the color of node(s) according to the given map.
   200 
   201   ///\param mapname is the name of the map which contains the new values
   202   ///\param node if it is given, only the color of the given node will be set, instead of all of them.
   203   int changeNodeColor (std::string mapname, Node node=INVALID);
   204 
   205   ///Resets color of node(s) to the default value
   206 
   207   ///\param node if it is given, only the color of the
   208   ///given node will be reset, instead of all of them.
   209   int resetNodeColor (Node node=INVALID);
   210 
   211   ///Changes the label of node(s) according to the given map.
   212 
   213   ///\param mapname is the name of the map which contains the new values
   214   ///\param node if it is given, only the label of the given node will be set, instead of all of them.
   215   int changeNodeText (std::string mapname, Node node=INVALID);
   216 
   217   ///Resets label of node(s) to the default value
   218 
   219   ///\param node if it is given, only the label of the
   220   ///given node will be reset, instead of all of them.
   221   int resetNodeText (Node node=INVALID);
   222 
   223   ///This function is called, when any of the displayed attributes have to be updated, or changed
   224 
   225   ///\param itisedge if true, edge property has to be changed, else node property
   226   ///\param prop the id of property that has to changed or updated
   227   void propertyChange(bool itisedge, int prop);
   228 
   229   ///updates the given property
   230 
   231   ///\param edge if it is not INVALID, only the property of the given edge will be updated, instead of all of them
   232   ///\param prop the property to update
   233   void propertyUpdate(Edge edge, int prop);
   234 
   235   ///updates the given property
   236 
   237   ///\param node if it is not INVALID, only the property of the given node will be updated, instead of all of them
   238   ///\param prop the property to update
   239   void propertyUpdate(Node node, int prop);
   240 
   241   ///updates all the property for the given edge
   242   void propertyUpdate(Edge);
   243 
   244   ///updates all the property for the given node
   245   void propertyUpdate(Node);
   246 
   247   ///Callback for 'ViewZoomIn' action.
   248   virtual void zoomIn();
   249   ///Callback for 'ViewZoomOut' action.
   250   virtual void zoomOut();
   251   ///Callback for 'ViewZoomFit' action.
   252   virtual void zoomFit();
   253   ///Callback for 'ViewZoom100' action.
   254   virtual void zoom100();
   255   ///Sets the scroll region of the convas to the bounding box of the graph.
   256   void updateScrollRegion();
   257 
   258   ///This function changes the tool in the graph-editor's hand
   259   void changeEditorialTool(int);
   260 
   261 protected:
   262 
   263   //maximizing, minimizing, restoring window, etc.
   264   virtual bool on_expose_event(GdkEventExpose *);
   265 
   266 private:
   267 
   268   ///This function is responsible for the correct
   269   ///reaction of any action happened in the territory
   270   ///of the canvas
   271   ///DEPRECATED!!!!
   272   bool eventHandler(GdkEvent* e, Node n);
   273 
   274   ///actual event handler
   275   ///
   276   ///Actual event handler should be stored, to be able to disconnect it and later reconnect it.
   277   sigc::connection actual_handler;
   278 
   279   ///event handler for the case when move-tool is active
   280   bool moveEventHandler(GdkEvent*);
   281   ///event handler for the case when create_node-tool is active
   282   bool createNodeEventHandler(GdkEvent*);
   283   ///event handler for the case when create_edge-tool is active
   284   bool createEdgeEventHandler(GdkEvent*);
   285   ///event handler for the case when eraser-tool is active
   286   bool eraserEventHandler(GdkEvent*);
   287   ///event handler for the case when map editor tool is active
   288   bool mapEditEventHandler(GdkEvent*);
   289   ///event handler for the case when user scrolls the mouse
   290   bool scrollEventHandler(GdkEvent*);
   291 
   292 private:
   293   ///moves node according to the given parameters
   294   void moveNode(double, double,  Gnome::Canvas::Item * item=NULL, Node node=INVALID);
   295 
   296 public:
   297   ///Moves the text to new place
   298   void textReposition(XY);
   299 
   300   ///Activates an edge belonging to an EdgeBase
   301 
   302   ///After we have activated an edge this way,
   303   ///the GDC object will know, which edge is under forming
   304   ///therefore it can redraw the necessary elements on the canvas,
   305   ///for example the text belonging to the \ref EdgeBase can be
   306   ///redrawn (\ref textReposition).
   307   void toggleEdgeActivity(EdgeBase*, bool);
   308 
   309 public:
   310 
   311   ///Returns the actual tool in hand
   312   int getActualTool();
   313 
   314   ///Sets node representation settings
   315   void setView(bool, bool, double, double);
   316 
   317   ///Gets node representation settings
   318   void getView(bool &, bool &, double&, double&);
   319 
   320   ///draws the graph
   321 
   322   ///Called when opening a file.
   323   void drawGraph();
   324 
   325   ///Clears the canvas
   326 
   327   ///It achieves this by deleting all data
   328   ///structure used to help handle the displayed graph.
   329   void clear();
   330 
   331   ///creates a new Nodemap
   332 
   333   ///\param init initial value of the map
   334   ///\param mapname name of new map
   335   int addNewNodeMap(double init,std::string mapname);
   336   ///creates a new Edgemap
   337 
   338   ///\param init initial value of the map
   339   ///\param mapname name of new map
   340   int addNewEdgeMap(double init,std::string mapname);
   341 
   342   void reDesignGraph();
   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   ///Was redesign run on this graph already?
   432   ///
   433   ///If not, the layout will be modified randomly
   434   ///to avoid frozen layout because of wrong
   435   ///initial state
   436   bool was_redesigned;
   437   
   438 private:
   439 
   440   ///reference to the container, in which the canvas is
   441   NoteBookTab & mytab;
   442 
   443   XY calcArrowPos(XY, XY, XY, XY, int);
   444 
   445   bool background_set;
   446   Glib::RefPtr<Gdk::Pixbuf> refBackground;
   447   Gnome::Canvas::Pixbuf *background;
   448 public:
   449   void setBackground();
   450 };
   451 
   452 #endif //GRAPH_DISPLAYER_CANVAS_H