gdc-broken_edge.cc
author hegyi
Mon, 30 Oct 2006 14:57:48 +0000
changeset 177 40f3006fba2e
parent 152 d99e115d0d26
child 189 8b69c54d5bf0
permissions -rw-r--r--
Redesign parameters can now be saved and loaded.
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
ladanyi@147
    96
  unit_vector_in_dir/=length;
ladanyi@147
    97
ladanyi@147
    98
  // update the arrow
ladanyi@147
    99
  drawArrow(unit_vector_in_dir);
ladanyi@98
   100
ladanyi@98
   101
  // update the edge
ladanyi@147
   102
  Gnome::Canvas::Points points;
ladanyi@147
   103
  Node source = ms.graph.source(edge);
ladanyi@147
   104
  Node target = ms.graph.target(edge);
ladanyi@147
   105
  points.push_back(Gnome::Art::Point(ms.coords[source].x,
ladanyi@147
   106
        ms.coords[source].y));
ladanyi@147
   107
  points.push_back(Gnome::Art::Point(ms.arrow_pos[edge].x,
ladanyi@147
   108
        ms.arrow_pos[edge].y));
ladanyi@147
   109
  points.push_back(Gnome::Art::Point(ms.coords[target].x,
ladanyi@147
   110
        ms.coords[target].y));
ladanyi@147
   111
  line.property_points().set_value(points);
hegyi@17
   112
}
hegyi@19
   113
hegyi@89
   114
bool GraphDisplayerCanvas::BrokenEdge::edgeFormerEventHandler(GdkEvent* e)
hegyi@19
   115
{
hegyi@19
   116
  switch(e->type)
ladanyi@147
   117
  {
hegyi@19
   118
    case GDK_BUTTON_PRESS:
ladanyi@147
   119
      //we mark the location of the event to be able to calculate parameters
ladanyi@147
   120
      //of dragging
ladanyi@147
   121
      if(canvas.getActualTool()!=CREATE_NODE)
ladanyi@147
   122
      {
ladanyi@147
   123
        canvas.toggleEdgeActivity(this, true);
ladanyi@147
   124
        clicked_x=e->button.x;
ladanyi@147
   125
        clicked_y=e->button.y;
ladanyi@147
   126
        isbutton=true;
ladanyi@147
   127
      }
hegyi@19
   128
      break;
hegyi@19
   129
    case GDK_BUTTON_RELEASE:
ladanyi@147
   130
      if(canvas.getActualTool()!=CREATE_NODE)
ladanyi@147
   131
      {
ladanyi@147
   132
        canvas.toggleEdgeActivity(this, false);
ladanyi@147
   133
        isbutton=false;
ladanyi@147
   134
      }
hegyi@19
   135
      break;
hegyi@19
   136
    case GDK_MOTION_NOTIFY:
hegyi@19
   137
      //we only have to do sg. if the mouse button is pressed
hegyi@19
   138
      if(isbutton)
ladanyi@147
   139
      {
ladanyi@147
   140
        //new coordinates will be the old values,
ladanyi@147
   141
        //because the item will be moved to the
ladanyi@147
   142
        //new coordinate therefore the new movement
ladanyi@147
   143
        //has to be calculated from here
hegyi@19
   144
ladanyi@147
   145
        double dx=e->motion.x-clicked_x;
ladanyi@147
   146
        double dy=e->motion.y-clicked_y;
hegyi@19
   147
ladanyi@147
   148
        Gnome::Canvas::Points points_new;
hegyi@19
   149
ladanyi@147
   150
        canvas.mytab.mapstorage.arrow_pos.set(edge, canvas.mytab.mapstorage.arrow_pos[edge] + XY(dx, dy));
hegyi@19
   151
ladanyi@147
   152
        draw();
ladanyi@147
   153
        canvas.textReposition(canvas.mytab.mapstorage.arrow_pos[edge]);
hegyi@19
   154
ladanyi@147
   155
        clicked_x=e->motion.x;
ladanyi@147
   156
        clicked_y=e->motion.y;
hegyi@19
   157
ladanyi@147
   158
      }
hegyi@19
   159
    default: break;
ladanyi@147
   160
  }
hegyi@19
   161
hegyi@19
   162
  return true;
hegyi@19
   163
}
ladanyi@147
   164
ladanyi@147
   165
void GraphDisplayerCanvas::BrokenEdge::setLineWidth(int w)
ladanyi@147
   166
{
ladanyi@147
   167
  line.property_width_units().set_value(w);
ladanyi@147
   168
}
ladanyi@147
   169
ladanyi@147
   170
void GraphDisplayerCanvas::BrokenEdge::setFillColor(Gdk::Color c)
ladanyi@147
   171
{
ladanyi@147
   172
  line.property_fill_color_gdk().set_value(c);
ladanyi@147
   173
}
ladanyi@147
   174
ladanyi@147
   175
