COIN-OR::LEMON - Graph Library

Ignore:
Timestamp:
08/23/05 09:36:09 (19 years ago)
Author:
Akos Ladanyi
Branch:
default
Phase:
public
Convert:
svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@2158
Message:
  • id maps are not editable
  • handle exceptions thrown by the file reader
  • texts are always above the edges
  • store a default value for all maps, so that edges and nodes created after adding a new map receive the default value too
  • create node on button release, not on click (fixes a few oddities)
File:
1 edited

Legend:

Unmodified
Added
Removed
  • gui/graph_displayer_canvas-event.cc

    r1643 r1645  
    215215{
    216216  switch(e->type)
    217     {
    218 
    219       //draw the new node in red at the clicked place
    220     case GDK_2BUTTON_PRESS:
    221       //std::cout << "double click" << std::endl;
    222       break;
    223     case GDK_BUTTON_PRESS:
     217  {
     218    //move the new node
     219    case GDK_MOTION_NOTIFY:
     220      {
     221        GdkEvent * generated=new GdkEvent();
     222        generated->motion.x=e->motion.x;
     223        generated->motion.y=e->motion.y;
     224        generated->type=GDK_MOTION_NOTIFY;
     225        moveEventHandler(generated);     
     226        break;
     227      }
     228
     229    case GDK_BUTTON_RELEASE:
    224230      mapstorage.modified = true;
    225231
     
    232238      window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
    233239
     240      // update coordinates
    234241      mapstorage.coords.set(active_node, xy<double>(clicked_x, clicked_y));
     242
     243      // update id map
    235244      (*mapstorage.nodemap_storage["id"])[active_node] =
    236245        mapstorage.graph.id(active_node);
    237246
     247      // update all other maps
     248      for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
     249          mapstorage.nodemap_storage.begin(); it !=
     250          mapstorage.nodemap_storage.end(); ++it)
     251      {
     252        if ((it->first != "id") &&
     253            (it->first != "coordinates_x") &&
     254            (it->first != "coordiantes_y"))
     255        {
     256          (*(it->second))[active_node] =
     257            mapstorage.nodemap_default[it->first];
     258        }
     259      }
     260
    238261      nodesmap[active_node]=new Gnome::Canvas::Ellipse(displayed_graph,
    239         clicked_x-20, clicked_y-20, clicked_x+20, clicked_y+20);
     262          clicked_x-20, clicked_y-20, clicked_x+20, clicked_y+20);
    240263      active_item=(Gnome::Canvas::Item *)(nodesmap[active_node]);
    241       *(nodesmap[active_node]) << Gnome::Canvas::Properties::fill_color("red");
    242       *(nodesmap[active_node]) << Gnome::Canvas::Properties::outline_color("black");
     264      *(nodesmap[active_node]) <<
     265        Gnome::Canvas::Properties::fill_color("blue");
     266      *(nodesmap[active_node]) <<
     267        Gnome::Canvas::Properties::outline_color("black");
     268      active_item->raise_to_top();
     269
    243270      (nodesmap[active_node])->show();
    244271
    245272      nodetextmap[active_node]=new Gnome::Canvas::Text(displayed_graph,
    246         clicked_x+node_property_defaults[N_RADIUS]+5,
    247         clicked_y+node_property_defaults[N_RADIUS]+5, "");
     273          clicked_x+node_property_defaults[N_RADIUS]+5,
     274          clicked_y+node_property_defaults[N_RADIUS]+5, "");
    248275      nodetextmap[active_node]->property_fill_color().set_value("darkblue");
     276      nodetextmap[active_node]->raise_to_top();
    249277
    250278      mapwin.updateNode(active_node);
    251279
    252       break;
    253 
    254       //move the new node
    255     case GDK_MOTION_NOTIFY:
    256       {
    257         GdkEvent * generated=new GdkEvent();
    258         generated->motion.x=e->motion.x;
    259         generated->motion.y=e->motion.y;
    260         generated->type=GDK_MOTION_NOTIFY;
    261         moveEventHandler(generated);     
    262         break;
    263       }
    264 
    265       //finalize the new node
    266     case GDK_BUTTON_RELEASE:
    267       window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
    268 
    269       active_item->lower_to_bottom();
    270 
    271       target_item=NULL;
    272       target_item=get_item_at(clicked_x, clicked_y);
    273 
    274       active_item->raise_to_top();
    275 
    276280      isbutton=0;
    277       if(target_item==active_item)
    278         {
    279           //Its appropriate color is given by update.
    280           *active_item << Gnome::Canvas::Properties::fill_color("blue");
    281         }
    282       else
    283         {
    284           //In this case the given color has to be overwritten, because the noe covers an other item.
    285           *active_item << Gnome::Canvas::Properties::fill_color("lightblue");
    286         }
    287281      target_item=NULL;
    288282      active_item=NULL;
    289283      active_node=INVALID;
     284
    290285      break;
    291286    default:
    292287      break;
    293     }
     288  }
    294289  return false;
    295290}
     
    298293{
    299294  switch(e->type)
    300     {
     295  {
    301296    case GDK_BUTTON_PRESS:
    302297      //in edge creation right button has special meaning
    303298      if(e->button.button!=3)
    304         {
    305           //there is not yet selected node
    306           if(active_node==INVALID)
    307             {
    308               //we mark the location of the event to be able to calculate parameters of dragging
    309 
    310               window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
    311 
    312               active_item=(get_item_at(clicked_x, clicked_y));
    313               active_node=INVALID;
    314               for (NodeIt i(mapstorage.graph); i!=INVALID; ++i)
    315                 {
    316                   if(nodesmap[i]==active_item)
    317                     {
    318                       active_node=i;
    319                     }
    320                 }
    321               //the clicked item is really a node
    322               if(active_node!=INVALID)
    323                 {
    324                   *(nodesmap[active_node]) << Gnome::Canvas::Properties::fill_color("red");
    325                   isbutton=1;
    326                 }
    327               //clicked item was not a node. It could be e.g. edge.
    328               else
    329                 {
    330                   active_item=NULL;
    331                 }
    332             }
    333           //we only have to do sg. if the mouse button
    334           // is pressed already once AND the click was
    335           // on a node that was found in the set of
    336           //nodes, and now we only search for the second
    337           //node
    338           else
    339             {
    340               window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
    341               target_item=(get_item_at(clicked_x, clicked_y));
    342               Node target_node=INVALID;
    343               for (NodeIt i(mapstorage.graph); i!=INVALID; ++i)
    344                 {
    345                   if(nodesmap[i]==target_item)
    346                     {
    347                       target_node=i;
    348                     }
    349                 }
    350               //the clicked item is a node, the edge can be drawn
    351               if(target_node!=INVALID)
    352                 {
    353                   if(target_node!=active_node)         
    354                     {
    355           mapstorage.modified = true;
    356 
    357                       *(nodesmap[target_node]) <<
    358                         Gnome::Canvas::Properties::fill_color("red");
    359 
    360                       //creating new edge
    361                       active_edge=mapstorage.graph.addEdge(active_node,
    362                                                            target_node);
    363 
    364                       //initiating values corresponding to new edge in maps
    365                       mapstorage.initMapsForEdge(active_edge);
    366                       (*mapstorage.edgemap_storage["id"])[active_edge] =
    367                         mapstorage.graph.id(active_edge);
    368          
    369                       //calculating coordinates of new edge
    370                       Gnome::Canvas::Points coos;
    371                       double x1, x2, y1, y2;
    372          
    373                       active_item->get_bounds(x1, y1, x2, y2);
    374                       coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
    375 
    376                       target_item->get_bounds(x1, y1, x2, y2);
    377                       coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
    378 
    379                       //drawing new edge
    380                       edgesmap[active_edge]=new BrokenEdge(displayed_graph, coos, *this);
    381                       *(edgesmap[active_edge]) << Gnome::Canvas::Properties::fill_color("green");
    382                       edgesmap[active_edge]->property_width_pixels().set_value(10);
    383 
    384                       //redraw nodes to blank terminations of the new edge
    385                       target_item->raise_to_top();
    386                       active_item->raise_to_top();
    387 
    388                       //initializing edge-text as well, to empty string
    389                       xy<double> text_pos=edgesmap[active_edge]->getArrowPos();
    390                       text_pos+=(xy<double>(10,10));
    391 
    392                       edgetextmap[active_edge]=new Gnome::Canvas::Text(displayed_graph, text_pos.x, text_pos.y, "");
    393                       edgetextmap[active_edge]->property_fill_color().set_value("darkgreen");
    394 
    395                       //updating its properties
    396                       mapwin.updateEdge(active_edge);
    397                     }
    398                   else
    399                     {
    400                       target_node=INVALID;
    401                       std::cerr << "Loop edge is not yet implemented!" << std::endl;
    402                     }
    403                 }
    404               //clicked item was not a node. it could be an e.g. edge. we do not deal with it furthermore.
    405               else
    406                 {
    407                   target_item=NULL;
    408                 }
    409             }
    410         }
     299      {
     300        //there is not yet selected node
     301        if(active_node==INVALID)
     302        {
     303          //we mark the location of the event to be able to calculate parameters of dragging
     304
     305          window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
     306
     307          active_item=(get_item_at(clicked_x, clicked_y));
     308          active_node=INVALID;
     309          for (NodeIt i(mapstorage.graph); i!=INVALID; ++i)
     310          {
     311            if(nodesmap[i]==active_item)
     312            {
     313              active_node=i;
     314            }
     315          }
     316          //the clicked item is really a node
     317          if(active_node!=INVALID)
     318          {
     319            *(nodesmap[active_node]) << Gnome::Canvas::Properties::fill_color("red");
     320            isbutton=1;
     321          }
     322          //clicked item was not a node. It could be e.g. edge.
     323          else
     324          {
     325            active_item=NULL;
     326          }
     327        }
     328        //we only have to do sg. if the mouse button
     329        // is pressed already once AND the click was
     330        // on a node that was found in the set of
     331        //nodes, and now we only search for the second
     332        //node
     333        else
     334        {
     335          window_to_world (e->button.x, e->button.y, clicked_x, clicked_y);
     336          target_item=(get_item_at(clicked_x, clicked_y));
     337          Node target_node=INVALID;
     338          for (NodeIt i(mapstorage.graph); i!=INVALID; ++i)
     339          {
     340            if(nodesmap[i]==target_item)
     341            {
     342              target_node=i;
     343            }
     344          }
     345          //the clicked item is a node, the edge can be drawn
     346          if(target_node!=INVALID)
     347          {
     348            if(target_node!=active_node)               
     349            {
     350              mapstorage.modified = true;
     351
     352              *(nodesmap[target_node]) <<
     353                Gnome::Canvas::Properties::fill_color("red");
     354
     355              //creating new edge
     356              active_edge=mapstorage.graph.addEdge(active_node,
     357                  target_node);
     358
     359              // update id map
     360              (*mapstorage.edgemap_storage["id"])[active_edge] =
     361                mapstorage.graph.id(active_edge);
     362 
     363              // update all other maps
     364              for (std::map<std::string,
     365                  Graph::EdgeMap<double>*>::const_iterator it =
     366                  mapstorage.edgemap_storage.begin(); it !=
     367                  mapstorage.edgemap_storage.end(); ++it)
     368              {
     369                if (it->first != "id")
     370                {
     371                  (*(it->second))[active_edge] =
     372                    mapstorage.edgemap_default[it->first];
     373                }
     374              }
     375
     376              //calculating coordinates of new edge
     377              Gnome::Canvas::Points coos;
     378              double x1, x2, y1, y2;
     379
     380              active_item->get_bounds(x1, y1, x2, y2);
     381              coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
     382
     383              target_item->get_bounds(x1, y1, x2, y2);
     384              coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
     385
     386              //drawing new edge
     387              edgesmap[active_edge]=new BrokenEdge(displayed_graph, coos,
     388                  *this);
     389              *(edgesmap[active_edge]) <<
     390                Gnome::Canvas::Properties::fill_color("green");
     391              edgesmap[active_edge]->property_width_pixels().set_value(10);
     392
     393              edgesmap[active_edge]->lower_to_bottom();
     394
     395              //initializing edge-text as well, to empty string
     396              xy<double> text_pos=edgesmap[active_edge]->getArrowPos();
     397              text_pos+=(xy<double>(10,10));
     398
     399              edgetextmap[active_edge]=new Gnome::Canvas::Text(displayed_graph,
     400                  text_pos.x, text_pos.y, "");
     401              edgetextmap[active_edge]->property_fill_color().set_value(
     402                  "darkgreen");
     403              edgetextmap[active_edge]->raise_to_top();
     404
     405              //updating its properties
     406              mapwin.updateEdge(active_edge);
     407            }
     408            else
     409            {
     410              target_node=INVALID;
     411              std::cerr << "Loop edge is not yet implemented!" << std::endl;
     412            }
     413          }
     414          //clicked item was not a node. it could be an e.g. edge. we do not
     415          //deal with it furthermore.
     416          else
     417          {
     418            target_item=NULL;
     419          }
     420        }
     421      }
    411422      break;
    412423    case GDK_BUTTON_RELEASE:
     
    416427      //2: the edge creation is cancelled with right button
    417428      if((target_item)||(e->button.button==3))
    418         {
    419           if(active_item)
    420             {
    421               *active_item << Gnome::Canvas::Properties::fill_color("blue");
    422               active_item=NULL;
    423             }
    424           if(target_item)
    425             {
    426               *target_item << Gnome::Canvas::Properties::fill_color("blue");
    427               target_item=NULL;
    428             }
    429           active_node=INVALID;
    430           active_edge=INVALID;
    431         }
     429      {
     430        if(active_item)
     431        {
     432          *active_item << Gnome::Canvas::Properties::fill_color("blue");
     433          active_item=NULL;
     434        }
     435        if(target_item)
     436        {
     437          *target_item << Gnome::Canvas::Properties::fill_color("blue");
     438          target_item=NULL;
     439        }
     440        active_node=INVALID;
     441        active_edge=INVALID;
     442      }
    432443      break;
    433444    default:
    434445      break;
    435     }
     446  }
    436447  return false;
    437448}
     
    589600            if(clicked_edge!=INVALID)
    590601              {
     602                // the id map is not editable
     603                if (nodemap_to_edit == "id") return 0;
    591604                //If there is already edited edge, it has to be saved first
    592605                if(entrywidget.is_visible())
     
    712725            if(clicked_node!=INVALID)
    713726              {
     727                // the id map is not editable
     728                if (nodemap_to_edit == "id") return 0;
    714729                //If there is already edited edge, it has to be saved first
    715730                if(entrywidget.is_visible())
     
    914929  //if addition was not successful addEdgeMap returns one.
    915930  //cause can be that there is already a map named like the new one
    916   if(mapstorage.addEdgeMap(mapname,emptr))
     931  if(mapstorage.addEdgeMap(mapname,emptr, default_value))
    917932    {
    918933      return 1;
     
    936951  //if addition was not successful addNodeMap returns one.
    937952  //cause can be that there is already a map named like the new one
    938   if(mapstorage.addNodeMap(mapname,emptr))
     953  if(mapstorage.addNodeMap(mapname,emptr, default_value))
    939954    {
    940955      return 1;
Note: See TracChangeset for help on using the changeset viewer.