COIN-OR::LEMON - Graph Library

Changeset 63:59768817442a in glemon-0.x


Ignore:
Timestamp:
08/23/05 09:36:09 (19 years ago)
Author:
Akos Ladanyi
Branch:
gui
Phase:
public
Convert:
svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk/gui@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)
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • broken_edge.cc

    r59 r63  
    99  *arrow << Gnome::Canvas::Properties::fill_color("red");
    1010  arrow->signal_event().connect(sigc::mem_fun(*this, &BrokenEdge::edgeFormerEventHandler));
     11  arrow->lower_to_bottom();
    1112  setPoints(p);
    1213}
  • graph_displayer_canvas-edge.cc

    r62 r63  
    123123  //in that case empty string will be written, because
    124124  //that is the deleter map
    125 
     125 
    126126  if(edge==INVALID)
    127127    {
  • graph_displayer_canvas-event.cc

    r62 r63  
    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;
  • graph_displayer_canvas.cc

    r59 r63  
    6363    *(edgesmap[i]) << Gnome::Canvas::Properties::fill_color("green");
    6464    edgesmap[i]->property_width_units().set_value(10);   
     65    edgesmap[i]->lower_to_bottom();
    6566   
    6667    //initializing edge-text as well, to empty string
     
    7273    edgetextmap[i]->property_fill_color().set_value("darkgreen");
    7374    edgetextmap[i]->signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::edgeMapEditEventHandler), false);
     75    edgetextmap[i]->raise_to_top();
    7476  }
    7577
     
    8890    *(nodesmap[i]) << Gnome::Canvas::Properties::fill_color("blue");
    8991    *(nodesmap[i]) << Gnome::Canvas::Properties::outline_color("black");
     92    nodesmap[i]->raise_to_top();
    9093
    9194    //initializing edge-text as well, to empty string
     
    99102    nodetextmap[i]->property_fill_color().set_value("darkblue");
    100103    nodetextmap[i]->signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::nodeMapEditEventHandler), false);
     104    nodetextmap[i]->raise_to_top();
    101105  }
    102106
  • main_win.cc

    r61 r63  
    265265  {
    266266    Glib::ustring filename = fcdialog.get_filename();
    267     mapstorage.readFromFile(filename);
    268     mapstorage.file_name = filename;
    269     mapstorage.modified = false;
    270     gd_canvas.drawGraph();
    271     mapwin.update();
     267    if (!mapstorage.readFromFile(filename))
     268    {
     269      mapstorage.file_name = filename;
     270      mapstorage.modified = false;
     271      gd_canvas.drawGraph();
     272      mapwin.update();
     273    }
    272274  }
    273275}
  • mapstorage.cc

    r62 r63  
    11#include "mapstorage.h"
     2#include <gtkmm.h>
    23#include <cmath>
    34
     
    2728}
    2829
    29 int MapStorage::addNodeMap(const std::string & name, Graph::NodeMap<double> *nodemap)
     30int MapStorage::addNodeMap(const std::string & name, Graph::NodeMap<double> *nodemap, double default_value = 0.0)
    3031{
    3132  if( nodemap_storage.find(name) == nodemap_storage.end() )
    3233    {
    3334      nodemap_storage[name]=nodemap;
     35      // set the maps default value
     36      nodemap_default[name] = default_value;
    3437      return 0;
    3538    }
     
    3740}
    3841
    39 int MapStorage::addEdgeMap(const std::string & name, Graph::EdgeMap<double> *edgemap)
     42int MapStorage::addEdgeMap(const std::string & name, Graph::EdgeMap<double> *edgemap, double default_value = 0.0)
    4043{
    4144  if( edgemap_storage.find(name) == edgemap_storage.end() )
    4245    {
    4346      edgemap_storage[name]=edgemap;
     47      // set the maps default value
     48      edgemap_default[name] = default_value;
    4449      return 0;
    4550    }
     
    117122}
    118123
    119 void MapStorage::initMapsForEdge(Edge e)
    120 {
    121   std::map< std::string,Graph::EdgeMap<double> * >::iterator ems_it;
    122   for(ems_it=edgemap_storage.begin();ems_it!=edgemap_storage.end();ems_it++)
    123     {
    124       (*((*ems_it).second))[e]=5;
    125     }
    126 }
    127 
    128 void MapStorage::readFromFile(const std::string &filename)
     124int MapStorage::readFromFile(const std::string &filename)
    129125{
    130126  bool read_x = false;
     
    180176    greader.run();
    181177  } catch (DataFormatError& error) {
    182     /*
    183178    Gtk::MessageDialog mdialog("Read Error");
    184179    mdialog.set_message(error.what());
    185180    mdialog.run();
    186     */
    187     // reset graph and mapstorage ?
    188     return;
     181    clear();
     182    return 1;
    189183  }
    190184
     
    206200    }
    207201  }
     202
     203  // fill in the default values for the maps
     204  for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
     205      nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
     206  {
     207    if ((it->first != "id") &&
     208        (it->first != "coordiantes_x") &&
     209        (it->first != "coordinates_y"))
     210    {
     211      nodemap_default[it->first] = 0.0;
     212    }
     213  }
     214  for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
     215      edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
     216  {
     217    if (it->first != "id")
     218    {
     219      edgemap_default[it->first] = 0.0;
     220    }
     221  }
     222
     223  return 0;
    208224}
    209225
     
    249265    }
    250266  }
     267  for (std::map<std::string, double>::iterator it =
     268      nodemap_default.begin(); it != nodemap_default.end(); ++it)
     269  {
     270    nodemap_default.erase(it);
     271  }
     272  for (std::map<std::string, double>::iterator it =
     273      edgemap_default.begin(); it != edgemap_default.end(); ++it)
     274  {
     275    edgemap_default.erase(it);
     276  }
    251277  graph.clear();
    252278  file_name = "";
  • mapstorage.h

    r53 r63  
    3838  std::vector<Graph::EdgeMap<double> > default_edgemaps;
    3939
     40  // Default values for the maps
     41  std::map< std::string, double > nodemap_default;
     42
     43  // Default values for the maps
     44  std::map< std::string, double > edgemap_default;
     45
    4046public:
    4147  ///Constructor of MapStorage. Expects the Graph of
     
    5359  ///\nodemap is the pointer of the given nodemap
    5460  ///\todo map should be given by reference!
    55   int addNodeMap(const std::string &,Graph::NodeMap<double> *);
     61  int addNodeMap(const std::string &,Graph::NodeMap<double> *, double);
    5662
    5763  ///Adds given map to storage. A name and the map itself has to be provided.
     
    5965  ///\edgemap is the pointer of the given edgemap
    6066  ///\todo map should be given by reference!
    61   int addEdgeMap(const std::string &,Graph::EdgeMap<double> *);
     67  int addEdgeMap(const std::string &,Graph::EdgeMap<double> *, double);
    6268
    6369  ///Returns how much nodemaps is stored in \ref MapStorage
     
    95101  std::map< std::string,Graph::EdgeMap<double> * >::iterator endOfEdgeMaps(){return edgemap_storage.end();};
    96102
    97   ///This function sets a default base value for the newly created node
    98   void initMapsForNode(NodeIt);
    99 
    100   ///This function sets a default base value for the newly created node
    101   void initMapsForEdge(Graph::Edge);
    102 
    103   void readFromFile(const std::string &);
     103  int readFromFile(const std::string &);
    104104  void writeToFile(const std::string &);
    105105
Note: See TracChangeset for help on using the changeset viewer.