|
41 | 41 | add_cmap, |
42 | 42 | build_icon_from_cmap, |
43 | 43 | cmap_exists, |
| 44 | + delete_cmap, |
44 | 45 | get_cmap, |
45 | 46 | ) |
46 | 47 | from plotpy.widgets.colormap.editor import ColorMapEditor |
@@ -142,16 +143,24 @@ def __init__( |
142 | 143 |
|
143 | 144 | self._cmap_choice.setIconSize(QC.QSize(LARGE_ICON_WIDTH, LARGE_ICON_HEIGHT)) |
144 | 145 | self._cmap_choice.setCurrentText(active_colormap) |
145 | | - select_gbox = QW.QGroupBox(_("Select or create a colormap")) |
146 | | - select_label = QW.QLabel(_("Colormap presets:")) |
| 146 | + |
147 | 147 | new_btn = QW.QPushButton(_("Create new colormap") + "...") |
148 | 148 | 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:")) |
149 | 156 | select_gbox_layout = QW.QHBoxLayout() |
150 | 157 | select_gbox_layout.addWidget(select_label) |
151 | 158 | select_gbox_layout.addWidget(self._cmap_choice) |
152 | 159 | select_gbox_layout.addSpacing(10) |
153 | 160 | select_gbox_layout.addWidget(new_btn) |
154 | 161 | select_gbox_layout.addStretch(1) |
| 162 | + select_gbox_layout.addWidget(self._del_btn) |
| 163 | + select_gbox_layout.addStretch(1) |
155 | 164 | select_gbox.setLayout(select_gbox_layout) |
156 | 165 |
|
157 | 166 | # Edit the selected colormap |
@@ -229,8 +238,12 @@ def set_colormap(self, index: int) -> None: |
229 | 238 | """ |
230 | 239 | cmap_copy: EditableColormap = deepcopy(self._cmap_choice.itemData(index)) |
231 | 240 | 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 | + |
233 | 245 | self._changes_saved = True |
| 246 | + is_new_colormap = not cmap_exists(cmap_copy.name) |
234 | 247 | self._save_btn.setEnabled(is_new_colormap) # type: ignore |
235 | 248 |
|
236 | 249 | def get_colormap(self) -> EditableColormap | None: |
@@ -292,6 +305,38 @@ def new_colormap(self) -> None: |
292 | 305 | cmap = EditableColormap(QG.QColor(0), QG.QColor(4294967295), name=_("New")) |
293 | 306 | self.save_colormap(cmap) |
294 | 307 |
|
| 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 | + |
295 | 340 | def save_colormap(self, cmap: EditableColormap | None = None) -> bool: |
296 | 341 | """Saves the current colormap and handles the validation process. The saved |
297 | 342 | colormaps can only be saved in the custom colormaps. |
|
0 commit comments