gdc-broken_edge.cc
author hegyi
Thu, 12 Oct 2006 11:39:29 +0000
changeset 160 14a76109b561
parent 151 72f1c33f89d4
child 174 95872af46fc4
permissions -rw-r--r--
Node antigravity and edge elasticity based graph layout redesigner.
hegyi@89
     1
#include "graph_displayer_canvas.h"
alpar@59
     2
#include <cmath>
hegyi@17
     3
ladanyi@147
     4
GraphDisplayerCanvas::EdgeBase::EdgeBase(Gnome::Canvas::Group& _group, Edge _edge, GraphDisplayerCanvas& _canvas) : 
ladanyi@147
     5
  Gnome::Canvas::Group(_group), edge(_edge), canvas(_canvas), arrow(*this)
hegyi@17
     6
{
ladanyi@147
     7
  arrow.property_fill_color().set_value("red");
ladanyi@147
     8
  arrow.lower_to_bottom();
ladanyi@152
     9
  lower_to_bottom();
ladanyi@147
    10
}
ladanyi@147
    11
ladanyi@147
    12
GraphDisplayerCanvas::EdgeBase::~EdgeBase()
ladanyi@147
    13
{
ladanyi@147
    14
}
ladanyi@147
    15
ladanyi@147
    16
void GraphDisplayerCanvas::EdgeBase::drawArrow(XY unit_vector_in_dir)
ladanyi@147
    17
{
ladanyi@147
    18
  MapStorage& ms = canvas.mytab.mapstorage;
ladanyi@147
    19
  XY center(ms.arrow_pos[edge]);
ladanyi@147
    20
  XY unit_norm_vector(0-unit_vector_in_dir.y, unit_vector_in_dir.x);
ladanyi@147
    21
ladanyi@147
    22
  //       /\       // top
ladanyi@147
    23
  //      /  \      //
ladanyi@147
    24
  //      -  -      // c(enter)l(eft), ccl, ccr, cr
ladanyi@147
    25
  //       ||       //
ladanyi@147
    26
  //       ||       // b(ottom)l, br
ladanyi@147
    27
ladanyi@147
    28
  double size=3;
ladanyi@147
    29
ladanyi@147
    30
  XY bl (center - unit_vector_in_dir * 3 * size + unit_norm_vector * size );
ladanyi@147
    31
  XY br (center - unit_vector_in_dir * 3 * size - unit_norm_vector * size );
ladanyi@147
    32
  XY ccl(center + unit_vector_in_dir *  size + unit_norm_vector * size );
ladanyi@147
    33
  XY ccr(center + unit_vector_in_dir *  size - unit_norm_vector * size );
ladanyi@147
    34
  XY cl (center + unit_vector_in_dir *  size + unit_norm_vector * 2 * size );
ladanyi@147
    35
  XY cr (center + unit_vector_in_dir *  size - unit_norm_vector * 2 * size );
ladanyi@147
    36
  XY top(center + unit_vector_in_dir * 3 * size);
ladanyi@147
    37
ladanyi@147
    38
  Gnome::Canvas::Points arrow_points;
ladanyi@147
    39
  arrow_points.push_back(Gnome::Art::Point( bl.x , bl.y  ) );
ladanyi@147
    40
  arrow_points.push_back(Gnome::Art::Point( br.x , br.y  ) );
ladanyi@147
    41
  arrow_points.push_back(Gnome::Art::Point( ccr.x, ccr.y ) );
ladanyi@147
    42
  arrow_points.push_back(Gnome::Art::Point( cr.x , cr.y  ) );
ladanyi@147
    43
  arrow_points.push_back(Gnome::Art::Point( top.x, top.y ) );
ladanyi@147
    44
  arrow_points.push_back(Gnome::Art::Point( cl.x , cl.y  ) );
ladanyi@147
    45
  arrow_points.push_back(Gnome::Art::Point( ccl.x, ccl.y ) );
ladanyi@147
    46
ladanyi@147
    47
  arrow.property_points().set_value(arrow_points);
ladanyi@147
    48
}
ladanyi@147
    49
ladanyi@147
    50
GraphDisplayerCanvas::BrokenEdge::BrokenEdge(Gnome::Canvas::Group & g,
ladanyi@147
    51
    Edge _edge, GraphDisplayerCanvas & gc) : EdgeBase(g, _edge, gc),
ladanyi@147
    52
  isbutton(false), line(*this)
ladanyi@147
    53
{
ladanyi@147
    54
  arrow.signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::BrokenEdge::edgeFormerEventHandler));
ladanyi@147
    55
ladanyi@147
    56
  line.property_fill_color().set_value("green");
ladanyi@147
    57
  line.property_width_units().set_value(10);    
ladanyi@147
    58
  line.lower_to_bottom();
ladanyi@147
    59
ladanyi@98
    60
  draw();
hegyi@19
    61
}
hegyi@19
    62
hegyi@89
    63
GraphDisplayerCanvas::BrokenEdge::~BrokenEdge()
hegyi@19
    64
{
hegyi@19
    65
}
hegyi@19
    66
