Skip to content

Commit

Permalink
Remove event deprecations strategy (for release 0.4.0) (#368)
Browse files Browse the repository at this point in the history
* remove event deprecations strategy (for release 0.4.0)

* catch deleted object

* try without itemdelegate

* try remove segfault

* try lambda in example

* try fix again

* revert change to print

* add timer
  • Loading branch information
tlambert03 committed Mar 18, 2022
1 parent 6b95690 commit 2749b2a
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 189 deletions.
71 changes: 33 additions & 38 deletions magicgui/backends/_qtpy/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import math
import re
from functools import partial
from typing import TYPE_CHECKING, Any, Iterable, Sequence

import qtpy
Expand Down Expand Up @@ -1184,22 +1185,21 @@ def _mgui_bind_column_headers_change_callback(self, callback) -> None:

def _mgui_bind_change_callback(self, callback):
"""Bind callback to event of changing any cell."""

def _item_callback(item, callback=callback):
col_head = item.tableWidget().horizontalHeaderItem(item.column())
col_head = col_head.text() if col_head is not None else ""
row_head = item.tableWidget().verticalHeaderItem(item.row())
row_head = row_head.text() if row_head is not None else ""
data = {
"data": item.data(self._DATA_ROLE),
"row": item.row(),
"column": item.column(),
"column_header": col_head,
"row_header": row_head,
}
callback(data)

self._qwidget.itemChanged.connect(_item_callback)
self._qwidget.itemChanged.connect(partial(self._item_callback, callback))

def _item_callback(self, callback, item: QtW.QTableWidgetItem):
col_head = item.tableWidget().horizontalHeaderItem(item.column())
col_head = col_head.text() if col_head is not None else ""
row_head = item.tableWidget().verticalHeaderItem(item.row())
row_head = row_head.text() if row_head is not None else ""
data = {
"data": item.data(self._DATA_ROLE),
"row": item.row(),
"column": item.column(),
"column_header": col_head,
"row_header": row_head,
}
callback(data)

# These are only here to implement the ValueWidget interface... but in this one
# case, all of the get/set value logic happens in magicgui.widgets.Table
Expand All @@ -1214,29 +1214,24 @@ def _mgui_set_value(self):
class _ItemDelegate(QtW.QStyledItemDelegate):
"""Displays table widget items with properly formatted numbers."""

def __init__(self, *args, ndigits: int = 4, **kwargs):
super().__init__(*args, **kwargs)
self.ndigits = ndigits

def displayText(self, value, locale):
value = self._format_number(value)
return super().displayText(value, locale)
return super().displayText(_format_number(value, 4), locale)

def _format_number(self, text: str) -> str:
"""convert string to int or float if possible"""

def _format_number(text: str, ndigits: int = 4) -> str:
"""convert string to int or float if possible"""
try:
value: int | float | None = int(text)
except ValueError:
try:
value: int | float | None = int(text)
value = float(text)
except ValueError:
try:
value = float(text)
except ValueError:
value = None

if isinstance(value, (int, float)):
dgt = self.ndigits
if 0.1 <= abs(value) < 10 ** (dgt + 1) or value == 0:
text = str(value) if isinstance(value, int) else f"{value:.{dgt}f}"
else:
text = f"{value:.{dgt-1}e}"

return text
value = None

if isinstance(value, (int, float)):
if 0.1 <= abs(value) < 10 ** (ndigits + 1) or value == 0:
text = str(value) if isinstance(value, int) else f"{value:.{ndigits}f}"
else:
text = f"{value:.{ndigits-1}e}"

return text
126 changes: 0 additions & 126 deletions magicgui/events.py

This file was deleted.

3 changes: 2 additions & 1 deletion magicgui/widgets/_bases/button_widget.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Optional

from magicgui.events import Signal, SignalInstance
from psygnal import Signal, SignalInstance

from magicgui.widgets import _protocols

from .value_widget import ValueWidget
Expand Down
3 changes: 2 additions & 1 deletion magicgui/widgets/_bases/container_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import inspect
from typing import TYPE_CHECKING, Any, Callable, MutableSequence, Sequence, overload

from psygnal import Signal

from magicgui._util import debounce
from magicgui.application import use_app
from magicgui.events import Signal
from magicgui.signature import MagicParameter, MagicSignature, magic_signature
from magicgui.widgets import _protocols
from magicgui.widgets._bases.mixins import _OrientationMixin
Expand Down
2 changes: 1 addition & 1 deletion magicgui/widgets/_bases/value_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

from typing import Any, Union

from psygnal import Signal
from typing_extensions import get_args, get_origin

from magicgui.events import Signal
from magicgui.widgets import _protocols

from .widget import Widget
Expand Down
3 changes: 2 additions & 1 deletion magicgui/widgets/_bases/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
from contextlib import contextmanager
from typing import TYPE_CHECKING, Any

from psygnal import Signal

from magicgui._type_wrapper import resolve_forward_refs
from magicgui.application import use_app
from magicgui.events import Signal
from magicgui.widgets import _protocols

BUILDING_DOCS = sys.argv[-2:] == ["build", "docs"]
Expand Down
3 changes: 2 additions & 1 deletion magicgui/widgets/_function_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@
cast,
)

from psygnal import Signal

from magicgui._type_wrapper import resolve_forward_refs
from magicgui.application import AppRef
from magicgui.events import Signal
from magicgui.signature import MagicSignature, magic_signature
from magicgui.widgets import Container, MainWindow, ProgressBar, PushButton
from magicgui.widgets._bases.value_widget import ValueWidget
Expand Down
16 changes: 0 additions & 16 deletions tests/test_deprecations.py

This file was deleted.

2 changes: 1 addition & 1 deletion tests/test_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def test_examples(fname):
pytest.mark.skip()
return
app = use_app()
app.start_timer(0, app.quit)
app.start_timer(50 if "table" in str(fname) else 5, app.quit)
try:
runpy.run_path(fname)
except ImportError as e:
Expand Down
5 changes: 2 additions & 3 deletions tests/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,9 +391,8 @@ def test_delete(qapp):


def test_item_delegate(qapp):
from magicgui.backends._qtpy.widgets import _ItemDelegate
from magicgui.backends._qtpy.widgets import _format_number

data = ["1.2", "1.23456789", "0.000123", "1234567", "0.0", "1", "s"]
idel = _ItemDelegate(ndigits=4)
results = [idel._format_number(v) for v in data]
results = [_format_number(v, 4) for v in data]
assert results == ["1.2000", "1.2346", "1.230e-04", "1.235e+06", "0.0000", "1", "s"]

0 comments on commit 2749b2a

Please sign in to comment.