| 1 |
from math import pi |
| 2 |
from typing import Tuple, Union |
| 3 |
|
| 4 |
from igraph.drawing.baseclasses import AbstractDrawer |
| 5 |
from igraph.drawing.utils import BoundingBox |
| 6 |
|
| 7 |
__all__ = ("AbstractCairoDrawer",) |
| 8 |
|
| 9 |
|
| 10 |
class AbstractCairoDrawer(AbstractDrawer): |
| 11 |
"""Abstract class that serves as a base class for anything that |
| 12 |
draws on a Cairo context within a given bounding box. |
| 13 |
|
| 14 |
A subclass of L{AbstractCairoDrawer} is guaranteed to have an |
| 15 |
attribute named C{context} that represents the Cairo context |
| 16 |
to draw on, and an attribute named C{bbox} for the L{BoundingBox} |
| 17 |
of the drawing area. |
| 18 |
""" |
| 19 |
|
| 20 |
_bbox: BoundingBox |
| 21 |
|
| 22 |
def __init__(self, context, bbox: BoundingBox or None): |
| 23 |
"""Constructs the drawer and associates it to the given |
| 24 |
Cairo context and the given L{BoundingBox}. |
| 25 |
|
| 26 |
@param context: the context on which we will draw |
| 27 |
@param bbox: the bounding box within which we will draw. |
| 28 |
Can be anything accepted by the constructor |
| 29 |
of L{BoundingBox} (i.e., a 2-tuple, a 4-tuple |
| 30 |
or a L{BoundingBox} object). |
| 31 |
""" |
| 32 |
self.context = context |
| 33 |
self._bbox = None # type: ignore |
| 34 |
# can be set at drawing time |
| 35 |
if bbox is not None: |
| 36 |
self.bbox = bbox |
| 37 |
|
| 38 |
@property |
| 39 |
def bbox(self) -> BoundingBox: |
| 40 |
"""The bounding box of the drawing area where this drawer will |
| 41 |
draw.""" |
| 42 |
return self._bbox |
| 43 |
|
| 44 |
@bbox.setter |
| 45 |
def bbox(self, bbox): |
| 46 |
"""Sets the bounding box of the drawing area where this drawer |
| 47 |
will draw.""" |
| 48 |
if not isinstance(bbox, BoundingBox): |
| 49 |
self._bbox = BoundingBox(bbox) |
| 50 |
else: |
| 51 |
self._bbox = bbox |
| 52 |
|
| 53 |
def _mark_point( |
| 54 |
self, |
| 55 |
x: float, |
| 56 |
y: float, |
| 57 |
color: Union[int, Tuple[float, ...]] = 0, |
| 58 |
size: float = 4, |
| 59 |
) -> None: |
| 60 |
"""Marks the given point with a small circle on the canvas. |
| 61 |
Used primarily for debugging purposes. |
| 62 |
|
| 63 |
@param x: the X coordinate of the point to mark |
| 64 |
@param y: the Y coordinate of the point to mark |
| 65 |
@param color: the color of the marker. It can be a |
| 66 |
3-tuple (RGB components, alpha=0.5), a 4-tuple |
| 67 |
(RGBA components) or an index where zero means red, 1 means |
| 68 |
green, 2 means blue and so on. |
| 69 |
@param size: the diameter of the marker. |
| 70 |
""" |
| 71 |
if isinstance(color, int): |
| 72 |
colors = [(1, 0, 0), (0, 1, 0), (0, 0, 1), (1, 1, 0), (0, 1, 1), (1, 0, 1)] |
| 73 |
color_tuple = colors[color % len(colors)] |
| 74 |
elif len(color) == 3: |
| 75 |
color_tuple = color + (0.5,) |
| 76 |
else: |
| 77 |
color_tuple = color |
| 78 |
|
| 79 |
ctx = self.context |
| 80 |
ctx.save() |
| 81 |
ctx.set_source_rgba(*color_tuple) |
| 82 |
ctx.arc(x, y, size / 2.0, 0, 2 * pi) |
| 83 |
ctx.fill() |
| 84 |
ctx.restore() |