Skip to content

Commit 0879d8c

Browse files
committed
added button to delete colormap in manager
1 parent c65ba1e commit 0879d8c

2 files changed

Lines changed: 56 additions & 3 deletions

File tree

plotpy/mathutils/colormap.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,3 +292,11 @@ def add_cmap(cmap: EditableColormap) -> None:
292292
ALL_COLORMAPS[cmap.name.lower()] = cmap
293293
CUSTOM_COLORMAPS[cmap.name.lower()] = cmap
294294
save_colormaps(CUSTOM_COLORMAPS_PATH, CUSTOM_COLORMAPS)
295+
296+
297+
def delete_cmap(cmap: EditableColormap) -> None:
298+
"""Deletes the given colormap from both ALL_COLORMAPS and CUSTOM_COLORMAPS global"""
299+
global ALL_COLORMAPS, CUSTOM_COLORMAPS
300+
if CUSTOM_COLORMAPS.pop(cmap.name.lower(), None) is not None:
301+
del ALL_COLORMAPS[cmap.name.lower()]
302+
save_colormaps(CUSTOM_COLORMAPS_PATH, CUSTOM_COLORMAPS)

plotpy/widgets/colormap/manager.py

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
add_cmap,
4242
build_icon_from_cmap,
4343
cmap_exists,
44+
delete_cmap,
4445
get_cmap,
4546
)
4647
from plotpy.widgets.colormap.editor import ColorMapEditor
@@ -142,16 +143,24 @@ def __init__(
142143

143144
self._cmap_choice.setIconSize(QC.QSize(LARGE_ICON_WIDTH, LARGE_ICON_HEIGHT))
144145
self._cmap_choice.setCurrentText(active_colormap)
145-
select_gbox = QW.QGroupBox(_("Select or create a colormap"))
146-
select_label = QW.QLabel(_("Colormap presets:"))
146+
147147
new_btn = QW.QPushButton(_("Create new colormap") + "...")
148148
new_btn.clicked.connect(self.new_colormap)
149+
self._del_btn = QW.QPushButton(_("Delete colormap") + "...")
150+
is_custom_cmap = cmap_exists(active_colormap, CUSTOM_COLORMAPS)
151+
self._del_btn.setEnabled(is_custom_cmap)
152+
self._del_btn.clicked.connect(self.delete_colormap)
153+
154+
select_gbox = QW.QGroupBox(_("Select or create a colormap"))
155+
select_label = QW.QLabel(_("Colormap presets:"))
149156
select_gbox_layout = QW.QHBoxLayout()
150157
select_gbox_layout.addWidget(select_label)
151158
select_gbox_layout.addWidget(self._cmap_choice)
152159
select_gbox_layout.addSpacing(10)
153160
select_gbox_layout.addWidget(new_btn)
154161
select_gbox_layout.addStretch(1)
162+
select_gbox_layout.addWidget(self._del_btn)
163+
select_gbox_layout.addStretch(1)
155164
select_gbox.setLayout(select_gbox_layout)
156165

157166
# Edit the selected colormap
@@ -229,8 +238,12 @@ def set_colormap(self, index: int) -> None:
229238
"""
230239
cmap_copy: EditableColormap = deepcopy(self._cmap_choice.itemData(index))
231240
self.colormap_editor.set_colormap(cmap_copy)
232-
is_new_colormap = not cmap_exists(cmap_copy.name)
241+
242+
is_custom_cmap = cmap_exists(cmap_copy.name, CUSTOM_COLORMAPS)
243+
self._del_btn.setEnabled(is_custom_cmap)
244+
233245
self._changes_saved = True
246+
is_new_colormap = not cmap_exists(cmap_copy.name)
234247
self._save_btn.setEnabled(is_new_colormap) # type: ignore
235248

236249
def get_colormap(self) -> EditableColormap | None:
@@ -292,6 +305,38 @@ def new_colormap(self) -> None:
292305
cmap = EditableColormap(QG.QColor(0), QG.QColor(4294967295), name=_("New"))
293306
self.save_colormap(cmap)
294307

308+
def delete_colormap(self) -> None:
309+
"""Delete the current colormap."""
310+
cmap = self.colormap_editor.get_colormap()
311+
if cmap is None:
312+
return
313+
if cmap_exists(cmap.name, DEFAULT_COLORMAPS):
314+
QW.QMessageBox.warning(
315+
self,
316+
_("Delete colormap"),
317+
_("Colormap <b>%s</b> is a default colormap and cannot be deleted.")
318+
% cmap.name,
319+
QW.QMessageBox.StandardButton.Cancel,
320+
)
321+
return
322+
if (
323+
QW.QMessageBox.question(
324+
self,
325+
_("Delete colormap"),
326+
_("Do you want to delete colormap <b>%s</b>?") % cmap.name,
327+
QW.QMessageBox.Yes | QW.QMessageBox.No,
328+
QW.QMessageBox.StandardButton.No,
329+
)
330+
== QW.QMessageBox.No
331+
):
332+
return
333+
delete_cmap(cmap)
334+
current_index = self._cmap_choice.currentIndex()
335+
self._cmap_choice.removeItem(current_index)
336+
self._changes_saved = True
337+
self._save_btn.setEnabled(False)
338+
self._cmap_choice.setCurrentIndex(max(current_index - 1, 0))
339+
295340
def save_colormap(self, cmap: EditableColormap | None = None) -> bool:
296341
"""Saves the current colormap and handles the validation process. The saved
297342
colormaps can only be saved in the custom colormaps.

0 commit comments

Comments
 (0)