fix: fix signal connection between GUI and guiclass instance
Fixes #690
This PR implements TwoWaySetAttr class for synchronization between widget and guiclass to safely handle the following problem.
The tricky part was that how many times signals will be emitted depends on the implementation of how values are set to the magicgui widget and to the backend widget.
Because Qt spin box widget does not emit Qt signal when the same value was set, the signal chain is like this:
-
obj.gui.x.valuechanged - emit value-changed signal (Qt)
- emit value-changed signal (psygnal)
- update
obj.x - emit
obj.events.x - update
obj.gui.x.value - does not emit value-changed signal (Qt) because same value was set
However, because the signal emissions of ListEdit is handled on the magicgui side, the signal chain becomes like this:
-
obj.gui.x.valuechanged ← first time - emit value-changed signal (psygnal)
- update
obj.x - emit
obj.events.x - update
obj.gui.x.value -
obj.gui.x.valuechanged ← second time - emit value-changed signal (psygnal)
- update
obj.x - does not emit
obj.events.xbecause same value was set, thanks to the implementation ofpsygnal.evented
(We can consider 6. as a bug of ListEdit: it emits signal when the same value was set. In my opinion, this implementation should be left as is because checking values such as list[np.ndarray] is time consuming ... but let's leave this in other issues if we need more discussion)
Even if we carefully implement all the set_value methods, we may still have problems if a set-value method of some GUI library emits signal regardless of whether the old and new values are the same. The safest solution I think is to connect callbacks that can stop recursive two-way emission by itself.
Codecov Report
Attention: Patch coverage is 95.65217% with 2 lines in your changes missing coverage. Please review.
Project coverage is 88.75%. Comparing base (
69c2808) to head (e68054e).
| Files with missing lines | Patch % | Lines |
|---|---|---|
| src/magicgui/schema/_guiclass.py | 95.12% | 2 Missing :warning: |
Additional details and impacted files
@@ Coverage Diff @@
## main #693 +/- ##
==========================================
- Coverage 88.85% 88.75% -0.10%
==========================================
Files 40 40
Lines 4809 4847 +38
==========================================
+ Hits 4273 4302 +29
- Misses 536 545 +9
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.
🚀 New features to boost your workflow:
- ❄ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.