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