ladanyi@98
    67
void GraphDisplayerCanvas::BrokenEdge::draw()
hegyi@19
    68
{
ladanyi@147
    69
  MapStorage& ms = canvas.mytab.mapstorage;
ladanyi@147
    70
ladanyi@147
    71
  //calculating coordinates of the direction indicator arrow
ladanyi@147
    72
  XY head(ms.coords[ms.graph.target(edge)]);
ladanyi@147
    73
  XY center(ms.arrow_pos[edge]);
ladanyi@147
    74
ladanyi@147
    75
  XY unit_vector_in_dir(head-center);
ladanyi@147
    76
  double length=sqrt( unit_vector_in_dir.normSquare() );
ladanyi@147
    77
ladanyi@147
    78
  unit_vector_in_dir/=length;
ladanyi@147
    79
ladanyi@147
    80
  // update the arrow
ladanyi@147
    81
  drawArrow(unit_vector_in_dir);
ladanyi@98
    82
ladanyi@98
    83
  // update the edge
ladanyi@147
    84
  Gnome::Canvas::Points points;
ladanyi@147
    85
  Node source = ms.graph.source(edge);
ladanyi@147
    86
  Node target = ms.graph.target(edge);
ladanyi@147
    87
  points.push_back(Gnome::Art::Point(ms.coords[source].x,
ladanyi@147
    88
        ms.coords[source].y));
ladanyi@147
    89
  points.push_back(Gnome::Art::Point(ms.arrow_pos[edge].x,
ladanyi@147
    90
        ms.arrow_pos[edge].y));
ladanyi@147
    91
  points.push_back(Gnome::Art::Point(ms.coords[target].x,
ladanyi@147
    92
        ms.coords[target].y));
ladanyi@147
    93
  line.property_points().set_value(points);
hegyi@17
    94
}
hegyi@19
    95
hegyi@89
    96
bool GraphDisplayerCanvas::BrokenEdge::edgeFormerEventHandler(GdkEvent* e)
hegyi@19
    97
{
hegyi@19
    98
  switch(e->type)
ladanyi@147
    99
  {
hegyi@19
   100
    case GDK_BUTTON_PRESS:
ladanyi@147
   101
      //we mark the location of the event to be able to calculate parameters
ladanyi@147
   102
      //of dragging
ladanyi@147
   103
      if(canvas.getActualTool()!=CREATE_NODE)
ladanyi@147
   104
      {
ladanyi@147
   105
        canvas.toggleEdgeActivity(this, true);
ladanyi@147
   106
        clicked_x=e->button.x;
ladanyi@147
   107
        clicked_y=e->button.y;
ladanyi@147
   108
        isbutton=true;
ladanyi@147
   109
      }
hegyi@19
   110
      break;
hegyi@19
   111
    case GDK_BUTTON_RELEASE:
ladanyi@147
   112
      if(canvas.getActualTool()!=CREATE_NODE)
ladanyi@147
   113
      {
ladanyi@147
   114
        canvas.toggleEdgeActivity(this, false);
ladanyi@147
   115
        isbutton=false;
ladanyi@147
   116
      }
hegyi@19
   117
      break;
hegyi@19
   118
    case GDK_MOTION_NOTIFY:
hegyi@19
   119
      //we only have to do sg. if the mouse button is pressed
hegyi@19
   120
      if(isbutton)
ladanyi@147
   121
      {
ladanyi@147
   122
        //new coordinates will be the old values,
ladanyi@147
   123
        //because the item will be moved to the
ladanyi@147
   124
        //new coordinate therefore the new movement
ladanyi@147
   125
        //has to be calculated from here
hegyi@19
   126
ladanyi@147
   127
        double dx=e->motion.x-clicked_x;
ladanyi@147
   128
        double dy=e->motion.y-clicked_y;
hegyi@19
   129
ladanyi@147
   130
        Gnome::Canvas::Points points_new;
hegyi@19
   131
ladanyi@147
   132
        canvas.mytab.mapstorage.arrow_pos.set(edge, canvas.mytab.mapstorage.arrow_pos[edge] + XY(dx, dy));
hegyi@19
   133
ladanyi@147
   134
        draw();
ladanyi@147
   135
        canvas.textReposition(canvas.mytab.mapstorage.arrow_pos[edge]);
hegyi@19
   136
ladanyi@147
   137
        clicked_x=e->motion.x;
ladanyi@147
   138
        clicked_y=e->motion.y;
hegyi@19
   139
ladanyi@147
   140
      }
hegyi@19
   141
    default: break;
ladanyi@147
   142
  }
hegyi@19
   143
hegyi@19
   144
  return true;
hegyi@19
   145
}
ladanyi@147
   146
ladanyi@147
   147
void GraphDisplayerCanvas::BrokenEdge::setLineWidth(int w)
ladanyi@147
   148
{
ladanyi@147
   149
  line.property_width_units().set_value(w);
ladanyi@147
   150
}
ladanyi@147
   151
ladanyi@147
   152
