From 8c558251f1902f65f22bd51d352e0ce5dd0de053 Mon Sep 17 00:00:00 2001 From: Qin Yu Date: Thu, 14 Mar 2024 16:32:59 +0100 Subject: [PATCH] Add Separator to ComboBox --- src/magicgui/backends/_qtpy/widgets.py | 10 +++++++--- src/magicgui/types.py | 6 ++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/magicgui/backends/_qtpy/widgets.py b/src/magicgui/backends/_qtpy/widgets.py index 7decaef0d..eac13e6ec 100644 --- a/src/magicgui/backends/_qtpy/widgets.py +++ b/src/magicgui/backends/_qtpy/widgets.py @@ -26,7 +26,7 @@ QTextDocument, ) -from magicgui.types import FileDialogMode +from magicgui.types import FileDialogMode, Separator from magicgui.widgets import Widget, protocols from magicgui.widgets._concrete import _LabeledWidget @@ -952,9 +952,13 @@ def _mgui_set_choices(self, choices: Iterable[tuple[str, Any]]) -> None: for i in reversed(range(self._qwidget.count())): if self._qwidget.itemText(i) not in choice_names: self._qwidget.removeItem(i) - # update choices + # update choices and insert separators for name, data in choices_: - self._mgui_set_choice(name, data) + if isinstance(data, Separator): + for _ in range(data.thickness): + self._qwidget.insertSeparator(self._mgui_get_count()) + else: + self._mgui_set_choice(name, data) # if the currently selected item is not in the new set, # remove it and select the first item in the list diff --git a/src/magicgui/types.py b/src/magicgui/types.py index d00b7fb56..625fde632 100644 --- a/src/magicgui/types.py +++ b/src/magicgui/types.py @@ -81,6 +81,12 @@ def __bool__(self) -> bool: Undefined = _Undefined() +class Separator: + """Separator sentinel for ComboBox choices.""" + def __init__(self, thickness: int = 1): + self.thickness: int = thickness + + JsonStringFormats = Literal[ # ISO 8601 format. # https://www.iso.org/iso-8601-date-and-time-format.html