Esempio n. 1
0
class ColormapToolButton(qt.QToolButton):
    def __init__(self, parent=None, plot=None):
        self._bg_dialog = None
        self._scatter_dialog = None
        super(ColormapToolButton, self).__init__(parent)
        self.plot = plot

        icon = icons.getQIcon('colormap')
        self.setIcon(icon)

        bgImageCmapAction = qt.QAction("Background image colormap", self)
        bgImageCmapAction.triggered.connect(self._setBgCmap)

        scatterCmapAction = qt.QAction("Scatter colormap", self)
        scatterCmapAction.triggered.connect(self._setScatterCmap)

        menu = qt.QMenu(self)
        menu.addAction(bgImageCmapAction)
        menu.addAction(scatterCmapAction)
        self.setMenu(menu)
        self.setPopupMode(qt.QToolButton.InstantPopup)

    def _setBgCmap(self):
        if self._bg_dialog is None:
            self._bg_dialog = ColormapDialog()

        image = self.plot.getBackgroundImage()
        if image is None:
            # No active image, set dialog from default info
            colormap = self.plot.getDefaultColormap()

            self._bg_dialog.setHistogram()  # Reset histogram and range if any

        else:
            # Set dialog from active image
            colormap = image.getColormap()

            data = image.getData(copy=False)

            goodData = data[numpy.isfinite(data)]
            if goodData.size > 0:
                dataMin = goodData.min()
                dataMax = goodData.max()
            else:
                qt.QMessageBox.warning(
                    self, "No Data",
                    "Image data does not contain any real value")
                dataMin, dataMax = 1., 10.

            self._bg_dialog.setHistogram()
            self._bg_dialog.setDataRange(dataMin, dataMax)

        self._bg_dialog.setColormap(**colormap)

        # Run the dialog listening to colormap change
        self._bg_dialog.sigColormapChanged.connect(self._bgColormapChanged)
        result = self._bg_dialog.exec_()
        self._bg_dialog.sigColormapChanged.disconnect(self._bgColormapChanged)

        if not result:  # Restore the previous colormap
            self._bgColormapChanged(colormap)

    def _bgColormapChanged(self, colormap):
        image = self.plot.getBackgroundImage()
        if image is not None:
            # Update image: This do not preserve pixmap
            self.plot.setBackgroundImage(image.getData(copy=False),
                                         colormap=colormap)

    def _setScatterCmap(self):
        if self._scatter_dialog is None:
            self._scatter_dialog = ColormapDialog()

        scatter = self.plot.getScatter()
        if scatter is None:
            # No active scatter, set dialog from default info
            colormap = self.plot.getDefaultColormap()

            self._scatter_dialog.setHistogram(
            )  # Reset histogram and range if any

        else:
            # Set dialog from active scatter
            colormap = scatter.getColormap()

            data = scatter.getValueData(copy=False)

            goodData = data[numpy.isfinite(data)]
            if goodData.size > 0:
                dataMin = goodData.min()
                dataMax = goodData.max()
            else:
                qt.QMessageBox.warning(
                    self, "No Data",
                    "Image data does not contain any real value")
                dataMin, dataMax = 1., 10.

            self._scatter_dialog.setHistogram()
            self._scatter_dialog.setDataRange(dataMin, dataMax)
        self._scatter_dialog.setColormap(**colormap)

        # Run the dialog listening to colormap change
        self._scatter_dialog.sigColormapChanged.connect(
            self._scatterColormapChanged)
        result = self._scatter_dialog.exec_()
        self._scatter_dialog.sigColormapChanged.disconnect(
            self._scatterColormapChanged)

        if not result:  # Restore the previous colormap
            self._bgColormapChanged(colormap)

    def _scatterColormapChanged(self, colormap):
        scatter = self.plot.getScatter()
        if scatter is not None:
            self.plot.setScatter(scatter.getXData(copy=False),
                                 scatter.getYData(copy=False),
                                 scatter.getValueData(copy=False),
                                 info=scatter.getInfo(),
                                 colormap=colormap)
Esempio n. 2
0
class ColormapAction(PlotAction):
    """QAction opening a ColormapDialog to update the colormap.

    Both the active image colormap and the default colormap are updated.

    :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(ColormapAction, self).__init__(
            plot, icon='colormap', text='Colormap',
            tooltip="Change colormap",
            triggered=self._actionTriggered,
            checkable=False, parent=parent)

    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()

        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

        else:
            # Set dialog from active image
            colormap = image.getColormap()

            data = image.getData(copy=False)

            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())

        # Run the dialog listening to colormap change
        self._dialog.sigColormapChanged.connect(self._colormapChanged)
        result = self._dialog.exec_()
        self._dialog.sigColormapChanged.disconnect(self._colormapChanged)

        if not result:  # Restore the previous colormap
            self._colormapChanged(colormap)

    def _colormapChanged(self, colormap):
        # Update default colormap
        self.plot.setDefaultColormap(colormap)

        # Update active image colormap
        activeImage = self.plot.getActiveImage()
        if isinstance(activeImage, items.ColormapMixIn):
            activeImage.setColormap(colormap)