|
16 | 16 | import re |
17 | 17 | import time |
18 | 18 | from types import SimpleNamespace |
| 19 | +import uuid |
19 | 20 | from weakref import WeakKeyDictionary |
20 | 21 |
|
21 | 22 | import numpy as np |
@@ -440,79 +441,34 @@ class ToolEnableNavigation(_ToolEnableNavigation): |
440 | 441 | pass |
441 | 442 |
|
442 | 443 |
|
443 | | -class _ToolGridBase(ToolBase): |
444 | | - """Common functionality between ToolGrid and ToolMinorGrid.""" |
445 | | - |
446 | | - _cycle = [(False, False), (True, False), (True, True), (False, True)] |
447 | | - |
448 | | - def trigger(self, sender, event, data=None): |
449 | | - ax = event.inaxes |
450 | | - if ax is None: |
451 | | - return |
452 | | - try: |
453 | | - x_state, x_which, y_state, y_which = self._get_next_grid_states(ax) |
454 | | - except ValueError: |
455 | | - pass |
456 | | - else: |
457 | | - ax.grid(x_state, which=x_which, axis="x") |
458 | | - ax.grid(y_state, which=y_which, axis="y") |
459 | | - ax.figure.canvas.draw_idle() |
460 | | - |
461 | | - @staticmethod |
462 | | - def _get_uniform_grid_state(ticks): |
463 | | - """ |
464 | | - Check whether all grid lines are in the same visibility state. |
465 | | -
|
466 | | - Returns True/False if all grid lines are on or off, None if they are |
467 | | - not all in the same state. |
468 | | - """ |
469 | | - if all(tick.gridline.get_visible() for tick in ticks): |
470 | | - return True |
471 | | - elif not any(tick.gridline.get_visible() for tick in ticks): |
472 | | - return False |
473 | | - else: |
474 | | - return None |
475 | | - |
476 | | - |
477 | | -class ToolGrid(_ToolGridBase): |
| 444 | +class ToolGrid(ToolBase): |
478 | 445 | """Tool to toggle the major grids of the figure.""" |
479 | 446 |
|
480 | 447 | description = 'Toggle major grids' |
481 | 448 | default_keymap = mpl.rcParams['keymap.grid'] |
482 | 449 |
|
483 | | - def _get_next_grid_states(self, ax): |
484 | | - if None in map(self._get_uniform_grid_state, |
485 | | - [ax.xaxis.minorTicks, ax.yaxis.minorTicks]): |
486 | | - # Bail out if minor grids are not in a uniform state. |
487 | | - raise ValueError |
488 | | - x_state, y_state = map(self._get_uniform_grid_state, |
489 | | - [ax.xaxis.majorTicks, ax.yaxis.majorTicks]) |
490 | | - cycle = self._cycle |
491 | | - # Bail out (via ValueError) if major grids are not in a uniform state. |
492 | | - x_state, y_state = ( |
493 | | - cycle[(cycle.index((x_state, y_state)) + 1) % len(cycle)]) |
494 | | - return (x_state, "major" if x_state else "both", |
495 | | - y_state, "major" if y_state else "both") |
496 | | - |
497 | | - |
498 | | -class ToolMinorGrid(_ToolGridBase): |
| 450 | + def trigger(self, sender, event, data=None): |
| 451 | + sentinel = str(uuid.uuid4()) |
| 452 | + # Trigger grid switching by temporarily setting :rc:`keymap.grid` |
| 453 | + # to a unique key and sending an appropriate event. |
| 454 | + with cbook._setattr_cm(event, key=sentinel), \ |
| 455 | + mpl.rc_context({'keymap.grid': sentinel}): |
| 456 | + mpl.backend_bases.key_press_handler(event, self.figure.canvas) |
| 457 | + |
| 458 | + |
| 459 | +class ToolMinorGrid(ToolBase): |
499 | 460 | """Tool to toggle the major and minor grids of the figure.""" |
500 | 461 |
|
501 | 462 | description = 'Toggle major and minor grids' |
502 | 463 | default_keymap = mpl.rcParams['keymap.grid_minor'] |
503 | 464 |
|
504 | | - def _get_next_grid_states(self, ax): |
505 | | - if None in map(self._get_uniform_grid_state, |
506 | | - [ax.xaxis.majorTicks, ax.yaxis.majorTicks]): |
507 | | - # Bail out if major grids are not in a uniform state. |
508 | | - raise ValueError |
509 | | - x_state, y_state = map(self._get_uniform_grid_state, |
510 | | - [ax.xaxis.minorTicks, ax.yaxis.minorTicks]) |
511 | | - cycle = self._cycle |
512 | | - # Bail out (via ValueError) if minor grids are not in a uniform state. |
513 | | - x_state, y_state = ( |
514 | | - cycle[(cycle.index((x_state, y_state)) + 1) % len(cycle)]) |
515 | | - return x_state, "both", y_state, "both" |
| 465 | + def trigger(self, sender, event, data=None): |
| 466 | + sentinel = str(uuid.uuid4()) |
| 467 | + # Trigger grid switching by temporarily setting :rc:`keymap.grid_minor` |
| 468 | + # to a unique key and sending an appropriate event. |
| 469 | + with cbook._setattr_cm(event, key=sentinel), \ |
| 470 | + mpl.rc_context({'keymap.grid_minor': sentinel}): |
| 471 | + mpl.backend_bases.key_press_handler(event, self.figure.canvas) |
516 | 472 |
|
517 | 473 |
|
518 | 474 | class ToolFullScreen(ToolToggleBase): |
|
0 commit comments