COIN-OR::LEMON - Graph Library

source: glemon-0.x/nbtab.cc @ 196:c220f9de6545

Last change on this file since 196:c220f9de6545 was 196:c220f9de6545, checked in by Hegyi Péter, 13 years ago

EpsWin? and DesignWin? does not need to know NoteBookTab?.

File size: 9.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#include <nbtab.h>
20#include <mapstorage.h>
21#include <eps_win.h>
22#include <map_win.h>
23#include <design_win.h>
24#include <graph_displayer_canvas.h>
25
26
27
28NoteBookTab::NoteBookTab():mapwinexists(false), designwinexists(false)
29{
30  mapstorage=new MapStorage();
31
32  Gtk::ScrolledWindow *pScrolledWindow = manage(new Gtk::ScrolledWindow);
33  gd_canvas=new GraphDisplayerCanvas(*this);
34  pScrolledWindow->add(*gd_canvas);
35  add(*pScrolledWindow);
36
37  //connecting signals - controller character
38  mapstorage->signal_prop_ch().connect(sigc::mem_fun(*gd_canvas, &GraphDisplayerCanvas::propertyChange));
39  mapstorage->signal_node_map_ch().connect(sigc::mem_fun(*this, &NoteBookTab::registerNewNodeMap));
40  mapstorage->signal_edge_map_ch().connect(sigc::mem_fun(*this, &NoteBookTab::registerNewEdgeMap));
41  mapstorage->signal_background_ch().connect(sigc::mem_fun(*gd_canvas, &GraphDisplayerCanvas::setBackground));
42  show_all_children();
43  show();
44}
45
46void NoteBookTab::readFile(const std::string &file)
47{
48  mapstorage->readFromFile(file);
49  mapstorage->file_name = file;
50  mapstorage->modified = false;
51  gd_canvas->drawGraph();
52  if(mapwinexists)
53    {
54      mapwin->update(mapstorage->getEdgeMapList(), mapstorage->getNodeMapList());
55    }
56  title_changed(Glib::filename_display_basename(file));
57}
58
59void NoteBookTab::newFile()
60{
61  if (mapstorage->modified)
62  {
63    Gtk::MessageDialog mdialog("<b>Save changes before closing?</b>", true,
64        Gtk::MESSAGE_WARNING, Gtk::BUTTONS_NONE);
65    mdialog.add_button("Close file _without Saving", Gtk::RESPONSE_REJECT);
66    mdialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
67    mdialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT);
68    switch (mdialog.run())
69    {
70      case Gtk::RESPONSE_CANCEL:
71        return;
72      case Gtk::RESPONSE_REJECT:
73        break;
74      case Gtk::RESPONSE_ACCEPT:
75        saveFile();
76        break;
77    }
78  }
79  gd_canvas->clear();
80  mapstorage->clear();
81  if(mapwinexists)
82    {
83      mapwin->update(mapstorage->getEdgeMapList(), mapstorage->getNodeMapList());
84    }
85  title_changed("unsaved file");
86}
87
88void NoteBookTab::openFile()
89{
90  if (mapstorage->modified)
91  {
92    Gtk::MessageDialog mdialog("<b>Save changes before closing?</b>", true,
93        Gtk::MESSAGE_WARNING, Gtk::BUTTONS_NONE);
94    mdialog.add_button("Close file _without Saving", Gtk::RESPONSE_REJECT);
95    mdialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
96    mdialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT);
97    switch (mdialog.run())
98    {
99      case Gtk::RESPONSE_CANCEL:
100        return;
101      case Gtk::RESPONSE_REJECT:
102        break;
103      case Gtk::RESPONSE_ACCEPT:
104        saveFile();
105        break;
106    }
107  }
108  Gtk::FileChooserDialog fcdialog("Open File");
109  fcdialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
110  fcdialog.add_button(Gtk::Stock::OPEN, Gtk::RESPONSE_ACCEPT);
111  if (fcdialog.run() == Gtk::RESPONSE_ACCEPT)
112  {
113    gd_canvas->clear();
114    mapstorage->clear();
115    Glib::ustring filename = fcdialog.get_filename();
116    if (!mapstorage->readFromFile(filename))
117    {
118      mapstorage->file_name = filename;
119      mapstorage->modified = false;
120      gd_canvas->drawGraph();
121      if(mapwinexists)
122        {
123          mapwin->update(mapstorage->getEdgeMapList(), mapstorage->getNodeMapList());
124        }
125      title_changed(Glib::filename_display_basename(filename));
126    }
127  }
128}
129
130void NoteBookTab::saveFile()
131{
132  if (mapstorage->file_name == "") {
133    saveFileAs();
134  }
135  else
136  {
137    mapstorage->writeToFile(mapstorage->file_name);
138    mapstorage->modified = false;
139    title_changed(Glib::filename_display_basename(mapstorage->file_name));
140  }
141}
142
143void NoteBookTab::saveFileAs()
144{
145  Gtk::FileChooserDialog fcdialog("Save File", Gtk::FILE_CHOOSER_ACTION_SAVE);
146  fcdialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
147  fcdialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT);
148  if (fcdialog.run() == Gtk::RESPONSE_ACCEPT)
149  {
150    Glib::ustring filename = fcdialog.get_filename();
151    mapstorage->file_name = filename;
152    mapstorage->writeToFile(filename);
153    mapstorage->modified = false;
154    title_changed(Glib::filename_display_basename(filename));
155  }
156}
157
158void NoteBookTab::close()
159{
160  if (mapstorage->modified)
161  {
162    Gtk::MessageDialog mdialog("<b>Save changes before closing?</b>", true,
163        Gtk::MESSAGE_WARNING, Gtk::BUTTONS_NONE);
164    mdialog.add_button("Close _without Saving", Gtk::RESPONSE_REJECT);
165    mdialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
166    mdialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT);
167    switch (mdialog.run())
168    {
169      case Gtk::RESPONSE_CANCEL:
170        return;
171      case Gtk::RESPONSE_REJECT:
172        break;
173      case Gtk::RESPONSE_ACCEPT:
174        saveFile();
175        break;
176    }
177  }
178  gd_canvas->clear();
179  mapstorage->clear();
180  if(mapwinexists)
181    {
182      mapwin->update(mapstorage->getEdgeMapList(), mapstorage->getNodeMapList());
183    }
184  title_changed("unsaved file");
185}
186
187void NoteBookTab::propertyChange(bool itisedge, int prop, std::string mapname)
188{
189  mapstorage->changeActiveMap(itisedge, prop, mapname);
190}
191
192sigc::signal<void, NoteBookTab *, bool> NoteBookTab::signal_newmap_needed()
193{
194  return signal_newmap;
195}
196
197void NoteBookTab::popupNewMapWin(bool itisedge)
198{
199  signal_newmap.emit(this, itisedge);
200}
201
202std::string NoteBookTab::getActiveEdgeMap(int prop)
203{
204  return mapstorage->getActiveEdgeMap(prop);
205}
206
207std::string NoteBookTab::getActiveNodeMap(int prop)
208{
209  return mapstorage->getActiveNodeMap(prop);
210}
211
212void NoteBookTab::registerNewEdgeMap(std::string mapname)
213{
214  if(mapwinexists)
215    {
216      mapwin->registerNewEdgeMap(mapname);
217    }
218}
219
220void NoteBookTab::registerNewNodeMap(std::string mapname)
221{
222  if(mapwinexists)
223    {
224      mapwin->registerNewNodeMap(mapname);
225    }
226}
227
228void NoteBookTab::createMapWin(std::string name)
229{
230  if(!mapwinexists)
231    {
232      mapwin=new MapWin("Map Setup - "+name, mapstorage->getEdgeMapList(), mapstorage->getNodeMapList(), *this);
233      mapst2mapwin=mapstorage->signal_map_win_ch().connect(sigc::mem_fun(*mapwin, &MapWin::changeEntry));
234      mapwin->show();
235      mapwinexists=true;
236    }
237}
238
239void NoteBookTab::createExportToEPSWin(std::string name)
240{
241  if(!epswinexists)
242    {
243      epswin=new EpsWin("Export to EPS - "+name);
244      epswin->show();
245      epswinexists=true;
246      epswin->signal_eps_details_ch().connect(sigc::mem_fun(*this, &NoteBookTab::exportGraphToEPS));
247      epswin->signal_eps_close_ch().connect(sigc::mem_fun(*this, &NoteBookTab::closeEpsWin));
248    }
249}
250
251
252void NoteBookTab::createDesignWin(std::string name)
253{
254  if(!designwinexists)
255    {
256      double attraction, propulsation;
257      int iterations;
258      mapstorage->get_design_data(attraction, propulsation, iterations);
259      designwin=new DesignWin("Design Setup - "+name, attraction, propulsation, iterations);
260
261      designwin->signal_attraction().connect(sigc::mem_fun(mapstorage, &MapStorage::set_attraction));
262      designwin->signal_propulsation().connect(sigc::mem_fun(mapstorage, &MapStorage::set_propulsation));
263      designwin->signal_iteration().connect(sigc::mem_fun(mapstorage, &MapStorage::set_iteration));
264      designwin->close_run().connect(sigc::mem_fun(*gd_canvas, &GraphDisplayerCanvas::reDesignGraph));
265
266      designwin->signal_delete_event().connect(sigc::mem_fun(*this, &NoteBookTab::closeDesignWin));
267
268      mapst2designwin=mapstorage->signal_design_win_ch().connect(sigc::mem_fun(*designwin, &DesignWin::set_data));
269
270      designwin->show();
271      designwinexists=true;
272    }
273}
274
275void NoteBookTab::closeMapWin()
276{
277  mapst2mapwin.disconnect();
278  mapwinexists=false;
279  delete mapwin;
280}
281
282void NoteBookTab::closeEpsWin()
283{
284  epswinexists=false;
285  delete epswin;
286}
287
288bool NoteBookTab::closeDesignWin(GdkEventAny * e)
289{
290  if(e->type==GDK_DELETE)
291    {
292      designwinexists=false;
293      mapst2designwin.disconnect();
294      delete designwin;
295    }
296}
297
298sigc::signal<void, std::string> NoteBookTab::signal_title_ch()
299{
300  return signal_title;
301}
302
303void NoteBookTab::setView(bool autoscale, bool zoomtrack, double width, double radius)
304{
305  gd_canvas->setView(autoscale, zoomtrack, width, radius);
306}
307
308void NoteBookTab::getView(bool & autoscale, bool & zoomtrack, double& width, double& radius)
309{
310  gd_canvas->getView(autoscale, zoomtrack, width, radius);
311}
312
313void NoteBookTab::reDesignGraph()
314{
315  gd_canvas->reDesignGraph();
316}
317
318void NoteBookTab::active_maps_needed()
319{
320  mapstorage->broadcastActiveMaps();
321}
322
323void NoteBookTab::exportGraphToEPS(std::vector<bool> options, std::string filename)
324{
325  mapstorage->exportGraphToEPS(options, filename);
326}
327
328void NoteBookTab::title_changed(std::string newtitle)
329{
330  signal_title.emit(newtitle);
331  if(epswinexists)
332    {
333      epswin->set_title(newtitle);
334    }
335  if(designwinexists)
336    {
337      designwin->set_title(newtitle);
338    }
339}
Note: See TracBrowser for help on using the repository browser.