Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ColormapDialog: upgrade _DataInPlotMode to DisplayMode #3964

Merged
merged 24 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
5371279
ColormapDialog: move _DataInPlotMode to public API
payno Nov 16, 2023
4953e3a
silx.gui.plot.actions.control: rename 'setColorDialog' to 'setColorma…
payno Nov 16, 2023
ef1fbb8
silx.gui.plot.actions.control: add 'setColorDialog'
payno Nov 16, 2023
294cffe
silx.gui.dialog.ColormapDialog: make DataInPlotMode benefit from silx…
payno Nov 16, 2023
e661e8b
ColormapDialog: add a getter for the HistoWidget
payno Nov 16, 2023
1116be1
ColormapAction: benefit from getColormapDialog
payno Nov 16, 2023
a89b80b
ColormapDialog: store "Data Range" and "Data Histogram" actions
payno Nov 16, 2023
c1ee5e2
ColormapDialog: make ActionGroup composed of "Data Range" and "Data H…
payno Nov 16, 2023
eada669
_ColormapHistogram: recreate '_setDataInPlotMode' and make 'setDataIn…
payno Nov 16, 2023
a76f154
ColormapAction: setColormapDialog: get ride of 'assert(self._dialog i…
payno Nov 16, 2023
139fb02
ColormapAction: setColormapDialog: rename `colorDialog` to `colormapD…
payno Nov 16, 2023
f334c2a
ColormapAction: add deprecation for `setColorDialog`
payno Nov 16, 2023
95dd28a
Update src/silx/gui/dialog/ColormapDialog.py
payno Nov 16, 2023
903c633
replace usage of Union by usage of __anotations__
payno Nov 16, 2023
1a58423
replace usage of setColorDialog by setColormapDialog
payno Nov 16, 2023
9c384a5
Update src/silx/gui/dialog/ColormapDialog.py
payno Nov 16, 2023
d359f2c
Update src/silx/gui/plot/actions/control.py
payno Nov 16, 2023
fdb4454
Update src/silx/gui/plot/actions/control.py
payno Nov 16, 2023
6a3bd6b
Update src/silx/gui/plot/actions/control.py
payno Nov 16, 2023
641a4ca
Merge branch 'fix_1963' of github.com:payno/silx into fix_1963
payno Nov 16, 2023
ac50db6
Update src/silx/gui/dialog/ColormapDialog.py
payno Nov 20, 2023
2efdebb
Rename DataInPlotMode (previously _DataInPlotMode) to DisplayMode
payno Nov 23, 2023
9d38fd7
fix import of __anotations__
payno Nov 28, 2023
31465d9
Fix naming of dialog argument
t20100 Nov 28, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions src/silx/gui/data/DataViews.py
Original file line number Diff line number Diff line change
Expand Up @@ -1032,7 +1032,7 @@ def createWidget(self, parent):
from silx.gui import plot
widget = plot.Plot2D(parent=parent)
widget.setDefaultColormap(self.defaultColormap())
widget.getColormapAction().setColorDialog(self.defaultColorDialog())
widget.getColormapAction().setColormapDialog(self.defaultColorDialog())
widget.getIntensityHistogramAction().setVisible(True)
widget.setKeepDataAspectRatio(True)
widget.getXAxis().setLabel('X')
Expand Down Expand Up @@ -1148,7 +1148,7 @@ def createWidget(self, parent):
widget.setColormap(self.defaultColormap(), mode=ComplexImageView.ComplexMode.SQUARE_AMPLITUDE)
widget.setColormap(self.defaultColormap(), mode=ComplexImageView.ComplexMode.REAL)
widget.setColormap(self.defaultColormap(), mode=ComplexImageView.ComplexMode.IMAGINARY)
widget.getPlot().getColormapAction().setColorDialog(self.defaultColorDialog())
widget.getPlot().getColormapAction().setColormapDialog(self.defaultColorDialog())
widget.getPlot().getIntensityHistogramAction().setVisible(True)
widget.getPlot().setKeepDataAspectRatio(True)
widget.getXAxis().setLabel('X')
Expand Down Expand Up @@ -1248,7 +1248,7 @@ def createWidget(self, parent):
from silx.gui import plot
widget = plot.StackView(parent=parent)
widget.setColormap(self.defaultColormap())
widget.getPlotWidget().getColormapAction().setColorDialog(self.defaultColorDialog())
widget.getPlotWidget().getColormapAction().setColormapDialog(self.defaultColorDialog())
widget.setKeepDataAspectRatio(True)
widget.setLabels(self.axesNames(None, None))
# hide default option panel
Expand Down Expand Up @@ -1657,7 +1657,7 @@ def createWidget(self, parent):
from silx.gui.data.NXdataWidgets import XYVScatterPlot
widget = XYVScatterPlot(parent)
widget.getScatterView().setColormap(self.defaultColormap())
widget.getScatterView().getScatterToolBar().getColormapAction().setColorDialog(
widget.getScatterView().getScatterToolBar().getColormapAction().setColormapDialog(
self.defaultColorDialog())
return widget

Expand Down Expand Up @@ -1720,7 +1720,7 @@ def createWidget(self, parent):
from silx.gui.data.NXdataWidgets import ArrayImagePlot
widget = ArrayImagePlot(parent)
widget.getPlot().setDefaultColormap(self.defaultColormap())
widget.getPlot().getColormapAction().setColorDialog(self.defaultColorDialog())
widget.getPlot().getColormapAction().setColormapDialog(self.defaultColorDialog())
return widget

def axesNames(self, data, info):
Expand Down Expand Up @@ -1775,7 +1775,7 @@ def __init__(self, parent):
def createWidget(self, parent):
from silx.gui.data.NXdataWidgets import ArrayComplexImagePlot
widget = ArrayComplexImagePlot(parent, colormap=self.defaultColormap())
widget.getPlot().getColormapAction().setColorDialog(self.defaultColorDialog())
widget.getPlot().getColormapAction().setColormapDialog(self.defaultColorDialog())
return widget

def clear(self):
Expand Down Expand Up @@ -1827,7 +1827,7 @@ def createWidget(self, parent):
from silx.gui.data.NXdataWidgets import ArrayStackPlot
widget = ArrayStackPlot(parent)
widget.getStackView().setColormap(self.defaultColormap())
widget.getStackView().getPlotWidget().getColormapAction().setColorDialog(self.defaultColorDialog())
widget.getStackView().getPlotWidget().getColormapAction().setColormapDialog(self.defaultColorDialog())
return widget

def axesNames(self, data, info):
Expand Down Expand Up @@ -1932,7 +1932,7 @@ def createWidget(self, parent):
from silx.gui.data.NXdataWidgets import ArrayStackPlot
widget = ArrayStackPlot(parent)
widget.getStackView().setColormap(self.defaultColormap())
widget.getStackView().getPlotWidget().getColormapAction().setColorDialog(self.defaultColorDialog())
widget.getStackView().getPlotWidget().getColormapAction().setColormapDialog(self.defaultColorDialog())
return widget

def axesNames(self, data, info):
Expand Down Expand Up @@ -1983,7 +1983,7 @@ def __init__(self, parent):
def createWidget(self, parent):
from silx.gui.data.NXdataWidgets import ArrayComplexImagePlot
widget = ArrayComplexImagePlot(parent, colormap=self.defaultColormap())
widget.getPlot().getColormapAction().setColorDialog(self.defaultColorDialog())
widget.getPlot().getColormapAction().setColormapDialog(self.defaultColorDialog())
return widget

def axesNames(self, data, info):
Expand Down
64 changes: 44 additions & 20 deletions src/silx/gui/dialog/ColormapDialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@
:attr:`ColormapDialog.sigColormapChanged`.
""" # noqa

from __future__ import annotations

__authors__ = ["V.A. Sole", "T. Vincent", "H. Payno"]
__license__ = "MIT"
__date__ = "08/12/2020"
Expand All @@ -84,6 +86,7 @@
from silx.math.histogram import Histogramnd
from silx.gui.plot.items.roi import RectangleROI
from silx.gui.plot.tools.roi import RegionOfInterestManager
from silx.utils.enum import Enum as _Enum

_logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -300,7 +303,7 @@ def setAutoRange(self, autoRange):
self.setChecked(autoRange[0] if autoRange[0] == autoRange[1] else False)

@enum.unique
class _DataInPlotMode(enum.Enum):
class DisplayMode(_Enum):
"""Enum for each mode of display of the data in the plot."""
RANGE = 'range'
HISTOGRAM = 'histogram'
Expand Down Expand Up @@ -332,7 +335,7 @@ class _ColormapHistogram(qt.QWidget):

def __init__(self, parent):
qt.QWidget.__init__(self, parent=parent)
self._dataInPlotMode = _DataInPlotMode.RANGE
self._displayMode = DisplayMode.RANGE
self._finiteRange = None, None
self._initPlot()

Expand All @@ -349,7 +352,7 @@ def __init__(self, parent):

def paintEvent(self, event):
if self._invalidated:
self._updateDataInPlot()
self._updateDisplayMode()
self._invalidated = False
self._updateMarkerPosition()
return super(_ColormapHistogram, self).paintEvent(event)
Expand Down Expand Up @@ -545,24 +548,28 @@ def _initPlot(self):

group = qt.QActionGroup(self._plotToolbar)
group.setExclusive(True)

# data range mode
action = qt.QAction("Data range", self)
action.setToolTip("Display the data range within the colormap range. A fast data processing have to be done.")
action.setIcon(icons.getQIcon('colormap-range'))
action.setCheckable(True)
action.setData(_DataInPlotMode.RANGE)
action.setChecked(action.data() == self._dataInPlotMode)
action.setData(DisplayMode.RANGE)
action.setChecked(action.data() == self._displayMode)
self._plotToolbar.addAction(action)
group.addAction(action)
self._dataRangeAction = action
# histogram mode
action = qt.QAction("Histogram", self)
action.setToolTip("Display the data histogram within the colormap range. A slow data processing have to be done. ")
action.setIcon(icons.getQIcon('colormap-histogram'))
action.setCheckable(True)
action.setData(_DataInPlotMode.HISTOGRAM)
action.setChecked(action.data() == self._dataInPlotMode)
action.setData(DisplayMode.HISTOGRAM)
action.setChecked(action.data() == self._displayMode)
self._plotToolbar.addAction(action)
group.addAction(action)
group.triggered.connect(self._displayDataInPlotModeChanged)
self._dataHistogramAction = action
group.setExclusive(True)
group.triggered.connect(self._displayModeChanged)

plotBoxLayout = qt.QHBoxLayout()
plotBoxLayout.setContentsMargins(0, 0, 0, 0)
Expand Down Expand Up @@ -749,24 +756,38 @@ def _plotGammaMarkerConstraint(self, x, y):
x = min(x, vmax)
return x, y

def _setDataInPlotMode(self, mode):
if self._dataInPlotMode == mode:
def setDisplayMode(self, mode: str | DisplayMode):
mode = DisplayMode.from_value(mode)
if mode is DisplayMode.HISTOGRAM:
action = self._dataHistogramAction
elif mode is DisplayMode.RANGE:
action = self._dataRangeAction
else:
raise ValueError("Mode not supported")
action.setChecked(True)
self._displayModeChanged(action)

def _setDisplayMode(self, mode):
if self._displayMode == mode:
return
self._dataInPlotMode = mode
self._updateDataInPlot()
self._displayMode = mode
self._updateDisplayMode()

def getDsiplayMode(self) -> DisplayMode:
return self._displayMode

def _displayDataInPlotModeChanged(self, action):
def _displayModeChanged(self, action):
mode = action.data()
self._setDataInPlotMode(mode)
self._setDisplayMode(mode)

def invalidateData(self):
self._histogramData = {}
self._dataRange = {}
self._invalidated = True
self.update()

def _updateDataInPlot(self):
mode = self._dataInPlotMode
def _updateDisplayMode(self):
mode = self._displayMode

norm = self._getNorm()
if norm == Colormap.LINEAR:
Expand All @@ -779,7 +800,7 @@ def _updateDataInPlot(self):
axis = self._plot.getXAxis()
axis.setScale(scale)

if mode == _DataInPlotMode.RANGE:
if mode == DisplayMode.RANGE:
dataRange = self._getNormalizedDataRange()
xmin, xmax = dataRange
if xmax is None or xmin is None:
Expand All @@ -795,7 +816,7 @@ def _updateDataInPlot(self):
fill=True,
z=1)

elif mode == _DataInPlotMode.HISTOGRAM:
elif mode == DisplayMode.HISTOGRAM:
histogram, bin_edges = self._getNormalizedHistogram()
if histogram is None or bin_edges is None:
self._plot.remove(legend='Data', kind='histogram')
Expand Down Expand Up @@ -829,7 +850,7 @@ def _getNorm(self):
return norm

def updateNormalization(self):
self._updateDataInPlot()
self._updateDisplayMode()
self.update()


Expand Down Expand Up @@ -1054,6 +1075,9 @@ def __init__(self, parent=None, title="Colormap Dialog"):

self._applyColormap()

def getHistogramWidget(self):
return self._histoWidget

def _invalidateColormap(self):
if self.isVisible():
self._applyColormap()
Expand Down
34 changes: 22 additions & 12 deletions src/silx/gui/plot/actions/control.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
from silx.gui.plot._utils import applyZoomToPlot as _applyZoomToPlot
from silx.gui import qt
from silx.gui import icons
from silx.utils.deprecation import deprecated

_logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -330,14 +331,26 @@ def __init__(self, plot, parent=None):
self.plot.sigActiveImageChanged.connect(self._updateColormap)
self.plot.sigActiveScatterChanged.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)
def setColormapDialog(self, dialog):
"""Set a specific colormap dialog instead of using the default one."""
assert dialog is not None
if self._dialog is not None:
self._dialog.visibleChanged.disconnect(self._dialogVisibleChanged)

self._dialog = dialog
self._dialog.visibleChanged.connect(self._dialogVisibleChanged, qt.Qt.UniqueConnection)
self.setChecked(self._dialog.isVisible())

@deprecated(replacement="setColormapDialog", since_version="2.0")
def setColorDialog(self, colorDialog):
self.setColormapDialog(colorDialog)

def getColormapDialog(self):
if self._dialog is None:
self._dialog = self._createDialog(self.plot)
self._dialog.visibleChanged.connect(self._dialogVisibleChanged)
t20100 marked this conversation as resolved.
Show resolved Hide resolved
return self._dialog

@staticmethod
def _createDialog(parent):
"""Create the dialog if not already existing
Expand All @@ -352,16 +365,13 @@ def _createDialog(parent):

def _actionTriggered(self, checked=False):
"""Create a cmap dialog and update active image and default cmap."""
if self._dialog is None:
self._dialog = self._createDialog(self.plot)
self._dialog.visibleChanged.connect(self._dialogVisibleChanged)

dialog = self.getColormapDialog()
# Run the dialog listening to colormap change
if checked is True:
self._updateColormap()
self._dialog.show()
dialog.show()
else:
self._dialog.hide()
dialog.hide()

def _dialogVisibleChanged(self, isVisible):
self.setChecked(isVisible)
Expand Down