Skip to content
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

feat: add quantity widget #483

Merged
merged 6 commits into from Nov 9, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions examples/pint_quantity.py
@@ -0,0 +1,11 @@
from pint import Quantity

from magicgui import magicgui


@magicgui
def widget(q=Quantity("1 ms")):
print(q)


widget.show(run=True)
2 changes: 2 additions & 0 deletions magicgui/backends/_qtpy/__init__.py
Expand Up @@ -17,6 +17,7 @@
MainWindow,
ProgressBar,
PushButton,
QuantityEdit,
RadioButton,
RadioButtons,
RangeSlider,
Expand Down Expand Up @@ -50,6 +51,7 @@
"MainWindow",
"ProgressBar",
"PushButton",
"QuantityEdit",
"RadioButton",
"RadioButtons",
"RangeSlider",
Expand Down
9 changes: 9 additions & 0 deletions magicgui/backends/_qtpy/widgets.py
Expand Up @@ -277,6 +277,15 @@ def _mgui_set_value(self, val: np.ndarray) -> None:
self._rescale()


class QuantityEdit(QBaseValueWidget):
_qwidget: superqt.QQuantity

def __init__(self, **kwargs):
super().__init__(
superqt.QQuantity, "value", "setValue", "valueChanged", **kwargs
)


class LineEdit(QBaseStringWidget):
_qwidget: QtW.QLineEdit

Expand Down
5 changes: 5 additions & 0 deletions magicgui/type_map.py
Expand Up @@ -4,6 +4,7 @@
import datetime
import inspect
import pathlib
import sys
import types
import warnings
from collections import defaultdict
Expand Down Expand Up @@ -123,6 +124,10 @@ def match_type(type_: Any, default: Any = None) -> WidgetTuple | None:
if get_origin(arg) is Literal:
return widgets.Select, {"choices": get_args(arg)}

pint = sys.modules.get("pint")
if pint and _is_subclass(origin, pint.Quantity):
return widgets.QuantityEdit, {}

return None


Expand Down
2 changes: 2 additions & 0 deletions magicgui/widgets/__init__.py
Expand Up @@ -32,6 +32,7 @@
MainWindow,
ProgressBar,
PushButton,
QuantityEdit,
RadioButton,
RadioButtons,
RangeEdit,
Expand Down Expand Up @@ -92,6 +93,7 @@
"MainWindow",
"PushButton",
"ProgressBar",
"QuantityEdit",
"RadioButton",
"RadioButtons",
"RangeEdit",
Expand Down
5 changes: 5 additions & 0 deletions magicgui/widgets/_concrete.py
Expand Up @@ -1041,3 +1041,8 @@ def label_width(self):
@label_width.setter
def label_width(self, width):
self._label_widget.min_width = width


@backend_widget
class QuantityEdit(ValueWidget):
"""A combined LineEdit and ComboBox to edit a pint.Quantity."""
5 changes: 4 additions & 1 deletion setup.cfg
Expand Up @@ -45,7 +45,7 @@ install_requires =
docstring_parser>=0.7
psygnal>=0.3.1
qtpy>=1.7.0
superqt>=0.3.6
superqt>=0.4.0
typing_extensions

[options.packages.find]
Expand Down Expand Up @@ -73,6 +73,8 @@ jupyter =
ipywidgets>=7.5.0
image =
pillow>=4.0
quantity =
pint>=0.13.0
testing =
tox
pytest
Expand All @@ -83,6 +85,7 @@ testing =
pandas
%(tqdm)s
%(image)s
%(quantity)s
matplotlib
toolz
ipywidgets
Expand Down