graph_displayer_canvas.h
author Peter Hegyi <hegyi@tmit.bme.hu>
Wed, 13 Aug 2008 17:24:25 +0100
changeset 6 3a44a2bb6da8
permissions -rw-r--r--
Remove lemon/graph_utils.h include.
     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 digraph can be drawn.
    29 class DigraphDisplayerCanvas : public Gnome::Canvas::CanvasAA
    30 {
    31   friend class BrokenArc;
    32   friend class LoopArc;
    33 
    34   class ArcBase : public Gnome::Canvas::Group
    35   {
    36     protected:
    37       ///Reference to the canvas, on which the digraph is drawn.
    38 
    39       ///It is needed, because some datas needed from
    40       ///digraph can be accessed by this or should be sent
    41       ///as parameter, but it would be complicated
    42       DigraphDisplayerCanvas& canvas;
    43 
    44       ///The arc that the class displays.
    45 
    46       ///It is needed, because some datas needed from
    47       ///digraph can be accessed by this or should be sent
    48       ///as parameter, but it would be complicated
    49       Arc arc;
    50 
    51       Gnome::Canvas::Polygon arrow;
    52 
    53       void drawArrow(XY);
    54     public:
    55       ArcBase(Gnome::Canvas::Group&, Arc, DigraphDisplayerCanvas&);
    56       virtual ~ArcBase();
    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   ///Arc displayer class
    64 
    65   ///This class is responsible for displaying arcs in digraph.
    66   ///The displayed arc is broken in the middle. The
    67   ///aim of this is to be able to indicate direction of arcs
    68   ///and to be able to display more then one arcs between the
    69   ///same source and target
    70   class BrokenArc : public ArcBase
    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 arcs
    91 
    92       ///\param event the
    93       ///event to handle
    94       bool arcFormerEventHandler(GdkEvent* event);
    95 
    96     public:
    97       ///Constructor of broken arc class.
    98 
    99       ///\param g the group to which the arc belongs
   100       ///\param _arc the represented arc
   101       ///\param gc the canvas
   102       BrokenArc(Gnome::Canvas::Group&, Arc, DigraphDisplayerCanvas&);
   103 
   104       ///Destructor of broken arc class
   105 
   106       ///Frees up
   107       ///reserved memory
   108       ~BrokenArc();
   109 
   110       ///The function that draws the arc 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 LoopArc : public ArcBase
   120   {
   121     private:
   122       Gnome::Canvas::Ellipse line;
   123       bool arcFormerEventHandler(GdkEvent* e);
   124       bool isbutton;
   125     public:
   126       LoopArc(Gnome::Canvas::Group&, Arc, DigraphDisplayerCanvas&);
   127       ~LoopArc();
   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 digraph 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 digraph is displayed
   141   DigraphDisplayerCanvas(NoteBookTab & nbt);
   142 
   143   ///destructor of the class
   144   virtual ~DigraphDisplayerCanvas();
   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 arc(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 arc if it is given, only the width of the given arc will be set, instead of all of them.
   155   int changeArcWidth (std::string mapname, Arc arc=INVALID);
   156 
   157   ///Resets width of arc(s) to the default value
   158 
   159   ///\param arc if it is given, only the width of the
   160   ///given arc will be reset, instead of all of them.
   161   int resetArcWidth (Arc arc=INVALID);
   162 
   163   ///Changes the color of arc(s) according to the given map.
   164 
   165   ///\param mapname is the name of the map which contains the new values
   166   ///\param arc if it is given, only the color of the given arc will be set, instead of all of them.
   167   int changeArcColor (std::string mapname, Arc arc=INVALID);
   168 
   169   ///Resets color of arc(s) to the default value
   170 
   171   ///\param arc if it is given, only the color of the
   172   ///given arc will be reset, instead of all of them.
   173   int resetArcColor (Arc arc=INVALID);
   174 
   175   ///Changes the label of arc(s) according to the given map.
   176 
   177   ///\param mapname is the name of the map which contains the new values
   178   ///\param arc if it is given, only the label of the given arc will be set, instead of all of them.
   179   int changeArcText (std::string mapname, Arc arc=INVALID);
   180 
   181   ///Resets label of arc(s) to the default value
   182 
   183   ///\param arc if it is given, only the color of the
   184   ///given arc will be reset, instead of all of them.
   185   int resetArcText (Arc arc=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 itisarc if true, arc 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 itisarc, int prop);
   228 
   229   ///updates the given property
   230 
   231   ///\param arc if it is not INVALID, only the property of the given arc will be updated, instead of all of them
   232   ///\param prop the property to update
   233   void propertyUpdate(Arc arc, 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 arc
   242   void propertyUpdate(Arc);
   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 digraph.
   256   void updateScrollRegion();
   257 
   258   ///This function changes the tool in the digraph-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_arc-tool is active
   284   bool createArcEventHandler(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 arc belonging to an ArcBase
   301 
   302   ///After we have activated an arc this way,
   303   ///the GDC object will know, which arc is under forming
   304   ///therefore it can redraw the necessary elements on the canvas,
   305   ///for example the text belonging to the \ref ArcBase can be
   306   ///redrawn (\ref textReposition).
   307   void toggleArcActivity(ArcBase*, 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 digraph
   321 
   322   ///Called when opening a file.
   323   void drawDigraph();
   324 
   325   ///Clears the canvas
   326 
   327   ///It achieves this by deleting all data
   328   ///structure used to help handle the displayed digraph.
   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 Arcmap
   337 
   338   ///\param init initial value of the map
   339   ///\param mapname name of new map
   340   int addNewArcMap(double init,std::string mapname);
   341 
   342   void reDesignDigraph();
   343 
   344   ///Show whether the digraph 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(Arc);
   352 
   353 private:
   354 
   355   ///Map of nodes of digraph
   356   Digraph::NodeMap<Gnome::Canvas::Ellipse *> nodesmap;
   357 
   358   ///Map of arcs of digraph
   359   Digraph::ArcMap<ArcBase*> arcsmap;
   360 
   361   ///Map of texts to write on arcs
   362   Digraph::ArcMap<Gnome::Canvas::Text *> arctextmap;
   363 
   364   ///Map of texts to write on nodes
   365   Digraph::NodeMap<Gnome::Canvas::Text *> nodetextmap;
   366 
   367   ///Group of digraphical 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 arc 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 arc 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 arc (for any editing)
   400   Arc active_arc;
   401 
   402   ///the arc that is selected by clicking on the red arrow in the middle of it
   403 
   404   ///This arc is stored only for the purpose of reshape it.
   405   ///That is why it is selected in a different manner.
   406   Arc forming_arc;
   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 arcmap_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   ///Arc width
   429   double arc_width;
   430 
   431   ///Was redesign run on this digraph 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