summaryrefslogtreecommitdiff
path: root/silx/gui/plot/utils/axis.py
blob: 693e8eb4ff6437980a7e239c994194ea63bcd730 (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2017-2018 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.
#
# ###########################################################################*/
"""This module contains utils class for axes management.
"""

__authors__ = ["V. Valls"]
__license__ = "MIT"
__date__ = "20/11/2018"

import functools
import logging
from contextlib import contextmanager
import weakref
import silx.utils.weakref as silxWeakref
from silx.gui.plot.items.axis import Axis, XAxis, YAxis

try:
    from ...qt.inspect import isValid as _isQObjectValid
except ImportError:  # PySide(1) fallback
    def _isQObjectValid(obj):
        return True


_logger = logging.getLogger(__name__)


class SyncAxes(object):
    """Synchronize a set of plot axes together.

    It is created with the expected axes and starts to synchronize them.

    It can be customized to synchronize limits, scale, and direction of axes
    together. By default everything is synchronized.

    The API :meth:`start` and :meth:`stop` can be used to enable/disable the
    synchronization while this object is still alive.

    If this object is destroyed the synchronization stop.

    .. versionadded:: 0.6
    """

    def __init__(self, axes,
                 syncLimits=True,
                 syncScale=True,
                 syncDirection=True,
                 syncCenter=False,
                 syncZoom=False,
                 filterHiddenPlots=False
                 ):
        """
        Constructor

        :param list(Axis) axes: A list of axes to synchronize together
        :param bool syncLimits: Synchronize axes limits
        :param bool syncScale: Synchronize axes scale
        :param bool syncDirection: Synchronize axes direction
        :param bool syncCenter: Synchronize the center of the axes in the center
            of the plots
        :param bool syncZoom: Synchronize the zoom of the plot
        :param bool filterHiddenPlots: True to avoid updating hidden plots.
            Default: False.
        """
        object.__init__(self)

        def implies(x, y): return bool(y ** x)

        assert(implies(syncZoom, not syncLimits))
        assert(implies(syncCenter, not syncLimits))
        assert(implies(syncLimits, not syncCenter))
        assert(implies(syncLimits, not syncZoom))

        self.__filterHiddenPlots = filterHiddenPlots
        self.__locked = False
        self.__axisRefs = []
        self.__syncLimits = syncLimits
        self.__syncScale = syncScale
        self.__syncDirection = syncDirection
        self.__syncCenter = syncCenter
        self.__syncZoom = syncZoom
        self.__callbacks = None
        self.__lastMainAxis = None

        for axis in axes:
            self.addAxis(axis)

        self.start()

    def start(self):
        """Start synchronizing axes together.

        The first axis is used as the reference for the first synchronization.
        After that, any changes to any axes will be used to synchronize other
        axes.
        """
        if self.isSynchronizing():
            raise RuntimeError("Axes already synchronized")
        self.__callbacks = {}

        axes = self.__getAxes()

        # register callback for further sync
        for axis in axes:
            self.__connectAxes(axis)
        self.synchronize()

    def isSynchronizing(self):
        """Returns true if events are connected to the axes to synchronize them
        all together

        :rtype: bool
        """
        return self.__callbacks is not None

    def __connectAxes(self, axis):
        refAxis = weakref.ref(axis)
        callbacks = []
        if self.__syncLimits:
            # the weakref is needed to be able ignore self references
            callback = silxWeakref.WeakMethodProxy(self.__axisLimitsChanged)
            callback = functools.partial(callback, refAxis)
            sig = axis.sigLimitsChanged
            sig.connect(callback)
            callbacks.append(("sigLimitsChanged", callback))
        elif self.__syncCenter and self.__syncZoom:
            # the weakref is needed to be able ignore self references
            callback = silxWeakref.WeakMethodProxy(self.__axisCenterAndZoomChanged)
            callback = functools.partial(callback, refAxis)
            sig = axis.sigLimitsChanged
            sig.connect(callback)
            callbacks.append(("sigLimitsChanged", callback))
        elif self.__syncZoom:
            raise NotImplementedError()
        elif self.__syncCenter:
            # the weakref is needed to be able ignore self references
            callback = silxWeakref.WeakMethodProxy(self.__axisCenterChanged)
            callback = functools.partial(callback, refAxis)
            sig = axis.sigLimitsChanged
            sig.connect(callback)
            callbacks.append(("sigLimitsChanged", callback))
        if self.__syncScale:
            # the weakref is needed to be able ignore self references
            callback = silxWeakref.WeakMethodProxy(self.__axisScaleChanged)
            callback = functools.partial(callback, refAxis)
            sig = axis.sigScaleChanged
            sig.connect(callback)
            callbacks.append(("sigScaleChanged", callback))
        if self.__syncDirection:
            # the weakref is needed to be able ignore self references
            callback = silxWeakref.WeakMethodProxy(self.__axisInvertedChanged)
            callback = functools.partial(callback, refAxis)
            sig = axis.sigInvertedChanged
            sig.connect(callback)
            callbacks.append(("sigInvertedChanged", callback))

        if self.__filterHiddenPlots:
            # the weakref is needed to be able ignore self references
            callback = silxWeakref.WeakMethodProxy(self.__axisVisibilityChanged)
            callback = functools.partial(callback, refAxis)
            plot = axis._getPlot()
            plot.sigVisibilityChanged.connect(callback)
            callbacks.append(("sigVisibilityChanged", callback))

        self.__callbacks[refAxis] = callbacks

    def __disconnectAxes(self, axis):
        if axis is not None and _isQObjectValid(axis):
            ref = weakref.ref(axis)
            callbacks = self.__callbacks.pop(ref)
            for sigName, callback in callbacks:
                if sigName == "sigVisibilityChanged":
                    obj = axis._getPlot()
                else:
                    obj = axis
                if obj is not None:
                    sig = getattr(obj, sigName)
                    sig.disconnect(callback)

    def addAxis(self, axis):
        """Add a new axes to synchronize.

        :param ~silx.gui.plot.items.Axis axis: The axis to synchronize
        """
        self.__axisRefs.append(weakref.ref(axis))
        if self.isSynchronizing():
            self.__connectAxes(axis)
            # This could be done faster as only this axis have to be fixed
            self.synchronize()

    def removeAxis(self, axis):
        """Remove an axis from the synchronized axes.

        :param ~silx.gui.plot.items.Axis axis: The axis to remove
        """
        ref = weakref.ref(axis)
        self.__axisRefs.remove(ref)
        if self.isSynchronizing():
            self.__disconnectAxes(axis)

    def synchronize(self, mainAxis=None):
        """Synchronize programatically all the axes.

        :param ~silx.gui.plot.items.Axis mainAxis:
            The axis to take as reference (Default: the first axis).
        """
        # sync the current state
        axes = self.__getAxes()
        if len(axes) == 0:
            return

        if mainAxis is None:
            mainAxis = axes[0]

        refMainAxis = weakref.ref(mainAxis)
        if self.__syncLimits:
            self.__axisLimitsChanged(refMainAxis, *mainAxis.getLimits())
        elif self.__syncCenter and self.__syncZoom:
            self.__axisCenterAndZoomChanged(refMainAxis, *mainAxis.getLimits())
        elif self.__syncCenter:
            self.__axisCenterChanged(refMainAxis, *mainAxis.getLimits())
        if self.__syncScale:
            self.__axisScaleChanged(refMainAxis, mainAxis.getScale())
        if self.__syncDirection:
            self.__axisInvertedChanged(refMainAxis, mainAxis.isInverted())

    def stop(self):
        """Stop the synchronization of the axes"""
        if not self.isSynchronizing():
            raise RuntimeError("Axes not synchronized")
        for ref in list(self.__callbacks.keys()):
            axis = ref()
            self.__disconnectAxes(axis)
        self.__callbacks = None

    def __del__(self):
        """Destructor"""
        # clean up references
        if self.__callbacks is not None:
            self.stop()

    def __getAxes(self):
        """Returns list of existing axes.

        :rtype: List[Axis]
        """
        axes = [ref() for ref in self.__axisRefs]
        return [axis for axis in axes if axis is not None]

    @contextmanager
    def __inhibitSignals(self):
        self.__locked = True
        yield
        self.__locked = False

    def __axesToUpdate(self, changedAxis):
        for axis in self.__getAxes():
            if axis is changedAxis:
                continue
            if self.__filterHiddenPlots:
                plot = axis._getPlot()
                if not plot.isVisible():
                    continue
            yield axis

    def __axisVisibilityChanged(self, changedAxis, isVisible):
        if not isVisible:
            return
        if self.__locked:
            return
        changedAxis = changedAxis()
        if self.__lastMainAxis is None:
            self.__lastMainAxis = self.__axisRefs[0]
        mainAxis = self.__lastMainAxis
        mainAxis = mainAxis()
        self.synchronize(mainAxis=mainAxis)
        # force back the main axis
        self.__lastMainAxis = weakref.ref(mainAxis)

    def __getAxesCenter(self, axis, vmin, vmax):
        """Returns the value displayed in the center of this axis range.

        :rtype: float
        """
        scale = axis.getScale()
        if scale == Axis.LINEAR:
            center = (vmin + vmax) * 0.5
        else:
            raise NotImplementedError("Log scale not implemented")
        return center

    def __getRangeInPixel(self, axis):
        """Returns the size of the axis in pixel"""
        bounds = axis._getPlot().getPlotBoundsInPixels()
        # bounds: left, top, width, height
        if isinstance(axis, XAxis):
            return bounds[2]
        elif isinstance(axis, YAxis):
            return bounds[3]
        else:
            assert(False)

    def __getLimitsFromCenter(self, axis, pos, pixelSize=None):
        """Returns the limits to apply to this axis to move the `pos` into the
        center of this axis.

        :param Axis axis:
        :param float pos: Position in the center of the computed limits
        :param Union[None,float] pixelSize: Pixel size to apply to compute the
            limits. If `None` the current pixel size is applyed.
        """
        scale = axis.getScale()
        if scale == Axis.LINEAR:
            if pixelSize is None:
                # Use the current pixel size of the axis
                limits = axis.getLimits()
                valueRange = limits[0] - limits[1]
                a = pos - valueRange * 0.5
                b = pos + valueRange * 0.5
            else:
                pixelRange = self.__getRangeInPixel(axis)
                a = pos - pixelRange * 0.5 * pixelSize
                b = pos + pixelRange * 0.5 * pixelSize

        else:
            raise NotImplementedError("Log scale not implemented")
        if a > b:
            return b, a
        return a, b

    def __axisLimitsChanged(self, changedAxis, vmin, vmax):
        if self.__locked:
            return
        self.__lastMainAxis = changedAxis
        changedAxis = changedAxis()
        with self.__inhibitSignals():
            for axis in self.__axesToUpdate(changedAxis):
                axis.setLimits(vmin, vmax)

    def __axisCenterAndZoomChanged(self, changedAxis, vmin, vmax):
        if self.__locked:
            return
        self.__lastMainAxis = changedAxis
        changedAxis = changedAxis()
        with self.__inhibitSignals():
            center = self.__getAxesCenter(changedAxis, vmin, vmax)
            pixelRange = self.__getRangeInPixel(changedAxis)
            if pixelRange == 0:
                return
            pixelSize = (vmax - vmin) / pixelRange
            for axis in self.__axesToUpdate(changedAxis):
                vmin, vmax = self.__getLimitsFromCenter(axis, center, pixelSize)
                axis.setLimits(vmin, vmax)

    def __axisCenterChanged(self, changedAxis, vmin, vmax):
        if self.__locked:
            return
        self.__lastMainAxis = changedAxis
        changedAxis = changedAxis()
        with self.__inhibitSignals():
            center = self.__getAxesCenter(changedAxis, vmin, vmax)
            for axis in self.__axesToUpdate(changedAxis):
                vmin, vmax = self.__getLimitsFromCenter(axis, center)
                axis.setLimits(vmin, vmax)

    def __axisScaleChanged(self, changedAxis, scale):
        if self.__locked:
            return
        self.__lastMainAxis = changedAxis
        changedAxis = changedAxis()
        with self.__inhibitSignals():
            for axis in self.__axesToUpdate(changedAxis):
                axis.setScale(scale)

    def __axisInvertedChanged(self, changedAxis, isInverted):
        if self.__locked:
            return
        self.__lastMainAxis = changedAxis
        changedAxis = changedAxis()
        with self.__inhibitSignals():
            for axis in self.__axesToUpdate(changedAxis):
                axis.setInverted(isInverted)