graph_displayer_canvas.h
author hegyi
Wed, 30 Aug 2006 15:55:18 +0000
changeset 146 afd1d8bfcccd
parent 118 cfd49e5c8723
child 147 10ef59f6633c
permissions -rw-r--r--
Size of MapWin cannot be changed.
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@98
    18
hegyi@118
    19
  ///Edge displayer class
hegyi@118
    20
hegyi@118
    21
  ///This class is responsible for displaying edges in graph.
hegyi@118
    22
  ///The displayed edge is broken in the middle. The
hegyi@118
    23
  ///aim of this is to be able to indicate direction of edges
hegyi@118
    24
  ///and to be able to display more then one edges between the
hegyi@118
    25
  ///same source and target
hegyi@89
    26
  class BrokenEdge : public Gnome::Canvas::Line
hegyi@89
    27
  {
hegyi@118
    28
    ///The edge that the class displays.
hegyi@118
    29
hegyi@118
    30
    ///It is needed, because some datas needed from
hegyi@118
    31
    ///graph can be accessed by this or should be sent
hegyi@118
    32
    ///as parameter, but it would be complicated
ladanyi@98
    33
    Edge edge;
hegyi@118
    34
hegyi@118
    35
    ///Reference to the canvas, on which the graph is drawn.
hegyi@118
    36
hegyi@118
    37
    ///It is needed, because some datas needed from
hegyi@118
    38
    ///graph can be accessed by this or should be sent
hegyi@118
    39
    ///as parameter, but it would be complicated
hegyi@89
    40
    GraphDisplayerCanvas & gdc;
hegyi@118
    41
hegyi@118
    42
    ///An arrow that indicates the direction of the edges
hegyi@118
    43
hegyi@118
    44
    ///in case of directional graph direction can be indicated
hegyi@118
    45
    ///by this polygon. The polygon formulates a red arrow.
hegyi@89
    46
    Gnome::Canvas::Polygon * arrow;
hegyi@89
    47
hegyi@118
    48
    ///Indicates whether the button of mouse is pressed or not at the moment.
hegyi@89
    49
    bool isbutton;
hegyi@89
    50
hegyi@118
    51
    ///At this location was the mousebutton pressed. Horizontal component.
hegyi@89
    52
hegyi@118
    53
    ///It helps to calculate the
hegyi@118
    54
    ///distance of dragging.
hegyi@118
    55
    double clicked_x;
hegyi@118
    56
hegyi@118
    57
    ///At this location was the mousebutton pressed. Vertical component.
hegyi@118
    58
hegyi@118
    59
    ///It helps to calculate the
hegyi@118
    60
    ///distance of dragging.
hegyi@118
    61
    double clicked_y;
hegyi@118
    62
hegyi@118
    63
    ///event handler for forming broken edges
hegyi@118
    64
hegyi@118
    65
    ///\param event the
hegyi@118
    66
    ///event to handle
hegyi@118
    67
    bool edgeFormerEventHandler(GdkEvent* event);
hegyi@89
    68
  public:
hegyi@118
    69
hegyi@118
    70
    ///Constructor of broken edge class.
hegyi@118
    71
hegyi@118
    72
    ///\param g the group to which the edge belongs
hegyi@118
    73
    ///\param _edge the represented edge
hegyi@118
    74
    ///\param gc the canvas
hegyi@118
    75
    BrokenEdge(Gnome::Canvas::Group & g, Edge _edge, GraphDisplayerCanvas & gc);
hegyi@118
    76
hegyi@118
    77
    ///Destructor of broken edge class
hegyi@118
    78
hegyi@118
    79
    ///Frees up
hegyi@118
    80
    ///reserved memory
hegyi@89
    81
    ~BrokenEdge();
hegyi@118
    82
hegyi@118
    83
    ///The function that draws the edge based on collected data
ladanyi@98
    84
    void draw();
hegyi@89
    85
  };
