Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/sugar/canvas/Grid.py
blob: 70bafa38f1c55f40a368ffb0be724b6b9a903ec2 (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
import gtk
import goocanvas
import cairo

from sugar.canvas.IconItem import IconItem

class Grid:
	COLS = 80.0
	ROWS = 60.0
	MACRO_CELL_FACTOR = 5.0

	def get_macro_rows(self):
		return Grid.ROWS / Grid.MACRO_CELL_FACTOR

	def get_macro_cols(self):
		return Grid.COLS / Grid.MACRO_CELL_FACTOR

	def macro_to_micro(self, x, y):
		return [round(x * Grid.MACRO_CELL_FACTOR),
				round(y * Grid.MACRO_CELL_FACTOR)]

	def micro_to_macro(self, x, y):
		return [round(x / Grid.MACRO_CELL_FACTOR),
				round(y / Grid.MACRO_CELL_FACTOR)]
	
	def convert_from_screen(self, x, y):
		factor = Grid.COLS / gtk.gdk.screen_width()

		grid_x = round(x * factor)
		grid_y = round(y * factor)

		return [grid_x, grid_y]

	def convert_to_canvas(self, grid_x, grid_y):
		scale = 1200 / Grid.COLS
		return [grid_x * scale, grid_y * scale]

	def set_constraints(self, component, x, y, width=-1, height=-1):
		if isinstance(component, gtk.Window):
			self._layout_window(component, x, y, width, height)
		elif isinstance(component, goocanvas.Item):
			self._layout_item(component, x, y, width, height)
		elif isinstance(component, goocanvas.CanvasView):
			self._layout_canvas(component, x, y, width, height)

	def _layout_window(self, window, x, y, width, height):
		scale = gtk.gdk.screen_width() / Grid.COLS

		window.move(int(x * scale), int(y * scale))
		window.resize(int(width * scale), int(height * scale))

	def _layout_item(self, item, x, y, width, height):
		scale = 1200 / Grid.COLS

		self._allocate_item_position(item, x * scale, y * scale)
		if width > 0 and height > 0:
			self._allocate_item_size(item, width * scale, height * scale)

	# FIXME We really need layout support in goocanvas
	def _allocate_item_size(self, item, width, height):
		if isinstance(item, goocanvas.Rect):
			item.props.width = width - (item.props.line_width - 1) * 2
			item.props.height = height - (item.props.line_width - 1) * 2
		elif isinstance(item, goocanvas.Text):
			item.props.width = width
		elif isinstance(item, IconItem):
			item.props.size = width

	def _allocate_item_position(self, item, x, y):
		if isinstance(item, goocanvas.Rect):
			x = x + (item.props.line_width - 1)
			y = y + (item.props.line_width - 1)

		matrix = cairo.Matrix(1, 0, 0, 1, 0, 0)
		matrix.translate(x, y)
		item.set_transform(matrix)

	def _layout_canvas(self, canvas, x, y, width, height):
		scale = 1200 / Grid.COLS
		canvas.set_bounds(x * scale, y * scale, width * scale, height * scale)