COIN-OR::LEMON - Graph Library

source: glemon-0.x/main_win.cc @ 162:aaa517c9dc23

Last change on this file since 162:aaa517c9dc23 was 162:aaa517c9dc23, checked in by Hegyi Péter, 15 years ago

Dijkstra in GUI.

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