COIN-OR::LEMON - Graph Library

source: glemon-0.x/graph_displayer_canvas.h @ 172:fc1e478697d3

Last change on this file since 172:fc1e478697d3 was 172:fc1e478697d3, checked in by Hegyi Péter, 17 years ago

Currently visualized map can be saved and loaded from file.

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