2006-08-12 17:49:37 +02:00
|
|
|
// src/simulation_widget.cc - Copyright 2005, 2006, University
|
2006-07-12 11:24:57 +02:00
|
|
|
// 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
|
|
|
|
|
2006-08-12 17:49:37 +02:00
|
|
|
#include "simulation_widget.hh"
|
2006-07-12 11:24:57 +02:00
|
|
|
|
2006-08-04 22:08:55 +02:00
|
|
|
#include "cairo_elements.hh"
|
2006-08-18 09:27:00 +02:00
|
|
|
#include "backend/history.hh"
|
|
|
|
#include "backend/simulation.hh"
|
|
|
|
#include "backend/string_utils.hh"
|
2006-08-03 13:16:16 +02:00
|
|
|
|
2006-08-03 17:43:21 +02:00
|
|
|
#include <cassert>
|
|
|
|
|
2006-08-12 17:49:37 +02:00
|
|
|
#ifndef NDEBUG
|
|
|
|
#include <iostream>
|
|
|
|
#endif
|
|
|
|
|
2006-07-12 11:24:57 +02:00
|
|
|
using namespace sgpem;
|
|
|
|
|
|
|
|
|
2006-08-12 17:49:37 +02:00
|
|
|
SimulationWidget::SimulationWidget()
|
2006-08-18 09:27:00 +02:00
|
|
|
: Glib::ObjectBase("sgpem_SimulationWidget"), CairoWidget(),
|
2006-08-20 05:49:17 +02:00
|
|
|
SimulationObserver(), _simulation(0),
|
|
|
|
_x_unit(10), _y_unit(10)
|
|
|
|
|
2006-08-18 09:27:00 +02:00
|
|
|
{
|
|
|
|
// Register this observer:
|
2006-08-20 05:49:17 +02:00
|
|
|
Simulation::get_instance().attach(*this);
|
|
|
|
|
2006-08-18 09:27:00 +02:00
|
|
|
}
|
2006-08-09 16:38:45 +02:00
|
|
|
|
2006-07-12 11:24:57 +02:00
|
|
|
|
2006-08-12 17:49:37 +02:00
|
|
|
SimulationWidget::~SimulationWidget()
|
2006-08-18 09:27:00 +02:00
|
|
|
{
|
2006-08-20 05:49:17 +02:00
|
|
|
Simulation::get_instance().detach(*this);
|
2006-08-18 09:27:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2006-08-20 05:49:17 +02:00
|
|
|
SimulationWidget::update(const Simulation& changed_simulation)
|
2006-08-18 09:27:00 +02:00
|
|
|
{
|
2006-08-20 05:49:17 +02:00
|
|
|
_simulation = &changed_simulation;
|
2006-08-18 09:27:00 +02:00
|
|
|
|
|
|
|
// Force redraw
|
|
|
|
redraw();
|
|
|
|
}
|
2006-07-28 17:24:56 +02:00
|
|
|
|
2006-08-02 15:48:26 +02:00
|
|
|
|
|
|
|
void
|
2006-08-12 17:49:37 +02:00
|
|
|
SimulationWidget::draw_widget(cairo_t* ctx)
|
2006-08-02 15:48:26 +02:00
|
|
|
{
|
2006-08-20 05:49:17 +02:00
|
|
|
/*
|
|
|
|
std::cout << " draw_widget start " << std::endl;
|
|
|
|
if(!_simulation)
|
|
|
|
return;
|
|
|
|
std::cout << " draw_widget continue " << std::endl;
|
2006-08-18 09:27:00 +02:00
|
|
|
|
2006-08-20 05:49:17 +02:00
|
|
|
*/
|
|
|
|
const History& hist = Simulation::get_instance().get_history();
|
|
|
|
const Environment::Processes& processes = hist.get_last_environment().get_processes();
|
|
|
|
int nproc = processes.size();
|
|
|
|
double text_maxw = 0;
|
|
|
|
bool* terminated = 0;
|
|
|
|
if(nproc>0)
|
|
|
|
terminated = new bool[nproc];
|
2006-08-18 09:27:00 +02:00
|
|
|
|
2006-08-20 05:49:17 +02:00
|
|
|
for(int i=0; i<nproc; i++)
|
2006-08-18 09:27:00 +02:00
|
|
|
{
|
2006-08-20 05:49:17 +02:00
|
|
|
cairo_text_extents_t extents;
|
|
|
|
cairo_text_extents(ctx, processes[i]->get_name().c_str(), &extents);
|
|
|
|
if(text_maxw<extents.width)
|
|
|
|
text_maxw=extents.width;
|
|
|
|
cairo_move_to(ctx, _x_unit, extents.height + (3*i+1)*_y_unit);
|
|
|
|
cairo_show_text(ctx,processes[i]->get_name().c_str());
|
|
|
|
terminated[i] = false;
|
2006-08-18 09:27:00 +02:00
|
|
|
}
|
2006-08-09 16:38:45 +02:00
|
|
|
|
2006-08-20 05:49:17 +02:00
|
|
|
if(_simulation && hist.get_size()>0
|
|
|
|
/* && _simulation->get_state()!=Simulation::state_stopped */ )
|
|
|
|
{
|
|
|
|
// std::cout << " draw_widget not_stop " << std::endl;
|
|
|
|
unsigned int pos = _simulation->get_front();
|
|
|
|
|
|
|
|
for(int t=1; t<=pos; t++)
|
|
|
|
{
|
|
|
|
const Environment::Processes& processes = hist.get_environment_at(t).get_processes();
|
|
|
|
double xpos = text_maxw + (1 + t)*_x_unit;
|
|
|
|
for(int i=0; i<nproc; i++)
|
|
|
|
{
|
|
|
|
double ypos = (3*i+1)*_y_unit;
|
|
|
|
Schedulable::state st = processes[i]->get_state();
|
|
|
|
switch(st)
|
|
|
|
{
|
|
|
|
case Schedulable::state_running:
|
|
|
|
cairo_set_source_rgb(ctx, 0, 1, 0);
|
|
|
|
cairo_rectangle(ctx, xpos, ypos, _x_unit, _y_unit);
|
|
|
|
cairo_fill(ctx);
|
|
|
|
break;
|
|
|
|
case Schedulable::state_ready:
|
|
|
|
cairo_set_source_rgb(ctx, 1, 1, 0);
|
|
|
|
cairo_rectangle(ctx, xpos, ypos, _x_unit, _y_unit);
|
|
|
|
cairo_fill(ctx);
|
|
|
|
break;
|
|
|
|
case Schedulable::state_blocked:
|
|
|
|
cairo_set_source_rgb(ctx, 1, 0, 0);
|
|
|
|
cairo_rectangle(ctx, xpos, ypos, _x_unit, _y_unit);
|
|
|
|
cairo_fill(ctx);
|
|
|
|
break;
|
|
|
|
case Schedulable::state_future:
|
|
|
|
break;
|
|
|
|
case Schedulable::state_terminated:
|
|
|
|
if(!terminated[i])
|
|
|
|
{
|
|
|
|
cairo_set_source_rgb(ctx, 0, 0, 0);
|
|
|
|
cairo_rectangle(ctx, xpos, ypos, _x_unit, _y_unit);
|
|
|
|
cairo_fill(ctx);
|
|
|
|
}
|
|
|
|
terminated[i] = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete[] terminated;
|
2006-08-02 15:48:26 +02:00
|
|
|
}
|
|
|
|
|
2006-07-28 17:24:56 +02:00
|
|
|
|
2006-08-18 09:27:00 +02:00
|
|
|
void
|
2006-08-18 21:54:24 +02:00
|
|
|
SimulationWidget::calc_drawing_size(size_t& width, size_t& height) const
|
2006-08-18 09:27:00 +02:00
|
|
|
{
|
2006-08-20 05:49:17 +02:00
|
|
|
if(!_simulation)
|
|
|
|
return;
|
|
|
|
const History& hist = _simulation->get_history();
|
|
|
|
const Environment::Processes& processes = hist.get_last_environment().get_processes();
|
|
|
|
width = (10 + 1 + hist.get_size()) * _x_unit;
|
|
|
|
height = 3 * processes.size() * _y_unit;
|
2006-08-18 09:27:00 +02:00
|
|
|
}
|
|
|
|
|
2006-08-20 05:49:17 +02:00
|
|
|
/*
|
2006-08-18 21:54:24 +02:00
|
|
|
bool
|
|
|
|
SimulationWidget::on_button_press_event(GdkEventButton* event)
|
|
|
|
{
|
|
|
|
std::cout << " on_button_press_event " << std::endl;
|
|
|
|
change_scaling_mode();
|
|
|
|
// Not here. Yet.
|
|
|
|
return true;
|
|
|
|
}
|
2006-08-20 05:49:17 +02:00
|
|
|
*/
|
2006-08-18 21:54:24 +02:00
|
|
|
|
2006-08-20 05:49:17 +02:00
|
|
|
/*
|
2006-08-12 17:49:37 +02:00
|
|
|
void
|
2006-08-18 21:54:24 +02:00
|
|
|
SimulationWidget::change_scaling_mode()
|
2006-08-02 15:48:26 +02:00
|
|
|
{
|
2006-08-12 17:49:37 +02:00
|
|
|
|
2006-08-18 21:54:24 +02:00
|
|
|
std::cout << " change_scaling_mode " << std::endl;
|
|
|
|
scaling_mode scaling = get_scaling_mode();
|
|
|
|
switch(scaling)
|
|
|
|
{
|
|
|
|
case scaling_none:
|
|
|
|
set_scaling_mode(scaling_to_w);
|
|
|
|
break;
|
|
|
|
case scaling_to_w:
|
|
|
|
case scaling_to_h:
|
|
|
|
case scaling_min:
|
|
|
|
case scaling_max:
|
|
|
|
set_scaling_mode((scaling_mode)(scaling << 1));
|
|
|
|
break;
|
|
|
|
case scaling_all:
|
|
|
|
set_scaling_mode(scaling_none);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
redraw();
|
2006-07-12 11:24:57 +02:00
|
|
|
}
|
2006-08-20 05:49:17 +02:00
|
|
|
*/
|