From 270d5ddc31c26b62379e3caa9044dd75ccc71847 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Picca=20Fr=C3=A9d=C3=A9ric-Emmanuel?= Date: Sun, 4 Mar 2018 10:20:27 +0100 Subject: New upstream version 0.7.0+dfsg --- silx/gui/plot/actions/control.py | 140 +++++++++++++++++++++++++-------------- 1 file changed, 92 insertions(+), 48 deletions(-) (limited to 'silx/gui/plot/actions/control.py') diff --git a/silx/gui/plot/actions/control.py b/silx/gui/plot/actions/control.py index 23e710e..ac6dc2f 100644 --- a/silx/gui/plot/actions/control.py +++ b/silx/gui/plot/actions/control.py @@ -1,7 +1,7 @@ # coding: utf-8 # /*########################################################################## # -# Copyright (c) 2004-2017 European Synchrotron Radiation Facility +# Copyright (c) 2004-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 @@ -50,11 +50,10 @@ from __future__ import division __authors__ = ["V.A. Sole", "T. Vincent", "P. Knobel"] __license__ = "MIT" -__date__ = "27/06/2017" +__date__ = "15/02/2018" from . import PlotAction import logging -import numpy from silx.gui.plot import items from silx.gui.plot.ColormapDialog import ColormapDialog from silx.gui.plot._utils import applyZoomToPlot as _applyZoomToPlot @@ -327,67 +326,112 @@ class ColormapAction(PlotAction): plot, icon='colormap', text='Colormap', tooltip="Change colormap", triggered=self._actionTriggered, - checkable=False, parent=parent) + checkable=True, parent=parent) + self.plot.sigActiveImageChanged.connect(self._updateColormap) + + def setColorDialog(self, colorDialog): + """Set a specific color dialog instead of using the default dialog.""" + assert(colorDialog is not None) + assert(self._dialog is None) + self._dialog = colorDialog + self._dialog.visibleChanged.connect(self._dialogVisibleChanged) + self.setChecked(self._dialog.isVisible()) + + @staticmethod + def _createDialog(parent): + """Create the dialog if not already existing + + :parent QWidget parent: Parent of the new colormap + :rtype: ColormapDialog + """ + dialog = ColormapDialog(parent=parent) + dialog.setModal(False) + return dialog def _actionTriggered(self, checked=False): """Create a cmap dialog and update active image and default cmap.""" - # Create the dialog if not already existing if self._dialog is None: - self._dialog = ColormapDialog() + self._dialog = self._createDialog(self.plot) + self._dialog.visibleChanged.connect(self._dialogVisibleChanged) + + # Run the dialog listening to colormap change + if checked is True: + self._dialog.show() + self._updateColormap() + else: + self._dialog.hide() + + def _dialogVisibleChanged(self, isVisible): + self.setChecked(isVisible) + def _updateColormap(self): + if self._dialog is None: + return image = self.plot.getActiveImage() - if not isinstance(image, items.ColormapMixIn): - # No active image or active image is RGBA, - # set dialog from default info - colormap = self.plot.getDefaultColormap() - self._dialog.setHistogram() # Reset histogram and range if any + if isinstance(image, items.ImageComplexData): + # Specific init for complex images + colormap = image.getColormap() - else: + mode = image.getVisualizationMode() + if mode in (items.ImageComplexData.Mode.AMPLITUDE_PHASE, + items.ImageComplexData.Mode.LOG10_AMPLITUDE_PHASE): + data = image.getData( + copy=False, mode=items.ImageComplexData.Mode.PHASE) + else: + data = image.getData(copy=False) + + # Set histogram and range if any + self._dialog.setData(data) + + elif isinstance(image, items.ColormapMixIn): # Set dialog from active image colormap = image.getColormap() - data = image.getData(copy=False) + # Set histogram and range if any + self._dialog.setData(data) - goodData = data[numpy.isfinite(data)] - if goodData.size > 0: - dataMin = goodData.min() - dataMax = goodData.max() - else: - qt.QMessageBox.warning( - None, "No Data", - "Image data does not contain any real value") - dataMin, dataMax = 1., 10. - - self._dialog.setHistogram() # Reset histogram if any - self._dialog.setDataRange(dataMin, dataMax) - # The histogram should be done in a worker thread - # hist, bin_edges = numpy.histogram(goodData, bins=256) - # self._dialog.setHistogram(hist, bin_edges) - - self._dialog.setColormap(name=colormap.getName(), - normalization=colormap.getNormalization(), - autoscale=colormap.isAutoscale(), - vmin=colormap.getVMin(), - vmax=colormap.getVMax(), - colors=colormap.getColormapLUT()) + else: + # No active image or active image is RGBA, + # set dialog from default info + colormap = self.plot.getDefaultColormap() + # Reset histogram and range if any + self._dialog.setData(None) - # Run the dialog listening to colormap change - self._dialog.sigColormapChanged.connect(self._colormapChanged) - result = self._dialog.exec_() - self._dialog.sigColormapChanged.disconnect(self._colormapChanged) + self._dialog.setColormap(colormap) - if not result: # Restore the previous colormap - self._colormapChanged(colormap) - def _colormapChanged(self, colormap): - # Update default colormap - self.plot.setDefaultColormap(colormap) +class ColorBarAction(PlotAction): + """QAction opening the ColorBarWidget of the specified plot. + + :param plot: :class:`.PlotWidget` instance on which to operate + :param parent: See :class:`QAction` + """ + def __init__(self, plot, parent=None): + self._dialog = None # To store an instance of ColormapDialog + super(ColorBarAction, self).__init__( + plot, icon='colorbar', text='Colorbar', + tooltip="Show/Hide the colorbar", + triggered=self._actionTriggered, + checkable=True, parent=parent) + colorBarWidget = self.plot.getColorBarWidget() + old = self.blockSignals(True) + self.setChecked(colorBarWidget.isVisibleTo(self.plot)) + self.blockSignals(old) + colorBarWidget.sigVisibleChanged.connect(self._widgetVisibleChanged) + + def _widgetVisibleChanged(self, isVisible): + """Callback when the colorbar `visible` property change.""" + if self.isChecked() == isVisible: + return + self.setChecked(isVisible) - # Update active image colormap - activeImage = self.plot.getActiveImage() - if isinstance(activeImage, items.ColormapMixIn): - activeImage.setColormap(colormap) + def _actionTriggered(self, checked=False): + """Create a cmap dialog and update active image and default cmap.""" + colorBarWidget = self.plot.getColorBarWidget() + if not colorBarWidget.isHidden() == checked: + return + self.plot.getColorBarWidget().setVisible(checked) class KeepAspectRatioAction(PlotAction): -- cgit v1.2.3