diff options
Diffstat (limited to 'silx/gui/plot/PlotWidget.py')
-rw-r--r-- | silx/gui/plot/PlotWidget.py | 183 |
1 files changed, 138 insertions, 45 deletions
diff --git a/silx/gui/plot/PlotWidget.py b/silx/gui/plot/PlotWidget.py index 2f7132c..e023a21 100644 --- a/silx/gui/plot/PlotWidget.py +++ b/silx/gui/plot/PlotWidget.py @@ -31,7 +31,7 @@ from __future__ import division __authors__ = ["V.A. Sole", "T. Vincent"] __license__ = "MIT" -__date__ = "14/06/2018" +__date__ = "12/10/2018" from collections import OrderedDict, namedtuple @@ -58,7 +58,8 @@ from .LimitsHistory import LimitsHistory from . import _utils from . import items -from .items.axis import TickMode +from .items.curve import CurveStyle +from .items.axis import TickMode # noqa from .. import qt from ._utils.panzoom import ViewConstraints @@ -68,27 +69,7 @@ _logger = logging.getLogger(__name__) _COLORDICT = colors.COLORDICT -_COLORLIST = [_COLORDICT['black'], - _COLORDICT['blue'], - _COLORDICT['red'], - _COLORDICT['green'], - _COLORDICT['pink'], - _COLORDICT['yellow'], - _COLORDICT['brown'], - _COLORDICT['cyan'], - _COLORDICT['magenta'], - _COLORDICT['orange'], - _COLORDICT['violet'], - # _COLORDICT['bluegreen'], - _COLORDICT['grey'], - _COLORDICT['darkBlue'], - _COLORDICT['darkRed'], - _COLORDICT['darkGreen'], - _COLORDICT['darkCyan'], - _COLORDICT['darkMagenta'], - _COLORDICT['darkYellow'], - _COLORDICT['darkBrown']] - +_COLORLIST = silx.config.DEFAULT_PLOT_CURVE_COLORS """ Object returned when requesting the data range. @@ -193,6 +174,25 @@ class PlotWidget(qt.QMainWindow): It provides the source as passed to :meth:`setInteractiveMode`. """ + sigItemAdded = qt.Signal(items.Item) + """Signal emitted when an item was just added to the plot + + It provides the added item. + """ + + sigItemAboutToBeRemoved = qt.Signal(items.Item) + """Signal emitted right before an item is removed from the plot. + + It provides the item that will be removed. + """ + + sigVisibilityChanged = qt.Signal(bool) + """Signal emitted when the widget becomes visible (or invisible). + This happens when the widget is hidden or shown. + + It provides the visible state. + """ + def __init__(self, parent=None, backend=None, legends=False, callback=None, **kw): self._autoreplot = False @@ -253,8 +253,8 @@ class PlotWidget(qt.QMainWindow): self._colorIndex = 0 self._styleIndex = 0 - self._activeCurveHandling = True - self._activeCurveColor = "#000000" + self._activeCurveSelectionMode = "atmostone" + self._activeCurveStyle = CurveStyle(color='#000000') self._activeLegend = {'curve': None, 'image': None, 'scatter': None} @@ -346,8 +346,18 @@ class PlotWidget(qt.QMainWindow): else: self._dirty = True - if self._autoreplot and not wasDirty: + if self._autoreplot and not wasDirty and self.isVisible(): + self._backend.postRedisplay() + + def showEvent(self, event): + if self._autoreplot and self._dirty: self._backend.postRedisplay() + super(PlotWidget, self).showEvent(event) + self.sigVisibilityChanged.emit(True) + + def hideEvent(self, event): + super(PlotWidget, self).hideEvent(event) + self.sigVisibilityChanged.emit(False) def _invalidateDataRange(self): """ @@ -447,6 +457,7 @@ class PlotWidget(qt.QMainWindow): self._invalidateDataRange() # TODO handle this automatically self._notifyContentChanged(item) + self.sigItemAdded.emit(item) def _notifyContentChanged(self, item): legend, kind = self._itemKey(item) @@ -461,6 +472,8 @@ class PlotWidget(qt.QMainWindow): if key not in self._content: raise RuntimeError('Item not in the plot') + self.sigItemAboutToBeRemoved.emit(item) + legend, kind = key if kind in self._ACTIVE_ITEM_KINDS: @@ -721,6 +734,12 @@ class PlotWidget(qt.QMainWindow): if wasActive: self.setActiveCurve(curve.getLegend()) + elif self.getActiveCurveSelectionMode() == "legacy": + if self.getActiveCurve(just_legend=True) is None: + if len(self.getAllCurves(just_legend=True, + withhidden=False)) == 1: + if curve.isVisible(): + self.setActiveCurve(curve.getLegend()) if resetzoom: # We ask for a zoom reset in order to handle the plot scaling @@ -840,10 +859,9 @@ class PlotWidget(qt.QMainWindow): (default: False) :param bool draggable: Indicate if the image can be moved. (default: False) - :param colormap: Description of the :class:`.Colormap` to use - (or None). - This is ignored if data is a RGB(A) image. - :type colormap: Union[silx.gui.colors.Colormap, dict] + :param colormap: Colormap object to use (or None). + This is ignored if data is a RGB(A) image. + :type colormap: Union[~silx.gui.colors.Colormap, dict] :param pixmap: Pixmap representation of the data (if any) :type pixmap: (nrows, ncolumns, RGBA) ubyte array or None (default) :param str xlabel: X axis label to show when this curve is active, @@ -986,8 +1004,8 @@ class PlotWidget(qt.QMainWindow): :param numpy.ndarray y: The data corresponding to the y coordinates :param numpy.ndarray value: The data value associated with each point :param str legend: The legend to be associated to the scatter (or None) - :param silx.gui.colors.Colormap colormap: - The :class:`.Colormap`. to be used for the scatter (or None) + :param ~silx.gui.colors.Colormap colormap: + Colormap object to be used for the scatter (or None) :param info: User-defined information associated to the curve :param str symbol: Symbol to be drawn at each (x, y) position:: @@ -1560,26 +1578,59 @@ class PlotWidget(qt.QMainWindow): # Active Curve/Image def isActiveCurveHandling(self): - """Returns True if active curve selection is enabled.""" - return self._activeCurveHandling + """Returns True if active curve selection is enabled. + + :rtype: bool + """ + return self.getActiveCurveSelectionMode() != 'none' def setActiveCurveHandling(self, flag=True): """Enable/Disable active curve selection. - :param bool flag: True (the default) to enable active curve selection. + :param bool flag: True to enable 'atmostone' active curve selection, + False to disable active curve selection. + """ + self.setActiveCurveSelectionMode('atmostone' if flag else 'none') + + def getActiveCurveStyle(self): + """Returns the current style applied to active curve + + :rtype: CurveStyle """ - if not flag: - self.setActiveCurve(None) # Reset active curve + return self._activeCurveStyle - self._activeCurveHandling = bool(flag) + def setActiveCurveStyle(self, + color=None, + linewidth=None, + linestyle=None, + symbol=None, + symbolsize=None): + """Set the style of active curve + :param color: Color + :param Union[str,None] linestyle: Style of the line + :param Union[float,None] linewidth: Width of the line + :param Union[str,None] symbol: Symbol of the markers + :param Union[float,None] symbolsize: Size of the symbols + """ + self._activeCurveStyle = CurveStyle(color=color, + linewidth=linewidth, + linestyle=linestyle, + symbol=symbol, + symbolsize=symbolsize) + curve = self.getActiveCurve() + if curve is not None: + curve.setHighlightedStyle(self.getActiveCurveStyle()) + + @deprecated(replacement="getActiveCurveStyle", since_version="0.9") def getActiveCurveColor(self): """Get the color used to display the currently active curve. See :meth:`setActiveCurveColor`. """ - return self._activeCurveColor + return self._activeCurveStyle.getColor() + @deprecated(replacement="setActiveCurveStyle", since_version="0.9") def setActiveCurveColor(self, color="#000000"): """Set the color to use to display the currently active curve. @@ -1590,7 +1641,7 @@ class PlotWidget(qt.QMainWindow): color = "black" if color in self.colorDict: color = self.colorDict[color] - self._activeCurveColor = color + self.setActiveCurveStyle(color=color) def getActiveCurve(self, just_legend=False): """Return the currently active curve. @@ -1621,9 +1672,43 @@ class PlotWidget(qt.QMainWindow): if not self.isActiveCurveHandling(): return + if legend is None and self.getActiveCurveSelectionMode() == "legacy": + _logger.info( + 'setActiveCurve(None) ignored due to active curve selection mode') + return return self._setActiveItem(kind='curve', legend=legend) + def setActiveCurveSelectionMode(self, mode): + """Sets the current selection mode. + + :param str mode: The active curve selection mode to use. + It can be: 'legacy', 'atmostone' or 'none'. + """ + assert mode in ('legacy', 'atmostone', 'none') + + if mode != self._activeCurveSelectionMode: + self._activeCurveSelectionMode = mode + if mode == 'none': # reset active curve + self._setActiveItem(kind='curve', legend=None) + + elif mode == 'legacy' and self.getActiveCurve() is None: + # Select an active curve + curves = self.getAllCurves(just_legend=False, + withhidden=False) + if len(curves) == 1: + if curves[0].isVisible(): + self.setActiveCurve(curves[0].getLegend()) + + def getActiveCurveSelectionMode(self): + """Returns the current selection mode. + + It can be "atmostone", "legacy" or "none". + + :rtype: str + """ + return self._activeCurveSelectionMode + def getActiveImage(self, just_legend=False): """Returns the currently active image. @@ -1707,7 +1792,7 @@ class PlotWidget(qt.QMainWindow): # Curve specific: handle highlight if kind == 'curve': - item.setHighlightedColor(self.getActiveCurveColor()) + item.setHighlightedStyle(self.getActiveCurveStyle()) item.setHighlighted(True) if isinstance(item, items.LabelsMixIn): @@ -1761,6 +1846,13 @@ class PlotWidget(qt.QMainWindow): # Getters + def getItems(self): + """Returns the list of items in the plot + + :rtype: List[silx.gui.plot.items.Item] + """ + return tuple(self._content.values()) + def getAllCurves(self, just_legend=False, withhidden=False): """Returns all curves legend or info and data. @@ -2273,8 +2365,9 @@ class PlotWidget(qt.QMainWindow): curve.setLineStyle(linestyle) def getDefaultColormap(self): - """Return the default :class:`.Colormap` used by :meth:`addImage`. + """Return the default colormap used by :meth:`addImage`. + :rtype: ~silx.gui.colors.Colormap """ return self._defaultColormap @@ -2286,9 +2379,9 @@ class PlotWidget(qt.QMainWindow): It only affects future calls to :meth:`addImage` without the colormap parameter. - :param silx.gui.colors.Colormap colormap: + :param ~silx.gui.colors.Colormap colormap: The description of the default colormap, or - None to set the :class:`.Colormap` to a linear + None to set the colormap to a linear autoscale gray colormap. """ if colormap is None: @@ -2328,7 +2421,7 @@ class PlotWidget(qt.QMainWindow): self._styleIndex = (self._styleIndex + 1) % len(self._styleList) # If color is the one of active curve, take the next one - if color == self.getActiveCurveColor(): + if colors.rgba(color) == self.getActiveCurveStyle().getColor(): color, style = self._getColorAndStyle() if not self._plotLines: |