graph_displayer_canvas.h
author hegyi
Tue, 19 Sep 2006 07:43:55 +0000
changeset 149 930e838ad5b6
parent 148 5adf29662354
child 150 86273bfe0e4d
permissions -rw-r--r--
Node and edge editor button are the same furthermore.
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@89
    12
#include <lemon/xy.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@147
   109
    public:
ladanyi@147
   110
      LoopEdge(Gnome::Canvas::Group&, Edge, GraphDisplayerCanvas&);
ladanyi@147
   111
      ~LoopEdge();
ladanyi@147
   112
      void draw();
ladanyi@147
   113
      void setLineWidth(int);
ladanyi@147
   114
      void setFillColor(Gdk::Color);
hegyi@149
   115
      Gnome::Canvas::Item * getLine() { return (Gnome::Canvas::Item *)(&line); };
hegyi@89
   116
  };
hegyi@118
   117
hegyi@118
   118
  ///Type of canvas, on which the graph is drawn
ladanyi@6
   119
  typedef Gnome::Canvas::CanvasAA Parent;
ladanyi@6
   120
ladanyi@6
   121
public:
hegyi@118
   122
  ///Constructor
hegyi@118
   123
hegyi@118
   124
  ///\param nbt the tab of the window, in which the graph is displayed
hegyi@118
   125
  GraphDisplayerCanvas(NoteBookTab & nbt);
hegyi@118
   126
hegyi@118
   127
  ///destructor of the class
ladanyi@6
   128
  virtual ~GraphDisplayerCanvas();
ladanyi@6
   129
hegyi@118
   130
  ///Changes the width of edge(s) according to the given map.
hegyi@118
   131
hegyi@118
   132
  ///\param mapname is the name of the map which contains the values to be set
hegyi@118
   133
  ///\param edge if it is given, only the width of the given edge will be set, instead of all of them.
hegyi@118
   134
  int changeEdgeWidth (std::string mapname, Edge edge=INVALID);
hegyi@118
   135
hegyi@118
   136
  ///Resets width of edge(s) to the default value
hegyi@118
   137
hegyi@118
   138
  ///\param edge if it is given, only the width of the
hegyi@118
   139
  ///given edge will be reset, instead of all of them.
hegyi@118
   140
  int resetEdgeWidth (Edge edge=INVALID);
hegyi@118
   141
hegyi@118
   142
  ///Changes the color of edge(s) according to the given map.
hegyi@118
   143
ladanyi@6
   144
  ///\param mapname is the name of the map which contains the new values
hegyi@118
   145
  ///\param edge if it is given, only the color of the given edge will be set, instead of all of them.
hegyi@118
   146
  int changeEdgeColor (std::string mapname, Edge edge=INVALID);
ladanyi@6
   147
hegyi@118
   148
  ///Resets color of edge(s) to the default value
hegyi@118
   149
hegyi@118
   150
  ///\param edge if it is given, only the color of the
hegyi@118
   151
  ///given edge will be reset, instead of all of them.
hegyi@118
   152
  int resetEdgeColor (Edge edge=INVALID);
hegyi@118
   153
hegyi@118
   154
  ///Changes the label of edge(s) according to the given map.
hegyi@118
   155
ladanyi@6
   156
  ///\param mapname is the name of the map which contains the new values
hegyi@118
   157
  ///\param edge if it is given, only the label of the given edge will be set, instead of all of them.
hegyi@118
   158
  int changeEdgeText (std::string mapname, Edge edge=INVALID);
ladanyi@6
   159
hegyi@118
   160
  ///Resets label of edge(s) to the default value
hegyi@118
   161
hegyi@118
   162
  ///\param edge if it is given, only the color of the
hegyi@118
   163
  ///given edge will be reset, instead of all of them.
hegyi@118
   164
  int resetEdgeText (Edge edge=INVALID);
hegyi@118
   165
hegyi@118
   166
  ///Changes the radius of node(s) according to the given map.
hegyi@118
   167
ladanyi@6
   168
  ///\param mapname is the name of the map which contains the new values
