COIN-OR::LEMON - Graph Library

source: lemon-0.x/gui/main_win.cc @ 1876:5ad84ad1b68f

Last change on this file since 1876:5ad84ad1b68f was 1876:5ad84ad1b68f, checked in by Hegyi Péter, 14 years ago

Communication with algorithm window is developed.

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