Skip to content

auto mixin class for generating add_<graphic> methods #248

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 6 additions & 28 deletions fastplotlib/layouts/_subplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@
)
from wgpu.gui.auto import WgpuCanvas

from .. import graphics
from ._utils import make_canvas_and_renderer
from ._base import PlotArea
from ..graphics import TextGraphic
from ._utils import make_canvas_and_renderer
from ._base import PlotArea
from ._defaults import create_camera, create_controller
from .graphic_methods_mixin import GraphicMethodsMixin


class Subplot(PlotArea):
class Subplot(PlotArea, GraphicMethodsMixin):
def __init__(
self,
position: Tuple[int, int] = None,
Expand Down Expand Up @@ -70,6 +72,8 @@ def __init__(

"""

super(GraphicMethodsMixin, self).__init__()

canvas, renderer = make_canvas_and_renderer(canvas, renderer)

if position is None:
Expand Down Expand Up @@ -113,36 +117,10 @@ def __init__(
self.docked_viewports[pos] = dv
self.children.append(dv)

# attach all the add_<graphic_name> methods
for graphic_cls_name in graphics.__all__:
cls = getattr(graphics, graphic_cls_name)

pfunc = partial(self._create_graphic, cls)
pfunc.__signature__ = signature(cls)
pfunc.__doc__ = cls.__init__.__doc__

# cls.type is defined in Graphic.__init_subclass__
setattr(self, f"add_{cls.type}", pfunc)

self._title_graphic: TextGraphic = None
if self.name is not None:
self.set_title(self.name)

def _create_graphic(self, graphic_class, *args, **kwargs) -> weakref.proxy:
if "center" in kwargs.keys():
center = kwargs.pop("center")
else:
center = False

if "name" in kwargs.keys():
self._check_graphic_name_exists(kwargs["name"])

graphic = graphic_class(*args, **kwargs)
self.add_graphic(graphic, center=center)

# only return a proxy to the real graphic
return weakref.proxy(graphic)

@property
def name(self) -> Any:
return self._name
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy