graph_displayer_canvas.h
author hegyi
Mon, 02 Oct 2006 18:52:00 +0000
changeset 157 7e6ad28aeb9e
parent 156 c5cdf6690cdf
child 160 14a76109b561
permissions -rw-r--r--
View settings also for edges.
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@21
   271
public:
hegyi@25
   272
  ///Moves the text to new place
hegyi@150
   273
  void textReposition(XY);
hegyi@118
   274
ladanyi@147
   275
  ///Activates an edge belonging to an EdgeBase
hegyi@118
   276
hegyi@35
   277
  ///After we have activated an edge this way,
hegyi@35
   278
  ///the GDC object will know, which edge is under forming
hegyi@118
   279
  ///therefore it can redraw the necessary elements on the canvas,
ladanyi@147
   280
  ///for example the text belonging to the \ref EdgeBase can be
hegyi@35
   281
  ///redrawn (\ref textReposition).
ladanyi@147
   282
  void toggleEdgeActivity(EdgeBase*, bool);
hegyi@25
   283
hegyi@25
   284
public:
hegyi@118
   285
hegyi@118
   286
  ///Returns the actual tool in hand
hegyi@30
   287
  int getActualTool();
hegyi@21
   288
hegyi@154
   289
  ///Sets node representation settings
hegyi@157
   290
  void setView(bool, bool, double, double);
hegyi@154
   291
hegyi@154
   292
  ///Gets node representation settings
hegyi@157
   293
  void getView(bool &, bool &, double&, double&);
hegyi@154
   294
hegyi@118
   295
  ///draws the graph
hegyi@118
   296
hegyi@118
   297
  ///Called when opening a file.
ladanyi@53
   298
  void drawGraph();
hegyi@118
   299
hegyi@118
   300
  ///Clears the canvas
hegyi@118
   301
hegyi@118
   302
  ///It achieves this by deleting all data
hegyi@118
   303
  ///structure used to help handle the displayed graph.
ladanyi@53
   304
  void clear();
ladanyi@53
   305
hegyi@37
   306
  ///creates a new Nodemap
hegyi@118
   307
hegyi@118
   308
  ///\param init initial value of the map
hegyi@118
   309
  ///\param mapname name of new map
hegyi@118
   310
  int addNewNodeMap(double init,std::string mapname);
hegyi@37
   311
  ///creates a new Edgemap
hegyi@118
   312
hegyi@118
   313
  ///\param init initial value of the map
hegyi@118
   314
  ///\param mapname name of new map
hegyi@118
   315
  int addNewEdgeMap(double init,std::string mapname);
hegyi@37
   316
hegyi@21
   317
private:
hegyi@14
   318
  ///Deletes the given element.
alpar@62
   319
  void deleteItem(Node);
hegyi@14
   320
  ///Deletes the given element.
alpar@62
   321
  void deleteItem(Edge);
hegyi@9
   322
hegyi@21
   323
private:
hegyi@21
   324
ladanyi@6
   325
  ///Map of nodes of graph
ladanyi@6
   326
  Graph::NodeMap<Gnome::Canvas::Ellipse *> nodesmap;
ladanyi@6
   327
ladanyi@6
   328
  ///Map of edges of graph
ladanyi@147
   329
  Graph::EdgeMap<EdgeBase*> edgesmap;
ladanyi@6
   330
ladanyi@6
   331
  ///Map of texts to write on edges
ladanyi@6
   332
  Graph::EdgeMap<Gnome::Canvas::Text *> edgetextmap;
ladanyi@6
   333
hegyi@28
   334
  ///Map of texts to write on nodes
hegyi@28
   335
  Graph::NodeMap<Gnome::Canvas::Text *> nodetextmap;
hegyi@28
   336
ladanyi@6
   337
  ///Group of graphical elements of displayed_graph
ladanyi@6
   338
  Gnome::Canvas::Group displayed_graph;
ladanyi@6
   339
hegyi@88
   340
private:
ladanyi@6
   341
  ///Indicates whether the button of mouse is pressed or not
hegyi@20
   342
  int isbutton;
ladanyi@6
   343
hegyi@21
   344
  ///Stores the actual tool in hand
hegyi@21
   345
  int actual_tool;
hegyi@21
   346
ladanyi@6
   347
  ///At this location was the mousebutton pressed.
ladanyi@6
   348
  ///It helps to calculate the distance of dragging.
ladanyi@6
   349
  double clicked_x, clicked_y;
ladanyi@6
   350
ladanyi@6
   351
  ///Remembers which Gnome::Canvas::Item was pressed.
hegyi@118
   352
hegyi@118
   353
  ///this variable is needed, to work on it after selection
hegyi@118
   354
  Gnome::Canvas::Item * active_item;
hegyi@118
   355
hegyi@118
   356
  ///Remembers which Gnome::Canvas::Item was pressed.
hegyi@118
   357
hegyi@118
   358
  ///this variable is used at edge creation, it will
hegyi@118
   359
  ///be the secondly selected node. No local variable
hegyi@118
   360
  ///can be used for this purpose inside the function,
hegyi@118
   361
  ///because the node selected by button press, and
hegyi@118
   362
  ///the edge is created by button release. Both of
hegyi@118
   363
  ///them is different function call.
hegyi@118
   364
  Gnome::Canvas::Item * target_item;
hegyi@118
   365
hegyi@118
   366
  ///selected node (for any editing)
alpar@62
   367
  Node active_node;
hegyi@118
   368
hegyi@118
   369
  ///selected edge (for any editing)
alpar@62
   370
  Edge active_edge;
hegyi@118
   371
hegyi@118
   372
  ///the edge that is selected by clicking on the red arrow in the middle of it
hegyi@118
   373
hegyi@118
   374
  ///This edge is stored only for the purpose of reshape it.
hegyi@118
   375
  ///That is why it is selected in a different manner.
alpar@62
   376
  Edge forming_edge;
hegyi@35
   377
hegyi@118
   378
  ///Map displayed by label can be edited.
hegyi@118
   379
  std::string nodemap_to_edit;
hegyi@118
   380
hegyi@118
   381
  ///Map displayed by label can be edited.
hegyi@118
   382
  std::string edgemap_to_edit;
ladanyi@6
   383
ladanyi@6
   384
  static const int zoom_step = 5;
hegyi@19
   385
hegyi@154
   386
  ///Is node radius autoscaled
hegyi@154
   387
  bool autoscale;
hegyi@154
   388
  
hegyi@156
   389
  ///Should we track zoomfactor changes
hegyi@156
   390
  bool zoomtrack;
hegyi@156
   391
hegyi@156
   392
  ///to store the zoom factor when it was "fixed"
hegyi@156
   393
  double fixed_zoom_factor;
hegyi@156
   394
  
hegyi@157
   395
  ///Node radius size
hegyi@157
   396
  double radius_size;
hegyi@154
   397
hegyi@157
   398
  ///Edge width
hegyi@157
   399
  double edge_width;
hegyi@154
   400
hegyi@88
   401
private:
hegyi@88
   402
hegyi@118
   403
  ///reference to the container, in which the canvas is
hegyi@96
   404
  NoteBookTab & mytab;
hegyi@55
   405
hegyi@148
   406
  XY calcArrowPos(XY, XY, XY, XY, int);
ladanyi@6
   407
};
ladanyi@6
   408
ladanyi@6
   409
#endif //GRAPH_DISPLAYER_CANVAS_H