diff --git a/plotpy/tools/image.py b/plotpy/tools/image.py index dd4c35c..c98cfeb 100644 --- a/plotpy/tools/image.py +++ b/plotpy/tools/image.py @@ -484,6 +484,7 @@ def activate_command(self, plot: BasePlot, checked: bool) -> None: manager = ColorMapManager( plot.parent(), active_colormap=self._active_colormap.name ) + manager.SIG_APPLY_COLORMAP.connect(self.update_plot) if exec_dialog(manager) and (cmap := manager.get_colormap()) is not None: self.activate_cmap(cmap) @@ -500,15 +501,24 @@ def activate_cmap(self, cmap: str | EditableColormap) -> None: self._active_colormap = cmap plot: BasePlot = self.get_active_plot() if self._active_colormap is not None and plot is not None: - items = get_selected_images(plot, IColormapImageItemType) - for item in items: - param: BaseImageParam = item.param - param.colormap = self._active_colormap.name - param.update_item(item) - plot.SIG_ITEM_PARAMETERS_CHANGED.emit(item) - plot.invalidate() + self.update_plot(self._active_colormap.name) self.update_status(plot) + def update_plot(self, cmap: str) -> None: + """Update the plot with the given colormap. + + Args: + cmap: Colormap name + """ + plot: BasePlot = self.get_active_plot() + items = get_selected_images(plot, IColormapImageItemType) + for item in items: + param: BaseImageParam = item.param + param.colormap = cmap + param.update_item(item) + plot.SIG_ITEM_PARAMETERS_CHANGED.emit(item) + plot.invalidate() + def update_status(self, plot: BasePlot) -> None: """Update tool status if the plot type is not PlotType.CURVE. diff --git a/plotpy/widgets/colormap/manager.py b/plotpy/widgets/colormap/manager.py index 0ed5686..32f2c8e 100644 --- a/plotpy/widgets/colormap/manager.py +++ b/plotpy/widgets/colormap/manager.py @@ -133,6 +133,8 @@ class ColorMapManager(QW.QDialog): that the colormap cannot be removed. """ + SIG_APPLY_COLORMAP = QC.Signal(str) + def __init__( self, parent: QW.QWidget | None = None, @@ -196,13 +198,16 @@ def __init__( self._cmap_choice.currentIndexChanged.connect(self.set_colormap) self.bbox = QW.QDialogButtonBox( - QW.QDialogButtonBox.Save + QW.QDialogButtonBox.Apply + | QW.QDialogButtonBox.Save | QW.QDialogButtonBox.Ok | QW.QDialogButtonBox.Cancel ) self._changes_saved = True self._save_btn = self.bbox.button(QW.QDialogButtonBox.Save) self._save_btn.setEnabled(False) # type: ignore + self._apply_btn = self.bbox.button(QW.QDialogButtonBox.Apply) + self._apply_btn.setEnabled(False) # type: ignore self.bbox.clicked.connect(self.button_clicked) dialog_layout = QW.QVBoxLayout() @@ -220,7 +225,12 @@ def button_clicked(self, button: QW.QAbstractButton) -> None: Args: button: button clicked """ - if button is self._save_btn: + if button is self._apply_btn: + if not self.current_changes_saved and not self.save_colormap(): + return + self._apply_btn.setEnabled(False) + self.SIG_APPLY_COLORMAP.emit(self.colormap_editor.get_colormap().name) + elif button is self._save_btn: self.save_colormap() elif self.bbox.buttonRole(button) == QW.QDialogButtonBox.AcceptRole: self.accept() @@ -231,6 +241,7 @@ def _changes_not_saved(self) -> None: """Callback function to be called when the colormap is modified. Enables the save button and sets the current_changes_saved attribute to False.""" self._save_btn.setEnabled(True) # type: ignore + self._apply_btn.setEnabled(True) # type: ignore self._changes_saved = False @property