sgpemv2/src/simulation_widget.cc

190 lines
5.0 KiB
C++
Raw Normal View History

// src/simulation_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 "simulation_widget.hh"
#include "cairo_elements.hh"
#include "backend/history.hh"
#include "backend/simulation.hh"
#include "backend/string_utils.hh"
#include <cassert>
#ifndef NDEBUG
#include <iostream>
#endif
using namespace sgpem;
SimulationWidget::SimulationWidget()
: Glib::ObjectBase("sgpem_SimulationWidget"), CairoWidget(),
SimulationObserver(), _simulation(0),
_x_unit(10), _y_unit(10)
{
// Register this observer:
Simulation::get_instance().attach(*this);
}
SimulationWidget::~SimulationWidget()
{
Simulation::get_instance().detach(*this);
}
void
SimulationWidget::update(const Simulation& changed_simulation)
{
_simulation = &changed_simulation;
// Force redraw
redraw();
}
void
SimulationWidget::draw_widget(cairo_t* ctx)
{
/*
std::cout << " draw_widget start " << std::endl;
if(!_simulation)
return;
std::cout << " draw_widget continue " << std::endl;
*/
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];
for(int i=0; i<nproc; i++)
{
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;
}
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;
}
void
SimulationWidget::calc_drawing_size(size_t& width, size_t& height) const
{
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;
}
/*
bool
SimulationWidget::on_button_press_event(GdkEventButton* event)
{
std::cout << " on_button_press_event " << std::endl;
change_scaling_mode();
// Not here. Yet.
return true;
}
*/
/*
void
SimulationWidget::change_scaling_mode()
{
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();
}
*/