summaryrefslogtreecommitdiff
path: root/silx/gui/plot/actions/PlotToolAction.py
blob: 77e8be21709f916c94913148725cf37d553528cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2004-2017 European Synchrotron Radiation Facility
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# ###########################################################################*/
"""
The class :class:`.PlotToolAction` help the creation of a qt.QAction associating
a tool window with a :class:`.PlotWidget`.
"""

from __future__ import division


__authors__ = ["V.A. Sole", "T. Vincent", "P. Knobel"]
__license__ = "MIT"
__date__ = "10/10/2018"


import weakref

from .PlotAction import PlotAction
from silx.gui import qt


class PlotToolAction(PlotAction):
    """Base class for QAction that maintain a tool window operating on a
    PlotWidget."""

    def __init__(self, plot, icon, text, tooltip=None,
                 triggered=None, checkable=False, parent=None):
        PlotAction.__init__(self,
                            plot=plot,
                            icon=icon,
                            text=text,
                            tooltip=tooltip,
                            triggered=self._triggered,
                            parent=parent,
                            checkable=True)
        self._previousGeometry = None
        self._toolWindow = None

    def _triggered(self, checked):
        """Update the plot of the histogram visibility status

        :param bool checked: status  of the action button
        """
        self._setToolWindowVisible(checked)

    def _setToolWindowVisible(self, visible):
        """Set the tool window visible or hidden."""
        tool = self._getToolWindow()
        if tool.isVisible() == visible:
            # Nothing to do
            return

        if visible:
            self._connectPlot(tool)
            tool.show()
            if self._previousGeometry is not None:
                # Restore the geometry
                tool.setGeometry(self._previousGeometry)
        else:
            self._disconnectPlot(tool)
            # Save the geometry
            self._previousGeometry = tool.geometry()
            tool.hide()

    def _connectPlot(self, window):
        """Called if the tool is visible and have to be updated according to
        event of the plot.

        :param qt.QWidget window: The tool window
        """
        pass

    def _disconnectPlot(self, window):
        """Called if the tool is not visible and dont have anymore to be updated
        according to event of the plot.

        :param qt.QWidget window: The tool window
        """
        pass

    def _isWindowInUse(self):
        """Returns true if the tool window is currently in use."""
        if not self.isChecked():
            return False
        return self._toolWindow is not None

    def _ownerVisibilityChanged(self, isVisible):
        """Called when the visibility of the parent of the tool window changes

        :param bool isVisible: True if the parent became visible
        """
        if self._isWindowInUse():
            self._setToolWindowVisible(isVisible)

    def eventFilter(self, qobject, event):
        """Observe when the close event is emitted then
        simply uncheck the action button

        :param qobject: the object observe
        :param event: the event received by qobject
        """
        if event.type() == qt.QEvent.Close:
            if self._toolWindow is not None:
                window = self._toolWindow()
                self._previousGeometry = window.geometry()
                window.hide()
            self.setChecked(False)

        return PlotAction.eventFilter(self, qobject, event)

    def _getToolWindow(self):
        """Returns the window containg tohe tool.

        It uses lazy loading to create this tool..
        """
        if self._toolWindow is None:
            window = self._createToolWindow()
            if self._previousGeometry is not None:
                window.setGeometry(self._previousGeometry)
            window.installEventFilter(self)
            plot = self.plot
            plot.sigVisibilityChanged.connect(self._ownerVisibilityChanged)
            self._toolWindow = weakref.ref(window)
        return self._toolWindow()

    def _createToolWindow(self):
        """Create the tool window managing the plot."""
        raise NotImplementedError()