2525 DEFAULT_COLORMAPS ,
2626 add_cmap ,
2727 build_icon_from_cmap ,
28+ cmap_exists ,
29+ get_cmap ,
2830)
2931from plotpy .widgets .colormap .editor import ColorMapEditor
3032from plotpy .widgets .colormap .widget import EditableColormap
@@ -110,11 +112,12 @@ def __init__(
110112
111113 self .__returned_colormap : EditableColormap | None = None
112114
113- if active_colormap is None or active_colormap . lower () not in ALL_COLORMAPS :
115+ if active_colormap is None or not cmap_exists ( active_colormap , ALL_COLORMAPS ) :
114116 active_colormap = next (iter (ALL_COLORMAPS ))
115117
116118 # Select the active colormap
117119 self ._cmap_choice = QW .QComboBox ()
120+ self ._cmap_choice .setMaxVisibleItems (15 )
118121 for cmap in ALL_COLORMAPS .values ():
119122 icon = build_icon_from_cmap (cmap )
120123 self ._cmap_choice .addItem (icon , cmap .name , cmap )
@@ -140,6 +143,9 @@ def __init__(
140143 edit_gbox_layout .setContentsMargins (0 , 0 , 0 , 0 )
141144 edit_gbox_layout .addWidget (self .colormap_editor )
142145 edit_gbox .setLayout (edit_gbox_layout )
146+ edit_gbox .setCheckable (True )
147+ edit_gbox .setChecked (False )
148+ new_btn .clicked .connect (lambda : edit_gbox .setChecked (True ))
143149 self .colormap_editor .colormap_widget .COLORMAP_CHANGED .connect (
144150 self ._changes_not_saved
145151 )
@@ -153,7 +159,7 @@ def __init__(
153159 )
154160 self ._changes_saved = True
155161 self ._save_btn = self .bbox .button (QW .QDialogButtonBox .Save )
156- self ._save_btn .setEnabled (False )
162+ self ._save_btn .setEnabled (False ) # type: ignore
157163 self .bbox .clicked .connect (self .button_clicked )
158164
159165 dialog_layout = QW .QVBoxLayout ()
@@ -181,7 +187,7 @@ def button_clicked(self, button: QW.QAbstractButton) -> None:
181187 def _changes_not_saved (self ) -> None :
182188 """Callback function to be called when the colormap is modified. Enables the
183189 save button and sets the current_changes_saved attribute to False."""
184- self ._save_btn .setEnabled (True )
190+ self ._save_btn .setEnabled (True ) # type: ignore
185191 self ._changes_saved = False
186192
187193 @property
@@ -203,11 +209,11 @@ def set_colormap(self, index: int) -> None:
203209 """
204210 cmap_copy : EditableColormap = deepcopy (self ._cmap_choice .itemData (index ))
205211 self .colormap_editor .set_colormap (cmap_copy )
206- is_new_colormap = cmap_copy .name . lower () not in ALL_COLORMAPS
212+ is_new_colormap = not cmap_exists ( cmap_copy .name , CUSTOM_COLORMAPS )
207213 self ._changes_saved = True
208- self ._save_btn .setEnabled (is_new_colormap )
214+ self ._save_btn .setEnabled (is_new_colormap ) # type: ignore
209215
210- def get_colormap (self ) -> EditableColormap :
216+ def get_colormap (self ) -> EditableColormap | None :
211217 """Return the selected colormap object.
212218
213219 Returns:
@@ -231,7 +237,7 @@ def __get_new_colormap_name(self, title: str, name: str) -> str | None:
231237 new_name = ColorMapNameEdit .edit (self , new_name )
232238 if new_name is None :
233239 return None
234- if new_name . lower () in DEFAULT_COLORMAPS :
240+ if cmap_exists ( new_name , DEFAULT_COLORMAPS ) :
235241 QW .QMessageBox .warning (
236242 self ,
237243 title ,
@@ -243,22 +249,21 @@ def __get_new_colormap_name(self, title: str, name: str) -> str | None:
243249 % new_name ,
244250 )
245251 continue
246- if new_name .lower () in CUSTOM_COLORMAPS :
247- if (
248- QW .QMessageBox .question (
249- self ,
250- title ,
251- _ (
252- "Name <b>%s</b> is already used by a custom colormap.<br><br>"
253- "Do you want to overwrite it?"
254- )
255- % new_name ,
256- QW .QMessageBox .Yes | QW .QMessageBox .No ,
257- QW .QMessageBox .No ,
252+ if cmap_exists (new_name , CUSTOM_COLORMAPS ) and (
253+ QW .QMessageBox .question (
254+ self ,
255+ title ,
256+ _ (
257+ "Name <b>%s</b> is already used by a custom colormap.<br><br>"
258+ "Do you want to overwrite it?"
258259 )
259- == QW .QMessageBox .No
260- ):
261- continue
260+ % new_name ,
261+ QW .QMessageBox .Yes | QW .QMessageBox .No ,
262+ QW .QMessageBox .No ,
263+ )
264+ == QW .QMessageBox .No
265+ ):
266+ continue
262267 break
263268 return new_name
264269
@@ -287,13 +292,13 @@ def save_colormap(self, cmap: EditableColormap | None = None) -> bool:
287292 return False
288293
289294 # Before modifying CUSTOM_COLORMAPS, save this boolean expression:
290- is_existing_custom_cmap = new_name . lower () in CUSTOM_COLORMAPS
295+ is_existing_custom_cmap = cmap_exists ( new_name , CUSTOM_COLORMAPS )
291296
292297 # Take into account the case where the color map exists in custom colormaps
293298 # but not with the same case (e.g. "viridis" vs "Viridis"). So we need to
294299 # get the original name of the colormap:
295300 if is_existing_custom_cmap :
296- new_name = CUSTOM_COLORMAPS [ new_name . lower ()] .name
301+ new_name = get_cmap ( new_name ) .name
297302
298303 cmap .name = new_name
299304 add_cmap (cmap )
@@ -308,7 +313,7 @@ def save_colormap(self, cmap: EditableColormap | None = None) -> bool:
308313 self ._cmap_choice .addItem (icon , new_name , cmap )
309314 self ._cmap_choice .setCurrentText (new_name )
310315
311- self ._save_btn .setEnabled (False )
316+ self ._save_btn .setEnabled (False ) # type: ignore
312317 self ._changes_saved = True
313318 return True
314319
0 commit comments