2007-06-24 14:45:05 +02:00
|
|
|
# Copyright (C) 2006-2007 Red Hat, Inc.
|
2006-10-15 01:24:45 +02:00
|
|
|
#
|
|
|
|
# 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
|
|
|
|
|
2007-08-06 21:33:27 +02:00
|
|
|
import colorsys
|
|
|
|
from gettext import gettext as _
|
2007-08-17 17:52:22 +02:00
|
|
|
import logging
|
2006-10-04 14:23:15 +02:00
|
|
|
import math
|
2007-08-24 16:28:33 +02:00
|
|
|
import os
|
2007-08-06 21:33:27 +02:00
|
|
|
|
|
|
|
import hippo
|
2007-01-07 07:18:57 +01:00
|
|
|
import gobject
|
2007-08-06 21:33:27 +02:00
|
|
|
import gtk
|
2006-10-04 14:23:15 +02:00
|
|
|
|
2007-08-25 13:15:28 +02:00
|
|
|
from sugar.graphics.icon import CanvasIcon
|
2007-08-21 21:39:05 +02:00
|
|
|
from sugar.graphics.menuitem import MenuItem
|
2007-08-06 21:33:27 +02:00
|
|
|
from sugar.graphics.palette import Palette
|
2007-07-31 16:46:06 +02:00
|
|
|
from sugar.graphics import style
|
2007-02-23 13:09:33 +01:00
|
|
|
from sugar.graphics import xocolor
|
2007-01-09 04:55:12 +01:00
|
|
|
from sugar import profile
|
2007-09-11 17:36:33 +02:00
|
|
|
import proc_smaps
|
2007-01-09 04:55:12 +01:00
|
|
|
|
2007-01-09 21:25:51 +01:00
|
|
|
# TODO: rgb_to_html and html_to_rgb are useful elsewhere
|
|
|
|
# we should put this in a common module
|
2007-01-09 04:55:12 +01:00
|
|
|
def rgb_to_html(r, g, b):
|
|
|
|
""" (r, g, b) tuple (in float format) -> #RRGGBB """
|
|
|
|
return '#%02x%02x%02x' % (int(r * 255), int(g * 255), int(b * 255))
|
|
|
|
|
|
|
|
def html_to_rgb(html_color):
|
|
|
|
""" #RRGGBB -> (r, g, b) tuple (in float format) """
|
|
|
|
html_color = html_color.strip()
|
|
|
|
if html_color[0] == '#':
|
|
|
|
html_color = html_color[1:]
|
|
|
|
if len(html_color) != 6:
|
|
|
|
raise ValueError, "input #%s is not in #RRGGBB format" % html_color
|
|
|
|
r, g, b = html_color[:2], html_color[2:4], html_color[4:]
|
|
|
|
r, g, b = [int(n, 16) for n in (r, g, b)]
|
|
|
|
r, g, b = (r / 255.0, g / 255.0, b / 255.0)
|
|
|
|
return (r, g, b)
|
2006-10-04 14:23:15 +02:00
|
|
|
|
2007-08-24 16:28:33 +02:00
|
|
|
_MAX_ACTIVITIES = 10
|
|
|
|
_MIN_WEDGE_SIZE = 1.0 / _MAX_ACTIVITIES
|
|
|
|
|
2007-01-07 07:18:57 +01:00
|
|
|
class ActivityIcon(CanvasIcon):
|
2007-01-16 16:44:43 +01:00
|
|
|
_INTERVAL = 250
|
2007-01-09 04:55:12 +01:00
|
|
|
|
2007-08-06 21:33:27 +02:00
|
|
|
__gsignals__ = {
|
|
|
|
'resume': (gobject.SIGNAL_RUN_FIRST,
|
|
|
|
gobject.TYPE_NONE, ([])),
|
|
|
|
'stop': (gobject.SIGNAL_RUN_FIRST,
|
|
|
|
gobject.TYPE_NONE, ([]))
|
|
|
|
}
|
|
|
|
|
2007-01-07 07:18:57 +01:00
|
|
|
def __init__(self, activity):
|
|
|
|
icon_name = activity.get_icon_name()
|
2007-07-15 12:51:06 +02:00
|
|
|
self._orig_color = activity.get_icon_color()
|
2007-01-14 20:22:12 +01:00
|
|
|
self._icon_colors = self._compute_icon_colors()
|
2007-01-15 00:22:43 +01:00
|
|
|
|
2007-01-09 04:55:12 +01:00
|
|
|
self._direction = 0
|
2007-01-14 20:22:12 +01:00
|
|
|
self._level_max = len(self._icon_colors) - 1
|
|
|
|
self._level = self._level_max
|
|
|
|
color = self._icon_colors[self._level]
|
2007-01-09 04:55:12 +01:00
|
|
|
|
2007-09-08 12:10:35 +02:00
|
|
|
CanvasIcon.__init__(self, file_name=icon_name, xo_color=color,
|
|
|
|
size=style.MEDIUM_ICON_SIZE, cache=True)
|
2007-01-07 07:18:57 +01:00
|
|
|
|
|
|
|
self._activity = activity
|
2007-06-01 11:03:18 +02:00
|
|
|
self._pulse_id = 0
|
|
|
|
|
2007-08-24 16:28:33 +02:00
|
|
|
self.size = _MIN_WEDGE_SIZE
|
|
|
|
|
2007-08-06 21:33:27 +02:00
|
|
|
palette = Palette(_('Starting...'))
|
|
|
|
self.set_palette(palette)
|
|
|
|
|
2007-06-01 11:03:18 +02:00
|
|
|
activity.connect('notify::launching', self._launching_changed_cb)
|
|
|
|
if activity.props.launching:
|
|
|
|
self._start_pulsing()
|
2007-08-06 21:33:27 +02:00
|
|
|
else:
|
|
|
|
self._setup_palette()
|
|
|
|
|
|
|
|
def _setup_palette(self):
|
|
|
|
palette = self.get_palette()
|
|
|
|
|
|
|
|
palette.set_primary_text(self._activity.get_title())
|
|
|
|
|
2007-08-21 21:39:05 +02:00
|
|
|
resume_menu_item = MenuItem(_('Resume'), 'zoom-activity')
|
2007-08-06 21:33:27 +02:00
|
|
|
resume_menu_item.connect('activate', self._resume_activate_cb)
|
2007-08-08 12:56:19 +02:00
|
|
|
palette.menu.append(resume_menu_item)
|
2007-08-06 21:33:27 +02:00
|
|
|
resume_menu_item.show()
|
|
|
|
|
|
|
|
# FIXME: kludge
|
|
|
|
if self._activity.get_type() != "org.laptop.JournalActivity":
|
2007-08-21 21:39:05 +02:00
|
|
|
stop_menu_item = MenuItem(_('Stop'), 'activity-stop')
|
2007-08-06 21:33:27 +02:00
|
|
|
stop_menu_item.connect('activate', self._stop_activate_cb)
|
2007-08-08 12:56:19 +02:00
|
|
|
palette.menu.append(stop_menu_item)
|
2007-08-06 21:33:27 +02:00
|
|
|
stop_menu_item.show()
|
2007-06-01 11:03:18 +02:00
|
|
|
|
|
|
|
def _launching_changed_cb(self, activity, pspec):
|
2007-08-24 16:28:33 +02:00
|
|
|
if not activity.props.launching:
|
2007-06-01 11:03:18 +02:00
|
|
|
self._stop_pulsing()
|
2007-08-24 16:28:33 +02:00
|
|
|
self._setup_palette()
|
2007-01-07 07:18:57 +01:00
|
|
|
|
|
|
|
def __del__(self):
|
2007-06-01 11:03:18 +02:00
|
|
|
self._cleanup()
|
2007-01-09 04:55:12 +01:00
|
|
|
|
2007-06-01 11:03:18 +02:00
|
|
|
def _cleanup(self):
|
|
|
|
if self._pulse_id:
|
2007-01-07 07:18:57 +01:00
|
|
|
gobject.source_remove(self._pulse_id)
|
2007-01-09 04:55:12 +01:00
|
|
|
self._pulse_id = 0
|
|
|
|
|
2007-01-14 20:22:12 +01:00
|
|
|
def _compute_icon_colors(self):
|
|
|
|
_LEVEL_MAX = 1.6
|
|
|
|
_LEVEL_STEP = 0.16
|
|
|
|
_LEVEL_MIN = 0.0
|
|
|
|
icon_colors = {}
|
|
|
|
level = _LEVEL_MIN
|
|
|
|
for i in range(0, int(_LEVEL_MAX / _LEVEL_STEP)):
|
|
|
|
icon_colors[i] = self._get_icon_color_for_level(level)
|
|
|
|
level += _LEVEL_STEP
|
|
|
|
return icon_colors
|
|
|
|
|
|
|
|
def _get_icon_color_for_level(self, level):
|
|
|
|
factor = math.sin(level)
|
2007-01-09 04:55:12 +01:00
|
|
|
h, s, v = colorsys.rgb_to_hsv(*html_to_rgb(self._orig_color.get_fill_color()))
|
|
|
|
new_fill = rgb_to_html(*colorsys.hsv_to_rgb(h, s * factor, v))
|
|
|
|
h, s, v = colorsys.rgb_to_hsv(*html_to_rgb(self._orig_color.get_stroke_color()))
|
|
|
|
new_stroke = rgb_to_html(*colorsys.hsv_to_rgb(h, s * factor, v))
|
2007-02-23 13:09:33 +01:00
|
|
|
return xocolor.XoColor("%s,%s" % (new_stroke, new_fill))
|
2007-01-07 07:18:57 +01:00
|
|
|
|
|
|
|
def _pulse_cb(self):
|
2007-01-09 04:55:12 +01:00
|
|
|
if self._direction == 1:
|
2007-01-14 20:22:12 +01:00
|
|
|
self._level += 1
|
|
|
|
if self._level > self._level_max:
|
2007-01-09 04:55:12 +01:00
|
|
|
self._direction = 0
|
2007-01-14 20:22:12 +01:00
|
|
|
self._level = self._level_max
|
2007-01-09 04:55:12 +01:00
|
|
|
elif self._direction == 0:
|
2007-01-14 20:22:12 +01:00
|
|
|
self._level -= 1
|
|
|
|
if self._level <= 0:
|
2007-01-09 04:55:12 +01:00
|
|
|
self._direction = 1
|
2007-01-14 20:22:12 +01:00
|
|
|
self._level = 0
|
2007-01-09 04:55:12 +01:00
|
|
|
|
2007-02-23 17:08:37 +01:00
|
|
|
self.props.xo_color = self._icon_colors[self._level]
|
2007-01-09 04:55:12 +01:00
|
|
|
self.emit_paint_needed(0, 0, -1, -1)
|
|
|
|
return True
|
2007-01-07 07:18:57 +01:00
|
|
|
|
2007-06-01 11:03:18 +02:00
|
|
|
def _start_pulsing(self):
|
|
|
|
if self._pulse_id:
|
2007-01-07 07:18:57 +01:00
|
|
|
return
|
2007-06-01 11:03:18 +02:00
|
|
|
|
|
|
|
self._pulse_id = gobject.timeout_add(self._INTERVAL, self._pulse_cb)
|
|
|
|
|
|
|
|
def _stop_pulsing(self):
|
|
|
|
if not self._pulse_id:
|
|
|
|
return
|
|
|
|
|
|
|
|
self._cleanup()
|
2007-01-09 04:55:12 +01:00
|
|
|
self._level = 100.0
|
2007-02-23 17:08:37 +01:00
|
|
|
self.props.xo_color = self._orig_color
|
2007-08-03 17:31:21 +02:00
|
|
|
|
2007-08-06 21:33:27 +02:00
|
|
|
def _resume_activate_cb(self, menuitem):
|
|
|
|
self.emit('resume')
|
|
|
|
|
|
|
|
def _stop_activate_cb(self, menuitem):
|
|
|
|
self.emit('stop')
|
|
|
|
|
2007-01-07 07:18:57 +01:00
|
|
|
def get_activity(self):
|
|
|
|
return self._activity
|
|
|
|
|
2006-10-04 14:23:15 +02:00
|
|
|
class ActivitiesDonut(hippo.CanvasBox, hippo.CanvasItem):
|
2006-12-04 20:12:24 +01:00
|
|
|
__gtype_name__ = 'SugarActivitiesDonut'
|
|
|
|
def __init__(self, shell, **kwargs):
|
|
|
|
hippo.CanvasBox.__init__(self, **kwargs)
|
2006-10-04 14:23:15 +02:00
|
|
|
|
2007-06-01 14:19:14 +02:00
|
|
|
self._activities = []
|
2006-12-24 12:19:24 +01:00
|
|
|
self._shell = shell
|
2007-08-03 17:31:21 +02:00
|
|
|
self._angles = []
|
2007-09-11 17:36:33 +02:00
|
|
|
self._shell_mappings = proc_smaps.get_shared_mapping_names(os.getpid())
|
2006-10-04 14:23:15 +02:00
|
|
|
|
2006-12-24 14:35:02 +01:00
|
|
|
self._model = shell.get_model().get_home()
|
2006-12-24 12:19:24 +01:00
|
|
|
self._model.connect('activity-added', self._activity_added_cb)
|
2007-01-07 07:18:57 +01:00
|
|
|
self._model.connect('activity-removed', self._activity_removed_cb)
|
2007-08-08 15:30:45 +02:00
|
|
|
self._model.connect('pending-activity-changed', self._activity_changed_cb)
|
2006-10-04 14:23:15 +02:00
|
|
|
|
2007-08-02 20:14:21 +02:00
|
|
|
self.connect('button-release-event', self._button_release_event_cb)
|
|
|
|
|
2007-06-01 14:19:14 +02:00
|
|
|
def _get_icon_from_activity(self, activity):
|
|
|
|
for icon in self._activities:
|
|
|
|
if icon.get_activity().equals(activity):
|
|
|
|
return icon
|
|
|
|
|
2007-01-07 07:18:57 +01:00
|
|
|
def _activity_added_cb(self, model, activity):
|
2007-06-01 11:03:18 +02:00
|
|
|
self._add_activity(activity)
|
2007-01-07 07:18:57 +01:00
|
|
|
|
2006-12-24 12:19:24 +01:00
|
|
|
def _activity_removed_cb(self, model, activity):
|
2006-12-04 20:12:24 +01:00
|
|
|
self._remove_activity(activity)
|
|
|
|
|
2007-01-31 17:16:33 +01:00
|
|
|
def _activity_changed_cb(self, model, activity):
|
|
|
|
self.emit_paint_needed(0, 0, -1, -1)
|
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
def _remove_activity(self, activity):
|
2007-06-01 14:19:14 +02:00
|
|
|
icon = self._get_icon_from_activity(activity)
|
|
|
|
if icon:
|
|
|
|
self.remove(icon)
|
|
|
|
icon._cleanup()
|
|
|
|
self._activities.remove(icon)
|
2007-08-24 16:28:33 +02:00
|
|
|
self._compute_angles()
|
2006-10-04 14:23:15 +02:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
def _add_activity(self, activity):
|
2007-01-07 07:18:57 +01:00
|
|
|
icon = ActivityIcon(activity)
|
2007-08-06 21:33:27 +02:00
|
|
|
icon.connect('resume', self._activity_icon_resumed_cb)
|
|
|
|
icon.connect('stop', self._activity_icon_stop_cb)
|
2006-12-04 20:12:24 +01:00
|
|
|
self.append(icon, hippo.PACK_FIXED)
|
2006-10-04 14:23:15 +02:00
|
|
|
|
2007-06-01 14:19:14 +02:00
|
|
|
self._activities.append(icon)
|
2007-08-24 16:28:33 +02:00
|
|
|
self._compute_angles()
|
2006-10-04 14:23:15 +02:00
|
|
|
|
2007-08-06 21:33:27 +02:00
|
|
|
def _activity_icon_resumed_cb(self, icon):
|
|
|
|
activity = icon.get_activity()
|
|
|
|
activity_host = self._shell.get_activity(activity.get_activity_id())
|
|
|
|
if activity_host:
|
|
|
|
activity_host.present()
|
|
|
|
else:
|
|
|
|
logging.error("Could not find ActivityHost for activity %s" %
|
|
|
|
activity.get_activity_id())
|
|
|
|
|
|
|
|
def _activity_icon_stop_cb(self, icon):
|
|
|
|
activity = icon.get_activity()
|
|
|
|
activity_host = self._shell.get_activity(activity.get_activity_id())
|
|
|
|
if activity_host:
|
|
|
|
activity_host.close()
|
|
|
|
else:
|
|
|
|
logging.error("Could not find ActivityHost for activity %s" %
|
|
|
|
activity.get_activity_id())
|
|
|
|
|
2007-08-02 20:14:21 +02:00
|
|
|
def _get_activity(self, x, y):
|
|
|
|
# Compute the distance from the center.
|
|
|
|
[width, height] = self.get_allocation()
|
|
|
|
x -= width / 2
|
|
|
|
y -= height / 2
|
|
|
|
r = math.hypot(x, y)
|
|
|
|
|
|
|
|
# Ignore the click if it's not inside the donut
|
|
|
|
if r < self._get_inner_radius() or r > self._get_radius():
|
|
|
|
return None
|
|
|
|
|
|
|
|
# Now figure out where in the donut the click was.
|
|
|
|
angle = math.atan2(-y, -x) + math.pi
|
|
|
|
|
|
|
|
# Unfortunately, _get_angles() doesn't count from 0 to 2pi, it
|
|
|
|
# counts from roughly pi/2 to roughly 5pi/2. So we have to
|
|
|
|
# compare its return values against both angle and angle+2pi
|
|
|
|
high_angle = angle + 2 * math.pi
|
|
|
|
|
|
|
|
for index, activity in enumerate(self._model):
|
|
|
|
[angle_start, angle_end] = self._get_angles(index)
|
|
|
|
if angle_start < angle and angle_end > angle:
|
|
|
|
return activity
|
|
|
|
elif angle_start < high_angle and angle_end > high_angle:
|
|
|
|
return activity
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
def _button_release_event_cb(self, item, event):
|
|
|
|
activity = self._get_activity(event.x, event.y)
|
|
|
|
if activity is None:
|
|
|
|
return False
|
|
|
|
|
2007-04-08 19:20:59 +02:00
|
|
|
activity_host = self._shell.get_activity(activity.get_activity_id())
|
2006-12-24 12:19:24 +01:00
|
|
|
if activity_host:
|
|
|
|
activity_host.present()
|
2007-08-02 20:14:21 +02:00
|
|
|
return True
|
2006-10-04 14:23:15 +02:00
|
|
|
|
2007-08-24 16:28:33 +02:00
|
|
|
def _update_activity_sizes(self):
|
|
|
|
# Get the memory mappings of each process that hosts an
|
|
|
|
# activity, and count how many activity instances each
|
|
|
|
# activity process hosts, and how many processes are mapping
|
|
|
|
# each shared library, etc
|
2007-09-11 17:36:33 +02:00
|
|
|
process_mappings = {}
|
2007-08-03 17:31:21 +02:00
|
|
|
num_activities = {}
|
2007-08-24 16:28:33 +02:00
|
|
|
num_mappings = {}
|
|
|
|
unknown_size_activities = 0
|
2007-08-03 17:31:21 +02:00
|
|
|
for activity in self._model:
|
|
|
|
pid = activity.get_pid()
|
|
|
|
if not pid:
|
|
|
|
# Still starting up, hasn't opened a window yet
|
2007-08-24 16:28:33 +02:00
|
|
|
unknown_size_activities += 1
|
2007-08-03 17:31:21 +02:00
|
|
|
continue
|
|
|
|
|
2007-08-24 16:28:33 +02:00
|
|
|
if num_activities.has_key(pid):
|
2007-08-03 17:31:21 +02:00
|
|
|
num_activities[pid] += 1
|
|
|
|
continue
|
|
|
|
|
|
|
|
try:
|
2007-09-11 17:36:33 +02:00
|
|
|
mappings = proc_smaps.get_mappings(pid, self._shell_mappings)
|
|
|
|
for mapping in mappings:
|
|
|
|
if mapping.shared > 0:
|
2007-08-24 16:28:33 +02:00
|
|
|
if num_mappings.has_key(mapping.name):
|
|
|
|
num_mappings[mapping.name] += 1
|
|
|
|
else:
|
|
|
|
num_mappings[mapping.name] = 1
|
2007-09-11 17:36:33 +02:00
|
|
|
process_mappings[pid] = mappings
|
2007-08-03 17:31:21 +02:00
|
|
|
num_activities[pid] = 1
|
2007-08-24 16:28:33 +02:00
|
|
|
except Exception, e:
|
|
|
|
logging.warn('ActivitiesDonut: could not read /proc/%s/smaps: %r'
|
|
|
|
% (pid, e))
|
|
|
|
|
|
|
|
# Compute total memory used per process
|
|
|
|
process_size = {}
|
|
|
|
total_activity_size = 0
|
|
|
|
for activity in self._model:
|
|
|
|
pid = activity.get_pid()
|
2007-09-11 17:36:33 +02:00
|
|
|
if not process_mappings.has_key(pid):
|
2007-08-24 16:28:33 +02:00
|
|
|
continue
|
|
|
|
|
2007-09-11 17:36:33 +02:00
|
|
|
mappings = process_mappings[pid]
|
2007-08-24 16:28:33 +02:00
|
|
|
size = 0
|
2007-09-11 17:36:33 +02:00
|
|
|
for mapping in mappings:
|
|
|
|
size += mapping.private
|
|
|
|
if mapping.shared > 0:
|
2007-08-24 16:28:33 +02:00
|
|
|
num = num_mappings[mapping.name]
|
2007-09-11 17:36:33 +02:00
|
|
|
size += mapping.shared / num
|
2007-08-24 16:28:33 +02:00
|
|
|
process_size[pid] = size
|
|
|
|
total_activity_size += size / num_activities[pid]
|
|
|
|
|
|
|
|
# Now, see how much free memory is left.
|
2007-08-17 17:52:22 +02:00
|
|
|
free_memory = 0
|
2007-08-03 17:31:21 +02:00
|
|
|
try:
|
|
|
|
meminfo = open('/proc/meminfo')
|
2007-08-17 17:52:22 +02:00
|
|
|
for line in meminfo.readlines():
|
|
|
|
if line.startswith('MemFree:') or line.startswith('SwapFree:'):
|
|
|
|
free_memory += int(line[9:-3])
|
2007-08-03 17:31:21 +02:00
|
|
|
meminfo.close()
|
|
|
|
except IOError:
|
|
|
|
logging.warn('ActivitiesDonut: could not read /proc/meminfo')
|
|
|
|
except (IndexError, ValueError):
|
|
|
|
logging.warn('ActivitiesDonut: /proc/meminfo was not in ' +
|
|
|
|
'expected format')
|
|
|
|
|
2007-08-24 16:28:33 +02:00
|
|
|
total_memory = float(total_activity_size + free_memory)
|
2007-08-03 17:31:21 +02:00
|
|
|
|
2007-08-24 16:28:33 +02:00
|
|
|
# Each activity has an ideal size of:
|
|
|
|
# process_size[pid] / num_activities[pid] / total_memory
|
|
|
|
# (And the free memory wedge is ideally free_memory /
|
|
|
|
# total_memory) However, no activity wedge is allowed to be
|
|
|
|
# smaller than _MIN_WEDGE_SIZE. This means the small
|
|
|
|
# activities will use up extra space, which would make the
|
|
|
|
# ring overflow. We fix that by reducing the large activities
|
|
|
|
# and the free space proportionately. If there are activities
|
|
|
|
# of unknown size, they are simply carved out of the free
|
|
|
|
# space.
|
2007-08-03 17:31:21 +02:00
|
|
|
|
2007-08-24 16:28:33 +02:00
|
|
|
free_percent = free_memory / total_memory
|
2007-08-03 17:31:21 +02:00
|
|
|
activity_sizes = []
|
2007-08-24 16:28:33 +02:00
|
|
|
overflow = 0.0
|
|
|
|
reducible = free_percent
|
|
|
|
for icon in self._activities:
|
|
|
|
pid = icon.get_activity().get_pid()
|
2007-08-03 17:31:21 +02:00
|
|
|
if process_size.has_key(pid):
|
2007-08-24 16:28:33 +02:00
|
|
|
icon.size = (process_size[pid] / num_activities[pid] /
|
|
|
|
total_memory)
|
|
|
|
if icon.size < _MIN_WEDGE_SIZE:
|
|
|
|
overflow += _MIN_WEDGE_SIZE - icon.size
|
|
|
|
icon.size = _MIN_WEDGE_SIZE
|
|
|
|
else:
|
|
|
|
reducible += icon.size - _MIN_WEDGE_SIZE
|
|
|
|
else:
|
|
|
|
icon.size = _MIN_WEDGE_SIZE
|
|
|
|
|
|
|
|
if reducible > 0.0:
|
|
|
|
reduction = overflow / reducible
|
|
|
|
if unknown_size_activities > 0:
|
|
|
|
unknown_percent = _MIN_WEDGE_SIZE * unknown_size_activities
|
|
|
|
if (free_percent * (1 - reduction) < unknown_percent):
|
|
|
|
# The free wedge won't be large enough to fit the
|
|
|
|
# unknown-size activities. So adjust things
|
|
|
|
overflow += unknown_percent - free_percent
|
|
|
|
reducible -= free_percent
|
|
|
|
reduction = overflow / reducible
|
|
|
|
|
|
|
|
if reduction > 0.0:
|
|
|
|
for icon in self._activities:
|
|
|
|
if icon.size > _MIN_WEDGE_SIZE:
|
|
|
|
icon.size -= (icon.size - _MIN_WEDGE_SIZE) * reduction
|
|
|
|
|
2007-08-03 17:31:21 +02:00
|
|
|
def _compute_angles(self):
|
|
|
|
self._angles = []
|
2007-08-24 16:28:33 +02:00
|
|
|
if len(self._activities) == 0:
|
2007-08-03 17:31:21 +02:00
|
|
|
return
|
|
|
|
|
2007-08-24 16:28:33 +02:00
|
|
|
# Normally we don't _update_activity_sizes() when launching a
|
|
|
|
# new activity; but if the new wedge would overflow the ring
|
|
|
|
# then we have no choice.
|
|
|
|
total = reduce(lambda s1,s2: s1 + s2,
|
|
|
|
[icon.size for icon in self._activities])
|
|
|
|
if total > 1.0:
|
|
|
|
self._update_activity_sizes()
|
|
|
|
|
2007-08-03 17:31:21 +02:00
|
|
|
# The first wedge (Journal) should be centered at 6 o'clock
|
2007-08-24 16:28:33 +02:00
|
|
|
size = self._activities[0].size or _MIN_WEDGE_SIZE
|
|
|
|
angle = (math.pi - size * 2 * math.pi) / 2
|
2007-08-03 17:31:21 +02:00
|
|
|
self._angles.append(angle)
|
|
|
|
|
2007-08-24 16:28:33 +02:00
|
|
|
for icon in self._activities:
|
|
|
|
size = icon.size or _MIN_WEDGE_SIZE
|
2007-08-03 17:31:21 +02:00
|
|
|
self._angles.append(self._angles[-1] + size * 2 * math.pi)
|
|
|
|
|
2007-08-24 16:28:33 +02:00
|
|
|
def redraw(self):
|
|
|
|
self._update_activity_sizes()
|
|
|
|
self._compute_angles()
|
|
|
|
self.emit_request_changed()
|
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
def _get_angles(self, index):
|
2007-08-03 17:31:21 +02:00
|
|
|
return [self._angles[index],
|
|
|
|
self._angles[(index + 1) % len(self._angles)]]
|
2006-10-04 14:23:15 +02:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
def _get_radius(self):
|
|
|
|
[width, height] = self.get_allocation()
|
|
|
|
return min(width, height) / 2
|
2006-10-04 14:23:15 +02:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
def _get_inner_radius(self):
|
|
|
|
return self._get_radius() * 0.5
|
2006-10-04 14:23:15 +02:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
def do_paint_below_children(self, cr, damaged_box):
|
|
|
|
[width, height] = self.get_allocation()
|
2006-10-04 14:23:15 +02:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
cr.translate(width / 2, height / 2)
|
2006-10-04 14:23:15 +02:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
radius = self._get_radius()
|
2006-10-04 14:23:15 +02:00
|
|
|
|
2007-01-31 17:16:33 +01:00
|
|
|
# Outer Ring
|
2006-12-04 20:12:24 +01:00
|
|
|
cr.set_source_rgb(0xf1 / 255.0, 0xf1 / 255.0, 0xf1 / 255.0)
|
|
|
|
cr.arc(0, 0, radius, 0, 2 * math.pi)
|
|
|
|
cr.fill()
|
2006-10-04 14:23:15 +02:00
|
|
|
|
2007-01-31 17:16:33 +01:00
|
|
|
# Selected Wedge
|
2007-08-08 15:30:45 +02:00
|
|
|
current_activity = self._model.get_pending_activity()
|
2007-01-31 17:16:33 +01:00
|
|
|
if current_activity is not None:
|
|
|
|
selected_index = self._model.index(current_activity)
|
|
|
|
[angle_start, angle_end] = self._get_angles(selected_index)
|
|
|
|
|
|
|
|
cr.new_path()
|
2006-12-04 20:12:24 +01:00
|
|
|
cr.move_to(0, 0)
|
|
|
|
cr.line_to(radius * math.cos(angle_start),
|
|
|
|
radius * math.sin(angle_start))
|
|
|
|
cr.arc(0, 0, radius, angle_start, angle_end)
|
|
|
|
cr.line_to(0, 0)
|
2007-01-31 17:16:33 +01:00
|
|
|
cr.set_source_rgb(1, 1, 1)
|
|
|
|
cr.fill()
|
|
|
|
|
|
|
|
# Edges
|
|
|
|
if len(self._model):
|
|
|
|
n_edges = len(self._model) + 1
|
|
|
|
else:
|
|
|
|
n_edges = 0
|
|
|
|
|
|
|
|
for i in range(0, n_edges):
|
|
|
|
cr.new_path()
|
|
|
|
cr.move_to(0, 0)
|
|
|
|
[angle, unused_angle] = self._get_angles(i)
|
|
|
|
cr.line_to(radius * math.cos(angle),
|
|
|
|
radius * math.sin(angle))
|
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
cr.set_source_rgb(0xe2 / 255.0, 0xe2 / 255.0, 0xe2 / 255.0)
|
|
|
|
cr.set_line_width(4)
|
|
|
|
cr.stroke_preserve()
|
2007-01-31 17:16:33 +01:00
|
|
|
|
|
|
|
# Inner Ring
|
|
|
|
cr.new_path()
|
2006-12-04 20:12:24 +01:00
|
|
|
cr.arc(0, 0, self._get_inner_radius(), 0, 2 * math.pi)
|
2007-01-31 17:16:33 +01:00
|
|
|
cr.set_source_rgb(0xe2 / 255.0, 0xe2 / 255.0, 0xe2 / 255.0)
|
2006-12-04 20:12:24 +01:00
|
|
|
cr.fill()
|
2006-10-04 14:23:15 +02:00
|
|
|
|
2007-01-19 15:47:33 +01:00
|
|
|
def do_allocate(self, width, height, origin_changed):
|
|
|
|
hippo.CanvasBox.do_allocate(self, width, height, origin_changed)
|
2006-10-04 14:23:15 +02:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
radius = (self._get_inner_radius() + self._get_radius()) / 2
|
2006-10-04 14:23:15 +02:00
|
|
|
|
2007-08-02 20:14:21 +02:00
|
|
|
for i, icon in enumerate(self._activities):
|
2006-12-04 20:12:24 +01:00
|
|
|
[angle_start, angle_end] = self._get_angles(i)
|
|
|
|
angle = angle_start + (angle_end - angle_start) / 2
|
2006-10-04 14:23:15 +02:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
[icon_width, icon_height] = icon.get_allocation()
|
2006-10-04 14:23:15 +02:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
x = int(radius * math.cos(angle)) - icon_width / 2
|
|
|
|
y = int(radius * math.sin(angle)) - icon_height / 2
|
2007-01-19 15:47:33 +01:00
|
|
|
self.set_position(icon, x + width / 2, y + height / 2)
|