From ee4d70daa6783edadbc4a2aef4207f5f6fdf349f Mon Sep 17 00:00:00 2001 From: Tomeu Vizoso Date: Fri, 24 Aug 2007 15:16:59 +0200 Subject: [PATCH 1/8] Added test for #2999. --- tests/graphics/ticket2999.py | 38 ++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 tests/graphics/ticket2999.py diff --git a/tests/graphics/ticket2999.py b/tests/graphics/ticket2999.py new file mode 100644 index 00000000..a7b92d56 --- /dev/null +++ b/tests/graphics/ticket2999.py @@ -0,0 +1,38 @@ +# Copyright (C) 2007, One Laptop Per Child +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2 of the License, or (at your option) any later version. +# +# This library 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 +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the +# Free Software Foundation, Inc., 59 Temple Place - Suite 330, +# Boston, MA 02111-1307, USA. + +""" +Spec in ticket #2999. +""" + +import gtk + +from sugar.graphics.palette import Palette +from sugar.graphics.icon import Icon + +import common + +test = common.Test() +test.set_border_width(60) + +text_view = gtk.TextView() +text_view.props.buffer.props.text = 'Blah blah blah, blah blah blah.' +test.pack_start(text_view) +text_view.show() + +if __name__ == "__main__": + common.main(test) From d499fba9b1f86b418a866cc6448e6214fefd0218 Mon Sep 17 00:00:00 2001 From: Tomeu Vizoso Date: Fri, 24 Aug 2007 15:24:30 +0200 Subject: [PATCH 2/8] Added test for #3000. --- tests/graphics/ticket3000.py | 48 ++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 tests/graphics/ticket3000.py diff --git a/tests/graphics/ticket3000.py b/tests/graphics/ticket3000.py new file mode 100644 index 00000000..c28b2cb8 --- /dev/null +++ b/tests/graphics/ticket3000.py @@ -0,0 +1,48 @@ +# Copyright (C) 2007, One Laptop Per Child +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2 of the License, or (at your option) any later version. +# +# This library 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 +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the +# Free Software Foundation, Inc., 59 Temple Place - Suite 330, +# Boston, MA 02111-1307, USA. + +""" +Spec in ticket #3000. +""" + +import gtk + +from sugar.graphics.toolbutton import ToolButton + +import common + +test = common.Test() + +toolbar = gtk.Toolbar() +test.pack_start(toolbar, False) +toolbar.show() + +button = ToolButton('go-previous') +toolbar.insert(button, -1) +button.show() + +separator = gtk.SeparatorToolItem() +toolbar.add(separator) +separator.show() + +button = ToolButton('go-next') +toolbar.insert(button, -1) +button.show() + + +if __name__ == "__main__": + common.main(test) From c25861bd1dc041c63d9bb9268e5394e28c1d7217 Mon Sep 17 00:00:00 2001 From: Dan Winship Date: Fri, 24 Aug 2007 10:26:10 -0400 Subject: [PATCH 3/8] remove debugging --- sugar/graphics/canvasicon.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/sugar/graphics/canvasicon.py b/sugar/graphics/canvasicon.py index 98f67fea..36325514 100644 --- a/sugar/graphics/canvasicon.py +++ b/sugar/graphics/canvasicon.py @@ -331,8 +331,6 @@ class CanvasIcon(hippo.CanvasBox, hippo.CanvasItem): target_w = int(target_w * scale_factor) target_h = int(target_h * scale_factor) - print "icon %s is %d x %d (at scale %r)" % (name, target_w, target_h, scale_factor) - target = cr.get_target() buf = target.create_similar(cairo.CONTENT_COLOR_ALPHA, target_w, target_h) From ebe2b4765e4c17327dc00c4d38ec0c3fc5a468a5 Mon Sep 17 00:00:00 2001 From: Dan Winship Date: Fri, 24 Aug 2007 10:28:33 -0400 Subject: [PATCH 4/8] Fix up the activity ring drawing to be more accurate and stable. #2030 TODO: move some of this code into shell/model rather than shell/view --- shell/view/home/HomeBox.py | 2 +- shell/view/home/Makefile.am | 1 + shell/view/home/activitiesdonut.py | 179 ++++++++++++++++++++--------- shell/view/home/proc_smaps.py | 149 ++++++++++++++++++++++++ 4 files changed, 278 insertions(+), 53 deletions(-) create mode 100644 shell/view/home/proc_smaps.py diff --git a/shell/view/home/HomeBox.py b/shell/view/home/HomeBox.py index f340c1a2..869c9d09 100644 --- a/shell/view/home/HomeBox.py +++ b/shell/view/home/HomeBox.py @@ -122,7 +122,7 @@ class HomeBox(hippo.CanvasBox, hippo.CanvasItem): self._redraw_id = None def _redraw_activity_ring(self): - self._donut.emit_request_changed() + self._donut.redraw() return True def has_activities(self): diff --git a/shell/view/home/Makefile.am b/shell/view/home/Makefile.am index a052dcf2..9beb651f 100644 --- a/shell/view/home/Makefile.am +++ b/shell/view/home/Makefile.am @@ -8,5 +8,6 @@ sugar_PYTHON = \ HomeWindow.py \ MeshBox.py \ MyIcon.py \ + proc_smaps.py \ snowflakelayout.py \ transitionbox.py diff --git a/shell/view/home/activitiesdonut.py b/shell/view/home/activitiesdonut.py index cb9e4b2a..8aa3933d 100644 --- a/shell/view/home/activitiesdonut.py +++ b/shell/view/home/activitiesdonut.py @@ -18,6 +18,7 @@ import colorsys from gettext import gettext as _ import logging import math +import os import hippo import gobject @@ -29,6 +30,7 @@ from sugar.graphics.palette import Palette from sugar.graphics import style from sugar.graphics import xocolor from sugar import profile +from proc_smaps import ProcSmaps # TODO: rgb_to_html and html_to_rgb are useful elsewhere # we should put this in a common module @@ -48,6 +50,9 @@ def html_to_rgb(html_color): r, g, b = (r / 255.0, g / 255.0, b / 255.0) return (r, g, b) +_MAX_ACTIVITIES = 10 +_MIN_WEDGE_SIZE = 1.0 / _MAX_ACTIVITIES + class ActivityIcon(CanvasIcon): _INTERVAL = 250 @@ -74,6 +79,8 @@ class ActivityIcon(CanvasIcon): self._activity = activity self._pulse_id = 0 + self.size = _MIN_WEDGE_SIZE + palette = Palette(_('Starting...')) self.set_palette(palette) @@ -101,11 +108,9 @@ class ActivityIcon(CanvasIcon): stop_menu_item.show() def _launching_changed_cb(self, activity, pspec): - if activity.props.launching: - self._start_pulsing() - else: + if not activity.props.launching: self._stop_pulsing() - self._setup_palette() + self._setup_palette() def __del__(self): self._cleanup() @@ -166,10 +171,6 @@ class ActivityIcon(CanvasIcon): self._level = 100.0 self.props.xo_color = self._orig_color - # Force the donut to redraw now that we know how much memory - # the activity is using. - self.emit_request_changed() - def _resume_activate_cb(self, menuitem): self.emit('resume') @@ -215,6 +216,7 @@ class ActivitiesDonut(hippo.CanvasBox, hippo.CanvasItem): self.remove(icon) icon._cleanup() self._activities.remove(icon) + self._compute_angles() def _add_activity(self, activity): icon = ActivityIcon(activity) @@ -223,8 +225,7 @@ class ActivitiesDonut(hippo.CanvasBox, hippo.CanvasItem): self.append(icon, hippo.PACK_FIXED) self._activities.append(icon) - - self.emit_paint_needed(0, 0, -1, -1) + self._compute_angles() def _activity_icon_resumed_cb(self, icon): activity = icon.get_activity() @@ -282,43 +283,72 @@ class ActivitiesDonut(hippo.CanvasBox, hippo.CanvasItem): activity_host.present() return True - MAX_ACTIVITIES = 10 - MIN_ACTIVITY_WEDGE_SIZE = 1.0 / MAX_ACTIVITIES + def _update_activity_sizes(self): + # First, get the shell's memory mappings; this memory won't be + # counted against the memory used by activities, since it + # would still be in use even if all activities exited. + shell_mappings = {} + try: + shell_smaps = ProcSmaps(os.getpid()) + for mapping in shell_smaps.mappings: + if mapping.shared_clean > 0 or mapping.shared_dirty > 0: + shell_mappings[mapping.name] = mapping + except Exception, e: + logging.warn('ActivitiesDonut: could not read own smaps: %r' % e) - def _get_activity_sizes(self): - # First get the size of each process that hosts an activity, - # and the number of activities it hosts. - process_size = {} + # 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 + process_smaps = {} num_activities = {} - total_activity_size = 0 + num_mappings = {} + unknown_size_activities = 0 for activity in self._model: pid = activity.get_pid() if not pid: # Still starting up, hasn't opened a window yet + unknown_size_activities += 1 continue - if process_size.has_key(pid): + if num_activities.has_key(pid): num_activities[pid] += 1 continue try: - statm = open('/proc/%s/statm' % pid) - # We use "RSS" (the second field in /proc/PID/statm) - # for the activity size because that's what ps and top - # use for calculating "%MEM". We multiply by 4 to - # convert from pages to kb. - process_size[pid] = int(statm.readline().split()[1]) * 4 - total_activity_size += process_size[pid] + smaps = ProcSmaps(pid) + _subtract_mappings(smaps, shell_mappings) + for mapping in smaps.mappings: + if mapping.shared_clean > 0 or mapping.shared_dirty > 0: + if num_mappings.has_key(mapping.name): + num_mappings[mapping.name] += 1 + else: + num_mappings[mapping.name] = 1 + process_smaps[pid] = smaps num_activities[pid] = 1 - statm.close() - except IOError: - logging.warn('ActivitiesDonut: could not read /proc/%s/statm' % - pid) - except (IndexError, ValueError): - logging.warn('ActivitiesDonut: /proc/%s/statm was not in ' + - 'expected format' % pid) + except Exception, e: + logging.warn('ActivitiesDonut: could not read /proc/%s/smaps: %r' + % (pid, e)) - # Next, see how much free memory is left. + # Compute total memory used per process + process_size = {} + total_activity_size = 0 + for activity in self._model: + pid = activity.get_pid() + if not process_smaps.has_key(pid): + continue + + smaps = process_smaps[pid] + size = 0 + for mapping in smaps.mappings: + size += mapping.private_clean + mapping.private_dirty + if mapping.shared_clean + mapping.shared_dirty > 0: + num = num_mappings[mapping.name] + size += (mapping.shared_clean + mapping.shared_dirty) / num + process_size[pid] = size + total_activity_size += size / num_activities[pid] + + # Now, see how much free memory is left. free_memory = 0 try: meminfo = open('/proc/meminfo') @@ -332,39 +362,85 @@ class ActivitiesDonut(hippo.CanvasBox, hippo.CanvasItem): logging.warn('ActivitiesDonut: /proc/meminfo was not in ' + 'expected format') - # Each activity starts with MIN_ACTIVITY_WEDGE_SIZE. The - # remaining space in the donut is allocated proportionately - # among the activities-of-known-size and the free space - used_space = ActivitiesDonut.MIN_ACTIVITY_WEDGE_SIZE * len(self._model) - remaining_space = max(0.0, 1.0 - used_space) + total_memory = float(total_activity_size + free_memory) - total_memory = total_activity_size + free_memory + # 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. + free_percent = free_memory / total_memory activity_sizes = [] - for activity in self._model: - percent = ActivitiesDonut.MIN_ACTIVITY_WEDGE_SIZE - pid = activity.get_pid() + overflow = 0.0 + reducible = free_percent + for icon in self._activities: + pid = icon.get_activity().get_pid() if process_size.has_key(pid): - size = process_size[pid] / num_activities[pid] - percent += remaining_space * size / total_memory - activity_sizes.append(percent) + 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 - return activity_sizes + 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 + + def _subtract_mappings(smaps, mappings_to_remove): + for mapping in smaps.mappings: + if mappings_to_remove.has_key(mapping.name): + mapping.shared_clean = 0 + mapping.shared_dirty = 0 def _compute_angles(self): - percentages = self._get_activity_sizes() self._angles = [] - if len(percentages) == 0: + if len(self._activities) == 0: return + # 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() + # The first wedge (Journal) should be centered at 6 o'clock - size = percentages[0] * 2 * math.pi - angle = (math.pi - size) / 2 + size = self._activities[0].size or _MIN_WEDGE_SIZE + angle = (math.pi - size * 2 * math.pi) / 2 self._angles.append(angle) - for size in percentages: + for icon in self._activities: + size = icon.size or _MIN_WEDGE_SIZE self._angles.append(self._angles[-1] + size * 2 * math.pi) + def redraw(self): + self._update_activity_sizes() + self._compute_angles() + self.emit_request_changed() + def _get_angles(self, index): return [self._angles[index], self._angles[(index + 1) % len(self._angles)]] @@ -431,7 +507,6 @@ class ActivitiesDonut(hippo.CanvasBox, hippo.CanvasItem): radius = (self._get_inner_radius() + self._get_radius()) / 2 - self._compute_angles() for i, icon in enumerate(self._activities): [angle_start, angle_end] = self._get_angles(i) angle = angle_start + (angle_end - angle_start) / 2 diff --git a/shell/view/home/proc_smaps.py b/shell/view/home/proc_smaps.py new file mode 100644 index 00000000..c7a81ec8 --- /dev/null +++ b/shell/view/home/proc_smaps.py @@ -0,0 +1,149 @@ +#################################################################### +# This class open the /proc/PID/maps and /proc/PID/smaps files +# to get useful information about the real memory usage +#################################################################### + +import os +import logging + +_smaps_has_references = None + +# Parse the /proc/PID/smaps file +class ProcSmaps: + + mappings = [] # Devices information + + def __init__(self, pid): + global _smaps_has_references + if _smaps_has_references is None: + _smaps_has_references = os.path.isfile('/proc/%s/clear_refs' % + os.getpid()) + + smapfile = "/proc/%s/smaps" % pid + self.mappings = [] + + # Coded by Federico Mena (script) + infile = open(smapfile, "r") + input = infile.read() + infile.close() + + lines = input.splitlines() + + num_lines = len (lines) + line_idx = 0 + + # 08065000-08067000 rw-p 0001c000 03:01 147613 /opt/gnome/bin/evolution-2.6 + # Size: 8 kB + # Rss: 8 kB + # Shared_Clean: 0 kB + # Shared_Dirty: 0 kB + # Private_Clean: 8 kB + # Private_Dirty: 0 kB + # Referenced: 4 kb -> Introduced in kernel 2.6.22 + + while num_lines > 0: + fields = lines[line_idx].split (" ", 5) + if len (fields) == 6: + (offsets, permissions, bin_permissions, device, inode, name) = fields + else: + (offsets, permissions, bin_permissions, device, inode) = fields + name = "" + + size = self.parse_smaps_size_line (lines[line_idx + 1]) + rss = self.parse_smaps_size_line (lines[line_idx + 2]) + shared_clean = self.parse_smaps_size_line (lines[line_idx + 3]) + shared_dirty = self.parse_smaps_size_line (lines[line_idx + 4]) + private_clean = self.parse_smaps_size_line (lines[line_idx + 5]) + private_dirty = self.parse_smaps_size_line (lines[line_idx + 6]) + if _smaps_has_references: + referenced = self.parse_smaps_size_line (lines[line_idx + 7]) + else: + referenced = None + name = name.strip () + + mapping = Mapping (size, rss, shared_clean, shared_dirty, \ + private_clean, private_dirty, referenced, permissions, name) + self.mappings.append (mapping) + + if _smaps_has_references: + num_lines -= 8 + line_idx += 8 + else: + num_lines -= 7 + line_idx += 7 + + if _smaps_has_references: + self._clear_reference(pid) + + def _clear_reference(self, pid): + os.system("echo 1 > /proc/%s/clear_refs" % pid) + + # Parses a line of the form "foo: 42 kB" and returns an integer for the "42" field + def parse_smaps_size_line (self, line): + # Rss: 8 kB + fields = line.split () + return int(fields[1]) + +class Mapping: + def __init__ (self, size, rss, shared_clean, shared_dirty, \ + private_clean, private_dirty, referenced, permissions, name): + self.size = size + self.rss = rss + self.shared_clean = shared_clean + self.shared_dirty = shared_dirty + self.private_clean = private_clean + self.private_dirty = private_dirty + self.referenced = referenced + self.permissions = permissions + self.name = name + +# Parse /proc/PID/maps file to get the clean memory usage by process, +# we avoid lines with backed-files +class ProcMaps: + + clean_size = 0 + + def __init__(self, pid): + mapfile = "/proc/%s/maps" % pid + + try: + infile = open(mapfile, "r") + except: + print "Error trying " + mapfile + return None + + sum = 0 + to_data_do = { + "[anon]": self.parse_size_line, + "[heap]": self.parse_size_line + } + + for line in infile: + arr = line.split() + + # Just parse writable mapped areas + if arr[1][1] != "w": + continue + + if len(arr) == 6: + # if we got a backed-file we skip this info + if os.path.isfile(arr[5]): + continue + else: + line_size = to_data_do.get(arr[5], self.skip)(line) + sum += line_size + else: + line_size = self.parse_size_line(line) + sum += line_size + + infile.close() + self.clean_size = sum + + def skip(self, line): + return 0 + + # Parse a maps line and return the mapped size + def parse_size_line(self, line): + start, end = line.split()[0].split('-') + size = int(end, 16) - int(start, 16) + return size From 628fe7b5da46b7618c391b91c9e130b30d4b6623 Mon Sep 17 00:00:00 2001 From: Tomeu Vizoso Date: Fri, 24 Aug 2007 20:15:30 +0200 Subject: [PATCH 5/8] Restore Icon's ability to load absolute file paths. --- NEWS | 1 + sugar/graphics/icon.py | 21 +++++++++++++++++---- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/NEWS b/NEWS index 922e24c6..a9b66bbe 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,4 @@ +* Restore Icon's ability to load absolute file paths. (tomeu) * #722 Show "charging" badge on battery. (danw) * #2010 Remember state when scrubbing. (marco) diff --git a/sugar/graphics/icon.py b/sugar/graphics/icon.py index e324d452..ff4e09a1 100644 --- a/sugar/graphics/icon.py +++ b/sugar/graphics/icon.py @@ -66,15 +66,25 @@ class Icon(gtk.Image): icon_theme = gtk.icon_theme_get_for_screen(self.get_screen()) icon_set = gtk.IconSet() - if icon_theme.has_icon(self._icon_name): + if icon_theme.has_icon(self._icon_name) or os.path.exists(self._icon_name): source = gtk.IconSource() - source.set_icon_name(self._icon_name) + + if os.path.exists(self._icon_name): + source.set_filename(self._icon_name) + else: + source.set_icon_name(self._icon_name) + icon_set.add_source(source) inactive_name = self._icon_name + '-inactive' - if icon_theme.has_icon(inactive_name): + if icon_theme.has_icon(inactive_name) or os.path.exists(inactive_name): source = gtk.IconSource() - source.set_icon_name(inactive_name) + + if os.path.exists(inactive_name): + source.set_filename(inactive_name) + else: + source.set_icon_name(inactive_name) + source.set_state(gtk.STATE_INSENSITIVE) icon_set.add_source(source) @@ -109,6 +119,9 @@ class Icon(gtk.Image): self.set_from_pixbuf(pixbuf) def _get_real_name(self, name): + if os.path.exists(name): + return name + info = self._theme.lookup_icon(name, self.props.icon_size, 0) if not info: raise ValueError("Icon '" + name + "' not found.") From 2cae66b0ea86235788220c9f7b05aa2ad4fdc58f Mon Sep 17 00:00:00 2001 From: "John (J5) Palmieri" Date: Fri, 24 Aug 2007 18:25:56 -0400 Subject: [PATCH 6/8] More follow_name_owner_changed optimiztion * reasoning changed from a FIXME to a NOTE to explain why we need to follow_name_owner_changed --- sugar/activity/registry.py | 9 +++++---- sugar/clipboard/clipboardservice.py | 9 ++++++++- sugar/presence/presenceservice.py | 9 ++++++++- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/sugar/activity/registry.py b/sugar/activity/registry.py index 2f6b567e..8fdae2f5 100644 --- a/sugar/activity/registry.py +++ b/sugar/activity/registry.py @@ -50,10 +50,11 @@ class ActivityRegistry(gobject.GObject): bus = dbus.SessionBus() - # FIXME: Is follow_name_owner_changes what we really want? - # It speeds up the start time by about 2 seconds - # but is really a side effect of starting a proxy - # in this state (i.e. we don't block in the constructor) + # NOTE: We need to follow_name_owner_changes here + # because we can not connect to a signal unless + # we follow the changes or we start the service + # before we connect. Starting the service here + # causes a major bottleneck during startup bus_object = bus.get_object(_ACTIVITY_REGISTRY_SERVICE_NAME, _ACTIVITY_REGISTRY_PATH, follow_name_owner_changes = True) diff --git a/sugar/clipboard/clipboardservice.py b/sugar/clipboard/clipboardservice.py index 0e357fef..d975330d 100644 --- a/sugar/clipboard/clipboardservice.py +++ b/sugar/clipboard/clipboardservice.py @@ -82,7 +82,14 @@ class ClipboardService(gobject.GObject): """Connect dbus signals to our GObject signal generating callbacks""" bus = dbus.SessionBus() if not self._connected: - proxy_obj = bus.get_object(DBUS_SERVICE, DBUS_PATH) + # NOTE: We need to follow_name_owner_changes here + # because we can not connect to a signal unless + # we follow the changes or we start the service + # before we connect. Starting the service here + # causes a major bottleneck during startup + proxy_obj = bus.get_object(DBUS_SERVICE, + DBUS_PATH, + follow_name_owner_changes=True) self._dbus_service = dbus.Interface(proxy_obj, DBUS_SERVICE) self._dbus_service.connect_to_signal('object_added', self._object_added_cb) diff --git a/sugar/presence/presenceservice.py b/sugar/presence/presenceservice.py index 4a92dd30..9e39a35b 100644 --- a/sugar/presence/presenceservice.py +++ b/sugar/presence/presenceservice.py @@ -108,8 +108,15 @@ class PresenceService(gobject.GObject): """ if not self._ps_: try: + # NOTE: We need to follow_name_owner_changes here + # because we can not connect to a signal unless + # we follow the changes or we start the service + # before we connect. Starting the service here + # causes a major bottleneck during startup ps = dbus.Interface( - self._bus.get_object(DBUS_SERVICE,DBUS_PATH), + self._bus.get_object(DBUS_SERVICE, + DBUS_PATH, + follow_name_owner_changes=True), DBUS_INTERFACE ) except dbus.exceptions.DBusException, err: From f1d588452eac011f2a9217e90c6ab7af61667bbe Mon Sep 17 00:00:00 2001 From: Marco Pesenti Gritti Date: Sat, 25 Aug 2007 13:15:28 +0200 Subject: [PATCH 7/8] Reorganize canvas items. --- shell/intro/colorpicker.py | 2 +- shell/intro/intro.py | 4 +- shell/view/BuddyIcon.py | 2 +- shell/view/clipboardicon.py | 2 +- shell/view/devices/battery.py | 11 +- shell/view/devices/deviceview.py | 2 +- shell/view/devices/network/wireless.py | 8 +- shell/view/frame/FriendsBox.py | 2 +- shell/view/home/FriendView.py | 2 +- shell/view/home/MeshBox.py | 7 +- shell/view/home/MyIcon.py | 2 +- shell/view/home/activitiesdonut.py | 2 +- shell/view/pulsingicon.py | 2 +- sugar/graphics/Makefile.am | 7 +- sugar/graphics/{canvasbutton.py => button.py} | 0 sugar/graphics/{canvasentry.py => entry.py} | 0 sugar/graphics/icon.py | 414 +++++++++++++++++- sugar/graphics/iconbutton.py | 2 +- sugar/graphics/objectchooser.py | 4 +- .../{canvasroundbox.py => roundbox.py} | 0 20 files changed, 443 insertions(+), 32 deletions(-) rename sugar/graphics/{canvasbutton.py => button.py} (100%) rename sugar/graphics/{canvasentry.py => entry.py} (100%) rename sugar/graphics/{canvasroundbox.py => roundbox.py} (100%) diff --git a/shell/intro/colorpicker.py b/shell/intro/colorpicker.py index f418807b..52b96871 100644 --- a/shell/intro/colorpicker.py +++ b/shell/intro/colorpicker.py @@ -16,7 +16,7 @@ import hippo -from sugar.graphics.canvasicon import CanvasIcon +from sugar.graphics.icon import CanvasIcon from sugar.graphics import style from sugar.graphics.xocolor import XoColor diff --git a/shell/intro/intro.py b/shell/intro/intro.py index ba633e39..54558130 100644 --- a/shell/intro/intro.py +++ b/shell/intro/intro.py @@ -26,8 +26,8 @@ import logging from sugar import env from sugar.graphics import style -from sugar.graphics.canvasbutton import CanvasButton -from sugar.graphics.canvasentry import CanvasEntry +from sugar.graphics.button import CanvasButton +from sugar.graphics.entry import CanvasEntry import colorpicker diff --git a/shell/view/BuddyIcon.py b/shell/view/BuddyIcon.py index 5079fe5b..1ab0ed74 100644 --- a/shell/view/BuddyIcon.py +++ b/shell/view/BuddyIcon.py @@ -14,7 +14,7 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA -from sugar.graphics.canvasicon import CanvasIcon +from sugar.graphics.icon import CanvasIcon from sugar.graphics.palette import Palette from view.BuddyMenu import BuddyMenu diff --git a/shell/view/clipboardicon.py b/shell/view/clipboardicon.py index 5f4c47e6..d7756f1d 100644 --- a/shell/view/clipboardicon.py +++ b/shell/view/clipboardicon.py @@ -20,7 +20,7 @@ from gettext import gettext as _ import gobject -from sugar.graphics.canvasicon import CanvasIcon +from sugar.graphics.icon import CanvasIcon from view.clipboardmenu import ClipboardMenu from sugar.graphics.xocolor import XoColor from sugar.graphics import style diff --git a/shell/view/devices/battery.py b/shell/view/devices/battery.py index 5aafc5cf..4ac3c17b 100644 --- a/shell/view/devices/battery.py +++ b/shell/view/devices/battery.py @@ -17,7 +17,8 @@ import gtk from gettext import gettext as _ -from sugar.graphics import canvasicon +from sugar.graphics.icon import CanvasIcon +from sugar.graphics.icon import get_icon_state from sugar.graphics import style from sugar.graphics.palette import Palette @@ -27,9 +28,9 @@ _STATUS_CHARGING = 0 _STATUS_DISCHARGING = 1 _STATUS_FULLY_CHARGED = 2 -class DeviceView(canvasicon.CanvasIcon): +class DeviceView(CanvasIcon): def __init__(self, model): - canvasicon.CanvasIcon.__init__(self, size=style.MEDIUM_ICON_SIZE) + CanvasIcon.__init__(self, size=style.MEDIUM_ICON_SIZE) self._model = model self._palette = BatteryPalette(_('My Battery life')) self.set_palette(self._palette) @@ -40,8 +41,8 @@ class DeviceView(canvasicon.CanvasIcon): self._update_info() def _update_info(self): - self.props.icon_name = canvasicon.get_icon_state( - _ICON_NAME, self._model.props.level) + name = get_icon_state(_ICON_NAME, self._model.props.level) + self.props.icon_name = name # Update palette if self._model.props.charging: diff --git a/shell/view/devices/deviceview.py b/shell/view/devices/deviceview.py index 5bff35a9..f58da021 100644 --- a/shell/view/devices/deviceview.py +++ b/shell/view/devices/deviceview.py @@ -14,7 +14,7 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA -from sugar.graphics.canvasicon import CanvasIcon +from sugar.graphics.icon import CanvasIcon def create(model): name = 'view.devices.' + model.get_type() diff --git a/shell/view/devices/network/wireless.py b/shell/view/devices/network/wireless.py index 9f0abd8b..5102f3bb 100644 --- a/shell/view/devices/network/wireless.py +++ b/shell/view/devices/network/wireless.py @@ -15,11 +15,12 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA -from sugar.graphics import canvasicon +from sugar.graphics.icon import get_icon_state +from sugar.graphics.icon import CanvasIcon from sugar.graphics import style from sugar.graphics import style + from model.devices.network import wireless -from sugar.graphics.canvasicon import CanvasIcon from model.devices import device _ICON_NAME = 'network-wireless' @@ -47,8 +48,7 @@ class DeviceView(CanvasIcon): self._update_state() def _update_icon(self): - icon_name = canvasicon.get_icon_state( - _ICON_NAME, self._model.props.strength) + icon_name = get_icon_state(_ICON_NAME, self._model.props.strength) if icon_name: self.props.icon_name = icon_name diff --git a/shell/view/frame/FriendsBox.py b/shell/view/frame/FriendsBox.py index 30f59635..fee5661c 100644 --- a/shell/view/frame/FriendsBox.py +++ b/shell/view/frame/FriendsBox.py @@ -17,7 +17,7 @@ import hippo from sugar.graphics.palette import Palette -from sugar.graphics.canvasicon import CanvasIcon +from sugar.graphics.icon import CanvasIcon from sugar.graphics import style from sugar.presence import presenceservice diff --git a/shell/view/home/FriendView.py b/shell/view/home/FriendView.py index ed058927..eb1eab55 100644 --- a/shell/view/home/FriendView.py +++ b/shell/view/home/FriendView.py @@ -17,7 +17,7 @@ import hippo import gobject -from sugar.graphics.canvasicon import CanvasIcon +from sugar.graphics.icon import CanvasIcon from sugar.graphics import style from sugar.presence import presenceservice from sugar import activity diff --git a/shell/view/home/MeshBox.py b/shell/view/home/MeshBox.py index ff6d2907..04ddf4ed 100644 --- a/shell/view/home/MeshBox.py +++ b/shell/view/home/MeshBox.py @@ -21,10 +21,10 @@ import gobject from gettext import gettext as _ from sugar.graphics.spreadlayout import SpreadLayout -from sugar.graphics.canvasicon import CanvasIcon +from sugar.graphics.icon import CanvasIcon from sugar.graphics import style from sugar.graphics import xocolor -from sugar.graphics import canvasicon +from sugar.graphics.icon import get_icon_state from sugar.graphics import style from sugar import profile @@ -86,8 +86,7 @@ class AccessPointView(PulsingIcon): self.set_tooltip(self._model.props.name) def _update_icon(self): - icon_name = canvasicon.get_icon_state( - _ICON_NAME, self._model.props.strength) + icon_name = get_icon_state(_ICON_NAME, self._model.props.strength) if icon_name: self.props.icon_name = icon_name diff --git a/shell/view/home/MyIcon.py b/shell/view/home/MyIcon.py index 894bd83e..b46c730d 100644 --- a/shell/view/home/MyIcon.py +++ b/shell/view/home/MyIcon.py @@ -14,7 +14,7 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA -from sugar.graphics.canvasicon import CanvasIcon +from sugar.graphics.icon import CanvasIcon from sugar import profile class MyIcon(CanvasIcon): diff --git a/shell/view/home/activitiesdonut.py b/shell/view/home/activitiesdonut.py index 8aa3933d..2a96a96b 100644 --- a/shell/view/home/activitiesdonut.py +++ b/shell/view/home/activitiesdonut.py @@ -24,7 +24,7 @@ import hippo import gobject import gtk -from sugar.graphics.canvasicon import CanvasIcon +from sugar.graphics.icon import CanvasIcon from sugar.graphics.menuitem import MenuItem from sugar.graphics.palette import Palette from sugar.graphics import style diff --git a/shell/view/pulsingicon.py b/shell/view/pulsingicon.py index 3820a6d8..9e7b3d90 100644 --- a/shell/view/pulsingicon.py +++ b/shell/view/pulsingicon.py @@ -16,7 +16,7 @@ import gobject -from sugar.graphics.canvasicon import CanvasIcon +from sugar.graphics.icon import CanvasIcon class PulsingIcon(CanvasIcon): __gproperties__ = { diff --git a/sugar/graphics/Makefile.am b/sugar/graphics/Makefile.am index 7fb63dcb..8911b68f 100644 --- a/sugar/graphics/Makefile.am +++ b/sugar/graphics/Makefile.am @@ -2,11 +2,9 @@ sugardir = $(pythondir)/sugar/graphics sugar_PYTHON = \ __init__.py \ animator.py \ - canvasbutton.py \ - canvasicon.py \ - canvasentry.py \ - canvasroundbox.py \ + button.py \ combobox.py \ + entry.py \ icon.py \ iconbutton.py \ iconentry.py \ @@ -17,6 +15,7 @@ sugar_PYTHON = \ palette.py \ palettegroup.py \ panel.py \ + roundbox.py \ spreadlayout.py \ style.py \ toggletoolbutton.py \ diff --git a/sugar/graphics/canvasbutton.py b/sugar/graphics/button.py similarity index 100% rename from sugar/graphics/canvasbutton.py rename to sugar/graphics/button.py diff --git a/sugar/graphics/canvasentry.py b/sugar/graphics/entry.py similarity index 100% rename from sugar/graphics/canvasentry.py rename to sugar/graphics/entry.py diff --git a/sugar/graphics/icon.py b/sugar/graphics/icon.py index ff4e09a1..13f60656 100644 --- a/sugar/graphics/icon.py +++ b/sugar/graphics/icon.py @@ -16,11 +16,20 @@ # Boston, MA 02111-1307, USA. import os +import re +import time +import logging + import gobject import gtk -import re +import hippo +import rsvg +import cairo from sugar.graphics.style import Color +from sugar.graphics.xocolor import XoColor +from sugar.graphics import style +from sugar.graphics.palette import Palette, CanvasInvoker class Icon(gtk.Image): __gtype_name__ = 'SugarIcon' @@ -152,3 +161,406 @@ class Icon(gtk.Image): return self._stroke_color else: return gtk.Image.do_get_property(self, pspec) + +_ICON_REQUEST_SIZE = 50 +_BADGE_SIZE = 0.45 + +class _IconCacheIcon: + def __init__(self, name, fill_color, stroke_color, now): + self.last_used = now + self.usage_count = 1 + self.badge_x = 1.0 - _BADGE_SIZE / 2 + self.badge_y = 1.0 - _BADGE_SIZE / 2 + + if name[0:6] == "theme:": + info = gtk.icon_theme_get_default().lookup_icon( + name[6:], _ICON_REQUEST_SIZE, 0) + if not info: + raise ValueError("Icon '" + name + "' not found.") + + fname = info.get_filename() + attach_points = info.get_attach_points() + if attach_points is not None: + self.badge_x = float(attach_points[0][0]) / _ICON_REQUEST_SIZE + self.badge_y = float(attach_points[0][1]) / _ICON_REQUEST_SIZE + del info + else: + fname = name + + self.handle = self._read_icon_data(fname, fill_color, stroke_color) + + def _read_icon_data(self, filename, fill_color, stroke_color): + icon_file = open(filename, 'r') + data = icon_file.read() + icon_file.close() + + if fill_color: + entity = '' % fill_color + data = re.sub('', entity, data) + + if stroke_color: + entity = '' % stroke_color + data = re.sub('', entity, data) + + self.data_size = len(data) + return rsvg.Handle(data=data) + +class _IconCache: + _CACHE_MAX = 50000 # in bytes + + def __init__(self): + self._icons = {} + self._cache_size = 0 + + def _cache_cleanup(self, key, now): + while self._cache_size > self._CACHE_MAX: + evict_key = None + oldest_key = None + oldest_time = now + for icon_key, icon in self._icons.items(): + # Don't evict the icon we are about to use if it's in the cache + if icon_key == key: + continue + + # evict large icons first + if icon.data_size > self._CACHE_MAX: + evict_key = icon_key + break + # evict older icons next; those used over 2 minutes ago + if icon.last_used < now - 120: + evict_key = icon_key + break + # otherwise, evict the oldest + if oldest_time > icon.last_used: + oldest_time = icon.last_used + oldest_key = icon_key + + # If there's nothing specific to evict, try evicting + # the oldest thing + if not evict_key: + if not oldest_key: + break + evict_key = oldest_key + + self._cache_size -= self._icons[evict_key].data_size + del self._icons[evict_key] + + def get_icon(self, name, fill_color, stroke_color): + if not name: + return None + + if fill_color or stroke_color: + key = (name, fill_color, stroke_color) + else: + key = name + + # If we're over the cache limit, evict something from the cache + now = time.time() + self._cache_cleanup(key, now) + + if self._icons.has_key(key): + icon = self._icons[key] + icon.usage_count += 1 + icon.last_used = now + else: + icon = _IconCacheIcon(name, fill_color, stroke_color, now) + self._icons[key] = icon + self._cache_size += icon.data_size + return icon + + +class CanvasIcon(hippo.CanvasBox, hippo.CanvasItem): + __gtype_name__ = 'CanvasIcon' + + __gproperties__ = { + 'icon-name' : (str, None, None, None, + gobject.PARAM_READWRITE), + 'xo-color' : (object, None, None, + gobject.PARAM_WRITABLE), + 'fill-color' : (object, None, None, + gobject.PARAM_READWRITE), + 'stroke-color' : (object, None, None, + gobject.PARAM_READWRITE), + 'size' : (int, None, None, 0, 1024, 0, + gobject.PARAM_READWRITE), + 'scale' : (int, None, None, 0, 1024, 0, + gobject.PARAM_READWRITE), + 'cache' : (bool, None, None, False, + gobject.PARAM_READWRITE), + 'active' : (bool, None, None, True, + gobject.PARAM_READWRITE), + 'badge-name' : (str, None, None, None, + gobject.PARAM_READWRITE) + } + + _cache = _IconCache() + + def __init__(self, **kwargs): + self._buffers = {} + self._cur_buffer = None + self._size = 0 + self._scale = 0 + self._fill_color = None + self._stroke_color = None + self._icon_name = None + self._cache = False + self._icon = None + self._active = True + self._palette = None + self._badge_name = None + self._badge_icon = None + + hippo.CanvasBox.__init__(self, **kwargs) + + self.connect_after('motion-notify-event', self._motion_notify_event_cb) + + def _clear_buffers(self): + icon_key = self._get_current_buffer_key(self._icon_name) + badge_key = None + if self._badge_name: + badge_key = self._get_current_buffer_key(self._badge_name) + for key in self._buffers.keys(): + if key != icon_key: + if not badge_key or (key != badge_key): + del self._buffers[key] + self._buffers = {} + + def do_set_property(self, pspec, value): + if pspec.name == 'icon-name': + if self._icon_name != value and not self._cache: + self._clear_buffers() + self._icon_name = value + self._icon = None + self.emit_paint_needed(0, 0, -1, -1) + elif pspec.name == 'xo-color': + self.props.fill_color = style.Color(value.get_fill_color()) + self.props.stroke_color = style.Color(value.get_stroke_color()) + elif pspec.name == 'fill-color': + if self._fill_color != value: + if not self._cache: + self._clear_buffers() + self._fill_color = value + self._icon = None + self._badge_icon = None + self.emit_paint_needed(0, 0, -1, -1) + elif pspec.name == 'stroke-color': + if self._stroke_color != value: + if not self._cache: + self._clear_buffers() + self._stroke_color = value + self._icon = None + self._badge_icon = None + self.emit_paint_needed(0, 0, -1, -1) + elif pspec.name == 'size': + if self._size != value and not self._cache: + self._clear_buffers() + self._size = value + self.emit_request_changed() + elif pspec.name == 'scale': + if self._scale != value and not self._cache: + self._clear_buffers() + self._scale = value + self.emit_request_changed() + elif pspec.name == 'cache': + self._cache = value + elif pspec.name == 'active': + if self._active != value: + if not self._cache: + self._clear_buffers() + self._active = value + self._icon = None + self.emit_paint_needed(0, 0, -1, -1) + elif pspec.name == 'badge-name': + if self._badge_name != value and not self._cache: + self._clear_buffers() + self._badge_name = value + self._badge_icon = None + self.emit_paint_needed(0, 0, -1, -1) + + def _choose_colors(self): + fill_color = None + stroke_color = None + if self._active: + if self._fill_color: + fill_color = self._fill_color.get_svg() + if self._stroke_color: + stroke_color = self._stroke_color.get_svg() + else: + stroke_color = color.ICON_STROKE_INACTIVE.get_svg() + if self._fill_color: + fill_color = self._fill_color.get_svg() + return [fill_color, stroke_color] + + def _get_icon_from_cache(self, name, icon): + if not icon: + cache = CanvasIcon._cache + + [fill_color, stroke_color] = self._choose_colors() + + icon = cache.get_icon(name, fill_color, stroke_color) + return icon + + def _get_icon(self): + self._icon = self._get_icon_from_cache(self._icon_name, self._icon) + return self._icon + + def _get_badge_icon(self): + self._badge_icon = self._get_icon_from_cache(self._badge_name, + self._badge_icon) + return self._badge_icon + + def _get_current_buffer_key(self, name): + [fill_color, stroke_color] = self._choose_colors() + return (name, fill_color, stroke_color, self._size) + + def do_get_property(self, pspec): + if pspec.name == 'size': + return self._size + elif pspec.name == 'icon-name': + return self._icon_name + elif pspec.name == 'fill-color': + return self._fill_color + elif pspec.name == 'stroke-color': + return self._stroke_color + elif pspec.name == 'cache': + return self._cache + elif pspec.name == 'active': + return self._active + elif pspec.name == 'badge-name': + return self._badge_name + elif pspec.name == 'scale': + return self._scale + + def _get_icon_size(self, icon): + if icon: + dimensions = icon.handle.get_dimension_data() + return int(dimensions[0]), int(dimensions[1]) + else: + return [0, 0] + + def _get_size(self, icon): + width, height = self._get_icon_size(icon) + if self._scale != 0: + width = int(width * self._scale) + height = int(height * self._scale) + elif self._size != 0: + width = height = self._size + + return [width, height] + + def _get_buffer(self, cr, name, icon, scale_factor=None): + """Return a cached cairo surface for the SVG icon, or if none exists, + create a new cairo surface with the right size.""" + buf = None + + key = self._get_current_buffer_key(name) + if self._buffers.has_key(key): + buf = self._buffers[key] + else: + [icon_w, icon_h] = self._get_icon_size(icon) + [target_w, target_h] = self._get_size(icon) + + if scale_factor: + target_w = int(target_w * scale_factor) + target_h = int(target_h * scale_factor) + + target = cr.get_target() + buf = target.create_similar(cairo.CONTENT_COLOR_ALPHA, + target_w, target_h) + ctx = cairo.Context(buf) + ctx.scale(float(target_w) / float(icon_w), + float(target_h) / float(icon_h)) + icon.handle.render_cairo(ctx) + + del ctx + self._buffers[key] = buf + + return buf + + def do_paint_below_children(self, cr, damaged_box): + icon = self._get_icon() + if icon is None: + return + + icon_buf = self._get_buffer(cr, self._icon_name, icon) + [width, height] = self.get_allocation() + icon_x = (width - icon_buf.get_width()) / 2 + icon_y = (height - icon_buf.get_height()) / 2 + + cr.set_source_surface(icon_buf, icon_x, icon_y) + cr.paint() + + if self._badge_name: + badge_icon = self._get_badge_icon() + if badge_icon: + badge_buf = self._get_buffer(cr, self._badge_name, badge_icon, _BADGE_SIZE) + badge_x = (icon_x + icon.badge_x * icon_buf.get_width() - + badge_buf.get_width() / 2) + badge_y = (icon_y + icon.badge_y * icon_buf.get_height() - + badge_buf.get_height() / 2) + cr.set_source_surface(badge_buf, badge_x, badge_y) + cr.paint() + + def do_get_content_width_request(self): + icon = self._get_icon() + [width, height] = self._get_size(icon) + if self._badge_name is not None: + # If the badge goes outside the bounding box, add space + # on *both* sides (to keep the main icon centered) + if icon.badge_x < 0.0: + width = int(width * 2 * (1.0 - icon.badge_x)) + elif icon.badge_x + _BADGE_SIZE > 1.0: + width = int(width * 2 * (icon.badge_x + _BADGE_SIZE - 1.0)) + return (width, width) + + def do_get_content_height_request(self, for_width): + icon = self._get_icon() + [width, height] = self._get_size(icon) + if self._badge_name is not None: + if icon.badge_y < 0.0: + height = int(height * 2 * (1.0 - icon.badge_y)) + elif icon.badge_y + _BADGE_SIZE > 1.0: + height = int(height * 2 * (icon.badge_y + _BADGE_SIZE - 1.0)) + return (height, height) + + def do_button_press_event(self, event): + self.emit_activated() + return True + + def _motion_notify_event_cb(self, button, event): + if event.detail == hippo.MOTION_DETAIL_ENTER: + self.prelight(True) + elif event.detail == hippo.MOTION_DETAIL_LEAVE: + self.prelight(False) + return False + + def prelight(self, enter): + """ + Override this method for adding prelighting behavior. + """ + pass + + def get_palette(self): + return self._palette + + def set_palette(self, palette): + self._palette = palette + if not self._palette.props.invoker: + self._palette.props.invoker = CanvasInvoker(self) + + def set_tooltip(self, text): + self.set_palette(Palette(text)) + + palette = property(get_palette, set_palette) + +def get_icon_state(base_name, perc): + step = 5 + strength = round(perc / step) * step + icon_theme = gtk.icon_theme_get_default() + + while strength <= 100: + icon_name = '%s-%03d' % (base_name, strength) + if icon_theme.has_icon(icon_name): + return 'theme:' + icon_name + + strength = strength + step diff --git a/sugar/graphics/iconbutton.py b/sugar/graphics/iconbutton.py index ba9fad8d..6489b344 100644 --- a/sugar/graphics/iconbutton.py +++ b/sugar/graphics/iconbutton.py @@ -24,7 +24,7 @@ import sys import gobject import hippo -from sugar.graphics.canvasicon import CanvasIcon +from sugar.graphics.icon import CanvasIcon from sugar.graphics import style class IconButton(CanvasIcon, hippo.CanvasItem): diff --git a/sugar/graphics/objectchooser.py b/sugar/graphics/objectchooser.py index f9290630..56a5a31b 100644 --- a/sugar/graphics/objectchooser.py +++ b/sugar/graphics/objectchooser.py @@ -24,9 +24,9 @@ import hippo from sugar.activity.bundle import Bundle from sugar.date import Date from sugar.graphics import style -from sugar.graphics.canvasicon import CanvasIcon +from sugar.graphics.icon import CanvasIcon from sugar.graphics.xocolor import XoColor -from sugar.graphics.canvasroundbox import CanvasRoundBox +from sugar.graphics.roundbox import CanvasRoundBox from sugar.datastore import datastore from sugar import activity from sugar.objects import objecttype diff --git a/sugar/graphics/canvasroundbox.py b/sugar/graphics/roundbox.py similarity index 100% rename from sugar/graphics/canvasroundbox.py rename to sugar/graphics/roundbox.py From 1cb214bc967f16f4fd2f1bdc5dec65b444e01ef6 Mon Sep 17 00:00:00 2001 From: Marco Pesenti Gritti Date: Sat, 25 Aug 2007 13:18:30 +0200 Subject: [PATCH 8/8] Remove canvasicon module. --- sugar/graphics/canvasicon.py | 433 ----------------------------------- 1 file changed, 433 deletions(-) delete mode 100644 sugar/graphics/canvasicon.py diff --git a/sugar/graphics/canvasicon.py b/sugar/graphics/canvasicon.py deleted file mode 100644 index 36325514..00000000 --- a/sugar/graphics/canvasicon.py +++ /dev/null @@ -1,433 +0,0 @@ -# Copyright (C) 2006-2007 Red Hat, Inc. -# -# This library is free software; you can redistribute it and/or -# modify it under the terms of the GNU Lesser General Public -# License as published by the Free Software Foundation; either -# version 2 of the License, or (at your option) any later version. -# -# This library 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 -# Lesser General Public License for more details. -# -# You should have received a copy of the GNU Lesser General Public -# License along with this library; if not, write to the -# Free Software Foundation, Inc., 59 Temple Place - Suite 330, -# Boston, MA 02111-1307, USA. - -import logging -import re - -import gobject -import gtk -import hippo -import rsvg -import cairo -import time - -from sugar.graphics.xocolor import XoColor -from sugar.graphics import style -from sugar.graphics.palette import Palette, CanvasInvoker - -_ICON_REQUEST_SIZE = 50 -_BADGE_SIZE = 0.45 - -class _IconCacheIcon: - def __init__(self, name, fill_color, stroke_color, now): - self.last_used = now - self.usage_count = 1 - self.badge_x = 1.0 - _BADGE_SIZE / 2 - self.badge_y = 1.0 - _BADGE_SIZE / 2 - - if name[0:6] == "theme:": - info = gtk.icon_theme_get_default().lookup_icon( - name[6:], _ICON_REQUEST_SIZE, 0) - if not info: - raise ValueError("Icon '" + name + "' not found.") - - fname = info.get_filename() - attach_points = info.get_attach_points() - if attach_points is not None: - self.badge_x = float(attach_points[0][0]) / _ICON_REQUEST_SIZE - self.badge_y = float(attach_points[0][1]) / _ICON_REQUEST_SIZE - del info - else: - fname = name - - self.handle = self._read_icon_data(fname, fill_color, stroke_color) - - def _read_icon_data(self, filename, fill_color, stroke_color): - icon_file = open(filename, 'r') - data = icon_file.read() - icon_file.close() - - if fill_color: - entity = '' % fill_color - data = re.sub('', entity, data) - - if stroke_color: - entity = '' % stroke_color - data = re.sub('', entity, data) - - self.data_size = len(data) - return rsvg.Handle(data=data) - -class _IconCache: - _CACHE_MAX = 50000 # in bytes - - def __init__(self): - self._icons = {} - self._cache_size = 0 - - def _cache_cleanup(self, key, now): - while self._cache_size > self._CACHE_MAX: - evict_key = None - oldest_key = None - oldest_time = now - for icon_key, icon in self._icons.items(): - # Don't evict the icon we are about to use if it's in the cache - if icon_key == key: - continue - - # evict large icons first - if icon.data_size > self._CACHE_MAX: - evict_key = icon_key - break - # evict older icons next; those used over 2 minutes ago - if icon.last_used < now - 120: - evict_key = icon_key - break - # otherwise, evict the oldest - if oldest_time > icon.last_used: - oldest_time = icon.last_used - oldest_key = icon_key - - # If there's nothing specific to evict, try evicting - # the oldest thing - if not evict_key: - if not oldest_key: - break - evict_key = oldest_key - - self._cache_size -= self._icons[evict_key].data_size - del self._icons[evict_key] - - def get_icon(self, name, fill_color, stroke_color): - if not name: - return None - - if fill_color or stroke_color: - key = (name, fill_color, stroke_color) - else: - key = name - - # If we're over the cache limit, evict something from the cache - now = time.time() - self._cache_cleanup(key, now) - - if self._icons.has_key(key): - icon = self._icons[key] - icon.usage_count += 1 - icon.last_used = now - else: - icon = _IconCacheIcon(name, fill_color, stroke_color, now) - self._icons[key] = icon - self._cache_size += icon.data_size - return icon - - -class CanvasIcon(hippo.CanvasBox, hippo.CanvasItem): - __gtype_name__ = 'CanvasIcon' - - __gproperties__ = { - 'icon-name' : (str, None, None, None, - gobject.PARAM_READWRITE), - 'xo-color' : (object, None, None, - gobject.PARAM_WRITABLE), - 'fill-color' : (object, None, None, - gobject.PARAM_READWRITE), - 'stroke-color' : (object, None, None, - gobject.PARAM_READWRITE), - 'size' : (int, None, None, 0, 1024, 0, - gobject.PARAM_READWRITE), - 'scale' : (int, None, None, 0, 1024, 0, - gobject.PARAM_READWRITE), - 'cache' : (bool, None, None, False, - gobject.PARAM_READWRITE), - 'active' : (bool, None, None, True, - gobject.PARAM_READWRITE), - 'badge-name' : (str, None, None, None, - gobject.PARAM_READWRITE) - } - - _cache = _IconCache() - - def __init__(self, **kwargs): - self._buffers = {} - self._cur_buffer = None - self._size = 0 - self._scale = 0 - self._fill_color = None - self._stroke_color = None - self._icon_name = None - self._cache = False - self._icon = None - self._active = True - self._palette = None - self._badge_name = None - self._badge_icon = None - - hippo.CanvasBox.__init__(self, **kwargs) - - self.connect_after('motion-notify-event', self._motion_notify_event_cb) - - def _clear_buffers(self): - icon_key = self._get_current_buffer_key(self._icon_name) - badge_key = None - if self._badge_name: - badge_key = self._get_current_buffer_key(self._badge_name) - for key in self._buffers.keys(): - if key != icon_key: - if not badge_key or (key != badge_key): - del self._buffers[key] - self._buffers = {} - - def do_set_property(self, pspec, value): - if pspec.name == 'icon-name': - if self._icon_name != value and not self._cache: - self._clear_buffers() - self._icon_name = value - self._icon = None - self.emit_paint_needed(0, 0, -1, -1) - elif pspec.name == 'xo-color': - self.props.fill_color = style.Color(value.get_fill_color()) - self.props.stroke_color = style.Color(value.get_stroke_color()) - elif pspec.name == 'fill-color': - if self._fill_color != value: - if not self._cache: - self._clear_buffers() - self._fill_color = value - self._icon = None - self._badge_icon = None - self.emit_paint_needed(0, 0, -1, -1) - elif pspec.name == 'stroke-color': - if self._stroke_color != value: - if not self._cache: - self._clear_buffers() - self._stroke_color = value - self._icon = None - self._badge_icon = None - self.emit_paint_needed(0, 0, -1, -1) - elif pspec.name == 'size': - if self._size != value and not self._cache: - self._clear_buffers() - self._size = value - self.emit_request_changed() - elif pspec.name == 'scale': - if self._scale != value and not self._cache: - self._clear_buffers() - self._scale = value - self.emit_request_changed() - elif pspec.name == 'cache': - self._cache = value - elif pspec.name == 'active': - if self._active != value: - if not self._cache: - self._clear_buffers() - self._active = value - self._icon = None - self.emit_paint_needed(0, 0, -1, -1) - elif pspec.name == 'badge-name': - if self._badge_name != value and not self._cache: - self._clear_buffers() - self._badge_name = value - self._badge_icon = None - self.emit_paint_needed(0, 0, -1, -1) - - def _choose_colors(self): - fill_color = None - stroke_color = None - if self._active: - if self._fill_color: - fill_color = self._fill_color.get_svg() - if self._stroke_color: - stroke_color = self._stroke_color.get_svg() - else: - stroke_color = color.ICON_STROKE_INACTIVE.get_svg() - if self._fill_color: - fill_color = self._fill_color.get_svg() - return [fill_color, stroke_color] - - def _get_icon_from_cache(self, name, icon): - if not icon: - cache = CanvasIcon._cache - - [fill_color, stroke_color] = self._choose_colors() - - icon = cache.get_icon(name, fill_color, stroke_color) - return icon - - def _get_icon(self): - self._icon = self._get_icon_from_cache(self._icon_name, self._icon) - return self._icon - - def _get_badge_icon(self): - self._badge_icon = self._get_icon_from_cache(self._badge_name, - self._badge_icon) - return self._badge_icon - - def _get_current_buffer_key(self, name): - [fill_color, stroke_color] = self._choose_colors() - return (name, fill_color, stroke_color, self._size) - - def do_get_property(self, pspec): - if pspec.name == 'size': - return self._size - elif pspec.name == 'icon-name': - return self._icon_name - elif pspec.name == 'fill-color': - return self._fill_color - elif pspec.name == 'stroke-color': - return self._stroke_color - elif pspec.name == 'cache': - return self._cache - elif pspec.name == 'active': - return self._active - elif pspec.name == 'badge-name': - return self._badge_name - elif pspec.name == 'scale': - return self._scale - - def _get_icon_size(self, icon): - if icon: - dimensions = icon.handle.get_dimension_data() - return int(dimensions[0]), int(dimensions[1]) - else: - return [0, 0] - - def _get_size(self, icon): - width, height = self._get_icon_size(icon) - if self._scale != 0: - width = int(width * self._scale) - height = int(height * self._scale) - elif self._size != 0: - width = height = self._size - - return [width, height] - - def _get_buffer(self, cr, name, icon, scale_factor=None): - """Return a cached cairo surface for the SVG icon, or if none exists, - create a new cairo surface with the right size.""" - buf = None - - key = self._get_current_buffer_key(name) - if self._buffers.has_key(key): - buf = self._buffers[key] - else: - [icon_w, icon_h] = self._get_icon_size(icon) - [target_w, target_h] = self._get_size(icon) - - if scale_factor: - target_w = int(target_w * scale_factor) - target_h = int(target_h * scale_factor) - - target = cr.get_target() - buf = target.create_similar(cairo.CONTENT_COLOR_ALPHA, - target_w, target_h) - ctx = cairo.Context(buf) - ctx.scale(float(target_w) / float(icon_w), - float(target_h) / float(icon_h)) - icon.handle.render_cairo(ctx) - - del ctx - self._buffers[key] = buf - - return buf - - def do_paint_below_children(self, cr, damaged_box): - icon = self._get_icon() - if icon is None: - return - - icon_buf = self._get_buffer(cr, self._icon_name, icon) - [width, height] = self.get_allocation() - icon_x = (width - icon_buf.get_width()) / 2 - icon_y = (height - icon_buf.get_height()) / 2 - - cr.set_source_surface(icon_buf, icon_x, icon_y) - cr.paint() - - if self._badge_name: - badge_icon = self._get_badge_icon() - if badge_icon: - badge_buf = self._get_buffer(cr, self._badge_name, badge_icon, _BADGE_SIZE) - badge_x = (icon_x + icon.badge_x * icon_buf.get_width() - - badge_buf.get_width() / 2) - badge_y = (icon_y + icon.badge_y * icon_buf.get_height() - - badge_buf.get_height() / 2) - cr.set_source_surface(badge_buf, badge_x, badge_y) - cr.paint() - - def do_get_content_width_request(self): - icon = self._get_icon() - [width, height] = self._get_size(icon) - if self._badge_name is not None: - # If the badge goes outside the bounding box, add space - # on *both* sides (to keep the main icon centered) - if icon.badge_x < 0.0: - width = int(width * 2 * (1.0 - icon.badge_x)) - elif icon.badge_x + _BADGE_SIZE > 1.0: - width = int(width * 2 * (icon.badge_x + _BADGE_SIZE - 1.0)) - return (width, width) - - def do_get_content_height_request(self, for_width): - icon = self._get_icon() - [width, height] = self._get_size(icon) - if self._badge_name is not None: - if icon.badge_y < 0.0: - height = int(height * 2 * (1.0 - icon.badge_y)) - elif icon.badge_y + _BADGE_SIZE > 1.0: - height = int(height * 2 * (icon.badge_y + _BADGE_SIZE - 1.0)) - return (height, height) - - def do_button_press_event(self, event): - self.emit_activated() - return True - - def _motion_notify_event_cb(self, button, event): - if event.detail == hippo.MOTION_DETAIL_ENTER: - self.prelight(True) - elif event.detail == hippo.MOTION_DETAIL_LEAVE: - self.prelight(False) - return False - - def prelight(self, enter): - """ - Override this method for adding prelighting behavior. - """ - pass - - def get_palette(self): - return self._palette - - def set_palette(self, palette): - self._palette = palette - if not self._palette.props.invoker: - self._palette.props.invoker = CanvasInvoker(self) - - def set_tooltip(self, text): - self.set_palette(Palette(text)) - - palette = property(get_palette, set_palette) - -def get_icon_state(base_name, perc): - step = 5 - strength = round(perc / step) * step - icon_theme = gtk.icon_theme_get_default() - - while strength <= 100: - icon_name = '%s-%03d' % (base_name, strength) - if icon_theme.has_icon(icon_name): - return 'theme:' + icon_name - - strength = strength + step