| 1 |
"""This module provides implementations of Cairo-specific vertex drawers, i.e. |
| 2 |
drawers that the Cairo graph drawer will use to draw vertices. |
| 3 |
""" |
| 4 |
|
| 5 |
from math import pi |
| 6 |
|
| 7 |
from igraph.drawing.baseclasses import AbstractVertexDrawer |
| 8 |
from igraph.drawing.metamagic import AttributeCollectorBase |
| 9 |
from igraph.drawing.shapes import ShapeDrawerDirectory |
| 10 |
|
| 11 |
from .base import AbstractCairoDrawer |
| 12 |
|
| 13 |
__all__ = ("AbstractCairoVertexDrawer", "CairoVertexDrawer") |
| 14 |
|
| 15 |
|
| 16 |
class AbstractCairoVertexDrawer(AbstractVertexDrawer, AbstractCairoDrawer): |
| 17 |
"""Abstract base class for vertex drawers that draw on a Cairo canvas.""" |
| 18 |
|
| 19 |
def __init__(self, context, bbox, palette, layout): |
| 20 |
"""Constructs the vertex drawer and associates it to the given |
| 21 |
Cairo context and the given L{BoundingBox}. |
| 22 |
|
| 23 |
@param context: the context on which we will draw |
| 24 |
@param bbox: the bounding box within which we will draw. |
| 25 |
Can be anything accepted by the constructor |
| 26 |
of L{BoundingBox} (i.e., a 2-tuple, a 4-tuple |
| 27 |
or a L{BoundingBox} object). |
| 28 |
@param palette: the palette that can be used to map integer |
| 29 |
color indices to colors when drawing vertices |
| 30 |
@param layout: the layout of the vertices in the graph being drawn |
| 31 |
""" |
| 32 |
AbstractCairoDrawer.__init__(self, context, bbox) |
| 33 |
AbstractVertexDrawer.__init__(self, palette, layout) |
| 34 |
|
| 35 |
|
| 36 |
class CairoVertexDrawer(AbstractCairoVertexDrawer): |
| 37 |
"""The default vertex drawer implementation of igraph.""" |
| 38 |
|
| 39 |
def __init__(self, context, bbox, palette, layout): |
| 40 |
super().__init__(context, bbox, palette, layout) |
| 41 |
self.VisualVertexBuilder = self._construct_visual_vertex_builder() |
| 42 |
|
| 43 |
def _construct_visual_vertex_builder(self): |
| 44 |
class VisualVertexBuilder(AttributeCollectorBase): |
| 45 |
"""Collects some visual properties of a vertex for drawing""" |
| 46 |
|
| 47 |
_kwds_prefix = "vertex_" |
| 48 |
color = ("red", self.palette.get) |
| 49 |
frame_color = ("black", self.palette.get) |
| 50 |
frame_width = 1.0 |
| 51 |
label = None |
| 52 |
label_angle = -pi / 2 |
| 53 |
label_dist = 0.0 |
| 54 |
label_color = ("black", self.palette.get) |
| 55 |
font = "sans-serif" |
| 56 |
label_size = 14.0 |
| 57 |
position = {"func": self.layout.__getitem__} |
| 58 |
shape = ("circle", ShapeDrawerDirectory.resolve_default) |
| 59 |
size = 20.0 |
| 60 |
width = None |
| 61 |
height = None |
| 62 |
|
| 63 |
return VisualVertexBuilder |
| 64 |
|
| 65 |
def draw(self, visual_vertex, vertex, coords): |
| 66 |
context = self.context |
| 67 |
|
| 68 |
width = ( |
| 69 |
visual_vertex.width |
| 70 |
if visual_vertex.width is not None |
| 71 |
else visual_vertex.size |
| 72 |
) |
| 73 |
height = ( |
| 74 |
visual_vertex.height |
| 75 |
if visual_vertex.height is not None |
| 76 |
else visual_vertex.size |
| 77 |
) |
| 78 |
|
| 79 |
visual_vertex.shape.draw_path(context, coords[0], coords[1], width, height) |
| 80 |
context.set_source_rgba(*visual_vertex.color) |
| 81 |
context.fill_preserve() |
| 82 |
context.set_source_rgba(*visual_vertex.frame_color) |
| 83 |
context.set_line_width(visual_vertex.frame_width) |
| 84 |
context.stroke() |