void GraphDisplayerCanvas::BrokenEdge::setFillColor(Gdk::Color c)
ladanyi@147
   153
{
ladanyi@147
   154
  line.property_fill_color_gdk().set_value(c);
ladanyi@147
   155
}
ladanyi@147
   156
ladanyi@147
   157
GraphDisplayerCanvas::LoopEdge::LoopEdge(Gnome::Canvas::Group& _group,
ladanyi@147
   158
    Edge _edge, GraphDisplayerCanvas& _canvas) :
ladanyi@151
   159
  EdgeBase(_group, _edge, _canvas), line(*this), isbutton(false)
ladanyi@147
   160
{
ladanyi@151
   161
  arrow.signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::LoopEdge::edgeFormerEventHandler));
ladanyi@151
   162
ladanyi@151
   163
  line.property_outline_color().set_value("green");
ladanyi@151
   164
  line.property_width_units().set_value(10);
ladanyi@147
   165
  line.lower_to_bottom();
ladanyi@147
   166
ladanyi@147
   167
  draw();
ladanyi@147
   168
}
ladanyi@147
   169
ladanyi@147
   170
GraphDisplayerCanvas::LoopEdge::~LoopEdge()
ladanyi@147
   171
{
ladanyi@147
   172
}
ladanyi@147
   173
ladanyi@147
   174
void GraphDisplayerCanvas::LoopEdge::draw()
ladanyi@147
   175
{
ladanyi@147
   176
  MapStorage& ms = canvas.mytab.mapstorage;
ladanyi@147
   177
ladanyi@147
   178
  Node node = ms.graph.source(edge);
ladanyi@147
   179
  XY center = (ms.coords[node] + ms.arrow_pos[edge]) / 2.0;
ladanyi@147
   180
ladanyi@147
   181
  XY unit_vector_in_dir(rot90(center - ms.arrow_pos[edge]));
ladanyi@147
   182
  double length = sqrt(unit_vector_in_dir.normSquare());
ladanyi@147
   183
  unit_vector_in_dir /= length;
ladanyi@147
   184
ladanyi@147
   185
  drawArrow(unit_vector_in_dir);
ladanyi@147
   186
ladanyi@147
   187
  double radius =
ladanyi@147
   188
    sqrt((ms.arrow_pos[edge] - ms.coords[node]).normSquare()) / 2.0;
ladanyi@147
   189
ladanyi@147
   190
  XY p1 = center + XY(-radius,  radius);
ladanyi@147
   191
  XY p2 = center + XY( radius, -radius);
ladanyi@147
   192
  line.property_x1().set_value(p1.x);
ladanyi@147
   193
  line.property_y1().set_value(p1.y);
ladanyi@147
   194
  line.property_x2().set_value(p2.x);
ladanyi@147
   195
  line.property_y2().set_value(p2.y);
ladanyi@147
   196
}
ladanyi@147
   197
ladanyi@147
   198
void GraphDisplayerCanvas::LoopEdge::setLineWidth(int w)
ladanyi@147
   199
{
ladanyi@147
   200
  line.property_width_units().set_value(w);
ladanyi@147
   201
}
ladanyi@147
   202
ladanyi@147
   203
void GraphDisplayerCanvas::LoopEdge::setFillColor(Gdk::Color c)
ladanyi@147
   204
{
ladanyi@151
   205
  line.property_outline_color_gdk().set_value(c);
ladanyi@147
   206
}
ladanyi@151
   207
ladanyi@151
   208
bool GraphDisplayerCanvas::LoopEdge::edgeFormerEventHandler(GdkEvent* e)
ladanyi@151
   209
{
ladanyi@151
   210
  switch(e->type)
ladanyi@151
   211
  {
ladanyi@151
   212
    case GDK_BUTTON_PRESS:
ladanyi@151
   213
      if(canvas.getActualTool()!=CREATE_NODE)
ladanyi@151
   214
      {
ladanyi@151
   215
        canvas.toggleEdgeActivity(this, true);
ladanyi@151
   216
        isbutton=true;
ladanyi@151
   217
      }
ladanyi@151
   218
      break;
ladanyi@151
   219
    case GDK_BUTTON_RELEASE:
ladanyi@151
   220
      if(canvas.getActualTool()!=CREATE_NODE)
ladanyi@151
   221
      {
ladanyi@151
   222
        canvas.toggleEdgeActivity(this, false);
ladanyi@151
   223
        isbutton=false;
ladanyi@151
   224
      }
ladanyi@151
   225
      break;
ladanyi@151
   226
    case GDK_MOTION_NOTIFY:
ladanyi@151
   227
      if(isbutton)
ladanyi@151
   228
      {
ladanyi@151
   229
        canvas.mytab.mapstorage.arrow_pos.set(edge, XY(e->motion.x, e->motion.y));
ladanyi@151
   230
ladanyi@151
   231
        draw();
ladanyi@151
   232
        canvas.textReposition(canvas.mytab.mapstorage.arrow_pos[edge]);
ladanyi@151
   233
      }
ladanyi@151
   234
    default: break;
ladanyi@151
   235
  }
ladanyi@151
   236
  return true;
ladanyi@151
   237
}