hegyi@118
    86
hegyi@118
    87
  ///Type of canvas, on which the graph is drawn
ladanyi@6
    88
  typedef Gnome::Canvas::CanvasAA Parent;
ladanyi@6
    89
ladanyi@6
    90
public:
hegyi@118
    91
  ///Constructor
hegyi@118
    92
hegyi@118
    93
  ///\param nbt the tab of the window, in which the graph is displayed
hegyi@118
    94
  GraphDisplayerCanvas(NoteBookTab & nbt);
hegyi@118
    95
hegyi@118
    96
  ///destructor of the class
ladanyi@6
    97
  virtual ~GraphDisplayerCanvas();
ladanyi@6
    98
hegyi@118
    99
  ///Changes the width of edge(s) according to the given map.
hegyi@118
   100
hegyi@118
   101
  ///\param mapname is the name of the map which contains the values to be set
hegyi@118
   102
  ///\param edge if it is given, only the width of the given edge will be set, instead of all of them.
hegyi@118
   103
  int changeEdgeWidth (std::string mapname, Edge edge=INVALID);
hegyi@118
   104
hegyi@118
   105
  ///Resets width of edge(s) to the default value
hegyi@118
   106
hegyi@118
   107
  ///\param edge if it is given, only the width of the
hegyi@118
   108
  ///given edge will be reset, instead of all of them.
hegyi@118
   109
  int resetEdgeWidth (Edge edge=INVALID);
hegyi@118
   110
hegyi@118
   111
  ///Changes the color of edge(s) according to the given map.
hegyi@118
   112
ladanyi@6
   113
  ///\param mapname is the name of the map which contains the new values
hegyi@118
   114
  ///\param edge if it is given, only the color of the given edge will be set, instead of all of them.
hegyi@118
   115
  int changeEdgeColor (std::string mapname, Edge edge=INVALID);
ladanyi@6
   116
hegyi@118
   117
  ///Resets color of edge(s) to the default value
hegyi@118
   118
hegyi@118
   119
  ///\param edge if it is given, only the color of the
hegyi@118
   120
  ///given edge will be reset, instead of all of them.
hegyi@118
   121
  int resetEdgeColor (Edge edge=INVALID);
hegyi@118
   122
hegyi@118
   123
  ///Changes the label of edge(s) according to the given map.
hegyi@118
   124
ladanyi@6
   125
  ///\param mapname is the name of the map which contains the new values
hegyi@118
   126
  ///\param edge if it is given, only the label of the given edge will be set, instead of all of them.
hegyi@118
   127
  int changeEdgeText (std::string mapname, Edge edge=INVALID);
ladanyi@6
   128
hegyi@118
   129
  ///Resets label of edge(s) to the default value
hegyi@118
   130
hegyi@118
   131
  ///\param edge if it is given, only the color of the
hegyi@118
   132
  ///given edge will be reset, instead of all of them.
hegyi@118
   133
  int resetEdgeText (Edge edge=INVALID);
hegyi@118
   134
hegyi@118
   135
  ///Changes the radius of node(s) according to the given map.
hegyi@118
   136
ladanyi@6
   137
  ///\param mapname is the name of the map which contains the new values
hegyi@118
   138
  ///\param node if it is given, only the radius of the given node will be set, instead of all of them.
hegyi@118
   139
  int changeNodeRadius (std::string mapname, Node node=INVALID);
hegyi@28
   140
hegyi@118
   141
  ///Resets radius of node(s) to the default value
hegyi@118
   142
hegyi@118
   143
  ///\param node if it is given, only the radius of the
hegyi@118
   144
  ///given node will be reset, instead of all of them.
hegyi@118
   145
  int resetNodeRadius (Node node=INVALID);
hegyi@118
   146
hegyi@118
   147
  ///Changes the color of node(s) according to the given map.
hegyi@118
   148
hegyi@28
   149
  ///\param mapname is the name of the map which contains the new values