hegyi@118
   169
  ///\param node if it is given, only the radius of the given node will be set, instead of all of them.
hegyi@118
   170
  int changeNodeRadius (std::string mapname, Node node=INVALID);
hegyi@28
   171
hegyi@118
   172
  ///Resets radius of node(s) to the default value
hegyi@118
   173
hegyi@118
   174
  ///\param node if it is given, only the radius of the
hegyi@118
   175
  ///given node will be reset, instead of all of them.
hegyi@118
   176
  int resetNodeRadius (Node node=INVALID);
hegyi@118
   177
hegyi@118
   178
  ///Changes the color of node(s) according to the given map.
hegyi@118
   179
hegyi@28
   180
  ///\param mapname is the name of the map which contains the new values
hegyi@118
   181
  ///\param node if it is given, only the color of the given node will be set, instead of all of them.
hegyi@118
   182
  int changeNodeColor (std::string mapname, Node node=INVALID);
hegyi@28
   183
hegyi@118
   184
  ///Resets color of node(s) to the default value
hegyi@118
   185
hegyi@118
   186
  ///\param node if it is given, only the color of the
hegyi@118
   187
  ///given node will be reset, instead of all of them.
hegyi@118
   188
  int resetNodeColor (Node node=INVALID);
hegyi@118
   189
hegyi@118
   190
  ///Changes the label of node(s) according to the given map.
hegyi@118
   191
hegyi@28
   192
  ///\param mapname is the name of the map which contains the new values
hegyi@118
   193
  ///\param node if it is given, only the label of the given node will be set, instead of all of them.
hegyi@118
   194
  int changeNodeText (std::string mapname, Node node=INVALID);
hegyi@28
   195
hegyi@118
   196
  ///Resets label of node(s) to the default value
ladanyi@6
   197
hegyi@118
   198
  ///\param node if it is given, only the label of the
hegyi@118
   199
  ///given node will be reset, instead of all of them.
hegyi@118
   200
  int resetNodeText (Node node=INVALID);
hegyi@94
   201
hegyi@118
   202
  ///This function is called, when any of the displayed attributes have to be updated, or changed
hegyi@118
   203
hegyi@118
   204
  ///\param itisedge if true, edge property has to be changed, else node property
hegyi@118
   205
  ///\param prop the id of property that has to changed or updated
hegyi@118
   206
  void propertyChange(bool itisedge, int prop);
hegyi@118
   207
hegyi@118
   208
  ///updates the given property
hegyi@118
   209
hegyi@118
   210
  ///\param edge if it is not INVALID, only the property of the given edge will be updated, instead of all of them
hegyi@118
   211
  ///\param prop the property to update
hegyi@118
   212
  void propertyUpdate(Edge edge, int prop);
hegyi@118
   213
hegyi@118
   214
  ///updates the given property
hegyi@118
   215
hegyi@118
   216
  ///\param node if it is not INVALID, only the property of the given node will be updated, instead of all of them
hegyi@118
   217
  ///\param prop the property to update
hegyi@118
   218
  void propertyUpdate(Node node, int prop);
hegyi@118
   219
hegyi@118
   220
  ///updates all the property for the given edge
hegyi@94
   221
  void propertyUpdate(Edge);
hegyi@118
   222
hegyi@118
   223
  ///updates all the property for the given node
hegyi@94
   224
  void propertyUpdate(Node);
hegyi@94
   225
ladanyi@6
   226
  ///Callback for 'ViewZoomIn' action.
ladanyi@6
   227
  virtual void zoomIn();
ladanyi@6
   228
  ///Callback for 'ViewZoomOut' action.
ladanyi@6
   229
  virtual void zoomOut();
ladanyi@6
   230
  ///Callback for 'ViewZoomFit' action.
ladanyi@6
   231
  virtual void zoomFit();
ladanyi@6
   232
  ///Callback for 'ViewZoom100' action.
ladanyi@6
   233
  virtual void zoom100();
ladanyi@6
   234
  ///Sets the scroll region of the convas to the bounding box of the graph.
ladanyi@6
   235
  void updateScrollRegion();
ladanyi@6
   236
