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-21 19:50:44 +02:00
|
|
|
SimulationWidget::SimulationWidget(Simulation& simulation)
|
|
|
|
: Glib::ObjectBase("sgpem_SimulationWidget"),
|
|
|
|
CairoWidget(),
|
|
|
|
SimulationObserver(),
|
|
|
|
HistoryObserver(),
|
|
|
|
_simulation(&simulation),
|
2006-08-20 05:49:17 +02:00
|
|
|
_x_unit(10), _y_unit(10)
|
|
|
|
|
2006-08-18 09:27:00 +02:00
|
|
|
{
|
2006-08-21 19:50:44 +02:00
|
|
|
// Register this SimulationObserver:
|
|
|
|
_simulation->attach(*this);
|
2006-08-20 05:49:17 +02:00
|
|
|
|
2006-08-21 19:50:44 +02:00
|
|
|
// Register this HistoryObserver:
|
|
|
|
_simulation->get_history().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-21 19:50:44 +02:00
|
|
|
// Unregister this HistoryObserver:
|
|
|
|
_simulation->get_history().detach(*this);
|
|
|
|
|
|
|
|
// Unregister this SimulationObserver:
|
|
|
|
_simulation->detach(*this);
|
2006-08-18 09:27:00 +02:00
|
|
|
}
|
|
|
|
|
2006-08-21 19:50:44 +02:00
|
|
|
#pragma argsused
|
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
|
|
|
{
|
|
|
|
// Force redraw
|
2006-08-21 15:02:04 +02:00
|
|
|
//redraw();
|
|
|
|
resize_redraw();
|
2006-08-18 09:27:00 +02:00
|
|
|
}
|
2006-07-28 17:24:56 +02:00
|
|
|
|
2006-08-21 19:50:44 +02:00
|
|
|
#pragma argsused
|
|
|
|
void
|
|
|
|
SimulationWidget::update(const History& changed_history)
|
|
|
|
{
|
|
|
|
// Force redraw
|
|
|
|
//redraw();
|
|
|
|
resize_redraw();
|
|
|
|
}
|
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
|
|
|
*/
|
2006-08-21 15:02:04 +02:00
|
|
|
|
|
|
|
double top_margin = _y_unit;
|
|
|
|
double left_margin = _x_unit;
|
|
|
|
double top_graph_margin = 1.0 * _y_unit; //3.0 * _y_unit;
|
|
|
|
double left_graph_margin = 11.0 * _x_unit;
|
|
|
|
double process_label_delta = 1.0 * _y_unit;
|
|
|
|
double process_bar_delta = 1.0 * _y_unit;
|
|
|
|
double process_bar_height = 1.0 * _y_unit;
|
|
|
|
double process_height = process_bar_height + 2*process_bar_delta;
|
2006-08-21 19:50:44 +02:00
|
|
|
Simulation::state sim_state = _simulation->get_state();
|
|
|
|
const History& hist = _simulation->get_history();
|
2006-08-20 05:49:17 +02:00
|
|
|
const Environment::Processes& processes = hist.get_last_environment().get_processes();
|
|
|
|
int nproc = processes.size();
|
|
|
|
double text_maxw = 0;
|
|
|
|
bool* terminated = 0;
|
2006-08-21 15:02:04 +02:00
|
|
|
cairo_text_extents_t extents;
|
|
|
|
|
|
|
|
// evaluate minimum x and y extensions
|
|
|
|
/*
|
|
|
|
// show simulation title
|
|
|
|
Glib::ustring title;
|
|
|
|
switch(sim_state)
|
|
|
|
{
|
|
|
|
case Simulation::state_running:
|
|
|
|
title = "Simulation Running";
|
|
|
|
break;
|
|
|
|
case Simulation::state_paused:
|
|
|
|
title = "Simulation Paused";
|
|
|
|
break;
|
|
|
|
case Simulation::state_stopped:
|
|
|
|
title = "Simulation Stopped";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
cairo_text_extents(ctx, title.c_str(), &extents);
|
|
|
|
cairo_move_to(ctx, left_margin, extents.height + top_margin);
|
|
|
|
cairo_show_text(ctx,title.c_str());
|
|
|
|
*/
|
|
|
|
|
2006-08-20 05:49:17 +02:00
|
|
|
if(nproc>0)
|
|
|
|
terminated = new bool[nproc];
|
2006-08-21 15:02:04 +02:00
|
|
|
|
|
|
|
// show processes names...
|
|
|
|
// set clip region to cut long names
|
|
|
|
cairo_rectangle(ctx, 0, top_graph_margin, left_graph_margin - _x_unit, nproc*process_height);
|
|
|
|
cairo_clip(ctx); // set the rectangular clip region
|
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-21 15:02:04 +02:00
|
|
|
// cairo_text_extents_t extents;
|
2006-08-20 05:49:17 +02:00
|
|
|
cairo_text_extents(ctx, processes[i]->get_name().c_str(), &extents);
|
|
|
|
if(text_maxw<extents.width)
|
|
|
|
text_maxw=extents.width;
|
2006-08-21 15:02:04 +02:00
|
|
|
cairo_move_to(ctx, left_margin, extents.height + top_graph_margin + process_height*i + process_label_delta);
|
2006-08-20 05:49:17 +02:00
|
|
|
cairo_show_text(ctx,processes[i]->get_name().c_str());
|
|
|
|
terminated[i] = false;
|
2006-08-18 09:27:00 +02:00
|
|
|
}
|
2006-08-21 15:02:04 +02:00
|
|
|
cairo_reset_clip(ctx); // remove clip region
|
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();
|
|
|
|
|
2006-08-21 15:02:04 +02:00
|
|
|
// show grid
|
|
|
|
cairo_save(ctx);
|
|
|
|
cairo_set_line_width(ctx, 0.5*cairo_get_line_width(ctx));
|
|
|
|
// nproc+1 horizontal lines
|
|
|
|
for(int i=0; i<=nproc; i++)
|
|
|
|
{
|
|
|
|
cairo_move_to(ctx, left_graph_margin, top_graph_margin + process_height*i);
|
|
|
|
cairo_line_to(ctx, left_graph_margin + (pos+2)*_x_unit, top_graph_margin + process_height*i);
|
|
|
|
}
|
|
|
|
// opening vertical line
|
|
|
|
cairo_move_to(ctx, left_graph_margin, top_graph_margin);
|
|
|
|
cairo_line_to(ctx, left_graph_margin, top_graph_margin + process_height*nproc);
|
|
|
|
cairo_stroke(ctx);
|
|
|
|
|
|
|
|
// closing vertical line
|
|
|
|
if(sim_state!=Simulation::state_stopped)
|
|
|
|
{
|
|
|
|
double dashes = 1.5;
|
|
|
|
cairo_set_dash(ctx, &dashes, 1, 0.0);
|
|
|
|
}
|
|
|
|
cairo_move_to(ctx, left_graph_margin + (pos+2)*_x_unit, top_graph_margin);
|
|
|
|
cairo_line_to(ctx, left_graph_margin + (pos+2)*_x_unit, top_graph_margin + process_height*nproc);
|
|
|
|
cairo_stroke(ctx);
|
|
|
|
cairo_restore(ctx);
|
|
|
|
|
2006-08-20 05:49:17 +02:00
|
|
|
for(int t=1; t<=pos; t++)
|
|
|
|
{
|
|
|
|
const Environment::Processes& processes = hist.get_environment_at(t).get_processes();
|
2006-08-21 15:02:04 +02:00
|
|
|
double xpos = left_graph_margin + t*_x_unit;
|
2006-08-20 05:49:17 +02:00
|
|
|
for(int i=0; i<nproc; i++)
|
|
|
|
{
|
2006-08-21 15:02:04 +02:00
|
|
|
double ypos = top_graph_margin + process_height*i + process_bar_delta;
|
2006-08-20 05:49:17 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-08-21 15:02:04 +02:00
|
|
|
|
|
|
|
|
|
|
|
cairo_save(ctx);
|
|
|
|
cairo_set_source_rgb(ctx, 0, 0, 0);
|
|
|
|
cairo_set_line_width(ctx, 0.25*cairo_get_line_width(ctx));
|
|
|
|
for(int t=0; t<=pos; t++)
|
|
|
|
{
|
|
|
|
cairo_move_to(ctx, left_graph_margin + (t+1)*_x_unit, top_graph_margin + process_height*nproc);
|
|
|
|
cairo_rel_line_to(ctx, 0, 0.5 * _y_unit);
|
|
|
|
Glib::ustring val;
|
|
|
|
int_to_string(t, val);
|
|
|
|
cairo_move_to(ctx, left_graph_margin + (t+1)*_x_unit,
|
|
|
|
top_graph_margin + process_height*nproc + _y_unit + extents.height);
|
|
|
|
cairo_show_text(ctx,val.c_str());
|
|
|
|
}
|
|
|
|
cairo_stroke(ctx);
|
|
|
|
cairo_restore(ctx);
|
|
|
|
|
|
|
|
/*
|
|
|
|
int w = left_graph_margin + (pos+4)*_x_unit;
|
|
|
|
int h = top_graph_margin + process_height*nproc + 4*_y_unit;
|
|
|
|
if(get_height()<h || get_width()<w)
|
|
|
|
set_size_request(w,h);
|
|
|
|
*/
|
2006-08-20 05:49:17 +02:00
|
|
|
}
|
|
|
|
delete[] terminated;
|
2006-08-21 15:02:04 +02:00
|
|
|
|
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-21 15:02:04 +02:00
|
|
|
SimulationWidget::calc_drawing_size(cairo_t* ctx, size_t& width, size_t& height)
|
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();
|
2006-08-21 15:02:04 +02:00
|
|
|
int pos = _simulation->get_front();
|
|
|
|
cairo_text_extents_t extents;
|
|
|
|
|
|
|
|
// std::cout << " x_unit: " << std::endl;
|
|
|
|
Glib::ustring val("999");
|
|
|
|
cairo_text_extents(ctx, val.c_str(), &extents);
|
|
|
|
if(_x_unit<extents.width)
|
|
|
|
_x_unit=extents.width;
|
|
|
|
if(_y_unit<extents.height)
|
|
|
|
_y_unit=extents.height;
|
|
|
|
|
|
|
|
// left margin, labels, graph
|
|
|
|
width = (1.0 + 11.0 + 3.0 + pos) * _x_unit;
|
|
|
|
height = (1.0 + 3.0 * processes.size() + 3.0) * _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
|
|
|
*/
|