Ticket #2143: 0001-spiral-from-outside-to-inside.patch

File 0001-spiral-from-outside-to-inside.patch, 5.4 KB (added by walter, 14 years ago)

this version spirals from the outside to the inside

  • src/jarabe/desktop/favoriteslayout.py

    From 612c1616455c3cf89a6e1214b05d8d9823563647 Mon Sep 17 00:00:00 2001
    From: Walter Bender <walter@sugarlabs.org>
    Date: Sun, 8 Aug 2010 08:03:21 -0400
    Subject: [PATCH] spiral from outside to inside
    
    ---
     src/jarabe/desktop/favoriteslayout.py |   83 +++++++++++++++++++++++++--------
     1 files changed, 64 insertions(+), 19 deletions(-)
    
    diff --git a/src/jarabe/desktop/favoriteslayout.py b/src/jarabe/desktop/favoriteslayout.py
    index 85e1b59..33ba031 100644
    a b  
    11# Copyright (C) 2008 One Laptop Per Child
     2# Copyright (C) 2010 Sugar Labs
    23#
    34# This program is free software; you can redistribute it and/or modify
    45# it under the terms of the GNU General Public License as published by
    _logger = logging.getLogger('FavoritesLayout') 
    3233
    3334_CELL_SIZE = 4
    3435_BASE_SCALE = 1000
     36_INTERMEDIATE = (style.STANDARD_ICON_SIZE + style.SMALL_ICON_SIZE) / 2
    3537
    3638class FavoritesLayout(gobject.GObject, hippo.CanvasLayout):
    3739    """Base class of the different layout types."""
    class RingLayout(FavoritesLayout): 
    201203    def __init__(self):
    202204        FavoritesLayout.__init__(self)
    203205        self._locked_children = {}
     206        self._spiral = False
     207        self._radius = _MINIMUM_RADIUS
     208        self._orientation = math.pi
     209        self._icon_size = style.STANDARD_ICON_SIZE
     210        self._count = -1
    204211
    205212    def append(self, icon, locked=False):
    206213        FavoritesLayout.append(self, icon, locked)
    class RingLayout(FavoritesLayout): 
    221228            self._locked_children[child] = (x, y)
    222229
    223230    def _calculate_radius_and_icon_size(self, children_count):
    224         # what's the radius required without downscaling?
     231        """ determine if we are drawing a circle or a spiral """
    225232        distance = style.STANDARD_ICON_SIZE + style.DEFAULT_SPACING
    226         icon_size = style.STANDARD_ICON_SIZE
    227         # circumference is 2*pi*r; we want this to be at least
    228         # 'children_count * distance'
     233
    229234        radius = children_count * distance / (2 * math.pi)
    230         # limit computed radius to reasonable bounds.
    231         radius = max(radius, _MINIMUM_RADIUS)
    232         radius = min(radius, _MAXIMUM_RADIUS)
    233         # recompute icon size from limited radius
    234         if children_count > 0:
    235             icon_size = (2 * math.pi * radius / children_count) \
    236                         - style.DEFAULT_SPACING
    237         # limit adjusted icon size.
    238         icon_size = max(icon_size, style.SMALL_ICON_SIZE)
    239         icon_size = min(icon_size, style.MEDIUM_ICON_SIZE)
    240         return radius, icon_size
     235        if radius < _MAXIMUM_RADIUS:
     236            self._spiral = False
     237            self._icon_size = style.STANDARD_ICON_SIZE
     238        else:
     239            self._spiral = True
     240            radius = _MAXIMUM_RADIUS
     241
     242        # If there are fewer children, try increasing icon_size.
     243        if self._count > children_count:
     244            if self._icon_size == style.SMALL_ICON_SIZE:
     245                self._icon_size = _INTERMEDIATE
     246            elif self._icon_size == _INTERMEDIATE:
     247                self._icon_size = style.STANDARD_ICON_SIZE
     248        self._count = children_count
     249
     250        if self._radius < _MINIMUM_RADIUS:
     251            if self._icon_size == style.STANDARD_ICON_SIZE:
     252                self._icon_size = _INTERMEDIATE
     253            elif self._icon_size == _INTERMEDIATE:
     254                self._icon_size = style.SMALL_ICON_SIZE
     255
     256        return radius, self._icon_size
     257
     258    def _calculate_xy(self, icon_size, width, height):
     259        """ Convert r, o to x, y """
     260        x = -math.sin(self._orientation) * self._radius
     261        y = math.cos(self._orientation) * self._radius
     262        self._calculate_new_radius_orientation(icon_size +\
     263                                                   style.DEFAULT_SPACING)
     264
     265        x = int(x) + (width - icon_size) / 2
     266        y = int(y) + (height - icon_size - (style.GRID_CELL_SIZE / 2) ) / 2
     267        return x, y
     268
     269    def _calculate_new_radius_orientation(self, icon_size):
     270        """ Based upon current radius, calculate new increments """
     271        circumference = self._radius * 2 * math.pi
     272        n = circumference / icon_size
     273        self._orientation += 2 * math.pi / n
     274        self._radius -= float(icon_size) / n
    241275
    242276    def _calculate_position(self, radius, icon_size, index, children_count,
    243277                            sin=math.sin, cos=math.cos):
     278        """ Try fitting a circle or a spiral """
     279
    244280        width, height = self.box.get_allocation()
    245         angle = index * (2 * math.pi / children_count) - math.pi / 2
    246         x = radius * cos(angle) + (width - icon_size) / 2
    247         y = radius * sin(angle) + (height - icon_size -
    248                                    (style.GRID_CELL_SIZE/2) ) / 2
     281        if not self._spiral:
     282            angle = index * (2 * math.pi / children_count) - math.pi / 2
     283            x = radius * cos(angle) + (width - icon_size) / 2
     284            y = radius * sin(angle) + (height - icon_size -
     285                                       (style.GRID_CELL_SIZE/2) ) / 2
     286        else:
     287            min_width_, box_width = self.box.get_width_request()
     288            min_height_, box_height = self.box.get_height_request(box_width)
     289            if index == 0:
     290                self._radius = _MAXIMUM_RADIUS
     291                self._orientation = math.pi
     292            x, y = self._calculate_xy(icon_size, width, height)
     293               
    249294        return x, y
    250295
    251296    def _get_children_in_ring(self):