hegyi@9
   237
  ///This function changes the tool in the graph-editor's hand
hegyi@9
   238
  void changeEditorialTool(int);
hegyi@9
   239
ladanyi@6
   240
protected:
ladanyi@6
   241
hegyi@118
   242
  //maximizing, minimizing, restoring window, etc.
ladanyi@6
   243
  virtual bool on_expose_event(GdkEventExpose *);
ladanyi@6
   244
ladanyi@6
   245
private:
ladanyi@6
   246
ladanyi@6
   247
  ///This function is responsible for the correct
ladanyi@6
   248
  ///reaction of any action happened in the territory
ladanyi@6
   249
  ///of the canvas
hegyi@25
   250
  ///DEPRECATED!!!!
hegyi@30
   251
  bool eventHandler(GdkEvent* e, Node n);
ladanyi@6
   252
hegyi@9
   253
  ///actual event handler
hegyi@9
   254
  ///
hegyi@9
   255
  ///Actual event handler should be stored, to be able to disconnect it and later reconnect it.
hegyi@9
   256
  sigc::connection actual_handler;
hegyi@9
   257
hegyi@9
   258
  ///event handler for the case when move-tool is active
hegyi@30
   259
  bool moveEventHandler(GdkEvent*);
hegyi@9
   260
  ///event handler for the case when create_node-tool is active
hegyi@30
   261
  bool createNodeEventHandler(GdkEvent*);
hegyi@9
   262
  ///event handler for the case when create_edge-tool is active
hegyi@30
   263
  bool createEdgeEventHandler(GdkEvent*);
hegyi@13
   264
  ///event handler for the case when eraser-tool is active
hegyi@30
   265
  bool eraserEventHandler(GdkEvent*);
hegyi@149
   266
  ///event handler for the case when map editor tool is active
hegyi@149
   267
  bool mapEditEventHandler(GdkEvent*);
hegyi@13
   268
hegyi@21
   269
public:
hegyi@25
   270
  ///Moves the text to new place
hegyi@30
   271
  void textReposition(xy<double>);
hegyi@118
   272
ladanyi@147
   273
  ///Activates an edge belonging to an EdgeBase
hegyi@118
   274
hegyi@35
   275
  ///After we have activated an edge this way,
hegyi@35
   276
  ///the GDC object will know, which edge is under forming
hegyi@118
   277
  ///therefore it can redraw the necessary elements on the canvas,
ladanyi@147
   278
  ///for example the text belonging to the \ref EdgeBase can be
hegyi@35
   279
  ///redrawn (\ref textReposition).
ladanyi@147
   280
  void toggleEdgeActivity(EdgeBase*, bool);
hegyi@25
   281
hegyi@25
   282
public:
hegyi@118
   283
hegyi@118
   284
  ///Returns the actual tool in hand
hegyi@30
   285
  int getActualTool();
hegyi@21
   286
hegyi@118
   287
  ///draws the graph
hegyi@118
   288
hegyi@118
   289
  ///Called when opening a file.
ladanyi@53
   290
  void drawGraph();
hegyi@118
   291
hegyi@118
   292
  ///Clears the canvas
hegyi@118
   293
hegyi@118
   294
  ///It achieves this by deleting all data
hegyi@118
   295
  ///structure used to help handle the displayed graph.
ladanyi@53
   296
  void clear();
ladanyi@53
   297
hegyi@37
   298
  ///creates a new Nodemap
hegyi@118
   299
hegyi@118
   300
  ///\param init initial value of the map
hegyi@118
   301
  ///\param mapname name of new map
hegyi@118
   302
  int addNewNodeMap(double init,std::string mapname);
hegyi@37
   303
  ///creates a new Edgemap
hegyi@118
   304
hegyi@118
   305
  ///\param init initial value of the map
hegyi@118
   306
  ///\param mapname name of new map
hegyi@118
   307
  int addNewEdgeMap(double init,std::string mapname);
hegyi@37
   308
hegyi@21
   309
private:
hegyi@14
   310
  ///Deletes the given element.
alpar@62
   311
  void deleteItem(Node);
