Adapt spread layout to hippo

This commit is contained in:
Marco Pesenti Gritti 2006-10-05 18:32:35 +02:00
parent aec5cdbdcd
commit 924fe94b16

View File

@ -3,74 +3,84 @@ import math
import cairo import cairo
class SpreadLayout: _DISTANCE_THRESHOLD = 120.0
DISTANCE_THRESHOLD = 120.0
class SpreadLayout:
def __init__(self): def __init__(self):
pass pass
def _get_distance(self, icon1, icon2): def _get_distance(self, box, icon1, icon2):
[icon1_x, icon1_y] = self._constraints[icon1] [icon1_x, icon1_y] = box.get_position(icon1)
[icon2_x, icon2_y] = self._constraints[icon2] [icon2_x, icon2_y] = box.get_position(icon2)
a = icon1_x - icon2_x a = icon1_x - icon2_x
b = icon1_y - icon2_y b = icon1_y - icon2_y
return math.sqrt(a * a + b * b) return math.sqrt(a * a + b * b)
def _get_repulsion(self, icon1, icon2): def _get_repulsion(self, box, icon1, icon2):
[icon1_x, icon1_y] = self._constraints[icon1] [icon1_x, icon1_y] = box.get_position(icon1)
[icon2_x, icon2_y] = self._constraints[icon2] [icon2_x, icon2_y] = box.get_position(icon2)
f_x = icon1_x - icon2_x f_x = icon1_x - icon2_x
f_y = icon1_y - icon2_y f_y = icon1_y - icon2_y
return [f_x, f_y] return [f_x, f_y]
def _spread_icons(self): def _clamp_position(self, box, icon, x, y):
self._stable = True x = max(0, x)
y = max(0, y)
for icon1 in self._icons: [item_w, item_h] = icon.get_allocation()
[box_w, box_h] = box.get_allocation()
x = min(box_w - item_w, x)
y = min(box_h - item_h, y)
return [x, y]
def _spread_icons(self, box):
stable = True
for icon1 in box.get_children():
vx = 0 vx = 0
vy = 0 vy = 0
[x, y] = self._constraints[icon1] [x, y] = box.get_position(icon1)
for icon2 in self._icons: for icon2 in box.get_children():
if icon1 != icon2: if icon1 != icon2:
distance = self._get_distance(icon1, icon2) distance = self._get_distance(box, icon1, icon2)
if distance <= IconLayout.DISTANCE_THRESHOLD: if distance <= _DISTANCE_THRESHOLD:
self._stable = False stable = False
[f_x, f_y] = self._get_repulsion(icon1, icon2) [f_x, f_y] = self._get_repulsion(box, icon1, icon2)
vx += f_x vx += f_x
vy += f_y vy += f_y
new_x = x + vx new_x = x + vx
new_y = y + vy new_y = y + vy
new_x = max(self._x1, new_x) [new_x, new_y] = self._clamp_position(box, icon1, new_x, new_y)
new_y = max(self._y1, new_y)
[width, height] = icon1.get_size_request() box.move(icon1, new_x, new_y)
new_x = min(self._x2 - width, new_x)
new_y = min(self._y2 - height, new_y)
self._constraints[icon1] = [new_x, new_y] return stable
matrix = cairo.Matrix(1, 0, 0, 1, 0, 0)
matrix.translate(new_x - (width / 2), new_y - (height / 2))
icon1.set_transform(matrix)
def update(self):
tries = 10
self._spread_icons()
while not self._stable and tries > 0:
self._spread_icons()
tries -= 1
def layout(self, box): def layout(self, box):
[width, height] = box.get_allocation() [width, height] = box.get_allocation()
for item in box.get_children(): for item in box.get_children():
[item_w, item_h] = item.get_allocation()
x = int(random.random() * width) x = int(random.random() * width)
y = int(random.random() * height) y = int(random.random() * height)
[x, y] = self._clamp_position(box, item, x, y)
box.move(item, x, y) box.move(item, x, y)
tries = 10
stable = self._spread_icons(box)
while not stable and tries > 0:
stable = self._spread_icons(box)
tries -= 1