Refactor the snowflake layout to use the new hippo layout managers.

Cleanup the logic.
Better test.
Make it internal.
This commit is contained in:
Marco Pesenti Gritti 2007-07-05 11:22:09 +02:00
parent 2164f22197
commit 91654729bf
6 changed files with 138 additions and 140 deletions

View File

@ -8,4 +8,5 @@ sugar_PYTHON = \
HomeWindow.py \ HomeWindow.py \
MeshBox.py \ MeshBox.py \
MyIcon.py \ MyIcon.py \
snowflakelayout.py \
transitionbox.py transitionbox.py

View File

@ -21,19 +21,20 @@ import gobject
from gettext import gettext as _ from gettext import gettext as _
from sugar.graphics.spreadbox import SpreadBox from sugar.graphics.spreadbox import SpreadBox
from sugar.graphics.snowflakebox import SnowflakeBox
from sugar.graphics.canvasicon import CanvasIcon from sugar.graphics.canvasicon import CanvasIcon
from sugar.graphics import color from sugar.graphics import color
from sugar.graphics import xocolor from sugar.graphics import xocolor
from sugar.graphics import canvasicon from sugar.graphics import canvasicon
from sugar.graphics import units from sugar.graphics import units
from sugar import profile
from model import accesspointmodel from model import accesspointmodel
from model.devices.network import mesh from model.devices.network import mesh
from hardware import hardwaremanager from hardware import hardwaremanager
from hardware import nmclient from hardware import nmclient
from view.BuddyIcon import BuddyIcon from view.BuddyIcon import BuddyIcon
from view.pulsingicon import PulsingIcon from view.pulsingicon import PulsingIcon
from sugar import profile from view.home.snowflakelayout import SnowflakeLayout
_ICON_NAME = 'device-network-wireless' _ICON_NAME = 'device-network-wireless'
@ -165,20 +166,22 @@ class MeshDeviceView(PulsingIcon):
color.HTMLColor(self._device_fill) ] color.HTMLColor(self._device_fill) ]
] ]
class ActivityView(SnowflakeBox): class ActivityView(hippo.CanvasBox):
def __init__(self, shell, model): def __init__(self, shell, model):
SnowflakeBox.__init__(self) hippo.CanvasBox.__init__(self)
self._shell = shell self._shell = shell
self._model = model self._model = model
self._icons = {} self._icons = {}
self._layout = SnowflakeLayout()
self.set_layout(self._layout)
self._icon = CanvasIcon(icon_name=model.get_icon_name(), self._icon = CanvasIcon(icon_name=model.get_icon_name(),
xo_color=model.get_color(), box_width=80) xo_color=model.get_color(), box_width=80)
self._icon.connect('activated', self._clicked_cb) self._icon.connect('activated', self._clicked_cb)
self._icon.set_tooltip(self._model.get_title()) self._icon.set_tooltip(self._model.get_title())
self.append(self._icon, hippo.PACK_FIXED) self._layout.add_center(self._icon)
self.set_root(self._icon)
def _update_name(self): def _update_name(self):
self.palette.set_primary_text(self._model.get_title()) self.palette.set_primary_text(self._model.get_title())
@ -188,7 +191,7 @@ class ActivityView(SnowflakeBox):
def add_buddy_icon(self, key, icon): def add_buddy_icon(self, key, icon):
self._icons[key] = icon self._icons[key] = icon
self.append(icon, hippo.PACK_FIXED) self._layout.add(icon)
def remove_buddy_icon(self, key): def remove_buddy_icon(self, key):
icon = self._icons[key] icon = self._icons[key]

View File

@ -0,0 +1,104 @@
# 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 math
import gobject
import hippo
from sugar.graphics import units
_BASE_RADIUS = units.points_to_pixels(20)
_CHILDREN_FACTOR = 1
class SnowflakeLayout(gobject.GObject,hippo.CanvasLayout):
__gtype_name__ = 'SugarSnowflakeLayout'
def __init__(self):
hippo.CanvasBox.__init__(self)
self._nflakes = 0
def add(self, child):
self._box.append(child)
box_child = self._box.find_box_child(child)
box_child.is_center = False
self._nflakes += 1
def add_center(self, child):
self._box.append(child)
box_child = self._box.find_box_child(child)
box_child.is_center = True
def do_set_box(self, box):
self._box = box
def do_get_height_request(self, for_width):
size = self._calculate_size()
return (size, size)
def do_get_width_request(self):
size = self._calculate_size()
return (size, size)
def do_allocate(self, x, y, width, height,
req_width, req_height, origin_changed):
r = self._get_radius()
index = 0
for child in self._box.get_layout_children():
cx = x + width / 2
cy = x + height / 2
min_width, child_width = child.get_width_request()
min_height, child_height = child.get_height_request(child_width)
if child.is_center:
child.allocate(x + (width - child_width) / 2,
y + (height - child_height) / 2,
child_width, child_height, origin_changed)
else:
angle = 2 * math.pi * index / self._nflakes
dx = math.cos(angle) * r
dy = math.sin(angle) * r
child_x = int(x + (width - child_width) / 2 + dx)
child_y = int(y + (height - child_height) / 2 + dy)
child.allocate(child_x, child_y, child_width,
child_height, origin_changed)
index += 1
def _get_radius(self):
return int(_BASE_RADIUS + _CHILDREN_FACTOR * self._nflakes)
def _calculate_size(self):
size = 0
for child in self._box.get_layout_children():
[min_width, child_width] = child.get_width_request()
[min_height, child_height] = child.get_height_request(child_width)
new_size = max(child_width, child_height)
if not child.is_center:
new_size += self._get_radius() * 2
if new_size > size:
size = new_size
return size