hegyi@118
   150
  ///\param node if it is given, only the color of the given node will be set, instead of all of them.
hegyi@118
   151
  int changeNodeColor (std::string mapname, Node node=INVALID);
hegyi@28
   152
hegyi@118
   153
  ///Resets color of node(s) to the default value
hegyi@118
   154
hegyi@118
   155
  ///\param node if it is given, only the color of the
hegyi@118
   156
  ///given node will be reset, instead of all of them.
hegyi@118
   157
  int resetNodeColor (Node node=INVALID);
hegyi@118
   158
hegyi@118
   159
  ///Changes the label of node(s) according to the given map.
hegyi@118
   160
hegyi@28
   161
  ///\param mapname is the name of the map which contains the new values
hegyi@118
   162
  ///\param node if it is given, only the label of the given node will be set, instead of all of them.
hegyi@118
   163
  int changeNodeText (std::string mapname, Node node=INVALID);
hegyi@28
   164
hegyi@118
   165
  ///Resets label of node(s) to the default value
ladanyi@6
   166
hegyi@118
   167
  ///\param node if it is given, only the label of the
hegyi@118
   168
  ///given node will be reset, instead of all of them.
hegyi@118
   169
  int resetNodeText (Node node=INVALID);
hegyi@94
   170
hegyi@118
   171
  ///This function is called, when any of the displayed attributes have to be updated, or changed
hegyi@118
   172
hegyi@118
   173
  ///\param itisedge if true, edge property has to be changed, else node property
hegyi@118
   174
  ///\param prop the id of property that has to changed or updated
hegyi@118
   175
  void propertyChange(bool itisedge, int prop);
hegyi@118
   176
hegyi@118
   177
  ///updates the given property
hegyi@118
   178
hegyi@118
   179
  ///\param edge if it is not INVALID, only the property of the given edge will be updated, instead of all of them
hegyi@118
   180
  ///\param prop the property to update
hegyi@118
   181
  void propertyUpdate(Edge edge, int prop);
hegyi@118
   182
hegyi@118
   183
  ///updates the given property
hegyi@118
   184
hegyi@118
   185
  ///\param node if it is not INVALID, only the property of the given node will be updated, instead of all of them
hegyi@118
   186
  ///\param prop the property to update
hegyi@118
   187
  void propertyUpdate(Node node, int prop);
hegyi@118
   188
hegyi@118
   189
  ///updates all the property for the given edge
hegyi@94
   190
  void propertyUpdate(Edge);
hegyi@118
   191
hegyi@118
   192
  ///updates all the property for the given node
hegyi@94
   193
  void propertyUpdate(Node);
hegyi@94
   194
ladanyi@6
   195
  ///Callback for 'ViewZoomIn' action.
ladanyi@6
   196
  virtual void zoomIn();
ladanyi@6
   197
  ///Callback for 'ViewZoomOut' action.
ladanyi@6
   198
  virtual void zoomOut();
ladanyi@6
   199
  ///Callback for 'ViewZoomFit' action.
ladanyi@6
   200
  virtual void zoomFit();
ladanyi@6
   201
  ///Callback for 'ViewZoom100' action.
ladanyi@6
   202
  virtual void zoom100();
ladanyi@6
   203
  ///Sets the scroll region of the convas to the bounding box of the graph.
ladanyi@6
   204
  void updateScrollRegion();
ladanyi@6
   205
hegyi@9
   206
  ///This function changes the tool in the graph-editor's hand
hegyi@9
   207
  void changeEditorialTool(int);
hegyi@9
   208
ladanyi@6
   209
protected:
ladanyi@6
   210
hegyi@118
   211
  //maximizing, minimizing, restoring window, etc.
ladanyi@6
   212
  virtual bool on_expose_event(GdkEventExpose *);
ladanyi@6
   213
ladanyi@6
   214
private:
ladanyi@6
   215
ladanyi@6
   216
  ///This function is responsible for the correct
