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