Skip to content

Commit

Permalink
Add scrollable widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
dstansby committed Mar 18, 2022
1 parent 3e3f046 commit 3e59fe9
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
16 changes: 16 additions & 0 deletions magicgui/backends/_qtpy/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,22 @@ def __init__(self, layout="vertical"):
self._layout = QtW.QVBoxLayout()
self._qwidget.setLayout(self._layout)

def _mgui_set_scrollable(self, scrollable: bool):
if scrollable == self._mgui_get_scrollable():
return

if scrollable:
self._scroll = QtW.QScrollArea()
self._scroll.setWidget(self._qwidget)
self._scroll.setWidgetResizable(False)
self._qwidget = self._scroll
if not scrollable:
self._qwidget = self._scroll.widget()
del self._scroll

def _mgui_get_scrollable(self):
return hasattr(self, "_scroll")

def _mgui_insert_widget(self, position: int, widget: Widget):
self._layout.insertWidget(position, widget.native)

Expand Down
9 changes: 9 additions & 0 deletions magicgui/widgets/_bases/container_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,15 @@ def layout(self, value):
"It is not yet possible to change layout after instantiation"
)

@property
def scrollable(self) -> bool:
"""Return if the widget can have scroll bars."""
return self._widget._mgui_get_scrollable()

@scrollable.setter
def scrollable(self, value: bool):
self._widget._mgui_set_scrollable(value)

def reset_choices(self, *_: Any):
"""Reset choices for all Categorical subWidgets to the default state.
Expand Down
8 changes: 8 additions & 0 deletions magicgui/widgets/_protocols.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,14 @@ def _mgui_get_margins(self) -> tuple[int, int, int, int]:
def _mgui_set_margins(self, margins: tuple[int, int, int, int]) -> None:
raise NotImplementedError()

@abstractmethod
def _mgui_get_scrollable(self) -> bool:
raise NotImplementedError()

@abstractmethod
def _mgui_set_scrollable(self, scrollable: bool) -> None:
raise NotImplementedError()


class MainWindowProtocol(ContainerProtocol, Protocol):
"""Application main widget."""
Expand Down

0 comments on commit 3e59fe9

Please sign in to comment.