gui/gdc-broken_edge.cc
author deba
Thu, 23 Feb 2006 08:55:54 +0000
changeset 1980 a954b780e3ab
parent 1831 75ab76fc4bf2
permissions -rw-r--r--
Renaming to be convient to the naming of the adaptors
Concept checking of the ugraph adaptors
hegyi@1819
     1
#include "graph_displayer_canvas.h"
alpar@1632
     2
#include <cmath>
hegyi@1497
     3
ladanyi@1860
     4
GraphDisplayerCanvas::BrokenEdge::BrokenEdge(Gnome::Canvas::Group & g, Edge _edge, GraphDisplayerCanvas & gc) : Line(g), edge(_edge), gdc(gc), isbutton(false)
hegyi@1497
     5
{
hegyi@1499
     6
  arrow=new Gnome::Canvas::Polygon(g);
hegyi@1499
     7
  *arrow << Gnome::Canvas::Properties::fill_color("red");
hegyi@1819
     8
  arrow->signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::BrokenEdge::edgeFormerEventHandler));
ladanyi@1645
     9
  arrow->lower_to_bottom();
ladanyi@1860
    10
  draw();
hegyi@1499
    11
}
hegyi@1499
    12
hegyi@1819
    13
GraphDisplayerCanvas::BrokenEdge::~BrokenEdge()
hegyi@1499
    14
{
hegyi@1499
    15
  if(arrow)delete(arrow);
hegyi@1499
    16
}
hegyi@1499
    17
ladanyi@1860
    18
void GraphDisplayerCanvas::BrokenEdge::draw()
hegyi@1499
    19
{
ladanyi@1860
    20
  MapStorage& ms = gdc.mytab.mapstorage;
ladanyi@1860
    21
ladanyi@1860
    22
  // update the edge
ladanyi@1860
    23
  {
ladanyi@1860
    24
    Gnome::Canvas::Points points;
ladanyi@1860
    25
    Node source = ms.graph.source(edge);
ladanyi@1860
    26
    Node target = ms.graph.target(edge);
ladanyi@1860
    27
    points.push_back(Gnome::Art::Point(ms.coords[source].x,
ladanyi@1860
    28
          ms.coords[source].y));
ladanyi@1860
    29
    points.push_back(Gnome::Art::Point(ms.arrow_pos[edge].x,
ladanyi@1860
    30
          ms.arrow_pos[edge].y));
ladanyi@1860
    31
    points.push_back(Gnome::Art::Point(ms.coords[target].x,
ladanyi@1860
    32
          ms.coords[target].y));
ladanyi@1860
    33
    property_points().set_value(points);
ladanyi@1860
    34
  }
ladanyi@1860
    35
ladanyi@1860
    36
  // update the arrow
ladanyi@1860
    37
  {
ladanyi@1860
    38
    //calculating coordinates of the direction indicator arrow
ladanyi@1860
    39
    XY target(ms.coords[ms.graph.target(edge)]);
ladanyi@1860
    40
    XY center(ms.arrow_pos[edge]);
ladanyi@1860
    41
ladanyi@1860
    42
    XY unit_vector_in_dir(target-center);
ladanyi@1860
    43
    double length=sqrt( unit_vector_in_dir.normSquare() );
ladanyi@1860
    44
ladanyi@1860
    45
    //       std::cout << target << " - " << center << " = " << unit_vector_in_dir << "    / " <<unit_vector_in_dir.normSquare() ;
ladanyi@1860
    46
    unit_vector_in_dir/=length;
ladanyi@1860
    47
    //       std::cout << " = " << unit_vector_in_dir << std::endl;
ladanyi@1860
    48
ladanyi@1860
    49
    XY unit_norm_vector(0-unit_vector_in_dir.y, unit_vector_in_dir.x);
ladanyi@1860
    50
    //       std::cout << unit_norm_vector << std::endl;
ladanyi@1860
    51
ladanyi@1860
    52
    {      
ladanyi@1860
    53
      //       /\       // top
ladanyi@1860
    54
      //      /  \      //
ladanyi@1860
    55
      //      -  -      // c(enter)l(eft), ccl, ccr, cr
ladanyi@1860
    56
      //       ||       //
ladanyi@1860
    57
      //       ||       // b(ottom)l, br
hegyi@1500
    58
    }
hegyi@1601
    59
ladanyi@1860
    60
    double size=3;
hegyi@1601
    61
ladanyi@1860
    62
    XY bl (center - unit_vector_in_dir * 3 * size + unit_norm_vector * size );
ladanyi@1860
    63
    XY br (center - unit_vector_in_dir * 3 * size - unit_norm_vector * size );
ladanyi@1860
    64
    XY ccl(center + unit_vector_in_dir *  size + unit_norm_vector * size );
ladanyi@1860
    65
    XY ccr(center + unit_vector_in_dir *  size - unit_norm_vector * size );
ladanyi@1860
    66
    XY cl (center + unit_vector_in_dir *  size + unit_norm_vector * 2 * size );
ladanyi@1860
    67
    XY cr (center + unit_vector_in_dir *  size - unit_norm_vector * 2 * size );
ladanyi@1860
    68
    XY top(center + unit_vector_in_dir * 3 * size);
hegyi@1601
    69
ladanyi@1860
    70
    //std::cout << bl << " " << br << " " << ccl << " "  << ccr << " " << cl << " " << cr << " " << top << std::endl;
hegyi@1601
    71
ladanyi@1860
    72
    Gnome::Canvas::Points arrow_points;
ladanyi@1860
    73
    arrow_points.push_back(Gnome::Art::Point( bl.x , bl.y  ) );
ladanyi@1860
    74
    arrow_points.push_back(Gnome::Art::Point( br.x , br.y  ) );
ladanyi@1860
    75
    arrow_points.push_back(Gnome::Art::Point( ccr.x, ccr.y ) );
ladanyi@1860
    76
    arrow_points.push_back(Gnome::Art::Point( cr.x , cr.y  ) );
ladanyi@1860
    77
    arrow_points.push_back(Gnome::Art::Point( top.x, top.y ) );
ladanyi@1860
    78
    arrow_points.push_back(Gnome::Art::Point( cl.x , cl.y  ) );
ladanyi@1860
    79
    arrow_points.push_back(Gnome::Art::Point( ccl.x, ccl.y ) );
hegyi@1601
    80
ladanyi@1860
    81
    arrow->property_points().set_value(arrow_points);
ladanyi@1860
    82
  }
hegyi@1497
    83
}
hegyi@1499
    84
