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