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 d49f2c2
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
18 changes: 18 additions & 0 deletions magicgui/backends/_qtpy/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,24 @@ def __init__(self, layout="vertical"):
else:
self._layout = QtW.QVBoxLayout()
self._qwidget.setLayout(self._layout)
# Create a scroll widget, but don't use it initially
self._scroll = QtW.QScrollArea()
# Allow widget to resize when window is larger than min widget size
self._scroll.setWidgetResizable(True)

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

if scrollable:
# Transfer widget from MainWindow to ScrollArea
self._scroll.setWidget(self._qwidget)
self._qwidget = self._scroll
if not scrollable:
self._qwidget = self._scroll.takeWidget()

def _mgui_get_scrollable(self):
return self._scroll.widget() is not None

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 d49f2c2

Please sign in to comment.