Skip to content

Commit

Permalink
Colormap manager dialog: added "Apply" button
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreRaybaut committed Apr 12, 2024
1 parent 412bf0a commit ae0cd87
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
24 changes: 17 additions & 7 deletions plotpy/tools/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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.
Expand Down
15 changes: 13 additions & 2 deletions plotpy/widgets/colormap/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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
Expand Down

0 comments on commit ae0cd87

Please sign in to comment.