Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/sugar/graphics/snowflakebox.py
blob: 81e598120f2f1bea5ab3cdd29fa94d1431bbb922 (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
# Copyright (C) 2006-2007 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.

import math

import cairo
import hippo

from sugar.graphics import units

_BASE_RADIUS = units.points_to_pixels(20)
_CHILDREN_FACTOR = 1
_FLAKE_DISTANCE = units.points_to_pixels(4)

class SnowflakeBox(hippo.CanvasBox, hippo.CanvasItem):
    __gtype_name__ = 'SugarSnowflakeBox'
    def __init__(self, **kwargs):
        hippo.CanvasBox.__init__(self, **kwargs)
        self._root = None

    def set_root(self, icon):
        self._root = icon

    def _get_center(self):
        [width, height] = self.get_allocation()
        return [width / 2, height / 2]

    def _get_radius(self):
        return _BASE_RADIUS + _CHILDREN_FACTOR * self._get_n_children()

    def _layout_root(self):
        [width, height] = self._root.get_allocation()
        [cx, cy] = self._get_center()

        x = cx - (width / 2)
        y = cy - (height / 2)

        self.set_position(self._root, int(x), int(y))

    def _get_n_children(self):
        return len(self.get_children()) - 1

    def _layout_child(self, child, index):
        r = self._get_radius()
        if (self._get_n_children() > 10):
            r += _FLAKE_DISTANCE * (index % 3)

        angle = 2 * math.pi * index / self._get_n_children()

        [width, height] = child.get_allocation()
        [cx, cy] = self._get_center()

        x = cx + math.cos(angle) * r - (width / 2)
        y = cy + math.sin(angle) * r - (height / 2)

        self.set_position(child, int(x), int(y))

    def _calculate_size(self):
        max_child_size = 0
        
        for child in self.get_children():
            [min_w, natural_w] = child.get_width_request()
            [min_h, natural_h] = child.get_height_request(min_w)
            max_child_size = max (max_child_size, min_w)
            max_child_size = max (max_child_size, min_h)

        return self._get_radius() * 2 + max_child_size + _FLAKE_DISTANCE * 2

    def do_get_height_request(self, for_width):
        hippo.CanvasBox.do_get_height_request(self, for_width)
        
        size = self._calculate_size()
        
        return (size, size)

    def do_get_width_request(self):
        hippo.CanvasBox.do_get_width_request(self)

        size = self._calculate_size()
        
        return (size, size)

    def do_allocate(self, width, height, origin_changed):
        hippo.CanvasBox.do_allocate(self, width, height, origin_changed)

        self._layout_root()

        index = 0
        for child in self.get_children():
            if child != self._root:
                self._layout_child(child, index)
                index += 1