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

Regenerate background when RectangleSelector active-flag is set back on. #17480

Merged
merged 1 commit into from Oct 2, 2020

Conversation

anntzer
Copy link
Contributor

@anntzer anntzer commented May 21, 2020

Otherwise, if the selector is made inactive and then back to active, the
"old" selector gets captured in the background image and spurious
appears.

No tests because good luck with that, but can be tried with
examples/widgets/rectangle_selector.py.

Closes #17476.

PR Summary

PR Checklist

  • Has Pytest style unit tests
  • Code is Flake 8 compliant
  • New features are documented, with examples if plot related
  • Documentation is sphinx and numpydoc compliant
  • Added an entry to doc/users/next_whats_new/ if major new feature (follow instructions in README.rst there)
  • Documented in doc/api/api_changes.rst if API changed in a backward-incompatible way

@QuLogic
Copy link
Member

QuLogic commented May 21, 2020

Is this only needed for RectangleSelector?

@QuLogic
Copy link
Member

QuLogic commented May 21, 2020

And if so, maybe this should override set_active instead?

@anntzer
Copy link
Contributor Author

anntzer commented May 21, 2020

set_active calls update_background, so that's fine?
Haven't checked the other widgets.

@QuLogic
Copy link
Member

QuLogic commented Oct 2, 2020

EllipseSelector derives from RectangleSelector so it'll be fine; the others don't appear to be problematic.

@QuLogic
Copy link
Member

QuLogic commented Oct 2, 2020

Wait, I forgot to set useblit=True; this does affect PolygonSelector, which doesn't derive from RectangleSelector.

Also affects SpanSelector if you toggle it in the middle of selecting something.

@anntzer
Copy link
Contributor Author

anntzer commented Oct 2, 2020

Do you have an example where this can easily be checked? Is that just a matter of moving the update_background implementation fully to the base class?

@QuLogic
Copy link
Member

QuLogic commented Oct 2, 2020

I essentially patched the examples to do what RectangleSelector did:

diff --git a/examples/widgets/polygon_selector_demo.py b/examples/widgets/polygon_selector_demo.py
index c1340679f3..68de0e0e9a 100644
--- a/examples/widgets/polygon_selector_demo.py
+++ b/examples/widgets/polygon_selector_demo.py
@@ -50,9 +50,17 @@ class SelectFromCollection:
         elif len(self.fc) == 1:
             self.fc = np.tile(self.fc, (self.Npts, 1))
 
-        self.poly = PolygonSelector(ax, self.onselect)
+        self.poly = PolygonSelector(ax, self.onselect, useblit=False)
         self.ind = []
 
+        self.cid = self.canvas.mpl_connect('key_press_event',
+                                           self.toggle_selector)
+
+    def toggle_selector(self, event):
+        if event.key == 't':
+            self.poly.set_active(not self.poly.active)
+            print('Selector toggled')
+
     def onselect(self, verts):
         path = Path(verts)
         self.ind = np.nonzero(path.contains_points(self.xys))[0]
diff --git a/examples/widgets/span_selector.py b/examples/widgets/span_selector.py
index 2e720f4c98..3955c8f540 100644
--- a/examples/widgets/span_selector.py
+++ b/examples/widgets/span_selector.py
@@ -46,9 +46,17 @@ def onselect(xmin, xmax):
 #
 
 
+def toggle_selector(event):
+    if event.key == 't':
+        span.set_active(not span.active)
+        print('Span toggled')
+
+
 span = SpanSelector(ax1, onselect, 'horizontal', useblit=True,
                     rectprops=dict(alpha=0.5, facecolor='red'))
 # Set useblit=True on most backends for enhanced performance.
 
+fig.canvas.mpl_connect('key_press_event', toggle_selector)
+
 
 plt.show()

@anntzer
Copy link
Contributor Author

anntzer commented Oct 2, 2020

OK, moving everything to the base class seems to work.

Otherwise, if the selector is made inactive and then back to active, the
"old" selector gets captured in the background image and spurious
appears.

No tests because good luck with that, but can be tried with
examples/widgets/rectangle_selector.py.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

RectangleSelector fails to clear itself after being toggled inactive and then back to active.
3 participants