2006-08-19 02:09:36 +02:00
|
|
|
// src/add_request_dialog.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 "add_request_dialog.hh"
|
|
|
|
#include "templates/sequences.tcc"
|
|
|
|
#include "backend/history.hh"
|
|
|
|
#include "backend/environment.hh"
|
|
|
|
#include "backend/simulation.hh"
|
|
|
|
#include "backend/resource.hh"
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
#include "gettext.h"
|
|
|
|
|
2006-08-20 19:52:46 +02:00
|
|
|
#include <gtkmm/spinbutton.h>
|
|
|
|
|
2006-08-21 13:58:06 +02:00
|
|
|
#include <cassert>
|
|
|
|
|
2006-08-19 02:09:36 +02:00
|
|
|
using namespace sgpem;
|
|
|
|
using namespace Gtk;
|
|
|
|
using namespace Glib;
|
|
|
|
using Gnome::Glade::Xml;
|
2006-08-25 02:29:28 +02:00
|
|
|
using std::vector;
|
2006-08-19 02:09:36 +02:00
|
|
|
|
|
|
|
AddRequestDialog::AddRequestDialog(BaseObjectType* cobject, const RefPtr<Xml>& glade) :
|
2006-08-20 19:52:46 +02:00
|
|
|
Dialog(cobject), _glade(glade)
|
2006-08-19 02:09:36 +02:00
|
|
|
{
|
2006-08-20 19:52:46 +02:00
|
|
|
_glade->get_widget("SubRequests.View", _list);
|
|
|
|
_glade->get_widget("Add", _add_button);
|
|
|
|
_glade->get_widget("Remove", _remove_button);
|
|
|
|
_glade->get_widget("Resource.Combo", _resource_combo);
|
2006-08-21 01:17:32 +02:00
|
|
|
_glade->get_widget("OK.Button", _ok_button);
|
2006-08-25 02:29:28 +02:00
|
|
|
_glade->get_widget("Instant.Spin", _instant_spin);
|
|
|
|
_glade->get_widget("Duration.Spin", _duration_spin);
|
|
|
|
|
2006-08-21 01:17:32 +02:00
|
|
|
|
2006-08-19 02:09:36 +02:00
|
|
|
/** ATTACH SIGNAL HANDLERS FOR BUTTONS **/
|
|
|
|
|
2006-08-21 01:17:32 +02:00
|
|
|
_add_button->signal_clicked().connect(
|
|
|
|
sigc::mem_fun(*this, &AddRequestDialog::_on_add));
|
|
|
|
|
|
|
|
_remove_button->signal_clicked().connect(
|
|
|
|
sigc::mem_fun(*this, &AddRequestDialog::_on_remove));
|
|
|
|
|
|
|
|
_ok_button->set_sensitive(false);
|
|
|
|
_remove_button->set_sensitive(false);
|
|
|
|
_add_button->set_sensitive(false);
|
2006-08-19 02:09:36 +02:00
|
|
|
|
|
|
|
/** INITIALIZE COMBOBOX **/
|
|
|
|
_combo_columns.add(_combo_key_column);
|
|
|
|
_combo_columns.add(_combo_resource_column);
|
|
|
|
|
|
|
|
_combo_model = ListStore::create(_combo_columns);
|
|
|
|
_resource_combo->set_model(_combo_model);
|
2006-08-20 19:52:46 +02:00
|
|
|
_resource_combo->pack_start(_combo_key_column, false);
|
|
|
|
_resource_combo->pack_start(_combo_resource_column, true);
|
2006-08-21 01:17:32 +02:00
|
|
|
|
|
|
|
_resource_combo->signal_changed().connect(
|
|
|
|
sigc::mem_fun(*this, &AddRequestDialog::_on_combo_selection_changed));
|
2006-08-19 02:09:36 +02:00
|
|
|
|
|
|
|
/** INITIALIZE LISTVIEW **/
|
|
|
|
|
2006-08-20 19:52:46 +02:00
|
|
|
_list_columns.add(_list_key_column);
|
2006-08-19 02:09:36 +02:00
|
|
|
_list_columns.add(_list_resource_column);
|
|
|
|
_list_columns.add(_list_length_column);
|
|
|
|
|
|
|
|
_list_model = ListStore::create(_list_columns);
|
|
|
|
_list->set_model(_list_model);
|
|
|
|
|
2006-08-21 01:17:32 +02:00
|
|
|
_list_model->signal_row_deleted().connect(
|
|
|
|
sigc::mem_fun(*this, &AddRequestDialog::_on_row_removed));
|
|
|
|
|
|
|
|
_list_model->signal_row_inserted().connect(
|
|
|
|
sigc::mem_fun(*this, &AddRequestDialog::_on_row_added));
|
|
|
|
|
2006-08-20 19:52:46 +02:00
|
|
|
_list->append_column(_("key"), _list_key_column);
|
2006-08-19 02:09:36 +02:00
|
|
|
_list->append_column(_("resource"), _list_resource_column);
|
|
|
|
_list->append_column(_("length"), _list_length_column);
|
2006-08-20 19:52:46 +02:00
|
|
|
|
|
|
|
_list->get_selection()->signal_changed().connect(
|
2006-08-21 01:17:32 +02:00
|
|
|
sigc::mem_fun(*this, &AddRequestDialog::_on_list_selection_changed));
|
2006-08-19 02:09:36 +02:00
|
|
|
}
|
2006-08-21 01:17:32 +02:00
|
|
|
|
2006-08-25 02:29:28 +02:00
|
|
|
Request*
|
|
|
|
AddRequestDialog::run_add(Thread& owner)
|
2006-08-21 01:17:32 +02:00
|
|
|
{
|
2006-08-25 02:29:28 +02:00
|
|
|
update_combo();
|
2006-08-21 01:17:32 +02:00
|
|
|
|
2006-08-25 02:29:28 +02:00
|
|
|
Request* r = NULL;
|
2006-08-19 02:09:36 +02:00
|
|
|
|
2006-08-25 02:29:28 +02:00
|
|
|
// reset the dialog data
|
|
|
|
_list_model->clear();
|
|
|
|
_instant_spin->set_value(0.0);
|
|
|
|
_duration_spin->set_value(0.0);
|
2006-08-21 01:17:32 +02:00
|
|
|
|
2006-08-25 02:29:28 +02:00
|
|
|
if(run() == RESPONSE_OK)
|
|
|
|
{
|
|
|
|
assert(_list_model->children());
|
|
|
|
|
|
|
|
History& h = Simulation::get_instance().get_history();
|
|
|
|
|
|
|
|
r = &h.add_request(owner, _instant_spin->get_value_as_int());
|
|
|
|
|
|
|
|
TreeNodeChildren sreq_container = _list_model->children();
|
|
|
|
|
|
|
|
for(Iseq<TreeIter> it = iseq(sreq_container); it; ++it)
|
|
|
|
h.add_subrequest(*r, (*it)[_list_key_column], (*it)[_list_length_column]);
|
|
|
|
}
|
|
|
|
|
|
|
|
hide();
|
2006-08-21 01:17:32 +02:00
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2006-08-19 02:09:36 +02:00
|
|
|
void
|
2006-08-25 02:29:28 +02:00
|
|
|
AddRequestDialog::run_edit(Request& request)
|
2006-08-19 02:09:36 +02:00
|
|
|
{
|
2006-08-25 02:29:28 +02:00
|
|
|
update_combo();
|
|
|
|
|
|
|
|
_list_model->clear();
|
|
|
|
|
|
|
|
History& history = Simulation::get_instance().get_history();
|
|
|
|
const Environment::Resources& resources = history.get_last_environment().get_resources();
|
|
|
|
|
|
|
|
_instant_spin->set_value(static_cast<double>(request.get_instant()));
|
2006-08-20 19:52:46 +02:00
|
|
|
|
2006-08-25 02:29:28 +02:00
|
|
|
// PLEASE KEEP THIS A COPY, WE *NEED* TO COPY IT
|
|
|
|
vector<SubRequest*> subrequests = request.get_subrequests();
|
|
|
|
|
|
|
|
for(Iseq<vector<SubRequest*>::iterator> it = iseq(subrequests); it; ++it)
|
|
|
|
{
|
|
|
|
SubRequest& sr = *(*it);
|
|
|
|
|
|
|
|
TreeModel::Row row = *(_list_model->append());
|
|
|
|
|
|
|
|
unsigned int key = sr.get_resource_key();
|
|
|
|
row[_list_key_column] = key;
|
|
|
|
|
|
|
|
const ustring name = resources.find(key)->second->get_name();
|
|
|
|
row[_list_resource_column] = name;
|
|
|
|
|
|
|
|
row[_list_length_column] = sr.get_length();
|
|
|
|
}
|
|
|
|
|
|
|
|
if(run() == RESPONSE_OK)
|
|
|
|
{
|
|
|
|
assert(_list_model->children());
|
|
|
|
|
|
|
|
// I know it's a bit hack-ish, but do you know an elegant alternative way?
|
|
|
|
for(Iseq<vector<SubRequest*>::iterator> it = iseq(subrequests); it; ++it)
|
|
|
|
history.remove(*(*it));
|
|
|
|
|
|
|
|
history.edit_request(request, _instant_spin->get_value_as_int());
|
|
|
|
|
|
|
|
TreeNodeChildren sreq_container = _list_model->children();
|
|
|
|
|
|
|
|
for(Iseq<TreeIter> it = iseq(sreq_container); it; ++it)
|
|
|
|
history.add_subrequest(request, (*it)[_list_key_column], (*it)[_list_length_column]);
|
|
|
|
}
|
|
|
|
|
|
|
|
hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AddRequestDialog::update_combo()
|
|
|
|
{
|
2006-08-19 02:09:36 +02:00
|
|
|
typedef Environment::Resources::const_iterator ResourceIt;
|
|
|
|
|
|
|
|
const Environment::Resources& resources =
|
|
|
|
Simulation::get_instance().get_history().get_last_environment().get_resources();
|
|
|
|
|
|
|
|
_combo_model->clear();
|
|
|
|
|
|
|
|
for(Iseq<ResourceIt> it = const_iseq(resources); it; ++it)
|
|
|
|
{
|
|
|
|
TreeModel::Row row = *(_combo_model->append());
|
|
|
|
row[_combo_key_column] = it->first;
|
|
|
|
row[_combo_resource_column] = it->second->get_name();
|
|
|
|
}
|
2006-08-21 01:17:32 +02:00
|
|
|
|
2006-08-20 19:52:46 +02:00
|
|
|
Dialog::on_show();
|
2006-08-19 02:09:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AddRequestDialog::_on_add()
|
|
|
|
{
|
2006-08-20 19:52:46 +02:00
|
|
|
TreeModel::iterator sel = _resource_combo->get_active();
|
|
|
|
|
2006-08-21 01:17:32 +02:00
|
|
|
TreeModel::Row row = *(_list_model->append());
|
2006-08-20 19:52:46 +02:00
|
|
|
|
2006-08-21 01:17:32 +02:00
|
|
|
const unsigned int key = (*sel)[_combo_key_column];
|
|
|
|
row[_list_key_column] = key;
|
2006-08-20 19:52:46 +02:00
|
|
|
|
2006-08-21 01:17:32 +02:00
|
|
|
const ustring resource = (*sel)[_combo_resource_column];
|
|
|
|
row[_list_resource_column] = resource;
|
|
|
|
|
2006-08-25 02:29:28 +02:00
|
|
|
const unsigned int length = _duration_spin->get_value_as_int();
|
2006-08-21 01:17:32 +02:00
|
|
|
row[_list_length_column] = length;
|
2006-08-19 02:09:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AddRequestDialog::_on_remove()
|
|
|
|
{
|
2006-08-20 19:52:46 +02:00
|
|
|
TreeModel::iterator it = _list->get_selection()->get_selected();
|
|
|
|
_list_model->erase(it);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2006-08-21 01:17:32 +02:00
|
|
|
AddRequestDialog::_on_list_selection_changed()
|
2006-08-20 19:52:46 +02:00
|
|
|
{
|
|
|
|
_remove_button->set_sensitive(
|
|
|
|
_list->get_selection()->count_selected_rows() > 0);
|
2006-08-19 02:09:36 +02:00
|
|
|
}
|
|
|
|
|
2006-08-21 01:17:32 +02:00
|
|
|
void
|
|
|
|
AddRequestDialog::_on_row_added(const Gtk::TreePath& path, const Gtk::TreeIter& it)
|
|
|
|
{
|
|
|
|
_ok_button->set_sensitive(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AddRequestDialog::_on_row_removed(const Gtk::TreePath& path)
|
|
|
|
{
|
|
|
|
_ok_button->set_sensitive(static_cast<bool>(_list_model->children()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AddRequestDialog::_on_combo_selection_changed()
|
|
|
|
{
|
|
|
|
_add_button->set_sensitive(
|
|
|
|
static_cast<bool>(_resource_combo->get_active()));
|
|
|
|
}
|
|
|
|
|