256 lines
6.8 KiB
C++
256 lines
6.8 KiB
C++
// 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 "backend/process.hh"
|
|
#include "backend/thread.hh"
|
|
#include "backend/request.hh"
|
|
|
|
#include <iostream>
|
|
#include <sstream>
|
|
#include <stack>
|
|
|
|
#include "gettext.h"
|
|
|
|
#include <gtk/gtk.h>
|
|
#include <gtkmm/entry.h>
|
|
#include <gtkmm/spinbutton.h>
|
|
|
|
|
|
using namespace sgpem;
|
|
using namespace Gtk;
|
|
using namespace Glib;
|
|
using Gnome::Glade::Xml;
|
|
|
|
ResourcesWidget::ResourcesWidget(BaseObjectType* cobject, const RefPtr<Xml>& 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);
|
|
}
|
|
|
|
bool
|
|
ResourcesWidget::get_selected_key(unsigned int& selection)
|
|
{
|
|
TreeModel::iterator it = get_selection()->get_selected();
|
|
|
|
if(it)
|
|
{
|
|
selection = (*it)[_key_column];
|
|
return true;
|
|
}
|
|
else
|
|
return false;
|
|
}
|
|
|
|
bool
|
|
ResourcesWidget::on_button_press_event(GdkEventButton* event)
|
|
{
|
|
TreeView::on_button_press_event(event);
|
|
|
|
if((event->type == GDK_BUTTON_PRESS) && (event->button == 3) )
|
|
{
|
|
RefPtr<ActionGroup> action_group = Gtk::ActionGroup::create();
|
|
|
|
action_group->add( Gtk::Action::create("AddResource", "Add Resource"),
|
|
sigc::mem_fun(*this, &ResourcesWidget::_on_add_resource) );
|
|
|
|
action_group->add( Gtk::Action::create("EditResource", "Edit Resource"),
|
|
sigc::mem_fun(*this, &ResourcesWidget::_on_edit_resource) );
|
|
|
|
action_group->add( Gtk::Action::create("RemoveResource", "Remove Resource"),
|
|
sigc::mem_fun(*this, &ResourcesWidget::_on_remove_resource) );
|
|
|
|
RefPtr<UIManager> UIManager = Gtk::UIManager::create();
|
|
UIManager->insert_action_group(action_group);
|
|
|
|
unsigned int selection;
|
|
Glib::ustring ui_info =
|
|
"<ui>"
|
|
" <popup name='PopupMenu'>"
|
|
" <menuitem action='AddResource'/>";
|
|
if(get_selected_key(selection))
|
|
ui_info +=
|
|
" <separator/>"
|
|
" <menuitem action='EditResource'/>"
|
|
" <separator/>"
|
|
" <menuitem action='RemoveResource'/>";
|
|
|
|
ui_info +=
|
|
" </popup>"
|
|
"</ui>";
|
|
|
|
UIManager->add_ui_from_string(ui_info);
|
|
Gtk::Menu* menu = dynamic_cast<Gtk::Menu*>(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();
|
|
|
|
for(Iseq<ResourceIt> 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()
|
|
{
|
|
add_edit_resource(true);
|
|
}
|
|
|
|
void
|
|
ResourcesWidget::_on_edit_resource()
|
|
{
|
|
add_edit_resource(false);
|
|
}
|
|
|
|
void
|
|
ResourcesWidget::add_edit_resource(bool adding)
|
|
{
|
|
Entry* name_entry;
|
|
SpinButton* places_spin;
|
|
|
|
Resource* resource = NULL;
|
|
|
|
_add_resource_dialog_glade->get_widget("Name.Entry", name_entry);
|
|
_add_resource_dialog_glade->get_widget("Places.Spin", places_spin);
|
|
|
|
if(adding)
|
|
{
|
|
name_entry->set_text("");
|
|
places_spin->set_value(1.0);
|
|
}
|
|
else
|
|
{
|
|
const Environment& env = Simulation::get_instance().get_history().get_last_environment();
|
|
unsigned int key;
|
|
get_selected_key(key);
|
|
resource = env.get_resources().find(key)->second;
|
|
|
|
name_entry->set_text(resource->get_name());
|
|
places_spin->set_value(static_cast<double>(resource->get_places()));
|
|
}
|
|
|
|
if(_add_resource_dialog->run() == RESPONSE_OK)
|
|
{
|
|
|
|
if(adding)
|
|
{
|
|
Simulation::get_instance().get_history().add_resource(name_entry->get_text(),
|
|
false,
|
|
places_spin->get_value_as_int());
|
|
}
|
|
else
|
|
{
|
|
Simulation::get_instance().get_history().edit_resource(*resource,
|
|
name_entry->get_text(),
|
|
false,
|
|
places_spin->get_value_as_int());
|
|
}
|
|
}
|
|
|
|
_add_resource_dialog->hide();
|
|
}
|
|
|
|
void
|
|
ResourcesWidget::_on_remove_resource()
|
|
{
|
|
unsigned int selection;
|
|
if(get_selected_key(selection))
|
|
Simulation::get_instance().get_history().remove(selection);
|
|
|
|
/** Delete empty requests, they are useless with the current GUI **/
|
|
|
|
typedef Environment::Processes::const_iterator ProcessIt;
|
|
using std::vector;
|
|
|
|
const Environment& env = Simulation::get_instance().get_history().get_last_environment();
|
|
const Environment::Processes& processes = env.get_processes();
|
|
|
|
std::stack<Request*> to_delete;
|
|
|
|
for(ProcessIt pit = processes.begin(); pit != processes.end(); ++pit)
|
|
{
|
|
vector<Thread*> threads = (*pit)->get_threads();
|
|
|
|
for(vector<Thread*>::iterator tit = threads.begin(); tit != threads.end(); ++tit)
|
|
{
|
|
vector<Request*> requests = (*tit)->get_requests();
|
|
|
|
for(vector<Request*>::iterator rit = requests.begin(); rit != requests.end(); ++rit)
|
|
{
|
|
if((*rit)->get_subrequests().empty())
|
|
to_delete.push(*rit);
|
|
}
|
|
}
|
|
}
|
|
|
|
while(!to_delete.empty())
|
|
{
|
|
Simulation::get_instance().get_history().remove(*to_delete.top());
|
|
to_delete.pop();
|
|
}
|
|
}
|