2006-10-15 01:24:45 +02:00
|
|
|
# Copyright (C) 2006, Red Hat, Inc.
|
|
|
|
#
|
|
|
|
# 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
|
|
|
|
|
2006-10-04 14:23:15 +02:00
|
|
|
import hippo
|
|
|
|
import math
|
2007-01-07 07:18:57 +01:00
|
|
|
import gobject
|
2007-01-09 04:55:12 +01:00
|
|
|
import colorsys
|
2006-10-04 14:23:15 +02:00
|
|
|
|
|
|
|
from sugar.graphics.canvasicon import CanvasIcon
|
2006-10-05 14:38:37 +02:00
|
|
|
from sugar.graphics import style
|
2007-01-09 04:55:12 +01:00
|
|
|
from sugar.graphics import colors
|
|
|
|
from sugar.graphics import iconcolor
|
|
|
|
from sugar import profile
|
|
|
|
|
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-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-01-07 07:18:57 +01:00
|
|
|
def __init__(self, activity):
|
|
|
|
icon_name = activity.get_icon_name()
|
2007-01-09 04:55:12 +01:00
|
|
|
self._orig_color = profile.get_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-01-15 00:22:43 +01:00
|
|
|
CanvasIcon.__init__(self, icon_name=icon_name, color=color, cache=True)
|
2007-01-07 07:18:57 +01:00
|
|
|
style.apply_stylesheet(self, 'ring.ActivityIcon')
|
|
|
|
|
|
|
|
self._activity = activity
|
|
|
|
self._launched = False
|
2007-01-09 04:55:12 +01:00
|
|
|
self._pulse_id = gobject.timeout_add(self._INTERVAL, self._pulse_cb)
|
2007-01-07 07:18:57 +01:00
|
|
|
|
|
|
|
def __del__(self):
|
2007-01-09 04:55:12 +01:00
|
|
|
self.cleanup()
|
|
|
|
|
|
|
|
def cleanup(self):
|
2007-01-07 07:18:57 +01:00
|
|
|
if self._pulse_id > 0:
|
|
|
|
gobject.source_remove(self._pulse_id)
|
2007-01-09 04:55:12 +01:00
|
|
|
self._pulse_id = 0
|
2007-01-15 00:22:43 +01:00
|
|
|
# dispose of all rendered icons from launch feedback
|
|
|
|
self._clear_buffers()
|
2007-01-09 04:55:12 +01:00
|
|
|
|
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-01-14 20:22:12 +01:00
|
|
|
return iconcolor.IconColor("%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-01-14 20:22:12 +01:00
|
|
|
self.props.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
|
|
|
|
|
|
|
def set_launched(self):
|
|
|
|
if self._launched:
|
|
|
|
return
|
|
|
|
self._launched = True
|
2007-01-09 04:55:12 +01:00
|
|
|
self.cleanup()
|
|
|
|
self._level = 100.0
|
|
|
|
self.props.color = self._orig_color
|
|
|
|
self.emit_paint_needed(0, 0, -1, -1)
|
2007-01-07 07:18:57 +01:00
|
|
|
|
|
|
|
def get_launched(self):
|
|
|
|
return self._launched
|
|
|
|
|
|
|
|
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
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
self._activities = {}
|
2006-12-24 12:19:24 +01:00
|
|
|
self._shell = shell
|
2006-10-04 14:23:15 +02:00
|
|
|
|
2006-12-24 14:35:02 +01:00
|
|
|
self._model = shell.get_model().get_home()
|
2007-01-07 07:18:57 +01:00
|
|
|
self._model.connect('activity-launched', self._activity_launched_cb)
|
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)
|
2006-10-04 14:23:15 +02:00
|
|
|
|
2007-01-07 07:18:57 +01:00
|
|
|
def _activity_launched_cb(self, model, activity):
|
2006-12-04 20:12:24 +01:00
|
|
|
self._add_activity(activity)
|
2006-10-04 14:23:15 +02:00
|
|
|
|
2007-01-07 07:18:57 +01:00
|
|
|
def _activity_added_cb(self, model, activity):
|
|
|
|
# Mark the activity as launched
|
|
|
|
act_id = activity.get_id()
|
|
|
|
if not self._activities.has_key(act_id):
|
2007-01-09 04:55:12 +01:00
|
|
|
self._add_activity(activity)
|
2007-01-07 07:18:57 +01:00
|
|
|
icon = self._activities[act_id]
|
|
|
|
icon.set_launched()
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
def _remove_activity(self, activity):
|
2007-01-07 07:18:57 +01:00
|
|
|
act_id = activity.get_id()
|
|
|
|
if not self._activities.has_key(act_id):
|
|
|
|
return
|
|
|
|
icon = self._activities[act_id]
|
2006-12-04 20:12:24 +01:00
|
|
|
self.remove(icon)
|
2007-01-18 21:33:22 +01:00
|
|
|
icon.cleanup()
|
2007-01-07 07:18:57 +01:00
|
|
|
del self._activities[act_id]
|
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)
|
|
|
|
icon.connect('activated', self._activity_icon_clicked_cb)
|
2006-12-04 20:12:24 +01:00
|
|
|
self.append(icon, hippo.PACK_FIXED)
|
2006-10-04 14:23:15 +02:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
self._activities[activity.get_id()] = icon
|
2006-10-04 14:23:15 +02:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
self.emit_paint_needed(0, 0, -1, -1)
|
2006-10-04 14:23:15 +02:00
|
|
|
|
2007-01-07 07:18:57 +01:00
|
|
|
def _activity_icon_clicked_cb(self, icon):
|
|
|
|
activity = icon.get_activity()
|
|
|
|
if not icon.get_launched():
|
|
|
|
return
|
|
|
|
|
2006-12-24 12:19:24 +01:00
|
|
|
activity_host = self._shell.get_activity(activity.get_id())
|
|
|
|
if activity_host:
|
|
|
|
activity_host.present()
|
2006-10-04 14:23:15 +02:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
def _get_angles(self, index):
|
|
|
|
angle = 2 * math.pi / 8
|
|
|
|
return [index * angle, (index + 1) * angle]
|
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
|
|
|
|
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
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
angle_end = 0
|
|
|
|
for i in range(0, len(self._activities)):
|
|
|
|
[angle_start, angle_end] = self._get_angles(i)
|
2006-10-04 14:23:15 +02:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
cr.new_path()
|
|
|
|
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)
|
2006-10-04 14:23:15 +02:00
|
|
|
|
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()
|
2006-10-04 14:23:15 +02:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
cr.set_source_rgb(1, 1, 1)
|
|
|
|
cr.fill()
|
2006-10-04 14:23:15 +02:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
cr.set_source_rgb(0xe2 / 255.0, 0xe2 / 255.0, 0xe2 / 255.0)
|
|
|
|
cr.arc(0, 0, self._get_inner_radius(), 0, 2 * math.pi)
|
|
|
|
cr.fill()
|
2006-10-04 14:23:15 +02:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
def do_allocate(self, width, height):
|
|
|
|
hippo.CanvasBox.do_allocate(self, width, height)
|
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
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
i = 0
|
|
|
|
for icon in self._activities.values():
|
|
|
|
[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
|
|
|
|
self.move(icon, x + width / 2, y + height / 2)
|
2006-10-04 14:23:15 +02:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
i += 1
|