2007-06-24 14:43:48 +02:00
|
|
|
# Copyright (C) 2006-2007 Red Hat, Inc.
|
2006-10-15 01:08:44 +02:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
|
2007-05-07 16:24:41 +02:00
|
|
|
from array import array
|
|
|
|
from random import random
|
2006-10-06 11:11:38 +02:00
|
|
|
|
2007-05-10 10:42:56 +02:00
|
|
|
import gobject
|
2006-10-06 11:11:38 +02:00
|
|
|
import hippo
|
|
|
|
|
2007-05-07 16:24:41 +02:00
|
|
|
_X_TILES = 120
|
2007-05-07 16:59:28 +02:00
|
|
|
_NUM_TRIALS = 20
|
|
|
|
_MAX_WEIGHT = 255
|
2006-10-06 11:11:38 +02:00
|
|
|
|
2007-05-07 16:24:41 +02:00
|
|
|
class _Grid(object):
|
|
|
|
def __init__(self, x_tiles, y_tiles, cell_size):
|
|
|
|
self._array = array('B')
|
|
|
|
self.x_tiles = x_tiles
|
|
|
|
self.y_tiles = y_tiles
|
|
|
|
self.cell_size = cell_size
|
|
|
|
|
|
|
|
for i in range(self.y_tiles * self.x_tiles):
|
|
|
|
self._array.append(0)
|
|
|
|
|
|
|
|
def __getitem__(self, (row, col)):
|
|
|
|
return self._array[col + row * self.x_tiles]
|
|
|
|
|
|
|
|
def __setitem__(self, (row, col), value):
|
|
|
|
self._array[col + row * self.x_tiles] = value
|
|
|
|
|
|
|
|
def compute_weight_at(self, x, y, width, height):
|
|
|
|
weight = 0
|
|
|
|
|
|
|
|
for i in range(x, x + width):
|
|
|
|
for j in range(y, y + height):
|
|
|
|
weight += self[j, i]
|
|
|
|
|
|
|
|
return weight
|
|
|
|
|
|
|
|
def add_weight_at(self, x, y, width, height):
|
|
|
|
for i in range(x, x + width):
|
|
|
|
for j in range(y, y + height):
|
|
|
|
self[j, i] += 1
|
|
|
|
|
|
|
|
def remove_weight_at(self, x, y, width, height):
|
|
|
|
for i in range(x, x + width):
|
|
|
|
for j in range(y, y + height):
|
|
|
|
self[j, i] -= 1
|
2007-05-10 10:42:56 +02:00
|
|
|
|
|
|
|
def from_canvas(self, dim):
|
|
|
|
return dim / self.cell_size
|
|
|
|
|
|
|
|
def to_canvas(self, dim):
|
|
|
|
return dim * self.cell_size
|
|
|
|
|
|
|
|
class _ItemInfo(gobject.GObject):
|
|
|
|
def __init__(self):
|
|
|
|
gobject.GObject.__init__(self)
|
|
|
|
self.x = -1
|
|
|
|
self.y = -1
|
|
|
|
self.width = -1
|
|
|
|
self.height = -1
|
|
|
|
|
|
|
|
def add_weight(self, grid):
|
|
|
|
grid.add_weight_at(self.x, self.y, self.width, self.height)
|
|
|
|
|
|
|
|
def remove_weight(self, grid):
|
|
|
|
grid.remove_weight_at(self.x, self.y, self.width, self.height)
|
|
|
|
|
|
|
|
def compute_weight(self, grid, x=-1, y=-1):
|
|
|
|
if x < 0:
|
|
|
|
x = self.x
|
|
|
|
if y < 0:
|
|
|
|
y = self.y
|
|
|
|
|
|
|
|
grid.compute_weight_at(x, y, self.width, self.height)
|
2006-10-06 11:11:38 +02:00
|
|
|
|
2007-04-16 10:36:15 +02:00
|
|
|
class SpreadBox(hippo.CanvasBox, hippo.CanvasItem):
|
|
|
|
__gtype_name__ = 'SugarSpreadBox'
|
2006-12-04 20:12:24 +01:00
|
|
|
def __init__(self, **kwargs):
|
|
|
|
hippo.CanvasBox.__init__(self, **kwargs)
|
2006-10-06 11:11:38 +02:00
|
|
|
|
2007-05-07 16:24:41 +02:00
|
|
|
self._grid = None
|
2007-04-16 10:36:15 +02:00
|
|
|
self._center = None
|
2007-05-07 16:24:41 +02:00
|
|
|
self._width = -1
|
|
|
|
self._height = -1
|
2006-10-06 11:11:38 +02:00
|
|
|
|
2007-04-16 10:36:15 +02:00
|
|
|
def set_center_item(self, item):
|
|
|
|
if self._center:
|
|
|
|
self.remove(self._center)
|
|
|
|
|
|
|
|
self.append(item, hippo.PACK_FIXED)
|
2007-05-07 16:24:41 +02:00
|
|
|
self._center = item
|
2007-04-16 10:36:15 +02:00
|
|
|
|
2007-05-07 16:24:41 +02:00
|
|
|
self._layout_center()
|
2006-10-07 14:33:08 +02:00
|
|
|
|
2007-05-07 16:24:41 +02:00
|
|
|
def add_item(self, item):
|
2007-05-10 10:42:56 +02:00
|
|
|
item.set_data('item-info', _ItemInfo())
|
2007-03-07 12:11:14 +01:00
|
|
|
self.append(item, hippo.PACK_FIXED)
|
2007-05-07 16:24:41 +02:00
|
|
|
self._layout_item(item)
|
2006-10-07 14:33:08 +02:00
|
|
|
|
2007-03-07 12:11:14 +01:00
|
|
|
def remove_item(self, item):
|
2007-05-11 23:10:07 +02:00
|
|
|
if self._grid:
|
2007-05-11 23:11:02 +02:00
|
|
|
info = item.get_data('item-info')
|
2007-05-11 23:10:07 +02:00
|
|
|
info.remove_weight(self._grid)
|
2007-05-10 10:42:56 +02:00
|
|
|
|
2007-03-07 12:11:14 +01:00
|
|
|
self.remove(item)
|
2007-05-07 16:24:41 +02:00
|
|
|
|
2007-05-10 10:42:56 +02:00
|
|
|
def _place_item(self, item, x, y):
|
|
|
|
info = item.get_data('item-info')
|
|
|
|
info.x = x
|
|
|
|
info.y = y
|
|
|
|
info.add_weight(self._grid)
|
2007-05-07 16:59:28 +02:00
|
|
|
|
2007-05-10 10:42:56 +02:00
|
|
|
self.set_position(item, self._grid.to_canvas(x),
|
|
|
|
self._grid.to_canvas(y))
|
2007-05-07 16:59:28 +02:00
|
|
|
|
2007-05-07 16:24:41 +02:00
|
|
|
def _layout_item(self, item):
|
|
|
|
if not self._grid:
|
|
|
|
return
|
2007-05-10 10:42:56 +02:00
|
|
|
|
|
|
|
info = item.get_data('item-info')
|
|
|
|
|
|
|
|
[width, height] = item.get_allocation()
|
|
|
|
info.width = self._grid.from_canvas(width)
|
|
|
|
info.height = self._grid.from_canvas(height)
|
|
|
|
|
2007-05-07 16:24:41 +02:00
|
|
|
trials = _NUM_TRIALS
|
|
|
|
placed = False
|
2007-05-07 16:59:28 +02:00
|
|
|
best_weight = _MAX_WEIGHT
|
2007-05-07 16:24:41 +02:00
|
|
|
|
2007-05-07 16:59:28 +02:00
|
|
|
while trials > 0 and not placed:
|
2007-05-10 10:42:56 +02:00
|
|
|
x = int(random() * (self._grid.x_tiles - info.width))
|
|
|
|
y = int(random() * (self._grid.y_tiles - info.height))
|
|
|
|
|
|
|
|
weight = info.compute_weight(self._grid, x, y)
|
2007-05-07 16:24:41 +02:00
|
|
|
|
|
|
|
if weight == 0:
|
2007-05-10 10:42:56 +02:00
|
|
|
self._place_item(item, x, y)
|
2007-05-07 16:24:41 +02:00
|
|
|
placed = True
|
2007-05-07 16:59:28 +02:00
|
|
|
elif weight < best_weight:
|
|
|
|
best_x = x
|
|
|
|
best_y = y
|
2007-05-07 16:24:41 +02:00
|
|
|
|
|
|
|
trials -= 1
|
2007-05-07 16:59:28 +02:00
|
|
|
|
|
|
|
if not placed:
|
2007-05-10 10:42:56 +02:00
|
|
|
self._place_item(item, best_x, best_y)
|
2007-05-07 16:24:41 +02:00
|
|
|
|
|
|
|
def _layout(self):
|
|
|
|
for item in self.get_children():
|
|
|
|
if item != self._center:
|
|
|
|
self._layout_item(item)
|
|
|
|
|
|
|
|
def _layout_center(self):
|
|
|
|
if not self._center or not self._grid:
|
|
|
|
return
|
2007-04-16 10:36:15 +02:00
|
|
|
|
2007-05-07 16:24:41 +02:00
|
|
|
[width, height] = self._center.get_allocation()
|
|
|
|
x = (self._width - width) / 2
|
|
|
|
y = (self._height - height) / 2
|
|
|
|
self.set_position(self._center, x, y)
|
|
|
|
|
2007-04-16 10:36:15 +02:00
|
|
|
def do_allocate(self, width, height, origin_changed):
|
|
|
|
hippo.CanvasBox.do_allocate(self, width, height, origin_changed)
|
|
|
|
|
2007-05-07 16:24:41 +02:00
|
|
|
if width != self._width or height != self._height:
|
|
|
|
cell_size = width / _X_TILES
|
|
|
|
y_tiles = height / cell_size
|
|
|
|
|
|
|
|
self._grid = _Grid(_X_TILES, y_tiles, cell_size)
|
|
|
|
self._width = width
|
|
|
|
self._height = height
|
|
|
|
|
|
|
|
self._layout()
|
|
|
|
|
|
|
|
self._layout_center()
|