From 916a11446e79744f8f095f5ef2e4d542d0853cba Mon Sep 17 00:00:00 2001 From: Arjan Molenaar Date: Fri, 22 May 2020 22:42:43 +0200 Subject: [PATCH] Let BoundingBoxPainter compose with ItemPainter Instead of inheriting from it. --- README.md | 2 +- examples/demo.py | 2 +- gaphas/painter.py | 28 ++++++++++++++++++---------- gaphas/view.py | 4 ++-- 4 files changed, 22 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index a60aa8f..643377f 100644 --- a/README.md +++ b/README.md @@ -428,7 +428,7 @@ cairo = CairoBoundingBoxContext(cairo) A type of ItemPainter which is used to calculate the bounding boxes (in canvas coordinates) for the items. ```python -view.bounding_box_painter = BoundingBoxPainter() +view.bounding_box_painter = BoundingBoxPainter(ItemPainter()) ``` #### Class: `gaphas.painter.HandlePainter` diff --git a/examples/demo.py b/examples/demo.py index 365323d..0a61940 100755 --- a/examples/demo.py +++ b/examples/demo.py @@ -120,7 +120,7 @@ def create_window(canvas, title, zoom=1.0): .append(FocusedItemPainter()) .append(ToolPainter()) ) - view.bounding_box_painter = FreeHandPainter(BoundingBoxPainter()) + view.bounding_box_painter = FreeHandPainter(BoundingBoxPainter(ItemPainter())) w = Gtk.Window() w.set_title(title) w.set_default_size(400, 120) diff --git a/gaphas/painter.py b/gaphas/painter.py index b020820..9c75c89 100644 --- a/gaphas/painter.py +++ b/gaphas/painter.py @@ -86,7 +86,7 @@ class DrawContext(Context): class ItemPainter(Painter): - def _draw_item(self, item, cairo, area=None): + def draw_item(self, item, cairo, area=None): view = self.view cairo.save() try: @@ -109,12 +109,12 @@ class ItemPainter(Painter): finally: cairo.restore() - def _draw_items(self, items, cairo, area=None): + def draw_items(self, items, cairo, area=None): """ Draw the items. """ for item in items: - self._draw_item(item, cairo, area=area) + self.draw_item(item, cairo, area=area) if DEBUG_DRAW_BOUNDING_BOX: self._draw_bounds(item, cairo) @@ -137,7 +137,7 @@ class ItemPainter(Painter): cairo = context.cairo cairo.set_tolerance(TOLERANCE) cairo.set_line_join(LINE_JOIN_ROUND) - self._draw_items(context.items, cairo, context.area) + self.draw_items(context.items, cairo, context.area) class CairoBoundingBoxContext: @@ -236,15 +236,23 @@ class CairoBoundingBoxContext: cr.show_text(utf8) -class BoundingBoxPainter(ItemPainter): +class BoundingBoxPainter(Painter): """ This specific case of an ItemPainter is used to calculate the bounding boxes (in canvas coordinates) for the items. """ - def _draw_item(self, item, cairo, area=None): + def __init__(self, item_painter, view=None): + super().__init__(view) + self.item_painter = item_painter + + def set_view(self, view): + super().set_view(view) + self.item_painter.set_view(view) + + def draw_item(self, item, cairo, area=None): cairo = CairoBoundingBoxContext(cairo) - super()._draw_item(item, cairo) + self.item_painter.draw_item(item, cairo) bounds = cairo.get_bounds() # Update bounding box with handles. @@ -257,15 +265,15 @@ class BoundingBoxPainter(ItemPainter): bounds.expand(1) view.set_item_bounding_box(item, bounds) - def _draw_items(self, items, cairo, area=None): + def draw_items(self, items, cairo, area=None): """ Draw the items. """ for item in items: - self._draw_item(item, cairo) + self.draw_item(item, cairo) def paint(self, context): - self._draw_items(context.items, context.cairo) + self.draw_items(context.items, context.cairo) class HandlePainter(Painter): diff --git a/gaphas/view.py b/gaphas/view.py index 87b7b9c..0b2367a 100644 --- a/gaphas/view.py +++ b/gaphas/view.py @@ -7,7 +7,7 @@ from gi.repository import Gdk, GLib, GObject, Gtk from gaphas.canvas import Context, instant_cairo_context from gaphas.decorators import AsyncIO, nonrecursive from gaphas.geometry import Rectangle, distance_point_point_fast -from gaphas.painter import BoundingBoxPainter, DefaultPainter +from gaphas.painter import BoundingBoxPainter, DefaultPainter, ItemPainter from gaphas.quadtree import Quadtree from gaphas.tool import DefaultTool @@ -27,7 +27,7 @@ class View: def __init__(self, canvas=None): self._matrix = cairo.Matrix() self._painter = DefaultPainter(self) - self._bounding_box_painter = BoundingBoxPainter(self) + self._bounding_box_painter = BoundingBoxPainter(ItemPainter(self), self) # Handling selections. # TODO: Move this to a context?