hegyi@14
   312
  ///Deletes the given element.
alpar@62
   313
  void deleteItem(Edge);
hegyi@9
   314
hegyi@21
   315
private:
hegyi@21
   316
ladanyi@6
   317
  ///Map of nodes of graph
ladanyi@6
   318
  Graph::NodeMap<Gnome::Canvas::Ellipse *> nodesmap;
ladanyi@6
   319
ladanyi@6
   320
  ///Map of edges of graph
ladanyi@147
   321
  Graph::EdgeMap<EdgeBase*> edgesmap;
ladanyi@6
   322
ladanyi@6
   323
  ///Map of texts to write on edges
ladanyi@6
   324
  Graph::EdgeMap<Gnome::Canvas::Text *> edgetextmap;
ladanyi@6
   325
hegyi@28
   326
  ///Map of texts to write on nodes
hegyi@28
   327
  Graph::NodeMap<Gnome::Canvas::Text *> nodetextmap;
hegyi@28
   328
ladanyi@6
   329
  ///Group of graphical elements of displayed_graph
ladanyi@6
   330
  Gnome::Canvas::Group displayed_graph;
ladanyi@6
   331
hegyi@88
   332
private:
ladanyi@6
   333
  ///Indicates whether the button of mouse is pressed or not
hegyi@20
   334
  int isbutton;
ladanyi@6
   335
hegyi@21
   336
  ///Stores the actual tool in hand
hegyi@21
   337
  int actual_tool;
hegyi@21
   338
ladanyi@6
   339
  ///At this location was the mousebutton pressed.
ladanyi@6
   340
  ///It helps to calculate the distance of dragging.
ladanyi@6
   341
  double clicked_x, clicked_y;
ladanyi@6
   342
ladanyi@6
   343
  ///Remembers which Gnome::Canvas::Item was pressed.
hegyi@118
   344
hegyi@118
   345
  ///this variable is needed, to work on it after selection
hegyi@118
   346
  Gnome::Canvas::Item * active_item;
hegyi@118
   347
hegyi@118
   348
  ///Remembers which Gnome::Canvas::Item was pressed.
hegyi@118
   349
hegyi@118
   350
  ///this variable is used at edge creation, it will
hegyi@118
   351
  ///be the secondly selected node. No local variable
hegyi@118
   352
  ///can be used for this purpose inside the function,
hegyi@118
   353
  ///because the node selected by button press, and
hegyi@118
   354
  ///the edge is created by button release. Both of
hegyi@118
   355
  ///them is different function call.
hegyi@118
   356
  Gnome::Canvas::Item * target_item;
hegyi@118
   357
hegyi@118
   358
  ///selected node (for any editing)
alpar@62
   359
  Node active_node;
hegyi@118
   360
hegyi@118
   361
  ///selected edge (for any editing)
alpar@62
   362
  Edge active_edge;
hegyi@118
   363
hegyi@118
   364
  ///the edge that is selected by clicking on the red arrow in the middle of it
hegyi@118
   365
hegyi@118
   366
  ///This edge is stored only for the purpose of reshape it.
hegyi@118
   367
  ///That is why it is selected in a different manner.
alpar@62
   368
  Edge forming_edge;
hegyi@35
   369
hegyi@118
   370
  ///Map displayed by label can be edited.
hegyi@118
   371
  std::string nodemap_to_edit;
hegyi@118
   372
hegyi@118
   373
  ///Map displayed by label can be edited.
hegyi@118
   374
  std::string edgemap_to_edit;
ladanyi@6
   375
ladanyi@6
   376
  static const int zoom_step = 5;
hegyi@19
   377
hegyi@88
   378
private:
hegyi@88
   379
hegyi@118
   380
  ///reference to the container, in which the canvas is
hegyi@96
   381
  NoteBookTab & mytab;
hegyi@55
   382
hegyi@148
   383
  XY calcArrowPos(XY, XY, XY, XY, int);
ladanyi@6
   384
};
ladanyi@6
   385
ladanyi@6
   386
#endif //GRAPH_DISPLAYER_CANVAS_H