hegyi@1819
    85
bool GraphDisplayerCanvas::BrokenEdge::edgeFormerEventHandler(GdkEvent* e)
hegyi@1499
    86
{
hegyi@1499
    87
  switch(e->type)
hegyi@1499
    88
    {
hegyi@1499
    89
    case GDK_BUTTON_PRESS:
hegyi@1499
    90
      //we mark the location of the event to be able to calculate parameters of dragging
hegyi@1524
    91
      if(gdc.getActualTool()!=CREATE_NODE)
hegyi@1501
    92
	{
hegyi@1524
    93
	  gdc.toggleEdgeActivity(this, true);
hegyi@1501
    94
	  clicked_x=e->button.x;
hegyi@1501
    95
	  clicked_y=e->button.y;
hegyi@1501
    96
	  isbutton=true;
hegyi@1501
    97
	}
hegyi@1499
    98
      break;
hegyi@1499
    99
    case GDK_BUTTON_RELEASE:
hegyi@1524
   100
      if(gdc.getActualTool()!=CREATE_NODE)
hegyi@1505
   101
	{
hegyi@1524
   102
	  gdc.toggleEdgeActivity(this, false);
hegyi@1505
   103
	  isbutton=false;
hegyi@1505
   104
	}
hegyi@1499
   105
      break;
hegyi@1499
   106
    case GDK_MOTION_NOTIFY:
hegyi@1499
   107
      //we only have to do sg. if the mouse button is pressed
hegyi@1499
   108
      if(isbutton)
hegyi@1499
   109
	{
hegyi@1499
   110
	  //new coordinates will be the old values,
hegyi@1499
   111
	  //because the item will be moved to the
hegyi@1499
   112
	  //new coordinate therefore the new movement
hegyi@1499
   113
	  //has to be calculated from here
hegyi@1499
   114
hegyi@1499
   115
	  double dx=e->motion.x-clicked_x;
hegyi@1499
   116
	  double dy=e->motion.y-clicked_y;
hegyi@1499
   117
hegyi@1499
   118
	  Gnome::Canvas::Points points_new;
hegyi@1499
   119
ladanyi@1860
   120
          gdc.mytab.mapstorage.arrow_pos.set(edge, gdc.mytab.mapstorage.arrow_pos[edge] + XY(dx, dy));
hegyi@1499
   121
ladanyi@1860
   122
	  draw();
ladanyi@1860
   123
	  gdc.textReposition(gdc.mytab.mapstorage.arrow_pos[edge]);
hegyi@1499
   124
hegyi@1499
   125
	  clicked_x=e->motion.x;
hegyi@1499
   126
	  clicked_y=e->motion.y;
hegyi@1499
   127
hegyi@1499
   128
	}
hegyi@1499
   129
    default: break;
hegyi@1499
   130
    }
hegyi@1499
   131
hegyi@1499
   132
  return true;
hegyi@1499
   133
}