Line data Source code
1 : # SPDX-FileCopyrightText: 2025 PairInteraction Developers
2 : # SPDX-License-Identifier: LGPL-3.0-or-later
3 1 : from __future__ import annotations
4 :
5 1 : from typing import TYPE_CHECKING, Any
6 :
7 1 : from matplotlib.backends.backend_qt import NavigationToolbar2QT as NavigationToolbar
8 :
9 : if TYPE_CHECKING:
10 : from collections.abc import Callable
11 :
12 : from matplotlib.backends.backend_qtagg import FigureCanvasQTAgg
13 : from PySide6.QtWidgets import QWidget
14 :
15 :
16 1 : class CustomNavigationToolbar(NavigationToolbar):
17 : """Custom navigation toolbar for matplotlib figures.
18 :
19 : See Also:
20 : https://stackoverflow.com/questions/12695678/how-to-modify-the-navigation-toolbar-easily-in-a-matplotlib-figure-window/15549675#15549675
21 :
22 : """
23 :
24 1 : toolitems = (
25 : ("Zoom", "Zoom to rectangle\nx/y fixes axis", "zoom_to_rect", "zoom"),
26 : ("Pan", "Left button pans, Right button zooms\nx/y fixes axis, CTRL fixes aspect", "move", "pan"),
27 : ("Home", "Reset original view", "home", "home"),
28 : ) # type: ignore [assignment]
29 :
30 1 : def __init__(self, canvas: FigureCanvasQTAgg, parent: QWidget | None = None) -> None:
31 : """Initialize the custom navigation toolbar."""
32 1 : super().__init__(canvas, parent, coordinates=False)
33 1 : self._home_callbacks: list[Callable[[], None]] = []
34 :
35 1 : def home(self, *args: Any) -> None:
36 : """Reset view and notify registered callbacks."""
37 0 : super().home(*args)
38 0 : for cb in self._home_callbacks:
39 0 : cb()
40 :
41 1 : def reset_home_view(self) -> None:
42 : """Reset the home view to the current axes state.
43 :
44 : I.e. if a user afterwards clicks the home/reset button, the view will be reset to the current view.
45 : """
46 1 : self.update()
47 1 : self.push_current()
|