sugar-toolkit-gtk3/sugar/graphics/snowflakelayout.py

62 lines
1.4 KiB
Python
Raw Normal View History

import math
import cairo
class SnowflakeLayout:
_BASE_RADIUS = 65
_CHILDREN_FACTOR = 1
_FLAKE_DISTANCE = 6
def __init__(self):
self._root = None
2006-09-25 20:50:15 +02:00
self._r = 0
def set_root(self, icon):
self._root = icon
2006-10-05 18:53:34 +02:00
def _layout_root(self, box):
[width, height] = self._root.get_allocation()
2006-10-05 18:53:34 +02:00
x = self._cx - (width / 2)
y = self._cy - (height / 2)
2006-10-05 18:53:34 +02:00
box.move(self._root, int(x), int(y))
2006-10-05 18:53:34 +02:00
def _layout_child(self, box, child, index):
r = self._r
2006-10-05 18:53:34 +02:00
if (len(box.get_children()) > 10):
r += SnowflakeLayout._FLAKE_DISTANCE * (index % 3)
2006-10-05 18:53:34 +02:00
angle = 2 * math.pi / len(box.get_children()) * index
2006-10-05 18:53:34 +02:00
[width, height] = child.get_allocation()
2006-09-25 20:50:15 +02:00
x = self._cx + math.cos(angle) * r - (width / 2)
y = self._cy + math.sin(angle) * r - (height / 2)
2006-10-05 18:53:34 +02:00
box.move(child, int(x), int(y))
2006-10-05 18:53:34 +02:00
def get_size(self, box):
2006-09-25 20:50:15 +02:00
max_child_size = 0
2006-10-05 18:53:34 +02:00
for child in box.get_children():
[width, height] = child.get_allocation()
2006-09-25 20:50:15 +02:00
max_child_size = max (max_child_size, width)
max_child_size = max (max_child_size, height)
return self._r * 2 + max_child_size + \
SnowflakeLayout._FLAKE_DISTANCE * 2
2006-09-25 16:40:15 +02:00
2006-10-05 18:53:34 +02:00
def layout(self, box):
self._r = SnowflakeLayout._BASE_RADIUS + \
2006-10-05 18:53:34 +02:00
SnowflakeLayout._CHILDREN_FACTOR * len(box.get_children())
2006-10-05 18:53:34 +02:00
size = self.get_size(box)
2006-09-25 20:50:15 +02:00
self._cx = size / 2
self._cy = size / 2
2006-10-05 18:53:34 +02:00
self._layout_root(box)
index = 0
2006-10-05 18:53:34 +02:00
for child in box.get_children():
self._layout_child(box, child, index)
index += 1