// src/resources_widget.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 "resources_widget.hh" #include "templates/sequences.tcc" #include "backend/history.hh" #include "backend/simulation.hh" #include "backend/resource.hh" #include #include #include "gettext.h" #include #include #include using namespace sgpem; using namespace Gtk; using namespace Glib; using Gnome::Glade::Xml; ResourcesWidget::ResourcesWidget(BaseObjectType* cobject, const RefPtr& glade) : TreeView(cobject), _add_resource_dialog_glade(Xml::create(GLADEDIR "/add-resource-dialog.glade")) { _columns.add(_key_column); _columns.add(_main_column); _columns.add(_handles_column); _model = ListStore::create(_columns); set_model(_model); append_column(_("key"), _key_column); append_column(_("resources"), _main_column); //invisible // append_column("handles", _handles_column); /** DIALOGS **/ _add_resource_dialog_glade->get_widget("AddResourceDialog", _add_resource_dialog); set_headers_visible(false); Simulation::get_instance().get_history().attach(*this); } ResourcesWidget::~ResourcesWidget() { Simulation::get_instance().get_history().detach(*this); } sgpem::Resource* ResourcesWidget::get_selected_resource() { TreeModel::iterator sel = get_selection()->get_selected(); if(!sel) return NULL; const void* p_handle = (*sel)[_handles_column]; return reinterpret_cast(const_cast(p_handle)); } bool ResourcesWidget::on_button_press_event(GdkEventButton* event) { TreeView::on_button_press_event(event); if((event->type == GDK_BUTTON_PRESS) && (event->button == 3) ) { RefPtr action_group = Gtk::ActionGroup::create(); action_group->add( Gtk::Action::create("AddResource", "Add Resource"), sigc::mem_fun(*this, &ResourcesWidget::_on_add_resource) ); RefPtr UIManager = Gtk::UIManager::create(); UIManager->insert_action_group(action_group); Glib::ustring ui_info = "" " " " " " " ""; UIManager->add_ui_from_string(ui_info); Gtk::Menu* menu = dynamic_cast(UIManager->get_widget("/PopupMenu")); menu->popup(event->button, event->time); return true; //It has been handled. } else return false; } void ResourcesWidget::update(const History& history) { typedef Environment::Resources::const_iterator ResourceIt; const Environment::Resources& resources = Simulation::get_instance().get_history().get_last_environment().get_resources(); _model->clear(); // TODO use the new sequence iterator, it's been made for // something!!! for(Iseq it = const_iseq(resources); it; ++it) { Resource& r = *(it->second); TreeModel::Row row = *(_model->append()); row[_key_column] = it->first; row[_main_column] = r.get_name(); row[_handles_column] = &r; } } void ResourcesWidget::_on_add_resource() { if(_add_resource_dialog->run() == RESPONSE_OK) { Entry* name_entry; SpinButton* places_spin; SpinButton* availability_spin; CheckButton* preemptable_check; _add_resource_dialog_glade->get_widget("Name.Entry", name_entry); _add_resource_dialog_glade->get_widget("Places.Spin", places_spin); _add_resource_dialog_glade->get_widget("Availability.Spin", availability_spin); _add_resource_dialog_glade->get_widget("Preemptable.Check", preemptable_check); Simulation::get_instance().get_history().add_resource(name_entry->get_text(), preemptable_check->get_active(), places_spin->get_value_as_int(), availability_spin->get_value_as_int()); } _add_resource_dialog->hide(); }