ladanyi@6
   217
  ///reaction of any action happened in the territory
ladanyi@6
   218
  ///of the canvas
hegyi@25
   219
  ///DEPRECATED!!!!
hegyi@30
   220
  bool eventHandler(GdkEvent* e, Node n);
ladanyi@6
   221
hegyi@9
   222
  ///actual event handler
hegyi@9
   223
  ///
hegyi@9
   224
  ///Actual event handler should be stored, to be able to disconnect it and later reconnect it.
hegyi@9
   225
  sigc::connection actual_handler;
hegyi@9
   226
hegyi@9
   227
  ///event handler for the case when move-tool is active
hegyi@30
   228
  bool moveEventHandler(GdkEvent*);
hegyi@9
   229
  ///event handler for the case when create_node-tool is active
hegyi@30
   230
  bool createNodeEventHandler(GdkEvent*);
hegyi@9
   231
  ///event handler for the case when create_edge-tool is active
hegyi@30
   232
  bool createEdgeEventHandler(GdkEvent*);
hegyi@13
   233
  ///event handler for the case when eraser-tool is active
hegyi@30
   234
  bool eraserEventHandler(GdkEvent*);
hegyi@32
   235
  ///event handler for the case when edge map editor tool is active
hegyi@30
   236
  bool edgeMapEditEventHandler(GdkEvent*);
hegyi@32
   237
  ///event handler for the case when node map editor tool is active
hegyi@32
   238
  bool nodeMapEditEventHandler(GdkEvent*);
hegyi@13
   239
hegyi@21
   240
public:
hegyi@25
   241
  ///Moves the text to new place
hegyi@30
   242
  void textReposition(xy<double>);
hegyi@118
   243
hegyi@25
   244
  ///Activates an edge belonging to a BrokenEdge
hegyi@118
   245
hegyi@35
   246
  ///After we have activated an edge this way,
hegyi@35
   247
  ///the GDC object will know, which edge is under forming
hegyi@118
   248
  ///therefore it can redraw the necessary elements on the canvas,
hegyi@35
   249
  ///for example the text belonging to the \ref BrokenEdge can be
hegyi@35
   250
  ///redrawn (\ref textReposition).
hegyi@30
   251
  void toggleEdgeActivity(BrokenEdge*, bool);
hegyi@25
   252
hegyi@25
   253
public:
hegyi@118
   254
hegyi@118
   255
  ///Returns the actual tool in hand
hegyi@30
   256
  int getActualTool();
hegyi@21
   257
hegyi@118
   258
  ///draws the graph
hegyi@118
   259
hegyi@118
   260
  ///Called when opening a file.
ladanyi@53
   261
  void drawGraph();
hegyi@118
   262
hegyi@118
   263
  ///Clears the canvas
hegyi@118
   264
hegyi@118
   265
  ///It achieves this by deleting all data
hegyi@118
   266
  ///structure used to help handle the displayed graph.
ladanyi@53
   267
  void clear();
ladanyi@53
   268
hegyi@37
   269
  ///creates a new Nodemap
hegyi@118
   270
hegyi@118
   271
  ///\param init initial value of the map
hegyi@118
   272
  ///\param mapname name of new map
hegyi@118
   273
  int addNewNodeMap(double init,std::string mapname);
hegyi@37
   274
  ///creates a new Edgemap
hegyi@118
   275
hegyi@118
   276
  ///\param init initial value of the map
hegyi@118
   277
  ///\param mapname name of new map
hegyi@118
   278
  int addNewEdgeMap(double init,std::string mapname);
hegyi@37
   279
hegyi@21
   280
private:
hegyi@14
   281
  ///Deletes the given element.
alpar@62
   282
  void deleteItem(Node);
hegyi@14
   283
  ///Deletes the given element.
alpar@62
   284
  void deleteItem(Edge);
hegyi@9
   285
hegyi@21
   286
