Let BoundingBoxPainter compose with ItemPainter

Instead of inheriting from it.
This commit is contained in:
Arjan Molenaar 2020-05-22 22:42:43 +02:00
parent 2d369f7815
commit 916a11446e
4 changed files with 22 additions and 14 deletions

View File

@ -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`

View File

@ -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)

View File

@ -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):

View File

@ -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?