COIN-OR::LEMON - Graph Library

source: glemon-0.x/main_win.cc @ 165:2cd447b0bd3a

Last change on this file since 165:2cd447b0bd3a was 165:2cd447b0bd3a, checked in by Hegyi Péter, 15 years ago

Suurballe algorithm is implemented in glemon.

File size: 16.5 KB
Line 
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
10MainWin::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  ag->add( Gtk::Action::create("ShowDesign", _("_Design")),
103           sigc::mem_fun(*this, &MainWin::createDesignWin));
104
105  ag->add( Gtk::Action::create("AlgoMenu", _("_Algorithms")) );
106  ag->add( Gtk::Action::create("AlgoGeneral", _("_General")),
107           sigc::bind( sigc::mem_fun ( *this, &MainWin::createAlgoWin ), 0) );
108  ag->add( Gtk::Action::create("AlgoKruskal", _("_Kruskal")),
109           sigc::bind( sigc::mem_fun ( *this, &MainWin::createAlgoWin ), 1) );
110  ag->add( Gtk::Action::create("AlgoDijkstra", _("_Dijkstra")),
111           sigc::bind( sigc::mem_fun ( *this, &MainWin::createAlgoWin ), 2) );
112  ag->add( Gtk::Action::create("AlgoSuurballe", _("_Suurballe")),
113           sigc::bind( sigc::mem_fun ( *this, &MainWin::createAlgoWin ), 3) );
114
115  Gtk::RadioAction::Group tool_group;
116  ag->add( Gtk::RadioAction::create(tool_group, "MoveItem", Gtk::StockID("gd-move"), _("Move")),
117      sigc::bind( sigc::mem_fun ( *this, &MainWin::changeEditorialTool ), 0) );
118  ag->add( Gtk::RadioAction::create(tool_group, "CreateNode", Gtk::StockID("gd-addnode"), _("Create node")),
119      sigc::bind( sigc::mem_fun ( *this, &MainWin::changeEditorialTool ), 1) );
120  ag->add( Gtk::RadioAction::create(tool_group, "CreateEdge", Gtk::StockID("gd-addlink"), _("Create edge")),
121      sigc::bind( sigc::mem_fun ( *this, &MainWin::changeEditorialTool ), 2) );
122  ag->add( Gtk::RadioAction::create(tool_group, "EraseItem", Gtk::StockID("gd-delete"), _("Delete")),
123      sigc::bind( sigc::mem_fun ( *this, &MainWin::changeEditorialTool ), 3) );
124
125  ag->add( Gtk::RadioAction::create(tool_group, "EditEdgeMap", Gtk::StockID("gd-editlink"), _("Edit edge map")),
126      sigc::bind( sigc::mem_fun ( *this, &MainWin::changeEditorialTool ), 4) );
127
128  ag->add( Gtk::Action::create("AddMap", Gtk::StockID("gd-newmap")),
129      sigc::mem_fun ( *this , &MainWin::createNewMapWin ) );
130
131  ag->add( Gtk::Action::create("DesignGraph", Gtk::Stock::REFRESH),
132      sigc::mem_fun ( *this , &MainWin::reDesignGraph ) );
133
134  uim=Gtk::UIManager::create();
135  uim->insert_action_group(ag);
136  add_accel_group(uim->get_accel_group());
137
138  try
139  {
140
141    Glib::ustring ui_info =
142      "<ui>"
143      "  <menubar name='MenuBar'>"
144      "    <menu action='FileMenu'>"
145      "      <menuitem action='FileNew'/>"
146      "      <menuitem action='FileOpen'/>"
147      "      <menuitem action='FileClearTab'/>"
148      "      <menuitem action='FileSave'/>"
149      "      <menuitem action='FileSaveAs'/>"
150      "      <menuitem action='Close'/>"
151      "      <menuitem action='Quit'/>"
152      "    </menu>"
153      "    <menu action='ViewMenu'>"
154      "      <menuitem action='ViewZoomIn' />"
155      "      <menuitem action='ViewZoomOut' />"
156      "      <menuitem action='ViewZoom100' />"
157      "      <menuitem action='ViewZoomFit' />"
158      "    </menu>"
159      "    <menu action='ShowMenu'>"
160      "      <menuitem action='ShowMaps'/>"
161      "      <menuitem action='ShowDesign'/>"
162      "    </menu>"
163      "    <menu action='AlgoMenu'>"
164      "      <menuitem action='AlgoGeneral'/>"
165      "      <menuitem action='AlgoKruskal'/>"
166      "      <menuitem action='AlgoDijkstra'/>"
167      "      <menuitem action='AlgoSuurballe'/>"
168      "    </menu>"
169      "  </menubar>"
170      "  <toolbar name='ToolBar'>"
171      "    <toolitem action='FileNew' />"
172      "    <toolitem action='FileOpen' />"
173      "    <toolitem action='FileSave' />"
174      "    <toolitem action='Close' />"
175      "    <separator />"
176      "    <toolitem action='ViewZoomIn' />"
177      "    <toolitem action='ViewZoomOut' />"
178      "    <toolitem action='ViewZoom100' />"
179      "    <toolitem action='ViewZoomFit' />"
180      "    <separator />"
181      "    <toolitem action='MoveItem' />"
182      "    <toolitem action='CreateNode' />"
183      "    <toolitem action='CreateEdge' />"
184      "    <toolitem action='EraseItem' />"
185      "    <toolitem action='EditEdgeMap' />"
186      "    <separator />"
187      "    <toolitem action='AddMap' />"
188      "    <toolitem action='DesignGraph' />"
189      "  </toolbar>"
190      "</ui>";
191
192    uim->add_ui_from_string(ui_info);
193
194  }
195  catch(const Glib::Error& ex)
196  {
197    std::cerr << "building menus failed: " <<  ex.what();
198  }
199
200  Gtk::Widget* menubar = uim->get_widget("/MenuBar");
201  if (menubar){
202    //vbox.pack_start(*menubar, Gtk::PACK_SHRINK);
203    table.attach(*menubar, 0, 1, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::SHRINK);
204  }
205
206  Gtk::Widget* toolbar = uim->get_widget("/ToolBar");
207  if (toolbar)
208  {
209    static_cast<Gtk::Toolbar*>(toolbar)->set_toolbar_style(Gtk::TOOLBAR_ICONS);
210    //hbox.pack_start(*toolbar, Gtk::PACK_EXPAND_WIDGET);
211
212    table.attach(*toolbar, 0, 1, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::SHRINK);
213
214  }
215 
216  table2.set_row_spacings(10);
217  table2.set_col_spacings(5);
218
219  auto_scale = new Gtk::CheckButton("Autoscale");
220  auto_scale->set_active(false);
221  auto_scale->signal_toggled().connect(sigc::mem_fun(*this, &MainWin::nodeViewChanged));
222  table2.attach(*auto_scale, 0,2,0,1);
223
224  Gtk::Label * width_label= new Gtk::Label("Edge Width:");
225  table2.attach(*width_label, 0,1,1,2);
226 
227  Gtk::Adjustment * adjustment_width=new Gtk::Adjustment(20, 1, 200, 5, 10);
228 
229  edge_width = new Gtk::SpinButton(*adjustment_width, 5,0);
230  edge_width->signal_value_changed().connect(sigc::mem_fun(*this, &MainWin::nodeViewChanged));
231  table2.attach(*edge_width, 1,2,1,2);
232
233  Gtk::Label * radius_label= new Gtk::Label("Node Radius:");
234  table2.attach(*radius_label, 2,3,1,2);
235 
236  Gtk::Adjustment * adjustment_radius=new Gtk::Adjustment(20, 0, 500, 5, 10);
237
238  radius_size = new Gtk::SpinButton(*adjustment_radius, 5,0);
239  radius_size->signal_value_changed().connect(sigc::mem_fun(*this, &MainWin::nodeViewChanged));
240  table2.attach(*radius_size, 3,4,1,2);
241
242  zoom_track = new Gtk::CheckButton("Zoom tracking");
243  zoom_track->set_active(false);
244  zoom_track->signal_toggled().connect(sigc::mem_fun(*this, &MainWin::nodeViewChanged));
245  table2.attach(*zoom_track, 2,4,0,1);
246
247
248  table.attach(table2, 1, 2, 0, 2, Gtk::SHRINK, Gtk::SHRINK);
249
250  tooltips.set_tip(*(uim->get_widget("/ToolBar/CreateNode")),"Create Node");
251  tooltips.enable();
252
253  active_tab=-1;
254  notebook.signal_switch_page().connect(sigc::mem_fun(*this, &MainWin::onChangeTab));
255
256  active_tool = MOVE;
257
258  table.attach(notebook,0,2,2,3, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL);
259 
260  show_all_children();
261}
262
263void MainWin::set_tabtitle(std::string name)
264{
265  if(strinst.find(name)==strinst.end())
266    {
267      tabnames[active_tab]=name;
268      strinst[name]=1;
269    }
270  else
271    {
272      strinst[name]++;
273      std::ostringstream o;
274      o << strinst[name];
275      tabnames[active_tab]=name+" - "+o.str();
276    }
277  set_title(tabnames[active_tab] + " - " + prog_name);
278  notebook.set_tab_label_text((Widget&)*(tabs[active_tab]), tabnames[active_tab]);
279  updateAlgoWinTabs();
280}
281
282void MainWin::readFile(const std::string & filename)
283{
284  newTab();
285  tabs[active_tab]->readFile(filename);
286}
287
288void MainWin::newTab()
289{
290  int size=tabs.size();
291  tabs.resize(size+1);
292  tabnames.resize(size+1);
293  active_tab=size;
294  tabs[active_tab]=new NoteBookTab();
295  tabs[active_tab]->signal_title_ch().connect(sigc::mem_fun(*this, &MainWin::set_tabtitle));
296  tabs[active_tab]->signal_newmap_needed().connect(sigc::mem_fun(*this, &MainWin::createNewMapWinAfterSignal));
297  tabs[active_tab]->gd_canvas->changeEditorialTool(active_tool);
298  notebook.append_page((Gtk::Widget&)(*(tabs[active_tab])));
299  notebook.set_current_page(size);
300  set_tabtitle(_("unsaved file"));
301  updateAlgoWinTabs();
302}
303
304void MainWin::closeTab()
305{
306  if(active_tab!=-1)
307    {
308      if (tabs[active_tab]->mapstorage.modified)
309        {
310          Gtk::MessageDialog mdialog(_("<b>Save changes before closing?</b>"), true,
311                                     Gtk::MESSAGE_WARNING, Gtk::BUTTONS_NONE);
312          mdialog.add_button(_("Close file _without Saving"), Gtk::RESPONSE_REJECT);
313          mdialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
314          mdialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT);
315          switch (mdialog.run())
316            {
317            case Gtk::RESPONSE_CANCEL:
318              return;
319            case Gtk::RESPONSE_REJECT:
320              break;
321            case Gtk::RESPONSE_ACCEPT:
322              tabs[active_tab]->saveFile();
323              break;
324            }
325        }
326      //tabs vector will be decreased with the deleted value
327      int size=tabs.size();
328      if(size>1)
329        {
330          for(int i=active_tab+1;i<size;i++)
331            {
332              tabnames[i-1]=tabnames[i];
333              tabs[i-1]=tabs[i];
334            }
335        }
336      //if size==1 resize will delete the only element
337      tabs.resize(size-1);
338      tabnames.resize(size-1);
339
340      int old_active_tab=active_tab;
341      notebook.remove_page(active_tab);
342
343      //If the first tab was active, upon delete notebook
344      //will first switch one tab upper and not lower like
345      //in the case, when not the first tab was active.
346      //But after deletion it will become the first tab,
347      //and this should be registrated in tabs vector,
348      //as well.
349      if((old_active_tab==0)&&(size!=1))
350        {
351          onChangeTab(NULL,0);
352        }
353
354      //if this was the last page in notebook, there is
355      //no active_tab now
356      if(size==1)
357        {
358          active_tab=-1;
359        }
360
361      updateAlgoWinTabs();
362    }
363}
364
365void MainWin::onChangeTab(GtkNotebookPage* page, guint page_num)
366{
367  page=page;
368  active_tab=page_num;
369  tabs[active_tab]->gd_canvas->changeEditorialTool(active_tool);
370  set_title(tabnames[active_tab]);
371  bool autoscale;
372  bool zoomtrack;
373  double width;
374  double radius;
375  tabs[active_tab]->getView(autoscale, zoomtrack, width, radius);
376  edge_width->set_value(width);
377  radius_size->set_value(radius);
378  zoom_track->set_active(zoomtrack);
379  auto_scale->set_active(autoscale);
380
381}
382
383void MainWin::newFile()
384{
385  if(active_tab!=-1)
386    {
387      tabs[active_tab]->newFile();
388    }
389}
390 
391void MainWin::openFile()
392{
393  if(active_tab==-1)
394    {
395      newTab();
396    }
397  tabs[active_tab]->openFile();
398}
399 
400void MainWin::saveFile()
401{
402  if(active_tab!=-1)
403    {
404      tabs[active_tab]->saveFile();
405    }
406}
407 
408void MainWin::saveFileAs()
409{
410  if(active_tab!=-1)
411    {
412      tabs[active_tab]->saveFileAs();
413    }
414}
415 
416void MainWin::close()
417{
418  if(active_tab!=-1)
419    {
420      tabs[active_tab]->close();
421    }
422}
423
424void MainWin::zoomIn()
425{
426  if(active_tab!=-1)
427    {
428      tabs[active_tab]->gd_canvas->zoomIn();
429    }
430}
431
432void MainWin::zoomOut()
433{
434  if(active_tab!=-1)
435    {
436      tabs[active_tab]->gd_canvas->zoomOut();
437    }
438}
439
440void MainWin::zoomFit()
441{
442  if(active_tab!=-1)
443    {
444      tabs[active_tab]->gd_canvas->zoomFit();
445    }
446}
447
448void MainWin::zoom100()
449{
450  if(active_tab!=-1)
451    {
452      tabs[active_tab]->gd_canvas->zoom100();
453    }
454}
455
456void MainWin::createMapWin()
457{
458  if(active_tab!=-1)
459    {
460      tabs[active_tab]->createMapWin(tabnames[active_tab]);
461    }
462}
463
464void MainWin::createDesignWin()
465{
466  if(active_tab!=-1)
467    {
468      tabs[active_tab]->createDesignWin(tabnames[active_tab]);
469    }
470}
471
472void MainWin::createAlgoWin(int algoid)
473{
474  AlgoWin * aw=new AlgoWin(algoid, tabnames);
475  aw->signal_closing().connect(sigc::mem_fun(*this, &MainWin::deRegisterAlgoWin));
476  aw->signal_maplist_needed().connect(sigc::mem_fun(*this, &MainWin::updateAlgoWinMaps));
477  aw->signal_newmapwin_needed().connect(sigc::mem_fun(*this, &MainWin::createNewMapWinTabString));
478  aws.insert(aw);
479  aw->show();
480}
481
482void MainWin::updateAlgoWinTabs()
483{
484  std::set< AlgoWin* >::iterator awsi=aws.begin();
485  for(;awsi!=aws.end();awsi++)
486    {
487      (*awsi)->update_tablist(tabnames);
488    }
489}
490
491void MainWin::updateAlgoWinMaps(AlgoWin * awp, std::string tabname)
492{
493  int i=0;
494  for(;(i<(int)tabnames.size())&&(tabnames[i]!=tabname);i++)
495    {
496    }
497  awp->update_maplist(&(tabs[i]->mapstorage));
498}
499
500void MainWin::deRegisterAlgoWin(AlgoWin * awp)
501{
502  aws.erase(awp);
503}
504
505void MainWin::changeEditorialTool(int tool)
506{
507  active_tool=tool;
508  if(active_tab!=-1)
509    {
510      tabs[active_tab]->gd_canvas->changeEditorialTool(tool);
511    }
512}
513
514void MainWin::createNewMapWin()
515{
516  if(active_tab!=-1)
517    {
518      NewMapWin * nmw=new NewMapWin(_("Create New Map - ")+tabnames[active_tab], *(tabs[active_tab]));
519      nmw->show();
520    }
521}
522
523void MainWin::createNewMapWinTabString(std::string tabname, bool itisedge)
524{
525  int i=0;
526  for(;((i<(int)tabnames.size())&&(tabnames[i]!=tabname));i++)
527    {
528    }
529  createNewMapWinAfterSignal(tabs[i], itisedge);
530}
531
532void MainWin::createNewMapWinAfterSignal(NoteBookTab * nbt, bool itisedge)
533{
534  std::vector<NoteBookTab*>::iterator nbti=tabs.begin();
535  int i=0;
536  for(;nbti!=tabs.end();nbti++)
537    {
538      if(*nbti!=nbt)
539        {
540          i++;
541        }
542      else
543        {
544          continue;
545        }
546    }
547  NewMapWin * nmw=new NewMapWin(_("Create New Map - ")+tabnames[i], *nbt, itisedge, false);
548  nmw->run();
549}
550
551
552void MainWin::nodeViewChanged()
553{
554  double width=edge_width->get_value();
555  double radius=radius_size->get_value();
556  bool zoomtrack=zoom_track->get_active();
557  bool autoscale=auto_scale->get_active();
558  tabs[active_tab]->setView(autoscale, zoomtrack, width, radius);
559}
560
561void MainWin::reDesignGraph()
562{
563  tabs[active_tab]->reDesignGraph();
564}
Note: See TracBrowser for help on using the repository browser.