From 8e918f865be7caa14ea714df8ab10cfac55b29ce Mon Sep 17 00:00:00 2001 From: Eduardo Silva Date: Sun, 10 Jun 2007 21:19:59 +0000 Subject: Console: new graphics package --- diff --git a/configure.ac b/configure.ac index 3c9bb7b..364e8fc 100644 --- a/configure.ac +++ b/configure.ac @@ -57,6 +57,7 @@ shell/model/Makefile shell/model/devices/Makefile shell/model/devices/network/Makefile services/console/lib/Makefile +services/console/lib/graphics/Makefile services/console/lib/procmem/Makefile services/console/Makefile services/console/interface/Makefile diff --git a/services/console/interface/xo/battery.py b/services/console/interface/xo/battery.py index f4b9b02..a5bff6b 100644 --- a/services/console/interface/xo/battery.py +++ b/services/console/interface/xo/battery.py @@ -20,6 +20,7 @@ import gtk import gobject from label import Label +from graphics.box import BoxGraphic class XO_Battery(gtk.Fixed): @@ -31,13 +32,13 @@ class XO_Battery(gtk.Fixed): self._battery_charge = self._get_battery_status() - self._battery_drw = gtk.DrawingArea() - self._battery_drw.set_size_request(70, 150) - self._battery_drw.connect("expose-event", self.do_expose) + self._battery_box = BoxGraphic() + self._battery_box.set_size_request(70, 150) + self._battery_box.set_capacity(self._battery_charge) fixed = gtk.Fixed(); fixed.set_border_width(10) - fixed.add(self._battery_drw) + fixed.add(self._battery_box) hbox = gtk.HBox(False, 0) hbox.pack_start(fixed, False, False, 4) @@ -50,7 +51,7 @@ class XO_Battery(gtk.Fixed): label_charge = Label('Charge: ' , Label.DESCRIPTION) self.label_charge_value = Label(str(self._battery_charge) + '%', Label.DESCRIPTION) - + table.attach(label_charge, 0, 1, 0, 1) table.attach(self.label_charge_value, 1,2, 0,1) @@ -85,22 +86,10 @@ class XO_Battery(gtk.Fixed): if new_charge != self._battery_charge: self._battery_charge = self._get_battery_status() self.label_charge_value.set_text(str(self._battery_charge) + '%') - self._battery_drw.queue_draw() + self._battery_box.set_capacity(self._battery_charge) return True - def do_expose(self, widget, event): - context = widget.window.cairo_create() - - [width, height] = widget.size_request() - context.rectangle(0, 0, width, height) - - context.set_source_rgb (0,0,0) - context.fill_preserve() - context.stroke() - - self._draw_battery_usage(context, width, height) - def _get_battery_status(self): battery_class_path = '/sys/class/battery/psu_0/' capacity_path = battery_class_path + 'capacity_percentage' @@ -113,20 +102,3 @@ class XO_Battery(gtk.Fixed): capacity = 0 return capacity - - def _draw_battery_usage(self, context, width, height): - - usage_height = (self._battery_charge*height)/100 - - context.rectangle(0, height - usage_height, width, height) - - if self._battery_charge > 50: - context.set_source_rgb (0,1,0) - - if self._battery_charge > 10 and self._battery_charge <= 50: - context.set_source_rgb (1,1,0) - - if self._battery_charge <= 10: - context.set_source_rgb (1,0,0) - - context.fill_preserve() diff --git a/services/console/interface/xo/cpu.py b/services/console/interface/xo/cpu.py index c64764b..520725c 100644 --- a/services/console/interface/xo/cpu.py +++ b/services/console/interface/xo/cpu.py @@ -21,9 +21,11 @@ import sys import gtk import string import gobject - +import cairo import procmem +from graphics.frequency import HorizontalGraphic + class CPU_Usage: _CPU_HZ = 0 @@ -54,7 +56,7 @@ class CPU_Usage: cpu_info = self._get_CPU_data() used_jiffies = (int(cpu_info[2]) + int(cpu_info[3]) + int(cpu_info[4])) - + if self._times ==0: self._last_jiffies = used_jiffies self._times +=1 @@ -87,6 +89,7 @@ class XO_CPU(gtk.Frame): width = (gtk.gdk.screen_width() * 99 / 100) - 50 height = (gtk.gdk.screen_height() * 15 / 100) - 20 + # Create graphic self._graphic = HorizontalGraphic() self._graphic.set_size_request(width, height) @@ -105,149 +108,7 @@ class XO_CPU(gtk.Frame): self._cpu = self._DRW_CPU._get_CPU_usage() self._updated = True - #print "Sending: " + str(self._cpu) self.set_label('System CPU Usage: ' + str(self._cpu) + '%') + # Draw the value into the graphic self._graphic.draw_value(self._cpu) return True - -class HorizontalGraphic(gtk.DrawingArea): - _MARGIN = 5 - _LINE_WIDTH = 2 - _GRAPH_OFFSET = 7 - _range_x = [] - _range_y = [] - _frequency_timer = 0 - - def __init__(self): - gtk.DrawingArea.__init__(self) - self.connect('expose-event', self.do_expose) - self.connect('size-allocate', self._change_size_cb) - - self._buffer = [0] - - def do_expose(self, widget, event): - context = widget.window.cairo_create() - context.rectangle(0, 0, self._width - 1, self._height - 1) - context.set_source_rgb (0,0,0) - context.fill_preserve() - context.set_line_width(self._LINE_WIDTH) - - if event.area.x == 0: - draw_all = True - - self._draw_border_lines(context) - context.stroke() - else: - draw_all = False - context.rectangle(event.area.x, event.area.y, event.area.width, event.area.height) - context.clip() - - context.set_source_rgb(1, 1, 1) - self._draw_buffer(event, widget, context, draw_all) - context.stroke() - - self._updated = False - return False - - def draw_value(self, percent): - redraw_all = False - - if (len(self._buffer) + 1) *self._GRAPH_OFFSET >= self._width: - redraw_all = True - self._buffer = [self._buffer[-1]] - length = 1 - else: - length = len(self._buffer) - 1 - - self._buffer.append(percent) - self._updated = True - - if redraw_all: - area_x = 0 - area_y = 0 - height = self._height - width = self._width - else: - area_x = (length*self._GRAPH_OFFSET) - area_y = self._graph_y - width = self._GRAPH_OFFSET * 2 - height = self._graph_height - - self.queue_draw_area(area_x, area_y, width, height) - self._frequency_timer += 1 - - return True - - def _draw_border_lines(self, context): - context.set_source_rgb(1, 1, 1) - self._draw_line(context, self._MARGIN, self._MARGIN, self._MARGIN, self._height - self._MARGIN) - self._draw_line(context, self._MARGIN, self._height - self._MARGIN - 1, self._width - self._MARGIN, self._height - self._MARGIN - 1) - - def _draw_line(self, context, from_x, from_y, to_x, to_y): - #print "" - #print "DRAWING LINE" - #print "from: " + str(from_x) + "," + str(from_y) + "," + str(to_x) + "," + str(to_y) - - context.move_to(from_x, from_y) - context.line_to(to_x, to_y) - - def _draw_buffer(self, event, drwarea, context, draw_all=True): - buffer_offset = 0 - freq = 1 # Frequency timer - - length = len(self._buffer) - - if length == 0: - return - - # Context properties - context.set_line_width(self._LINE_WIDTH) - context.set_source_rgb(0,1,0) - - if draw_all == True: - buffer_offset = 0 - freq = 0 - else: - freq = buffer_offset = (event.area.x/self._GRAPH_OFFSET) - - for percent in self._buffer[buffer_offset:length]: - if buffer_offset == 0: - from_y = self._height - self._GRAPH_OFFSET - from_x = self._GRAPH_OFFSET - else: - from_y = self._get_y(self._buffer[buffer_offset-1]) - from_x = (freq * self._GRAPH_OFFSET) - - to_x = (freq+1) * self._GRAPH_OFFSET - to_y = self._get_y(percent) - - self._draw_line(context, from_x, from_y, to_x, to_y) - buffer_offset+=1 - freq+=1 - - context.stroke() - - def _get_y(self, percent): - y_value = self._GRAPH_OFFSET + (self._graph_height - ((percent*self._graph_height)/100)) - return int(y_value) - - def _change_size_cb(self, widget, allocation): - self._width = allocation.width - self._height = allocation.height - - self._graph_x = self._MARGIN + self._LINE_WIDTH - self._graph_y = self._MARGIN + self._LINE_WIDTH - self._graph_width = self._width - (self._MARGIN + self._LINE_WIDTH) - self._graph_height = self._height - ((self._MARGIN + self._LINE_WIDTH)*2) - - -# Just for test >:) -#graph = HorizontalGraphic() -#graph.set_size_request(400,200) -""" -window = gtk.Window() -window.set_size_request(500, 250) -window.add(XO_CPU()) -window.show_all() -gtk.main() -""" diff --git a/services/console/interface/xo/drwarea.py b/services/console/interface/xo/drwarea.py deleted file mode 100644 index e896b1f..0000000 --- a/services/console/interface/xo/drwarea.py +++ /dev/null @@ -1,53 +0,0 @@ -#!/usr/bin/env python - -# Copyright (C) 2007, Eduardo Silva (edsiper@gmail.com). -# -# This program 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. -# -# This program 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 this program; if not, write to the Free Software -# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - -############################################# -# Drawing area tools -############################################# - -import gtk -import gtk.gdk - -class Drawing_Area_Tools: - - height = 0 - width = 0 - - margin = 5 # Left and bottom margin - - range_x = [] - range_y = [] - - def __init__(self, drwarea): - drwarea_size = drwarea.get_size_request() - - self.width = drwarea_size[0] - self.height = drwarea_size[1] - - self.range_x = {'from': self.margin+2, 'to': self.width - (self.margin+2)} - self.range_y = {'from': self.margin+2, 'to': self.height - (self.margin+2)} - - def draw_line(self, context, from_x, from_y, to_x, to_y): - context.move_to(from_x, from_y) - context.line_to(to_x, to_y) - - def draw_border_lines(self, context): - context.set_source_rgb(1, 1, 1) - self.draw_line(context, self.margin, self.margin, self.margin, self.height - self.margin) - self.draw_line(context, self.margin, self.height - self.margin - 1, self.width - self.margin, self.height - self.margin - 1) - context.stroke() diff --git a/services/console/lib/Makefile.am b/services/console/lib/Makefile.am index 16782d7..fba9382 100644 --- a/services/console/lib/Makefile.am +++ b/services/console/lib/Makefile.am @@ -1,4 +1,4 @@ -SUBDIRS = procmem +SUBDIRS = procmem graphics sugardir = $(pkgdatadir)/shell/console/lib sugar_PYTHON = diff --git a/services/console/lib/graphics/Makefile.am b/services/console/lib/graphics/Makefile.am new file mode 100644 index 0000000..159f2bb --- /dev/null +++ b/services/console/lib/graphics/Makefile.am @@ -0,0 +1,7 @@ + +sugardir = $(pkgdatadir)/services/console/lib/graphics + +sugar_PYTHON = \ + __init__.py \ + box.py \ + frequency.py diff --git a/services/console/lib/graphics/__init__.py b/services/console/lib/graphics/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/services/console/lib/graphics/__init__.py diff --git a/services/console/lib/graphics/box.py b/services/console/lib/graphics/box.py new file mode 100644 index 0000000..f2363ac --- /dev/null +++ b/services/console/lib/graphics/box.py @@ -0,0 +1,63 @@ +##!/usr/bin/env python + +# Copyright (C) 2007, Eduardo Silva (edsiper@gmail.com). +# +# This program 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. +# +# This program 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 this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +import gtk +import cairo + +class BoxGraphic(gtk.DrawingArea): + def __init__(self): + gtk.DrawingArea.__init__(self) + self.connect("expose-event", self.do_expose) + self.connect('size-allocate', self._change_size_cb) + self.set_capacity(0) + + def do_expose(self, widget, event): + context = widget.window.cairo_create() + context.rectangle(0, 0, self._width, self._height) + + context.set_source_rgb (0,0,0) + context.fill_preserve() + context.stroke() + + print self._percent + self._draw_content(context, self._percent) + + def set_capacity(self, percent): + self._percent = percent + self.queue_draw() + + def _draw_content(self, context, percent): + usage_height = (percent*self._height)/100 + + context.rectangle(0, self._height - usage_height, self._width, self._height) + + if self._percent > 50: + context.set_source_rgb (0,1,0) + + if self._percent > 10 and self._percent <= 50: + context.set_source_rgb (1,1,0) + + if self._percent <= 10: + context.set_source_rgb (1,0,0) + + context.fill_preserve() + + def _change_size_cb(self, widget, allocation): + self._width = allocation.width + self._height = allocation.height + self.queue_draw() diff --git a/services/console/lib/graphics/frequency.py b/services/console/lib/graphics/frequency.py new file mode 100644 index 0000000..4e184c9 --- /dev/null +++ b/services/console/lib/graphics/frequency.py @@ -0,0 +1,144 @@ +##!/usr/bin/env python + +# Copyright (C) 2007, Eduardo Silva (edsiper@gmail.com). +# +# This program 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. +# +# This program 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 this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +import gtk + +class HorizontalGraphic(gtk.DrawingArea): + _MARGIN = 5 + _LINE_WIDTH = 2 + _GRAPH_OFFSET = 7 + _range_x = [] + _range_y = [] + _frequency_timer = 0 + + def __init__(self): + gtk.DrawingArea.__init__(self) + self.connect('expose-event', self.do_expose) + self.connect('size-allocate', self._change_size_cb) + self._buffer = [0] + + def do_expose(self, widget, event): + context = widget.window.cairo_create() + context.rectangle(0, 0, self._width - 1, self._height - 1) + context.set_source_rgb (0,0,0) + context.fill_preserve() + context.set_line_width(self._LINE_WIDTH) + + if event.area.x == 0: + draw_all = True + + self._draw_border_lines(context) + context.stroke() + else: + draw_all = False + context.rectangle(event.area.x, event.area.y, event.area.width, event.area.height) + context.clip() + + context.set_source_rgb(1, 1, 1) + self._draw_buffer(event, widget, context, draw_all) + context.stroke() + + self._updated = False + return False + + def draw_value(self, percent): + redraw_all = False + + if (len(self._buffer) + 1) *self._GRAPH_OFFSET >= self._width: + redraw_all = True + self._buffer = [self._buffer[-1]] + length = 1 + else: + length = len(self._buffer) - 1 + + self._buffer.append(percent) + self._updated = True + + if redraw_all: + area_x = 0 + area_y = 0 + height = self._height + width = self._width + else: + area_x = (length*self._GRAPH_OFFSET) + area_y = self._graph_y + width = self._GRAPH_OFFSET * 2 + height = self._graph_height + + self.queue_draw_area(area_x, area_y, width, height) + self._frequency_timer += 1 + + return True + + def _draw_border_lines(self, context): + context.set_source_rgb(1, 1, 1) + self._draw_line(context, self._MARGIN, self._MARGIN, self._MARGIN, self._height - self._MARGIN) + self._draw_line(context, self._MARGIN, self._height - self._MARGIN - 1, self._width - self._MARGIN, self._height - self._MARGIN - 1) + + def _draw_line(self, context, from_x, from_y, to_x, to_y): + context.move_to(from_x, from_y) + context.line_to(to_x, to_y) + + def _draw_buffer(self, event, drwarea, context, draw_all=True): + buffer_offset = 0 + freq = 1 # Frequency timer + + length = len(self._buffer) + + if length == 0: + return + + # Context properties + context.set_line_width(self._LINE_WIDTH) + context.set_source_rgb(0,1,0) + + if draw_all == True: + buffer_offset = 0 + freq = 0 + else: + freq = buffer_offset = (event.area.x/self._GRAPH_OFFSET) + + for percent in self._buffer[buffer_offset:length]: + if buffer_offset == 0: + from_y = self._height - self._GRAPH_OFFSET + from_x = self._GRAPH_OFFSET + else: + from_y = self._get_y(self._buffer[buffer_offset-1]) + from_x = (freq * self._GRAPH_OFFSET) + + to_x = (freq+1) * self._GRAPH_OFFSET + to_y = self._get_y(percent) + + self._draw_line(context, from_x, from_y, to_x, to_y) + buffer_offset+=1 + freq+=1 + + context.stroke() + + def _get_y(self, percent): + y_value = self._GRAPH_OFFSET + (self._graph_height - ((percent*self._graph_height)/100)) + return int(y_value) + + def _change_size_cb(self, widget, allocation): + self._width = allocation.width + self._height = allocation.height + + self._graph_x = self._MARGIN + self._LINE_WIDTH + self._graph_y = self._MARGIN + self._LINE_WIDTH + self._graph_width = self._width - (self._MARGIN + self._LINE_WIDTH) + self._graph_height = self._height - ((self._MARGIN + self._LINE_WIDTH)*2) -- cgit v0.9.1