Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/sugar/graphics/spreadbox.py
blob: f4cf5ea9a70ef87bc337218487a3b90ad58b3415 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# Copyright (C) 2006, 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.

from array import array
from random import random

import hippo

_X_TILES = 120
_NUM_TRIALS = 100

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

class SpreadBox(hippo.CanvasBox, hippo.CanvasItem):
    __gtype_name__ = 'SugarSpreadBox'
    def __init__(self, **kwargs):
        hippo.CanvasBox.__init__(self, **kwargs)

        self._grid = None
        self._center = None
        self._width = -1
        self._height = -1

    def set_center_item(self, item):
        if self._center:
            self.remove(self._center)

        self.append(item, hippo.PACK_FIXED)
        self._center = item

        self._layout_center()

    def add_item(self, item):
        self.append(item, hippo.PACK_FIXED)
        self._layout_item(item)

    def remove_item(self, item):
        self.remove(item)

    def _layout_item(self, item):
        if not self._grid:
            return
    
        trials = _NUM_TRIALS
        placed = False

        while trials > 0 and not placed:
            [width, height] = item.get_allocation()
            cell_size = self._grid.cell_size

            w = int(width / cell_size)
            h = int(height / cell_size)
            x = int(random() * (self._grid.x_tiles - w))
            y = int(random() * (self._grid.y_tiles - h))

            weight = self._grid.compute_weight_at(x, y, w, h)
            if weight == 0:
                self._grid.add_weight_at(x, y, w, h)
                self.set_position(item, cell_size * x, cell_size * y)
                placed = True

            trials -= 1

    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
        
        [width, height] = self._center.get_allocation()
        x = (self._width - width) / 2
        y = (self._height - height) / 2        
        self.set_position(self._center, x, y)
                          
    def do_allocate(self, width, height, origin_changed):
        hippo.CanvasBox.do_allocate(self, width, height, origin_changed)

        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()