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