/usr/share/pyshared/gaphas/painter.py is in python-gaphas 0.7.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 | """
The painter module provides different painters for parts of the canvas.
Painters can be swapped in and out.
Each painter takes care of a layer in the canvas (such as grid, items
and handles).
"""
__version__ = "$Revision$"
# $HeadURL$
from cairo import Matrix, ANTIALIAS_NONE, LINE_JOIN_ROUND
from gaphas.canvas import Context
from gaphas.geometry import Rectangle
from gaphas.item import Line
from gaphas.aspect import PaintFocused
DEBUG_DRAW_BOUNDING_BOX = False
# The tolerance for Cairo. Bigger values increase speed and reduce accuracy
# (default: 0.1)
TOLERANCE = 0.8
class Painter(object):
"""
Painter interface.
"""
def __init__(self, view=None):
self.view = view
def set_view(self, view):
self.view = view
def paint(self, context):
"""
Do the paint action (called from the View).
"""
pass
class PainterChain(Painter):
"""
Chain up a set of painters.
like ToolChain.
"""
def __init__(self, view=None):
super(PainterChain, self).__init__(view)
self._painters = []
def set_view(self, view):
self.view = view
for painter in self._painters:
painter.set_view(self.view)
def append(self, painter):
"""
Add a painter to the list of painters.
"""
self._painters.append(painter)
painter.set_view(self.view)
return self
def prepend(self, painter):
"""
Add a painter to the beginning of the list of painters.
"""
self._painters.insert(0, painter)
def paint(self, context):
"""
See Painter.paint().
"""
for painter in self._painters:
painter.paint(context)
class DrawContext(Context):
"""
Special context for draw()'ing the item. The draw-context contains
stuff like the cairo context and properties like selected and
focused.
"""
deprecated = False
def __init__(self, **kwargs):
super(DrawContext, self).__init__(**kwargs)
class ItemPainter(Painter):
draw_all = False
def _draw_item(self, item, cairo, area=None):
view = self.view
cairo.save()
try:
cairo.set_matrix(view.matrix)
cairo.transform(view.canvas.get_matrix_i2c(item))
item.draw(DrawContext(painter=self,
cairo=cairo,
_area=area,
_item=item,
selected=(item in view.selected_items),
focused=(item is view.focused_item),
hovered=(item is view.hovered_item),
dropzone=(item is view.dropzone_item),
draw_all=self.draw_all))
finally:
cairo.restore()
def _draw_items(self, items, cairo, area=None):
"""
Draw the items.
"""
for item in items:
#if not area or area - view.get_item_bounding_box(item):
self._draw_item(item, cairo, area=area)
if DEBUG_DRAW_BOUNDING_BOX:
self._draw_bounds(item, cairo)
def _draw_bounds(self, item, cairo):
view = self.view
try:
b = view.get_item_bounding_box(item)
except KeyError:
pass # No bounding box right now..
else:
cairo.save()
cairo.identity_matrix()
cairo.set_source_rgb(.8, 0, 0)
cairo.set_line_width(1.0)
cairo.rectangle(*b)
cairo.stroke()
cairo.restore()
def paint(self, context):
cairo = context.cairo
cairo.set_tolerance(TOLERANCE)
cairo.set_line_join(LINE_JOIN_ROUND)
self._draw_items(context.items, cairo, context.area)
class CairoBoundingBoxContext(object):
"""
Delegate all calls to the wrapped CairoBoundingBoxContext, intercept
``stroke()``, ``fill()`` and a few others so the bounding box of the
item involved can be calculated.
"""
def __init__(self, cairo):
self._cairo = cairo
self._bounds = None # a Rectangle object
def __getattr__(self, key):
return getattr(self._cairo, key)
def get_bounds(self):
"""
Return the bounding box.
"""
return self._bounds or Rectangle()
def _update_bounds(self, bounds):
if bounds:
if not self._bounds:
self._bounds = bounds
else:
self._bounds += bounds
def _extents(self, extents_func, line_width=False):
"""
Calculate the bounding box for a given drawing operation.
if ``line_width`` is True, the current line-width is taken into account.
"""
cr = self._cairo
cr.save()
cr.identity_matrix()
x0, y0, x1, y1 = extents_func()
b = Rectangle(x0, y0, x1=x1, y1=y1)
cr.restore()
if b and line_width:
# Do this after the restore(), so we can get the proper width.
lw = cr.get_line_width()/2
d = cr.user_to_device_distance(lw, lw)
b.expand(d[0]+d[1])
self._update_bounds(b)
return b
def fill(self, b=None):
"""
Interceptor for Cairo drawing method.
"""
cr = self._cairo
if not b:
b = self._extents(cr.fill_extents)
cr.fill()
def fill_preserve(self, b=None):
"""
Interceptor for Cairo drawing method.
"""
cr = self._cairo
if not b:
b = self._extents(cr.fill_extents)
def stroke(self, b=None):
"""
Interceptor for Cairo drawing method.
"""
cr = self._cairo
if not b:
b = self._extents(cr.stroke_extents, line_width=True)
cr.stroke()
def stroke_preserve(self, b=None):
"""
Interceptor for Cairo drawing method.
"""
cr = self._cairo
if not b:
b = self._extents(cr.stroke_extents, line_width=True)
def show_text(self, utf8, b=None):
"""
Interceptor for Cairo drawing method.
"""
cr = self._cairo
if not b:
x, y = cr.get_current_point()
e = cr.text_extents(utf8)
x0, y0 = cr.user_to_device(x+e[0], y+e[1])
x1, y1 = cr.user_to_device(x+e[0]+e[2], y+e[1]+e[3])
b = Rectangle(x0, y0, x1=x1, y1=y1)
self._update_bounds(b)
cr.show_text(utf8)
class BoundingBoxPainter(ItemPainter):
"""
This specific case of an ItemPainter is used to calculate the bounding
boxes (in canvas coordinates) for the items.
"""
draw_all = True
def _draw_item(self, item, cairo, area=None):
cairo = CairoBoundingBoxContext(cairo)
super(BoundingBoxPainter, self)._draw_item(item, cairo)
bounds = cairo.get_bounds()
# Update bounding box with handles.
view = self.view
i2v = view.get_matrix_i2v(item).transform_point
for h in item.handles():
cx, cy = i2v(*h.pos)
bounds += (cx - 5, cy - 5, 9, 9)
bounds.expand(1)
view.set_item_bounding_box(item, bounds)
def _draw_items(self, items, cairo, area=None):
"""
Draw the items.
"""
for item in items:
self._draw_item(item, cairo)
def paint(self, context):
self._draw_items(context.items, context.cairo)
class HandlePainter(Painter):
"""
Draw handles of items that are marked as selected in the view.
"""
def _draw_handles(self, item, cairo, opacity=None, inner=False):
"""
Draw handles for an item.
The handles are drawn in non-antialiased mode for clearity.
"""
view = self.view
cairo.save()
i2v = view.get_matrix_i2v(item)
if not opacity:
opacity = (item is view.focused_item) and .7 or .4
cairo.set_line_width(1)
get_connection = view.canvas.get_connection
for h in item.handles():
if not h.visible:
continue
# connected and not being moved, see HandleTool.on_button_press
if get_connection(h):
r, g, b = 1, 0, 0
# connected but being moved, see HandleTool.on_button_press
elif get_connection(h):
r, g, b = 1, 0.6, 0
elif h.movable:
r, g, b = 0, 1, 0
else:
r, g, b = 0, 0, 1
cairo.identity_matrix()
cairo.set_antialias(ANTIALIAS_NONE)
cairo.translate(*i2v.transform_point(*h.pos))
cairo.rectangle(-4, -4, 8, 8)
if inner:
cairo.rectangle(-3, -3, 6, 6)
cairo.set_source_rgba(r, g, b, opacity)
cairo.fill_preserve()
if h.connectable:
cairo.move_to(-2, -2)
cairo.line_to(2, 3)
cairo.move_to(2, -2)
cairo.line_to(-2, 3)
cairo.set_source_rgba(r/4., g/4., b/4., opacity*1.3)
cairo.stroke()
cairo.restore()
def paint(self, context):
view = self.view
canvas = view.canvas
cairo = context.cairo
# Order matters here:
for item in canvas.sort(view.selected_items):
self._draw_handles(item, cairo)
# Draw nice opaque handles when hovering an item:
item = view.hovered_item
if item and item not in view.selected_items:
self._draw_handles(item, cairo, opacity=.25)
item = view.dropzone_item
if item and item not in view.selected_items:
self._draw_handles(item, cairo, opacity=.25, inner=True)
class ToolPainter(Painter):
"""
ToolPainter allows the Tool defined on a view to do some special
drawing.
"""
def paint(self, context):
view = self.view
cairo = context.cairo
if view.tool:
cairo.save()
cairo.identity_matrix()
view.tool.draw(context)
cairo.restore()
class FocusedItemPainter(Painter):
"""
This painter allows for drawing on top off all other layers for the
focused item.
"""
def paint(self, context):
view = self.view
item = view.hovered_item
if item and item is view.focused_item:
PaintFocused(item, view).paint(context)
def DefaultPainter(view=None):
"""
Default painter, containing item, handle and tool painters.
"""
return PainterChain(view). \
append(ItemPainter()). \
append(HandlePainter()). \
append(FocusedItemPainter()). \
append(ToolPainter())
# vim: sw=4:et:ai
|