main_win.cc
author hegyi
Mon, 02 Oct 2006 18:52:00 +0000
changeset 157 7e6ad28aeb9e
parent 156 c5cdf6690cdf
child 158 aa50a64b3a6e
permissions -rw-r--r--
View settings also for edges.
     1 #ifdef HAVE_CONFIG_H
     2 #include <config.h>
     3 #endif
     4 
     5 #include "main_win.h"
     6 #include "guipixbufs.h"
     7 
     8 #include "i18n.h"
     9 
    10 MainWin::MainWin()
    11 {
    12   set_title ("no file");
    13   set_default_size(WIN_WIDTH,WIN_HEIGHT);
    14   //add(vbox);
    15   add(table);
    16 
    17   // custom icons for the toolbar
    18   Glib::RefPtr<Gtk::IconFactory> p_icon_factory = Gtk::IconFactory::create();
    19  
    20   Glib::RefPtr<Gdk::Pixbuf> p_move_pixbuf = Gdk::Pixbuf::create_from_inline(
    21       2328, gui_icons_move);
    22   Glib::RefPtr<Gdk::Pixbuf> p_addnode_pixbuf = Gdk::Pixbuf::create_from_inline(
    23       2328, gui_icons_addnode);
    24   Glib::RefPtr<Gdk::Pixbuf> p_addlink_pixbuf = Gdk::Pixbuf::create_from_inline(
    25       2328, gui_icons_addlink);
    26   Glib::RefPtr<Gdk::Pixbuf> p_delete_pixbuf = Gdk::Pixbuf::create_from_inline(
    27       2328, gui_icons_delete);
    28   Glib::RefPtr<Gdk::Pixbuf> p_editlink_pixbuf = Gdk::Pixbuf::create_from_inline(
    29       2328, gui_icons_editlink);
    30   Glib::RefPtr<Gdk::Pixbuf> p_newmap_pixbuf = Gdk::Pixbuf::create_from_inline(
    31       2328, gui_icons_newmap);
    32 
    33   Gtk::IconSource move_icon_source;
    34   move_icon_source.set_pixbuf(p_move_pixbuf);
    35   Gtk::IconSet move_icon_set;
    36   move_icon_set.add_source(move_icon_source);
    37   p_icon_factory->add(Gtk::StockID("gd-move"), move_icon_set);
    38 
    39   Gtk::IconSource addnode_icon_source;
    40   addnode_icon_source.set_pixbuf(p_addnode_pixbuf);
    41   Gtk::IconSet addnode_icon_set;
    42   addnode_icon_set.add_source(addnode_icon_source);
    43   p_icon_factory->add(Gtk::StockID("gd-addnode"), addnode_icon_set);
    44 
    45   Gtk::IconSource addlink_icon_source;
    46   addlink_icon_source.set_pixbuf(p_addlink_pixbuf);
    47   Gtk::IconSet addlink_icon_set;
    48   addlink_icon_set.add_source(addlink_icon_source);
    49   p_icon_factory->add(Gtk::StockID("gd-addlink"), addlink_icon_set);
    50 
    51   Gtk::IconSource delete_icon_source;
    52   delete_icon_source.set_pixbuf(p_delete_pixbuf);
    53   Gtk::IconSet delete_icon_set;
    54   delete_icon_set.add_source(delete_icon_source);
    55   p_icon_factory->add(Gtk::StockID("gd-delete"), delete_icon_set);
    56 
    57   Gtk::IconSource editlink_icon_source;
    58   editlink_icon_source.set_pixbuf(p_editlink_pixbuf);
    59   Gtk::IconSet editlink_icon_set;
    60   editlink_icon_set.add_source(editlink_icon_source);
    61   p_icon_factory->add(Gtk::StockID("gd-editlink"), editlink_icon_set);
    62 
    63   Gtk::IconSource newmap_icon_source;
    64   newmap_icon_source.set_pixbuf(p_newmap_pixbuf);
    65   Gtk::IconSet newmap_icon_set;
    66   newmap_icon_set.add_source(newmap_icon_source);
    67   p_icon_factory->add(Gtk::StockID("gd-newmap"), newmap_icon_set);
    68 
    69   p_icon_factory->add_default();
    70   
    71   ag=Gtk::ActionGroup::create();
    72 
    73   ag->add( Gtk::Action::create("FileMenu", _("_File")) );
    74   ag->add( Gtk::Action::create("FileNew", Gtk::Stock::NEW),
    75       sigc::mem_fun(*this, &MainWin::newTab));
    76   ag->add( Gtk::Action::create("FileOpen", Gtk::Stock::OPEN),
    77       sigc::mem_fun(*this, &MainWin::openFile));
    78   ag->add( Gtk::Action::create("FileClearTab", _("Clear Tab")),
    79       sigc::mem_fun(*this, &MainWin::newFile));
    80   ag->add( Gtk::Action::create("FileSave", Gtk::Stock::SAVE),
    81       sigc::mem_fun(*this, &MainWin::saveFile));
    82   ag->add( Gtk::Action::create("FileSaveAs", Gtk::Stock::SAVE_AS),
    83       sigc::mem_fun(*this, &MainWin::saveFileAs));
    84   ag->add( Gtk::Action::create("Close", Gtk::Stock::CLOSE),
    85       sigc::mem_fun(*this, &MainWin::closeTab));
    86   ag->add( Gtk::Action::create("Quit", Gtk::Stock::QUIT),
    87       sigc::mem_fun(*this, &MainWin::hide));
    88 
    89   ag->add( Gtk::Action::create("ViewMenu", _("_View")) );
    90   ag->add( Gtk::Action::create("ViewZoomIn", Gtk::Stock::ZOOM_IN),
    91       sigc::mem_fun(*this, &MainWin::zoomIn));
    92   ag->add( Gtk::Action::create("ViewZoomOut", Gtk::Stock::ZOOM_OUT),
    93       sigc::mem_fun(*this, &MainWin::zoomOut));
    94   ag->add( Gtk::Action::create("ViewZoomFit", Gtk::Stock::ZOOM_FIT),
    95       sigc::mem_fun(*this, &MainWin::zoomFit));
    96   ag->add( Gtk::Action::create("ViewZoom100", Gtk::Stock::ZOOM_100),
    97       sigc::mem_fun(*this, &MainWin::zoom100));
    98   
    99   ag->add( Gtk::Action::create("ShowMenu", _("_Show")) );
   100   ag->add( Gtk::Action::create("ShowMaps", _("_Maps")),
   101 	   sigc::mem_fun(*this, &MainWin::createMapWin));
   102 
   103   ag->add( Gtk::Action::create("AlgoMenu", _("_Algorithms")) );
   104   ag->add( Gtk::Action::create("AlgoGeneral", _("_General")),
   105 	   sigc::bind( sigc::mem_fun ( *this, &MainWin::createAlgoWin ), 0) );
   106   ag->add( Gtk::Action::create("AlgoKruskal", _("_Kruskal")),
   107 	   sigc::bind( sigc::mem_fun ( *this, &MainWin::createAlgoWin ), 1) );
   108 
   109   Gtk::RadioAction::Group tool_group;
   110   ag->add( Gtk::RadioAction::create(tool_group, "MoveItem", Gtk::StockID("gd-move"), _("Move")),
   111       sigc::bind( sigc::mem_fun ( *this, &MainWin::changeEditorialTool ), 0) );
   112   ag->add( Gtk::RadioAction::create(tool_group, "CreateNode", Gtk::StockID("gd-addnode"), _("Create node")),
   113       sigc::bind( sigc::mem_fun ( *this, &MainWin::changeEditorialTool ), 1) );
   114   ag->add( Gtk::RadioAction::create(tool_group, "CreateEdge", Gtk::StockID("gd-addlink"), _("Create edge")),
   115       sigc::bind( sigc::mem_fun ( *this, &MainWin::changeEditorialTool ), 2) );
   116   ag->add( Gtk::RadioAction::create(tool_group, "EraseItem", Gtk::StockID("gd-delete"), _("Delete")),
   117       sigc::bind( sigc::mem_fun ( *this, &MainWin::changeEditorialTool ), 3) );
   118 
   119   ag->add( Gtk::RadioAction::create(tool_group, "EditEdgeMap", Gtk::StockID("gd-editlink"), _("Edit edge map")),
   120       sigc::bind( sigc::mem_fun ( *this, &MainWin::changeEditorialTool ), 4) );
   121 
   122   ag->add( Gtk::Action::create("AddMap", Gtk::StockID("gd-newmap")),
   123       sigc::mem_fun ( *this , &MainWin::createNewMapWin ) );
   124 
   125   uim=Gtk::UIManager::create();
   126   uim->insert_action_group(ag);
   127   add_accel_group(uim->get_accel_group());
   128 
   129   try
   130   {
   131 
   132     Glib::ustring ui_info =
   133       "<ui>"
   134       "  <menubar name='MenuBar'>"
   135       "    <menu action='FileMenu'>"
   136       "      <menuitem action='FileNew'/>"
   137       "      <menuitem action='FileOpen'/>"
   138       "      <menuitem action='FileClearTab'/>"
   139       "      <menuitem action='FileSave'/>"
   140       "      <menuitem action='FileSaveAs'/>"
   141       "      <menuitem action='Close'/>"
   142       "      <menuitem action='Quit'/>"
   143       "    </menu>"
   144       "    <menu action='ViewMenu'>"
   145       "      <menuitem action='ViewZoomIn' />"
   146       "      <menuitem action='ViewZoomOut' />"
   147       "      <menuitem action='ViewZoom100' />"
   148       "      <menuitem action='ViewZoomFit' />"
   149       "    </menu>"
   150       "    <menu action='ShowMenu'>"
   151       "      <menuitem action='ShowMaps'/>"
   152       "    </menu>"
   153       "    <menu action='AlgoMenu'>"
   154       "      <menuitem action='AlgoGeneral'/>"
   155       "      <menuitem action='AlgoKruskal'/>"
   156       "    </menu>"
   157       "  </menubar>"
   158       "  <toolbar name='ToolBar'>"
   159       "    <toolitem action='FileNew' />"
   160       "    <toolitem action='FileOpen' />"
   161       "    <toolitem action='FileSave' />"
   162       "    <toolitem action='Close' />"
   163       "    <separator />"
   164       "    <toolitem action='ViewZoomIn' />"
   165       "    <toolitem action='ViewZoomOut' />"
   166       "    <toolitem action='ViewZoom100' />"
   167       "    <toolitem action='ViewZoomFit' />"
   168       "    <separator />"
   169       "    <toolitem action='MoveItem' />"
   170       "    <toolitem action='CreateNode' />"
   171       "    <toolitem action='CreateEdge' />"
   172       "    <toolitem action='EraseItem' />"
   173       "    <toolitem action='EditEdgeMap' />"
   174       "    <separator />"
   175       "    <toolitem action='AddMap' />"
   176       "  </toolbar>"
   177       "</ui>";
   178 
   179     uim->add_ui_from_string(ui_info);
   180 
   181   }
   182   catch(const Glib::Error& ex)
   183   {
   184     std::cerr << "building menus failed: " <<  ex.what();
   185   }
   186 
   187   Gtk::Widget* menubar = uim->get_widget("/MenuBar");
   188   if (menubar){
   189     //vbox.pack_start(*menubar, Gtk::PACK_SHRINK);
   190     table.attach(*menubar, 0, 1, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::SHRINK);
   191   }
   192 
   193   Gtk::Widget* toolbar = uim->get_widget("/ToolBar");
   194   if (toolbar)
   195   {
   196     static_cast<Gtk::Toolbar*>(toolbar)->set_toolbar_style(Gtk::TOOLBAR_ICONS);
   197     //hbox.pack_start(*toolbar, Gtk::PACK_EXPAND_WIDGET);
   198 
   199     table.attach(*toolbar, 0, 1, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::SHRINK);
   200 
   201   }
   202  
   203   table2.set_row_spacings(10);
   204   table2.set_col_spacings(5);
   205 
   206   auto_scale = new Gtk::CheckButton("Autoscale");
   207   auto_scale->set_active(false);
   208   auto_scale->signal_toggled().connect(sigc::mem_fun(*this, &MainWin::nodeViewChanged));
   209   table2.attach(*auto_scale, 0,2,0,1);
   210 
   211   Gtk::Label * width_label= new Gtk::Label("Edge Width:");
   212   table2.attach(*width_label, 0,1,1,2);
   213   
   214   Gtk::Adjustment * adjustment_width=new Gtk::Adjustment(20, 1, 200, 5, 10);
   215   
   216   edge_width = new Gtk::SpinButton(*adjustment_width, 5,0);
   217   edge_width->signal_value_changed().connect(sigc::mem_fun(*this, &MainWin::nodeViewChanged));
   218   table2.attach(*edge_width, 1,2,1,2);
   219 
   220   Gtk::Label * radius_label= new Gtk::Label("Node Radius:");
   221   table2.attach(*radius_label, 2,3,1,2);
   222   
   223   Gtk::Adjustment * adjustment_radius=new Gtk::Adjustment(20, 0, 500, 5, 10);
   224 
   225   radius_size = new Gtk::SpinButton(*adjustment_radius, 5,0);
   226   radius_size->signal_value_changed().connect(sigc::mem_fun(*this, &MainWin::nodeViewChanged));
   227   table2.attach(*radius_size, 3,4,1,2);
   228 
   229   zoom_track = new Gtk::CheckButton("Zoom tracking");
   230   zoom_track->set_active(false);
   231   zoom_track->signal_toggled().connect(sigc::mem_fun(*this, &MainWin::nodeViewChanged));
   232   table2.attach(*zoom_track, 2,4,0,1);
   233 
   234 
   235   table.attach(table2, 1, 2, 0, 2, Gtk::SHRINK, Gtk::SHRINK);
   236 
   237   tooltips.set_tip(*(uim->get_widget("/ToolBar/CreateNode")),"Create Node");
   238   tooltips.enable();
   239 
   240   active_tab=-1;
   241   notebook.signal_switch_page().connect(sigc::mem_fun(*this, &MainWin::onChangeTab));
   242 
   243   active_tool = MOVE;
   244 
   245   table.attach(notebook,0,2,2,3, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL);
   246   
   247   show_all_children();
   248 }
   249 
   250 void MainWin::set_tabtitle(std::string name)
   251 {
   252   if(strinst.find(name)==strinst.end())
   253     {
   254       tabnames[active_tab]=name;
   255       strinst[name]=1;
   256     }
   257   else
   258     {
   259       strinst[name]++;
   260       std::ostringstream o;
   261       o << strinst[name];
   262       tabnames[active_tab]=name+" - "+o.str();
   263     }
   264   set_title(tabnames[active_tab] + " - " + prog_name);
   265   notebook.set_tab_label_text((Widget&)*(tabs[active_tab]), tabnames[active_tab]);
   266   updateAlgoWinTabs();
   267 }
   268 
   269 void MainWin::readFile(const std::string & filename)
   270 {
   271   newTab();
   272   tabs[active_tab]->readFile(filename);
   273 }
   274 
   275 void MainWin::newTab()
   276 {
   277   int size=tabs.size();
   278   tabs.resize(size+1);
   279   tabnames.resize(size+1);
   280   active_tab=size;
   281   tabs[active_tab]=new NoteBookTab();
   282   tabs[active_tab]->signal_title_ch().connect(sigc::mem_fun(*this, &MainWin::set_tabtitle));
   283   tabs[active_tab]->signal_newmap_needed().connect(sigc::mem_fun(*this, &MainWin::createNewMapWinAfterSignal));
   284   tabs[active_tab]->gd_canvas->changeEditorialTool(active_tool);
   285   notebook.append_page((Gtk::Widget&)(*(tabs[active_tab])));
   286   notebook.set_current_page(size);
   287   set_tabtitle(_("unsaved file"));
   288   updateAlgoWinTabs();
   289 }
   290 
   291 void MainWin::closeTab()
   292 {
   293   if(active_tab!=-1)
   294     {
   295       if (tabs[active_tab]->mapstorage.modified)
   296 	{
   297 	  Gtk::MessageDialog mdialog(_("<b>Save changes before closing?</b>"), true, 
   298 				     Gtk::MESSAGE_WARNING, Gtk::BUTTONS_NONE);
   299 	  mdialog.add_button(_("Close file _without Saving"), Gtk::RESPONSE_REJECT);
   300 	  mdialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
   301 	  mdialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT);
   302 	  switch (mdialog.run())
   303 	    {
   304 	    case Gtk::RESPONSE_CANCEL:
   305 	      return;
   306 	    case Gtk::RESPONSE_REJECT:
   307 	      break;
   308 	    case Gtk::RESPONSE_ACCEPT:
   309 	      tabs[active_tab]->saveFile();
   310 	      break;
   311 	    }
   312 	}
   313       //tabs vector will be decreased with the deleted value
   314       int size=tabs.size();
   315       if(size>1)
   316 	{
   317 	  for(int i=active_tab+1;i<size;i++)
   318 	    {
   319 	      tabnames[i-1]=tabnames[i];
   320 	      tabs[i-1]=tabs[i];
   321 	    }
   322 	}
   323       //if size==1 resize will delete the only element
   324       tabs.resize(size-1);
   325       tabnames.resize(size-1);
   326 
   327       int old_active_tab=active_tab;
   328       notebook.remove_page(active_tab);
   329 
   330       //If the first tab was active, upon delete notebook
   331       //will first switch one tab upper and not lower like
   332       //in the case, when not the first tab was active.
   333       //But after deletion it will become the first tab,
   334       //and this should be registrated in tabs vector,
   335       //as well.
   336       if((old_active_tab==0)&&(size!=1))
   337 	{
   338 	  onChangeTab(NULL,0);
   339 	}
   340 
   341       //if this was the last page in notebook, there is
   342       //no active_tab now
   343       if(size==1)
   344 	{
   345 	  active_tab=-1;
   346 	}
   347 
   348       updateAlgoWinTabs();
   349     }
   350 }
   351 
   352 void MainWin::onChangeTab(GtkNotebookPage* page, guint page_num)
   353 {
   354   page=page;
   355   active_tab=page_num;
   356   tabs[active_tab]->gd_canvas->changeEditorialTool(active_tool);
   357   set_title(tabnames[active_tab]);
   358   bool autoscale;
   359   bool zoomtrack;
   360   double width;
   361   double radius;
   362   double unit;
   363   tabs[active_tab]->getView(autoscale, zoomtrack, width, radius);
   364   edge_width->set_value(width);
   365   radius_size->set_value(radius);
   366   zoom_track->set_active(zoomtrack);
   367   auto_scale->set_active(autoscale);
   368 
   369 }
   370 
   371 void MainWin::newFile()
   372 {
   373   if(active_tab!=-1)
   374     {
   375       tabs[active_tab]->newFile();
   376     }
   377 }
   378  
   379 void MainWin::openFile()
   380 {
   381   if(active_tab==-1)
   382     {
   383       newTab();
   384     }
   385   tabs[active_tab]->openFile();
   386 }
   387  
   388 void MainWin::saveFile()
   389 {
   390   if(active_tab!=-1)
   391     {
   392       tabs[active_tab]->saveFile();
   393     }
   394 }
   395  
   396 void MainWin::saveFileAs()
   397 {
   398   if(active_tab!=-1)
   399     {
   400       tabs[active_tab]->saveFileAs();
   401     }
   402 }
   403  
   404 void MainWin::close()
   405 {
   406   if(active_tab!=-1)
   407     {
   408       tabs[active_tab]->close();
   409     }
   410 }
   411 
   412 void MainWin::zoomIn()
   413 {
   414   if(active_tab!=-1)
   415     {
   416       tabs[active_tab]->gd_canvas->zoomIn();
   417     }
   418 }
   419 
   420 void MainWin::zoomOut()
   421 {
   422   if(active_tab!=-1)
   423     {
   424       tabs[active_tab]->gd_canvas->zoomOut();
   425     }
   426 }
   427 
   428 void MainWin::zoomFit()
   429 {
   430   if(active_tab!=-1)
   431     {
   432       tabs[active_tab]->gd_canvas->zoomFit();
   433     }
   434 }
   435 
   436 void MainWin::zoom100()
   437 {
   438   if(active_tab!=-1)
   439     {
   440       tabs[active_tab]->gd_canvas->zoom100();
   441     }
   442 }
   443 
   444 void MainWin::createMapWin()
   445 {
   446   if(active_tab!=-1)
   447     {
   448       tabs[active_tab]->createMapWin(tabnames[active_tab]);
   449     }
   450 }
   451 
   452 void MainWin::createAlgoWin(int algoid)
   453 {
   454   AlgoWin * aw=new AlgoWin(algoid, tabnames);
   455   aw->signal_closing().connect(sigc::mem_fun(*this, &MainWin::deRegisterAlgoWin));
   456   aw->signal_maplist_needed().connect(sigc::mem_fun(*this, &MainWin::updateAlgoWinMaps));
   457   aw->signal_newmapwin_needed().connect(sigc::mem_fun(*this, &MainWin::createNewMapWinTabString));
   458   aws.insert(aw);
   459   aw->show();
   460 }
   461 
   462 void MainWin::updateAlgoWinTabs()
   463 {
   464   std::set< AlgoWin* >::iterator awsi=aws.begin();
   465   for(;awsi!=aws.end();awsi++)
   466     {
   467       (*awsi)->update_tablist(tabnames);
   468     }
   469 }
   470 
   471 void MainWin::updateAlgoWinMaps(AlgoWin * awp, std::string tabname)
   472 {
   473   int i=0;
   474   for(;(i<(int)tabnames.size())&&(tabnames[i]!=tabname);i++)
   475     {
   476     }
   477   awp->update_maplist(&(tabs[i]->mapstorage));
   478 }
   479 
   480 void MainWin::deRegisterAlgoWin(AlgoWin * awp)
   481 {
   482   aws.erase(awp);
   483 }
   484 
   485 void MainWin::changeEditorialTool(int tool)
   486 {
   487   active_tool=tool;
   488   if(active_tab!=-1)
   489     {
   490       tabs[active_tab]->gd_canvas->changeEditorialTool(tool);
   491     }
   492 }
   493 
   494 void MainWin::createNewMapWin()
   495 {
   496   if(active_tab!=-1)
   497     {
   498       NewMapWin * nmw=new NewMapWin(_("Create New Map - ")+tabnames[active_tab], *(tabs[active_tab]));
   499       nmw->show();
   500     }
   501 }
   502 
   503 void MainWin::createNewMapWinTabString(std::string tabname, bool itisedge)
   504 {
   505   int i=0;
   506   for(;((i<(int)tabnames.size())&&(tabnames[i]!=tabname));i++)
   507     {
   508     }
   509   createNewMapWinAfterSignal(tabs[i], itisedge);
   510 }
   511 
   512 void MainWin::createNewMapWinAfterSignal(NoteBookTab * nbt, bool itisedge)
   513 {
   514   std::vector<NoteBookTab*>::iterator nbti=tabs.begin();
   515   int i=0;
   516   for(;nbti!=tabs.end();nbti++)
   517     {
   518       if(*nbti!=nbt)
   519 	{
   520 	  i++;
   521 	}
   522       else
   523 	{
   524 	  continue;
   525 	}
   526     }
   527   NewMapWin * nmw=new NewMapWin(_("Create New Map - ")+tabnames[i], *nbt, itisedge, false);
   528   nmw->run();
   529 }
   530 
   531 
   532 void MainWin::nodeViewChanged()
   533 {
   534   double width=edge_width->get_value();
   535   double radius=radius_size->get_value();
   536   double unit;
   537   bool zoomtrack=zoom_track->get_active();
   538   bool autoscale=auto_scale->get_active();
   539   tabs[active_tab]->setView(autoscale, zoomtrack, width, radius);
   540 }