2006-08-29 16:07:23 +02:00
|
|
|
import random
|
2006-09-25 00:08:33 +02:00
|
|
|
import math
|
2006-08-29 16:07:23 +02:00
|
|
|
|
2006-09-25 11:35:30 +02:00
|
|
|
import cairo
|
|
|
|
|
2006-10-05 18:32:35 +02:00
|
|
|
_DISTANCE_THRESHOLD = 120.0
|
2006-09-25 00:08:33 +02:00
|
|
|
|
2006-10-05 18:32:35 +02:00
|
|
|
class SpreadLayout:
|
2006-10-03 16:31:32 +02:00
|
|
|
def __init__(self):
|
|
|
|
pass
|
2006-09-25 11:35:30 +02:00
|
|
|
|
2006-10-05 18:32:35 +02:00
|
|
|
def _get_distance(self, box, icon1, icon2):
|
|
|
|
[icon1_x, icon1_y] = box.get_position(icon1)
|
|
|
|
[icon2_x, icon2_y] = box.get_position(icon2)
|
2006-09-25 11:35:30 +02:00
|
|
|
|
|
|
|
a = icon1_x - icon2_x
|
|
|
|
b = icon1_y - icon2_y
|
2006-08-29 16:07:23 +02:00
|
|
|
|
2006-09-25 00:08:33 +02:00
|
|
|
return math.sqrt(a * a + b * b)
|
|
|
|
|
2006-10-05 18:32:35 +02:00
|
|
|
def _get_repulsion(self, box, icon1, icon2):
|
|
|
|
[icon1_x, icon1_y] = box.get_position(icon1)
|
|
|
|
[icon2_x, icon2_y] = box.get_position(icon2)
|
2006-09-25 11:35:30 +02:00
|
|
|
|
|
|
|
f_x = icon1_x - icon2_x
|
|
|
|
f_y = icon1_y - icon2_y
|
|
|
|
|
|
|
|
return [f_x, f_y]
|
|
|
|
|
2006-10-05 18:32:35 +02:00
|
|
|
def _clamp_position(self, box, icon, x, y):
|
|
|
|
x = max(0, x)
|
|
|
|
y = max(0, y)
|
|
|
|
|
|
|
|
[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]
|
2006-09-25 00:08:33 +02:00
|
|
|
|
2006-10-05 18:32:35 +02:00
|
|
|
def _spread_icons(self, box):
|
|
|
|
stable = True
|
|
|
|
|
|
|
|
for icon1 in box.get_children():
|
2006-09-25 00:08:33 +02:00
|
|
|
vx = 0
|
|
|
|
vy = 0
|
|
|
|
|
2006-10-05 18:32:35 +02:00
|
|
|
[x, y] = box.get_position(icon1)
|
2006-09-25 11:35:30 +02:00
|
|
|
|
2006-10-05 18:32:35 +02:00
|
|
|
for icon2 in box.get_children():
|
2006-09-25 00:08:33 +02:00
|
|
|
if icon1 != icon2:
|
2006-10-05 18:32:35 +02:00
|
|
|
distance = self._get_distance(box, icon1, icon2)
|
|
|
|
if distance <= _DISTANCE_THRESHOLD:
|
|
|
|
stable = False
|
|
|
|
[f_x, f_y] = self._get_repulsion(box, icon1, icon2)
|
2006-09-25 11:35:30 +02:00
|
|
|
vx += f_x
|
|
|
|
vy += f_y
|
2006-09-25 00:08:33 +02:00
|
|
|
|
2006-09-25 11:35:30 +02:00
|
|
|
new_x = x + vx
|
|
|
|
new_y = y + vy
|
2006-09-25 00:08:33 +02:00
|
|
|
|
2006-10-05 18:32:35 +02:00
|
|
|
[new_x, new_y] = self._clamp_position(box, icon1, new_x, new_y)
|
2006-09-25 11:35:30 +02:00
|
|
|
|
2006-10-05 18:32:35 +02:00
|
|
|
box.move(icon1, new_x, new_y)
|
2006-09-25 00:08:33 +02:00
|
|
|
|
2006-10-05 18:32:35 +02:00
|
|
|
return stable
|
2006-09-25 22:01:35 +02:00
|
|
|
|
2006-10-03 16:31:32 +02:00
|
|
|
def layout(self, box):
|
|
|
|
[width, height] = box.get_allocation()
|
2006-10-05 18:32:35 +02:00
|
|
|
|
2006-10-03 16:31:32 +02:00
|
|
|
for item in box.get_children():
|
2006-10-05 18:32:35 +02:00
|
|
|
[item_w, item_h] = item.get_allocation()
|
|
|
|
|
2006-10-03 16:31:32 +02:00
|
|
|
x = int(random.random() * width)
|
|
|
|
y = int(random.random() * height)
|
2006-10-05 18:32:35 +02:00
|
|
|
|
|
|
|
[x, y] = self._clamp_position(box, item, x, y)
|
|
|
|
|
2006-10-03 16:31:32 +02:00
|
|
|
box.move(item, x, y)
|
2006-10-05 18:32:35 +02:00
|
|
|
|
|
|
|
tries = 10
|
|
|
|
stable = self._spread_icons(box)
|
|
|
|
while not stable and tries > 0:
|
|
|
|
stable = self._spread_icons(box)
|
|
|
|
tries -= 1
|