sugar-toolkit-gtk3/sugar/canvas/GridLayout.py

139 lines
3.3 KiB
Python
Raw Normal View History

2006-09-08 11:31:11 +02:00
import cairo
2006-09-07 15:11:51 +02:00
import gobject
import goocanvas
2006-09-07 11:36:04 +02:00
class GridConstraints:
def __init__(self, x, y, width, height, padding=0):
2006-09-07 11:36:04 +02:00
self.x = x
self.y = y
self.width = width
self.height = height
self.padding = padding
2006-09-07 11:36:04 +02:00
class GridLayout:
2006-09-07 18:47:15 +02:00
def __init__(self, cols=16, rows=12):
2006-09-07 11:36:04 +02:00
self._rows = rows
self._cols = cols
self._constraints = {}
2006-09-07 15:11:51 +02:00
def set_constraints(self, component, constraints):
2006-09-07 11:36:04 +02:00
self._constraints[component] = constraints
if isinstance(component, goocanvas.Item):
self.layout_canvas_item(component)
2006-09-07 11:36:04 +02:00
def _get_geometry(self, container, component):
constraints = self._constraints[component]
if constraints:
2006-09-07 15:11:51 +02:00
return self.get_bounds(container, constraints)
2006-09-07 11:36:04 +02:00
else:
return [0, 0, 0, 0]
2006-09-07 15:11:51 +02:00
def get_bounds(self, container, constraints):
w = container.props.width
h = container.props.height
padding = constraints.padding
2006-09-07 18:47:15 +02:00
x = constraints.x * w / self._cols + padding
y = constraints.y * h / self._rows + padding
2006-09-07 15:11:51 +02:00
2006-09-07 18:47:15 +02:00
width = constraints.width * w / self._cols - padding * 2
2006-09-08 02:59:05 +02:00
height = constraints.height * h / self._rows - padding * 2
2006-09-07 15:11:51 +02:00
2006-09-08 02:11:46 +02:00
width = max(0, width)
height = max(0, height)
2006-09-07 15:11:51 +02:00
return [x, y, width, height]
2006-09-09 12:23:01 +02:00
def layout_canvas_item(self, item):
group = item.get_parent()
if group == None:
return
2006-09-09 12:23:01 +02:00
[x, y, width, height] = self._get_geometry(group, item)
2006-09-07 11:36:04 +02:00
2006-09-09 12:23:01 +02:00
item.props.x = x
item.props.y = y
2006-09-07 15:11:51 +02:00
2006-09-09 12:23:01 +02:00
try:
item.props.width = width
item.props.height = height
except:
item.props.size = width
2006-09-07 15:11:51 +02:00
def layout_screen(self, screen):
for window in screen.get_windows():
[x, y, width, height] = self._get_geometry(screen, window)
2006-09-08 10:38:45 +02:00
window.move(int(x), int(y))
window.resize(int(width), int(height))
2006-09-07 15:11:51 +02:00
class GridGroup(goocanvas.Group):
__gproperties__ = {
2006-09-08 10:16:23 +02:00
'x' : (float, None, None, -10e6, 10e6, 800.0,
2006-09-07 15:11:51 +02:00
gobject.PARAM_READWRITE),
2006-09-08 10:16:23 +02:00
'y' : (float, None, None, -10e6, 10e6, 600.0,
2006-09-07 15:11:51 +02:00
gobject.PARAM_READWRITE),
2006-09-08 10:16:23 +02:00
'width' : (float, None, None, 0, 10e6, 800.0,
2006-09-07 15:11:51 +02:00
gobject.PARAM_READWRITE),
2006-09-08 10:16:23 +02:00
'height' : (float, None, None, 0, 10e6, 600.0,
2006-09-07 15:11:51 +02:00
gobject.PARAM_READWRITE)
}
def _update_position(self):
if self._x != 0 or self._y != 0:
2006-09-08 11:31:11 +02:00
matrix = cairo.Matrix(1, 0, 0, 1, 0, 0)
matrix.translate(self._x, self._y)
self.set_transform(matrix)
2006-09-07 15:11:51 +02:00
2006-09-09 12:23:01 +02:00
def _update_layout(self):
i = 0
while i < self.get_n_children():
self._layout.layout_canvas_item(self.get_child(i))
i += 1
2006-09-07 15:11:51 +02:00
def do_set_property(self, pspec, value):
if pspec.name == 'width':
self._width = value
2006-09-09 12:23:01 +02:00
self._update_layout()
2006-09-07 15:11:51 +02:00
elif pspec.name == 'height':
self._height = value
2006-09-09 12:23:01 +02:00
self._update_layout()
2006-09-07 15:11:51 +02:00
elif pspec.name == 'x':
self._x = value
self._update_position()
elif pspec.name == 'y':
self._y = value
self._update_position()
def do_get_property(self, pspec):
if pspec.name == 'width':
return self._width
elif pspec.name == 'height':
return self._height
elif pspec.name == 'x':
return self._x
elif pspec.name == 'x':
return self._x
2006-09-07 18:47:15 +02:00
def __init__(self, cols=-1, rows=-1):
2006-09-07 15:11:51 +02:00
self._x = 0
self._y = 0
self._width = 0
self._height = 0
goocanvas.Group.__init__(self)
if rows < 0 and cols < 0:
self._layout = GridLayout()
else:
2006-09-07 18:47:15 +02:00
self._layout = GridLayout(cols, rows)
2006-09-07 15:11:51 +02:00
self.connect('child-added', self.__child_added_cb)
def get_layout(self):
return self._layout
2006-09-09 12:23:01 +02:00
def __child_added_cb(self, group, position):
item = group.get_child(position)
self._layout.layout_canvas_item(item)