Speed up text drawing by caching the parameters
[pysize.git] / pysize / ui / gtk / pysize_widget_draw.py
blobbabf1abf5865418a83f412d622a6a7efb750ea05
1 # This program is free software; you can redistribute it and/or modify
2 # it under the terms of the GNU General Public License as published by
3 # the Free Software Foundation; either version 2 of the License, or
4 # (at your option) any later version.
6 # This program is distributed in the hope that it will be useful,
7 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 # GNU Library General Public License for more details.
11 # You should have received a copy of the GNU General Public License
12 # along with this program; if not, write to the Free Software
13 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15 # See the COPYING file for license information.
17 # Copyright (c) 2006, 2007 Guillaume Chazarain <guichaz@yahoo.fr>
19 import pygtk
20 pygtk.require('2.0')
21 import gtk
22 assert gtk.pygtk_version >= (2, 8)
23 import pango
24 import math
25 import cairo
27 from pysize.ui.utils import human_unit, min_size_to_consider, sanitize_string
29 RADIUS = 10
30 LINE_WIDTH = 4
32 class PysizeWidget_Draw(object):
33 def __init__(self, options, args):
34 self.connect('expose-event', type(self)._expose_event)
35 self.modify_font(pango.FontDescription('Monospace 12'))
36 self.max_text_height = self.measure_font_height()
37 self.fast = options.fast
39 def measure_font_height(self):
40 w, h = self.create_pango_layout('a').get_pixel_size()
41 return h
43 def _get_requested_height(self):
44 return self.max_text_height * self.tree.root.size / \
45 min_size_to_consider(self.options.min_size)
47 def queue_node_redraw(self, node):
48 if node:
49 x0, x1, y0, y1 = map(int, node.rectangle)
50 self.queue_draw_area(x0 - LINE_WIDTH, y0 - LINE_WIDTH,
51 x1 - x0 + 2*LINE_WIDTH, y1 - y0 + 2*LINE_WIDTH)
53 def _make_draw_labels_lambda(self, context, text, (x0, x1, y0, y1),
54 accept_ellipse=True):
55 pl = self.create_pango_layout(text)
56 pl.set_alignment(pango.ALIGN_CENTER)
57 w = x1 - x0
58 h = y1 - y0
59 pl.set_width(int(w*pango.SCALE))
60 if accept_ellipse:
61 ellipse_mode = pango.ELLIPSIZE_MIDDLE
62 else:
63 ellipse_mode = pango.ELLIPSIZE_NONE
64 pl.set_ellipsize(ellipse_mode)
65 real_w, real_h = pl.get_pixel_size()
66 line_count = pl.get_line_count()
67 line_height = float(real_h) / line_count
68 if line_height > self.max_text_height:
69 self.max_text_height = line_height
70 if line_count == text.count('\n') + 1 and real_w <= w and real_h <= h:
71 y0 += (h - real_h) / 2.0
72 def draw(context):
73 context.move_to(x0, y0)
74 context.show_layout(pl)
75 return draw
77 def _get_node_colors(self, node, is_selected, size_color, colors):
78 def transform(colors, dr, dg, db):
79 def clamp(c):
80 return max(0, min(1, c))
81 return map(lambda (r, g, b): (clamp(r + dr),
82 clamp(g + dg),
83 clamp(b + db)),
84 colors)
86 if node.is_real() and not node.is_dir():
87 colors = map(lambda (r, g, b): (b, g, r), colors)
89 if node.is_real():
90 size_delta = size_color(node.size)
91 colors = transform(colors, size_delta, size_delta, size_delta)
92 else:
93 colors = map(lambda c: (min(c), max(c), max(c)), colors)
95 if node == self.cursor_node:
96 colors = transform(colors, 0.2, 0.2, 0.2)
97 if is_selected:
98 colors = transform(colors, -0.4, -0.4, -0.4)
99 return colors
101 def _draw_box(self, context, x0, x1, y0, y1, node, size_color):
102 if x0 == 0.0:
103 x0 += LINE_WIDTH/2.0
104 else:
105 x0 += LINE_WIDTH/4.0
106 x1 -= LINE_WIDTH/4.0
107 if y0 == 0.0:
108 y0 += LINE_WIDTH/2.0
109 else:
110 y0 += LINE_WIDTH/4.0
111 y1 -= LINE_WIDTH/4.0
112 node.rectangle = x0, x1, y0, y1
113 is_selected = set(node.get_fullpaths()) <= self.selected_paths
114 colors = self._get_node_colors(node, is_selected, size_color,
115 ((0.5, 0.4, 1.0), (0.2, 0.4, 1.0)))
116 context.set_source_rgb(0, 0, 0)
117 if self.fast:
118 context.rectangle(x0, y0, x1 - x0, y1 - y0)
119 context.stroke_preserve()
121 context.set_source_rgb(*colors[0])
122 context.fill()
123 else:
124 context.new_path()
125 context.arc(x0 + RADIUS, y0 + RADIUS, RADIUS,
126 - math.pi, - math.pi / 2.0)
127 context.rel_line_to(x1 - x0 - 2*RADIUS, 0)
128 context.arc(x1 - RADIUS, y0 + RADIUS, RADIUS,
129 - math.pi / 2.0, 0)
130 context.rel_line_to(0, y1 - y0 - 2*RADIUS)
131 context.arc(x1 - RADIUS, y1 - RADIUS, RADIUS,
132 0, math.pi / 2.0)
133 context.rel_line_to(- x1 + x0 + 2*RADIUS, 0)
134 context.arc(x0 + RADIUS, y1 - RADIUS, RADIUS,
135 math.pi / 2.0, math.pi)
136 context.close_path()
137 context.stroke_preserve()
139 gradient = cairo.LinearGradient(0, y0, 0, y1)
141 gradient.add_color_stop_rgb(0.0, *colors[0])
142 gradient.add_color_stop_rgb(1.0, *colors[1])
143 context.set_source(gradient)
144 context.fill()
146 if is_selected:
147 context.set_source_rgb(1, 1, 1)
148 else:
149 context.set_source_rgb(0, 0, 0)
150 if not hasattr(node, 'draw_labels_lambda'):
151 name = sanitize_string(node.get_name())
152 size = human_unit(node.size)
153 position = x0, x1, y0, y1
154 attempt = lambda text, pos, *flags: \
155 self._make_draw_labels_lambda(context, text, pos, *flags) or \
156 self._make_draw_labels_lambda(context, text,
157 (pos[0] - 1, pos[1] + 1, pos[2] - 1, pos[3] + 1),
158 *flags)
159 node.draw_labels_lambda = attempt(name + '\n' + size, position) or \
160 attempt(name + ' ' + size, position, False) or \
161 attempt(name, position) or \
162 attempt(size, position)
163 if node.draw_labels_lambda:
164 node.draw_labels_lambda(context)
166 def _draw_boxes(self, context, node, depth, offset, size_color):
167 w = self.allocation.width
168 h = self.allocation.height
169 x0 = depth * (w - 1.0) / (self.tree.height or 1)
170 x1 = (depth + 1.0) * (w - 1.0) / (self.tree.height or 1)
171 y0 = (h - 1.0) * offset / self.tree.root.size
172 y1 = (h - 1.0) * (offset + node.size) / self.tree.root.size
174 self._draw_box(context, x0, x1, y0, y1, node, size_color)
175 depth += 1
176 for child in node.children:
177 self._draw_boxes(context, child, depth, offset, size_color)
178 offset += child.size
180 def _draw(self, context):
181 max_text_height_before = self.max_text_height
182 if self.tree.root.children:
183 max_size = self.tree.root.children[0].size
184 min_size = self.tree.root.minimum_node_size()
185 diff = max(1, max_size - min_size)
186 def size_color(size):
187 return 0.3 - (size - min_size) / (2.0 * diff)
188 else:
189 def size_color(size):
190 return 0
191 context.set_line_width(LINE_WIDTH)
192 offset = 0
193 for child in self.tree.root.children or [self.tree.root]:
194 if child.size:
195 self._draw_boxes(context, child, 0, offset, size_color)
196 offset += child.size
197 if self.max_text_height != max_text_height_before:
198 self.schedule_new_tree()
200 def _expose_event(self, event):
201 context = self.window.cairo_create()
203 # set a clip region for the expose event
204 context.rectangle(event.area.x, event.area.y,
205 event.area.width, event.area.height)
206 context.clip()
208 self._draw(context)
209 return False
211 def max_number_of_nodes(self):
212 return max(2, self.allocation.height / self.max_text_height)
214 def _get_actual_min_size(self):
215 min_size = self.options.min_size
216 if min_size == 'auto':
217 min_size = self.tree.root.size * self.min_size_requested()
218 return int(min_size)
220 def _zoom(self, func):
221 min_size = self._get_actual_min_size()
222 self.options.min_size = func(min_size)
223 self.schedule_new_tree()
225 def zoom_fit(self):
226 self._zoom(lambda min_size: 'auto')
228 def zoom_in(self):
229 self._zoom(lambda min_size: str(int(min_size / 1.5)))
231 def zoom_out(self):
232 self._zoom(lambda min_size: str(int(min_size * 1.5)))