Skip to content

Commit

Permalink
fix: Fix parent attribute to point to proper magicgui widget parent (#…
Browse files Browse the repository at this point in the history
…583)

* fix: fix parent

* test: add one more
  • Loading branch information
tlambert03 authored Oct 4, 2023
1 parent c578b61 commit edf00f7
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
5 changes: 4 additions & 1 deletion src/magicgui/backends/_ipynb/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ def _mgui_set_enabled(self, enabled: bool):

def _mgui_get_parent(self):
# TODO: how does ipywidgets handle this?
return getattr(self._ipywidget, "parent", None)
# return getattr(self._ipywidget, "parent", None)
raise NotImplementedError(
"parent not implemented for ipywidgets backend. Please open an issue"
)

def _mgui_set_parent(self, widget):
# TODO: how does ipywidgets handle this?
Expand Down
16 changes: 13 additions & 3 deletions src/magicgui/backends/_qtpy/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

from magicgui.types import FileDialogMode
from magicgui.widgets import Widget, protocols
from magicgui.widgets._concrete import _LabeledWidget

if TYPE_CHECKING:
import numpy
Expand Down Expand Up @@ -82,9 +83,18 @@ def _mgui_get_enabled(self) -> bool:
def _mgui_set_enabled(self, enabled: bool) -> None:
self._qwidget.setEnabled(enabled)

# TODO: this used to return _magic_widget ... figure out what we should be returning
def _mgui_get_parent(self) -> QObject | None: # type: ignore
return self._qwidget.parent()
def _mgui_get_parent(self) -> Widget | None:
par = self._qwidget.parent()
# FIXME: This whole thing is hacky.
while par is not None:
mgui_wdg = getattr(par, "_magic_widget", None)
# the labeled widget itself should be considered a "hidden" layer.
if isinstance(mgui_wdg, Widget) and not isinstance(
mgui_wdg, _LabeledWidget
):
return mgui_wdg
par = par.parent()
return None

def _mgui_set_parent(self, widget: Widget) -> None:
self._qwidget.setParent(widget.native if widget else None)
Expand Down
11 changes: 11 additions & 0 deletions tests/test_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,14 @@ def _on_clicked(self):
...

assert isinstance(C(), widgets.Container)


def test_parent():
lbl = widgets.Label()
inner = widgets.Container(widgets=[lbl])
outer = widgets.Container()
outer.append(inner)

assert lbl.parent is inner
assert inner.parent is outer
assert outer.parent is None
2 changes: 1 addition & 1 deletion tests/test_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ def test_basic_widget_attributes():

assert widget.parent is None
container.append(widget)
assert widget.parent is container.native
assert widget.parent is container
widget.parent = None
assert widget.parent is None
assert widget.label == "my name"
Expand Down

0 comments on commit edf00f7

Please sign in to comment.