COIN-OR::LEMON - Graph Library

source: glemon-0.x/graph_displayer_canvas.h @ 194:6b2b718420eb

Last change on this file since 194:6b2b718420eb was 194:6b2b718420eb, checked in by Hegyi Péter, 17 years ago

Header reorganising

File size: 13.7 KB
RevLine 
[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
[194]22class NoteBookTab;
[21]23
[53]24#include "all_include.h"
[6]25#include <libgnomecanvasmm.h>
26#include <libgnomecanvasmm/polygon.h>
27
28///This class is the canvas, on which the graph can be drawn.
29class GraphDisplayerCanvas : public Gnome::Canvas::CanvasAA
30{
[98]31  friend class BrokenEdge;
[147]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;
[149]60      virtual Gnome::Canvas::Item * getLine() = 0;
[147]61  };
[98]62
[118]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
[147]70  class BrokenEdge : public EdgeBase
[89]71  {
[147]72    private:
73      Gnome::Canvas::Line line;
[118]74
[147]75      ///Indicates whether the button of mouse is pressed or not at the moment.
76      bool isbutton;
[118]77
[147]78      ///At this location was the mousebutton pressed. Horizontal component.
[118]79
[147]80      ///It helps to calculate the
81      ///distance of dragging.
82      double clicked_x;
[118]83
[147]84      ///At this location was the mousebutton pressed. Vertical component.
[118]85
[147]86      ///It helps to calculate the
87      ///distance of dragging.
88      double clicked_y;
[89]89
[147]90      ///event handler for forming broken edges
[89]91
[147]92      ///\param event the
93      ///event to handle
94      bool edgeFormerEventHandler(GdkEvent* event);
[89]95
[147]96    public:
97      ///Constructor of broken edge class.
[118]98
[147]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&);
[118]103
[147]104      ///Destructor of broken edge class
[118]105
[147]106      ///Frees up
107      ///reserved memory
108      ~BrokenEdge();
[118]109
[147]110      ///The function that draws the edge based on collected data
111      void draw();
[118]112
[147]113      void setLineWidth(int);
114      void setFillColor(Gdk::Color);
[149]115
116      Gnome::Canvas::Item * getLine() { return (Gnome::Canvas::Item *)(&line); };
[147]117  };
[118]118
[147]119  class LoopEdge : public EdgeBase
120  {
121    private:
122      Gnome::Canvas::Ellipse line;
[151]123      bool edgeFormerEventHandler(GdkEvent* e);
124      bool isbutton;
[147]125    public:
126      LoopEdge(Gnome::Canvas::Group&, Edge, GraphDisplayerCanvas&);
127      ~LoopEdge();
128      void draw();
129      void setLineWidth(int);
130      void setFillColor(Gdk::Color);
[149]131      Gnome::Canvas::Item * getLine() { return (Gnome::Canvas::Item *)(&line); };
[89]132  };
[118]133
134  ///Type of canvas, on which the graph is drawn
[6]135  typedef Gnome::Canvas::CanvasAA Parent;
136
137public:
[118]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
[6]144  virtual ~GraphDisplayerCanvas();
145
[179]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
[118]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
[6]165  ///\param mapname is the name of the map which contains the new values
[118]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);
[6]168
[118]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
[6]177  ///\param mapname is the name of the map which contains the new values
[118]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);
[6]180
[118]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
[6]189  ///\param mapname is the name of the map which contains the new values
[118]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);
[28]192
[118]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
[28]201  ///\param mapname is the name of the map which contains the new values
[118]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);
[28]204
[118]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
[28]213  ///\param mapname is the name of the map which contains the new values
[118]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);
[28]216
[118]217  ///Resets label of node(s) to the default value
[6]218
[118]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);
[94]222
[118]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
[94]242  void propertyUpdate(Edge);
[118]243
244  ///updates all the property for the given node
[94]245  void propertyUpdate(Node);
246
[6]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
[9]258  ///This function changes the tool in the graph-editor's hand
259  void changeEditorialTool(int);
260
[6]261protected:
262
[118]263  //maximizing, minimizing, restoring window, etc.
[6]264  virtual bool on_expose_event(GdkEventExpose *);
265
266private:
267
268  ///This function is responsible for the correct
269  ///reaction of any action happened in the territory
270  ///of the canvas
[25]271  ///DEPRECATED!!!!
[30]272  bool eventHandler(GdkEvent* e, Node n);
[6]273
[9]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
[30]280  bool moveEventHandler(GdkEvent*);
[9]281  ///event handler for the case when create_node-tool is active
[30]282  bool createNodeEventHandler(GdkEvent*);
[9]283  ///event handler for the case when create_edge-tool is active
[30]284  bool createEdgeEventHandler(GdkEvent*);
[13]285  ///event handler for the case when eraser-tool is active
[30]286  bool eraserEventHandler(GdkEvent*);
[149]287  ///event handler for the case when map editor tool is active
288  bool mapEditEventHandler(GdkEvent*);
[187]289  ///event handler for the case when user scrolls the mouse
290  bool scrollEventHandler(GdkEvent*);
[13]291
[160]292private:
293  ///moves node according to the given parameters
294  void moveNode(double, double,  Gnome::Canvas::Item * item=NULL, Node node=INVALID);
295
[21]296public:
[25]297  ///Moves the text to new place
[150]298  void textReposition(XY);
[118]299
[147]300  ///Activates an edge belonging to an EdgeBase
[118]301
[35]302  ///After we have activated an edge this way,
303  ///the GDC object will know, which edge is under forming
[118]304  ///therefore it can redraw the necessary elements on the canvas,
[147]305  ///for example the text belonging to the \ref EdgeBase can be
[35]306  ///redrawn (\ref textReposition).
[147]307  void toggleEdgeActivity(EdgeBase*, bool);
[25]308
309public:
[118]310
311  ///Returns the actual tool in hand
[30]312  int getActualTool();
[21]313
[154]314  ///Sets node representation settings
[157]315  void setView(bool, bool, double, double);
[154]316
317  ///Gets node representation settings
[157]318  void getView(bool &, bool &, double&, double&);
[154]319
[118]320  ///draws the graph
321
322  ///Called when opening a file.
[53]323  void drawGraph();
[118]324
325  ///Clears the canvas
326
327  ///It achieves this by deleting all data
328  ///structure used to help handle the displayed graph.
[53]329  void clear();
330
[37]331  ///creates a new Nodemap
[118]332
333  ///\param init initial value of the map
334  ///\param mapname name of new map
335  int addNewNodeMap(double init,std::string mapname);
[37]336  ///creates a new Edgemap
[118]337
338  ///\param init initial value of the map
339  ///\param mapname name of new map
340  int addNewEdgeMap(double init,std::string mapname);
[37]341
[160]342  void reDesignGraph();
343
[172]344  ///Show whether the graph is already drawn.
345  bool is_drawn;
346
[21]347private:
[14]348  ///Deletes the given element.
[62]349  void deleteItem(Node);
[14]350  ///Deletes the given element.
[62]351  void deleteItem(Edge);
[9]352
[21]353private:
354
[6]355  ///Map of nodes of graph
356  Graph::NodeMap<Gnome::Canvas::Ellipse *> nodesmap;
357
358  ///Map of edges of graph
[147]359  Graph::EdgeMap<EdgeBase*> edgesmap;
[6]360
361  ///Map of texts to write on edges
362  Graph::EdgeMap<Gnome::Canvas::Text *> edgetextmap;
363
[28]364  ///Map of texts to write on nodes
365  Graph::NodeMap<Gnome::Canvas::Text *> nodetextmap;
366
[6]367  ///Group of graphical elements of displayed_graph
368  Gnome::Canvas::Group displayed_graph;
369
[88]370private:
[6]371  ///Indicates whether the button of mouse is pressed or not
[20]372  int isbutton;
[6]373
[21]374  ///Stores the actual tool in hand
375  int actual_tool;
376
[6]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.
[118]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)
[62]397  Node active_node;
[118]398
399  ///selected edge (for any editing)
[62]400  Edge active_edge;
[118]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.
[62]406  Edge forming_edge;
[35]407
[118]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;
[6]413
414  static const int zoom_step = 5;
[19]415
[154]416  ///Is node radius autoscaled
417  bool autoscale;
418 
[156]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 
[157]425  ///Node radius size
426  double radius_size;
[154]427
[157]428  ///Edge width
429  double edge_width;
[154]430
[166]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;
[160]437 
[88]438private:
439
[118]440  ///reference to the container, in which the canvas is
[96]441  NoteBookTab & mytab;
[55]442
[148]443  XY calcArrowPos(XY, XY, XY, XY, int);
[184]444
445  bool background_set;
446  Glib::RefPtr<Gdk::Pixbuf> refBackground;
447  Gnome::Canvas::Pixbuf *background;
448public:
449  void setBackground();
[6]450};
451
452#endif //GRAPH_DISPLAYER_CANVAS_H
Note: See TracBrowser for help on using the repository browser.