GraphDisplayerCanvas::LoopEdge::LoopEdge(Gnome::Canvas::Group& _group,
ladanyi@147
   176
    Edge _edge, GraphDisplayerCanvas& _canvas) :
ladanyi@151
   177
  EdgeBase(_group, _edge, _canvas), line(*this), isbutton(false)
ladanyi@147
   178
{
ladanyi@151
   179
  arrow.signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::LoopEdge::edgeFormerEventHandler));
ladanyi@151
   180
ladanyi@151
   181
  line.property_outline_color().set_value("green");
ladanyi@151
   182
  line.property_width_units().set_value(10);
ladanyi@147
   183
  line.lower_to_bottom();
ladanyi@147
   184
ladanyi@147
   185
  draw();
ladanyi@147
   186
}
ladanyi@147
   187
ladanyi@147
   188
GraphDisplayerCanvas::LoopEdge::~LoopEdge()
ladanyi@147
   189
{
ladanyi@147
   190
}
ladanyi@147
   191
ladanyi@147
   192
void GraphDisplayerCanvas::LoopEdge::draw()
ladanyi@147
   193
{
ladanyi@147
   194
  MapStorage& ms = canvas.mytab.mapstorage;
ladanyi@147
   195
ladanyi@147
   196
  Node node = ms.graph.source(edge);
ladanyi@147
   197
  XY center = (ms.coords[node] + ms.arrow_pos[edge]) / 2.0;
ladanyi@147
   198
ladanyi@147
   199
  XY unit_vector_in_dir(rot90(center - ms.arrow_pos[edge]));
ladanyi@147
   200
  double length = sqrt(unit_vector_in_dir.normSquare());
ladanyi@147
   201
  unit_vector_in_dir /= length;
ladanyi@147
   202
ladanyi@147
   203
  drawArrow(unit_vector_in_dir);
ladanyi@147
   204
ladanyi@147
   205
  double radius =
ladanyi@147
   206
    sqrt((ms.arrow_pos[edge] - ms.coords[node]).normSquare()) / 2.0;
ladanyi@147
   207
ladanyi@147
   208
  XY p1 = center + XY(-radius,  radius);
ladanyi@147
   209
  XY p2 = center + XY( radius, -radius);
ladanyi@147
   210
  line.property_x1().set_value(p1.x);
ladanyi@147
   211
  line.property_y1().set_value(p1.y);
ladanyi@147
   212
  line.property_x2().set_value(p2.x);
ladanyi@147
   213
  line.property_y2().set_value(p2.y);
ladanyi@147
   214
}
ladanyi@147
   215
ladanyi@147
   216
void GraphDisplayerCanvas::LoopEdge::setLineWidth(int w)
ladanyi@147
   217
{
ladanyi@147
   218
  line.property_width_units().set_value(w);
ladanyi@147
   219
}
ladanyi@147
   220
ladanyi@147
   221
void GraphDisplayerCanvas::LoopEdge::setFillColor(Gdk::Color c)
ladanyi@147
   222
{
ladanyi@151
   223
  line.property_outline_color_gdk().set_value(c);
ladanyi@147
   224
}
ladanyi@151
   225
ladanyi@151
   226
bool GraphDisplayerCanvas::LoopEdge::edgeFormerEventHandler(GdkEvent* e)
ladanyi@151
   227
{
ladanyi@151
   228
  switch(e->type)
ladanyi@151
   229
  {
ladanyi@151
   230
    case GDK_BUTTON_PRESS:
ladanyi@151
   231
      if(canvas.getActualTool()!=CREATE_NODE)
ladanyi@151
   232
      {
ladanyi@151
   233
        canvas.toggleEdgeActivity(this, true);
ladanyi@151
   234
        isbutton=true;
ladanyi@151
   235
      }
ladanyi@151
   236
      break;
ladanyi@151
   237
    case GDK_BUTTON_RELEASE:
ladanyi@151
   238
      if(canvas.getActualTool()!=CREATE_NODE)
ladanyi@151
   239
      {
ladanyi@151
   240
        canvas.toggleEdgeActivity(this, false);
ladanyi@151
   241
        isbutton=false;
ladanyi@151
   242
      }
ladanyi@151
   243
      break;
ladanyi@151
   244
    case GDK_MOTION_NOTIFY:
ladanyi@151
   245
      if(isbutton)
ladanyi@151
   246
      {
ladanyi@151
   247
        canvas.mytab.mapstorage.arrow_pos.set(edge, XY(e->motion.x, e->motion.y));
ladanyi@151
   248
ladanyi@151
   249
        draw();
ladanyi@151
   250
        canvas.textReposition(canvas.mytab.mapstorage.arrow_pos[edge]);
ladanyi@151
   251
      }
ladanyi@151
   252
    default: break;
ladanyi@151
   253
  }
ladanyi@151
   254
  return true;
ladanyi@151
   255
}