sgpemv2/src/gui_builder.cc

314 lines
10 KiB
C++
Raw Normal View History

// src/startgui.cc - Copyright 2005, 2006, University
// of Padova, dept. of Pure and Applied
// Mathematics
//
// This file is part of SGPEMv2.
//
// This is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// SGPEMv2 is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with SGPEMv2; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#include "config.h"
#include "gettext.h"
#include "schedulables_tree_widget.hh"
#include "simulation_widget.hh"
#include "resources_widget.hh"
#include "gui_builder.hh"
#include "graphical_preferences_editor.hh"
#include "backend/cpu_policies_gatekeeper.hh"
#include "backend/cpu_policy.hh"
#include "backend/cpu_policy_manager.hh"
#include "backend/history.hh"
#include "backend/simulation.hh"
#include "backend/serializers_gatekeeper.hh"
#include "backend/serializer.hh"
#include <gdkmm/pixbuf.h>
#include <glibmm/ustring.h>
#include <gtkmm/aboutdialog.h>
#include <gtkmm/messagedialog.h>
#include <gtkmm/filechooserdialog.h>
#include <gtkmm/main.h>
#include <gtkmm/menuitem.h>
#include <gtkmm/scrolledwindow.h>
#include <gtkmm/stock.h>
#include <gtkmm/toolbutton.h>
#include <iostream>
using namespace sgpem;
using Gnome::Glade::Xml;
void
GuiBuilder::on_edit_preferences_activate()
{
new PreferencesEditor(); //FIXME: are we leaking this way?
}
void
GuiBuilder::on_file_open_activate()
{
Glib::ustring msg;
try
{
std::vector<Serializer*> serializers =
SerializersGatekeeper::get_instance().get_registered();
// FIXME using the first serializer available, this
// will need to be changed when multiple serializers will
// be made available
Serializer& serializer = *serializers.at(0);
History& history = Simulation::get_instance().get_history();
// open file dialog...
Gtk::FileChooserDialog dialog("Please choose a file", Gtk::FILE_CHOOSER_ACTION_OPEN);
dialog.set_transient_for(get_initial_window());
//Add response buttons the the dialog:
dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
dialog.add_button(Gtk::Stock::OPEN, Gtk::RESPONSE_OK);
//Add filters, so that only certain file types can be selected:
for(std::vector<Serializer*>::const_iterator iter=serializers.begin(); iter!=serializers.end(); iter++)
{
Serializer* ser = *iter;
Gtk::FileFilter filter_sgpem;
filter_sgpem.set_name(ser->get_filename_description());
filter_sgpem.add_pattern(ser->get_filename_extension());
dialog.add_filter(filter_sgpem);
}
Gtk::FileFilter filter_any;
filter_any.set_name("Any files");
filter_any.add_pattern("*");
dialog.add_filter(filter_any);
//Show the dialog and wait for a user response:
int result = dialog.run();
if(result==Gtk::RESPONSE_OK)
{
// DISABLED - Actually locks computer
// serializer.restore_snapshot("prova1.xgp", history);
msg = "File: " + dialog.get_filename() + " loaded.";
} // end - if(result==Gtk::RESPONSE_OK)
}
catch (std::out_of_range e)
{
msg = _("ERROR: No registered serializer available");
}
catch (SerializerError e)
{
msg = _("ERROR: ") + Glib::ustring(e.what());
}
if(!msg.empty())
{
Gtk::MessageDialog msg_dialog(get_initial_window(), msg);
msg_dialog.run();
}
}
void
GuiBuilder::on_file_save_activate()
{
// _simulation_widget->change_scaling_mode();
}
void
GuiBuilder::on_file_saveas_activate()
{
Glib::ustring msg;
try
{
std::vector<Serializer*> serializers =
SerializersGatekeeper::get_instance().get_registered();
// FIXME using the first serializer available, this
// will need to be changed when multiple serializers will
// be made available
Serializer& serializer = *serializers.at(0);
History& history = Simulation::get_instance().get_history();
// open file dialog...
Gtk::FileChooserDialog dialog("Please choose a file", Gtk::FILE_CHOOSER_ACTION_SAVE);
dialog.set_transient_for(get_initial_window());
//Add response buttons the the dialog:
dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
dialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_OK);
//Add filters, so that only certain file types can be selected:
Gtk::FileFilter filter_sgpem;
filter_sgpem.set_name("Sgpem files");
filter_sgpem.add_pattern("*.xgp");
dialog.add_filter(filter_sgpem);
Gtk::FileFilter filter_any;
filter_any.set_name("Any files");
filter_any.add_pattern("*");
dialog.add_filter(filter_any);
//Show the dialog and wait for a user response:
int result = dialog.run();
if(result==Gtk::RESPONSE_OK)
{
// DISABLED - Actually locks computer
serializer.save_snapshot(dialog.get_filename(), history);
msg = "File: " + dialog.get_filename() + " saved.";
} // end - if(result==Gtk::RESPONSE_OK)
}
catch (std::out_of_range e)
{
msg = _("ERROR: No registered serializer available");
}
catch (SerializerError e)
{
msg = _("ERROR: ") + Glib::ustring(e.what());
}
if(!msg.empty())
{
Gtk::MessageDialog msg_dialog(get_initial_window(), msg);
msg_dialog.run();
}
}
GuiBuilder::GuiBuilder(const std::string& gladefile)
: _refXml(Xml::create(gladefile))
{
using namespace Gtk;
//Window& main_window = get_initial_window();
// file open dialog
MenuItem* file_open = NULL;
_refXml->get_widget("MenuItem.File.Open", file_open);
file_open->signal_activate().connect(sigc::mem_fun(*this, &GuiBuilder::on_file_open_activate));
// file save dialog
MenuItem* file_save = NULL;
_refXml->get_widget("MenuItem.File.Save", file_save);
file_save->signal_activate().connect(sigc::mem_fun(*this, &GuiBuilder::on_file_save_activate));
// file save dialog
MenuItem* file_saveas = NULL;
_refXml->get_widget("MenuItem.File.SaveAs", file_saveas);
file_saveas->signal_activate().connect(sigc::mem_fun(*this, &GuiBuilder::on_file_saveas_activate));
// Start, pause and stop simulation from the toolbar
// TODO: can we use action groups instead of this?
Simulation& sim = Simulation::get_instance();
// FIXME: just to try; a proper way to select a policy has to be installed
CPUPoliciesGatekeeper& pgk = CPUPoliciesGatekeeper::get_instance();
CPUPolicy* policy = pgk.get_registered()[0]->get_avail_policies()[1]; // RoundRobin at the moment of writing
std::cout << "Selected policy : " << policy->get_name() << std::endl;
pgk.activate_policy(&sim.get_history(), policy);
sim.set_policy(policy);
ToolButton *toolbt_start, *toolbt_pause, *toolbt_stop;
_refXml->get_widget("ToolBar.Play", toolbt_start);
_refXml->get_widget("ToolBar.Pause", toolbt_pause);
_refXml->get_widget("ToolBar.Stop", toolbt_stop);
toolbt_start->signal_clicked().connect(sigc::mem_fun(sim, &Simulation::run));
toolbt_pause->signal_clicked().connect(sigc::mem_fun(sim, &Simulation::pause));
toolbt_stop->signal_clicked().connect(sigc::mem_fun(sim, &Simulation::stop));
// Sensitivities
toolbt_start->signal_clicked().connect(sigc::bind(sigc::mem_fun(*toolbt_stop, &ToolButton::set_sensitive), true));
toolbt_start->signal_clicked().connect(sigc::bind(sigc::mem_fun(*toolbt_pause, &ToolButton::set_sensitive), true));
toolbt_start->signal_clicked().connect(sigc::bind(sigc::mem_fun(*toolbt_start, &ToolButton::set_sensitive), false));
toolbt_pause->signal_clicked().connect(sigc::bind(sigc::mem_fun(*toolbt_start, &ToolButton::set_sensitive), true));
toolbt_pause->signal_clicked().connect(sigc::bind(sigc::mem_fun(*toolbt_pause, &ToolButton::set_sensitive), false));
toolbt_stop->signal_clicked().connect(sigc::bind(sigc::mem_fun(*toolbt_stop, &ToolButton::set_sensitive), false));
toolbt_stop->signal_clicked().connect(sigc::bind(sigc::mem_fun(*toolbt_pause, &ToolButton::set_sensitive), false));
toolbt_stop->signal_clicked().connect(sigc::bind(sigc::mem_fun(*toolbt_start, &ToolButton::set_sensitive), true));
// Connect extra signals (decide where to do this...
// here -- ugly -- derive widgets and then use
// Glade::Xml::get_widget_derived -- better --)
MenuItem* file_quit = NULL;
_refXml->get_widget("MenuItem.File.Quit", file_quit);
file_quit->signal_activate().connect(sigc::ptr_fun(&Main::quit));
// preferences dialog
MenuItem* edit_preferences;
_refXml->get_widget("MenuItem.Edit.Preferences", edit_preferences);
edit_preferences->signal_activate().connect(sigc::mem_fun(*this, &GuiBuilder::on_edit_preferences_activate));
// About dialog
MenuItem* help_about;
_refXml->get_widget("MenuItem.Help.About", help_about);
AboutDialog* about_dialog = NULL;
_refXml->get_widget("AboutDialog", about_dialog);
help_about->signal_activate().connect(sigc::mem_fun(*about_dialog, &Window::show));
about_dialog->set_wrap_license(true);
about_dialog->set_logo(Gdk::Pixbuf::create_from_file(GLADEDIR "/logo.png"));
// Insert the schedulables TreeView custom widget
ScrolledWindow* schedulables_sw = NULL;
_refXml->get_widget("SchedulablesScrolledWindow", schedulables_sw);
SchedulablesTreeWidget* scheds_tree = manage(new SchedulablesTreeWidget());
schedulables_sw->add(*scheds_tree);
// we have to remember to manually show custom added widgets:
scheds_tree->show();
ResourcesWidget* resources_widget = NULL;
_refXml->get_widget_derived("Resources.Tree", resources_widget);
resources_widget->show();
// Main simulation widget
ScrolledWindow* simulation_window = NULL;
_refXml->get_widget("SimulationScrolledWindow", simulation_window);
_simulation_widget = new SimulationWidget(Simulation::get_instance());
SimulationWidget* simulation_widget = manage(_simulation_widget);
simulation_window->add(*simulation_widget);
simulation_widget->show();
}
GuiBuilder::~GuiBuilder()
{}
Gtk::Window&
GuiBuilder::get_initial_window() const
{
Gtk::Window* main_window = NULL;
_refXml->get_widget("MainWindow", main_window);
return *main_window;
}
void
GuiBuilder::open_file(const std::string& filename)
{
// FIXME: to be written.
// Debug line (erase me when done):
std::cout << _("Filename to open: ") << filename << std::endl;
}