View File

@ -16,7 +16,6 @@ sugar_PYTHON = \
roundbox.py \ roundbox.py \
palette.py \ palette.py \
panel.py \ panel.py \
snowflakebox.py \
spreadbox.py \ spreadbox.py \
toggletoolbutton.py \ toggletoolbutton.py \
toolbox.py \ toolbox.py \

View File

@ -1,106 +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 math
import cairo
import hippo
from sugar.graphics import units
_BASE_RADIUS = units.points_to_pixels(20)
_CHILDREN_FACTOR = 1
_FLAKE_DISTANCE = units.points_to_pixels(4)
class SnowflakeBox(hippo.CanvasBox, hippo.CanvasItem):
__gtype_name__ = 'SugarSnowflakeBox'
def __init__(self, **kwargs):
hippo.CanvasBox.__init__(self, **kwargs)
self._root = None
def set_root(self, icon):
self._root = icon
def _get_center(self):
[width, height] = self.get_allocation()
return [width / 2, height / 2]
def _get_radius(self):
return _BASE_RADIUS + _CHILDREN_FACTOR * self._get_n_children()
def _layout_root(self):
[width, height] = self._root.get_allocation()
[cx, cy] = self._get_center()
x = cx - (width / 2)
y = cy - (height / 2)
self.set_position(self._root, int(x), int(y))
def _get_n_children(self):
return len(self.get_children()) - 1
def _layout_child(self, child, index):
r = self._get_radius()
if (self._get_n_children() > 10):
r += _FLAKE_DISTANCE * (index % 3)
angle = 2 * math.pi * index / self._get_n_children()
[width, height] = child.get_allocation()
[cx, cy] = self._get_center()
x = cx + math.cos(angle) * r - (width / 2)
y = cy + math.sin(angle) * r - (height / 2)
self.set_position(child, int(x), int(y))
def _calculate_size(self):
max_child_size = 0
for child in self.get_children():
[min_w, natural_w] = child.get_width_request()
[min_h, natural_h] = child.get_height_request(min_w)
max_child_size = max (max_child_size, min_w)
max_child_size = max (max_child_size, min_h)
return self._get_radius() * 2 + max_child_size + _FLAKE_DISTANCE * 2
def do_get_height_request(self, for_width):
hippo.CanvasBox.do_get_height_request(self, for_width)
size = self._calculate_size()
return (size, size)
def do_get_width_request(self):
hippo.CanvasBox.do_get_width_request(self)
size = self._calculate_size()
return (size, size)
def do_allocate(self, width, height, origin_changed):
hippo.CanvasBox.do_allocate(self, width, height, origin_changed)
self._layout_root()
index = 0
for child in self.get_children():
if child != self._root:
self._layout_child(child, index)
index += 1

View File

@ -17,32 +17,33 @@
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
import sys import sys
from random import random
import pygtk
pygtk.require('2.0')
import gobject
import gtk import gtk
import hippo import hippo
from sugar.graphics.snowflakebox import SnowflakeBox
from sugar.graphics.spreadbox import SpreadBox
from sugar.graphics.xocolor import XoColor from sugar.graphics.xocolor import XoColor
from sugar.graphics.canvasicon import CanvasIcon from sugar.graphics.canvasicon import CanvasIcon
from sugar import env
def _create_snowflake(parent, children): sys.path.append(env.get_shell_path())
color = XoColor()
icon = CanvasIcon(scale=0.8, xo_color=color, from view.home.snowflakelayout import SnowflakeLayout
def add_snowflake(parent, size):
box = hippo.CanvasBox()
parent.append(box)
layout = SnowflakeLayout()
box.set_layout(layout)
icon = CanvasIcon(scale=0.8, xo_color=XoColor(),
icon_name='theme:object-link') icon_name='theme:object-link')
parent.append(icon, hippo.PACK_FIXED) layout.add_center(icon)
parent.set_root(icon)
for i in range(0, children): for k in range(0, size):
color = XoColor() icon = CanvasIcon(scale=0.4, xo_color=XoColor(),
icon = CanvasIcon(scale=0.4, xo_color=color,
icon_name='theme:stock-buddy') icon_name='theme:stock-buddy')
parent.append(icon, hippo.PACK_FIXED) layout.add(icon)
window = gtk.Window() window = gtk.Window()
window.set_default_size(gtk.gdk.screen_width(), gtk.gdk.screen_height()) window.set_default_size(gtk.gdk.screen_width(), gtk.gdk.screen_height())
@ -51,17 +52,13 @@ window.show()
canvas = hippo.Canvas() canvas = hippo.Canvas()
root_box = SpreadBox(background_color=0xe2e2e2ff) root = hippo.CanvasBox(background_color=0xe2e2e2ff)
canvas.set_root(root_box) canvas.set_root(root)
box = SnowflakeBox() add_snowflake(root, 10)
snow_flake = _create_snowflake(box, 0) add_snowflake(root, 20)
root_box.set_center_item(box) add_snowflake(root, 15)
add_snowflake(root, 5)
for i in range(0, 30):
box = SnowflakeBox()
snow_flake = _create_snowflake(box, int(2 + random() * 8))
root_box.add_item(box)
canvas.show() canvas.show()
window.add(canvas) window.add(canvas)