private:
hegyi@21
   287
ladanyi@6
   288
  ///Map of nodes of graph
ladanyi@6
   289
  Graph::NodeMap<Gnome::Canvas::Ellipse *> nodesmap;
ladanyi@6
   290
ladanyi@6
   291
  ///Map of edges of graph
hegyi@19
   292
  Graph::EdgeMap<BrokenEdge *> edgesmap;
ladanyi@6
   293
ladanyi@6
   294
  ///Map of texts to write on edges
ladanyi@6
   295
  Graph::EdgeMap<Gnome::Canvas::Text *> edgetextmap;
ladanyi@6
   296
hegyi@28
   297
  ///Map of texts to write on nodes
hegyi@28
   298
  Graph::NodeMap<Gnome::Canvas::Text *> nodetextmap;
hegyi@28
   299
ladanyi@6
   300
  ///Group of graphical elements of displayed_graph
ladanyi@6
   301
  Gnome::Canvas::Group displayed_graph;
ladanyi@6
   302
hegyi@88
   303
private:
ladanyi@6
   304
  ///Indicates whether the button of mouse is pressed or not
hegyi@20
   305
  int isbutton;
ladanyi@6
   306
hegyi@21
   307
  ///Stores the actual tool in hand
hegyi@21
   308
  int actual_tool;
hegyi@21
   309
ladanyi@6
   310
  ///At this location was the mousebutton pressed.
ladanyi@6
   311
  ///It helps to calculate the distance of dragging.
ladanyi@6
   312
  double clicked_x, clicked_y;
ladanyi@6
   313
ladanyi@6
   314
  ///Remembers which Gnome::Canvas::Item was pressed.
hegyi@118
   315
hegyi@118
   316
  ///this variable is needed, to work on it after selection
hegyi@118
   317
  Gnome::Canvas::Item * active_item;
hegyi@118
   318
hegyi@118
   319
  ///Remembers which Gnome::Canvas::Item was pressed.
hegyi@118
   320
hegyi@118
   321
  ///this variable is used at edge creation, it will
hegyi@118
   322
  ///be the secondly selected node. No local variable
hegyi@118
   323
  ///can be used for this purpose inside the function,
hegyi@118
   324
  ///because the node selected by button press, and
hegyi@118
   325
  ///the edge is created by button release. Both of
hegyi@118
   326
  ///them is different function call.
hegyi@118
   327
  Gnome::Canvas::Item * target_item;
hegyi@118
   328
hegyi@118
   329
  ///selected node (for any editing)
alpar@62
   330
  Node active_node;
hegyi@118
   331
hegyi@118
   332
  ///selected edge (for any editing)
alpar@62
   333
  Edge active_edge;
hegyi@118
   334
hegyi@118
   335
  ///the edge that is selected by clicking on the red arrow in the middle of it
hegyi@118
   336
hegyi@118
   337
  ///This edge is stored only for the purpose of reshape it.
hegyi@118
   338
  ///That is why it is selected in a different manner.
alpar@62
   339
  Edge forming_edge;
hegyi@35
   340
hegyi@118
   341
  ///Map displayed by label can be edited.
hegyi@118
   342
  std::string nodemap_to_edit;
hegyi@118
   343
hegyi@118
   344
  ///Map displayed by label can be edited.
hegyi@118
   345
  std::string edgemap_to_edit;
ladanyi@6
   346
ladanyi@6
   347
  static const int zoom_step = 5;
hegyi@19
   348
hegyi@88
   349
private:
hegyi@88
   350
hegyi@118
   351
  ///reference to the container, in which the canvas is
hegyi@96
   352
  NoteBookTab & mytab;
hegyi@55
   353
alpar@127
   354
  XY calcArrowPos(XY, XY, XY, XY, bool);
ladanyi@6
   355
};
ladanyi@6
   356
ladanyi@6
   357
#endif //GRAPH_DISPLAYER_CANVAS_H