COIN-OR::LEMON - Graph Library

source: glemon-0.x/main_win.cc @ 183:48580778851e

Last change on this file since 183:48580778851e was 174:95872af46fc4, checked in by Alpar Juttner, 17 years ago

Add copyright headers

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