COIN-OR::LEMON - Graph Library

source: glemon-0.x/main_win.cc @ 160:14a76109b561

Last change on this file since 160:14a76109b561 was 160:14a76109b561, checked in by Hegyi Péter, 17 years ago

Node antigravity and edge elasticity based graph layout redesigner.

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