88
99from guidata .configtools import get_icon
1010from guidata .dataset import BoolItem , DataSet , FloatItem
11- from guidata .qthelpers import add_actions
11+ from guidata .qthelpers import add_actions , exec_dialog
1212from guidata .widgets .arrayeditor import ArrayEditor
1313from qtpy import QtCore as QC
1414from qtpy import QtWidgets as QW
5151)
5252from plotpy .tools .misc import OpenFileTool
5353from plotpy .tools .shape import CircleTool , RectangleTool , RectangularShapeTool
54- from plotpy .widgets .colormap_manager import ColorMapManagerDialog
54+ from plotpy .widgets .colormap_manager import ColorMapManager
5555from plotpy .widgets .colormap_widget import CustomQwtLinearColormap
5656from plotpy .widgets .imagefile import exec_image_save_dialog
5757
@@ -463,73 +463,24 @@ def __init__(
463463 self .action .setIconText ("" )
464464 self .action .setIcon (self .default_icon )
465465
466- def create_action_menu (self , manager : PlotManager ) -> QW .QMenu :
467- """Create and return menu for the tool's action with all the colormaps and
468- colormap editor available
466+ def activate_command (self , plot : BasePlot , checked : bool ) -> None :
467+ """Triggers tool action.
469468
470469 Args:
471- manager: PlotManager instance
472- """
473- menu = QW .QMenu ()
474-
475- default_cmaps_action_group = QActionGroup (menu )
476- for cmap in DEFAULT_COLORMAPS .values ():
477- icon = build_icon_from_cmap (cmap )
478- action = menu .addAction (icon , cmap .name )
479- action .setEnabled (True ) # type: ignore
480- action .setData (cmap ) # type: ignore
481- action = default_cmaps_action_group .addAction (action )
482- default_cmaps_action_group .triggered .connect (self .activate_cmap )
483-
484- menu .addSeparator ()
485-
486- custom_cmaps_action_group = QActionGroup (menu )
487- for cmap in CUSTOM_COLORMAPS .values ():
488- icon = build_icon_from_cmap (cmap )
489- action = menu .addAction (icon , cmap .name )
490- custom_cmaps_action_group .addAction (action )
491- action .setEnabled (True ) # type: ignore
492- action .setData (cmap ) # type: ignore
493- custom_cmaps_action_group .triggered .connect (self .activate_cmap )
494-
495- menu .addSeparator ()
496-
497- action = menu .addAction (_ ("Edit..." ))
498- action .setEnabled (True ) # type: ignore
499- action .setIcon (get_icon ("edit.png" )) # type: ignore
500- action .triggered .connect (self .open_cmap_manager ) # type: ignore
501-
502- return menu
503-
504- def open_cmap_manager (self ) -> None :
505- """Opens the colormap manager in a new dialog. The chosen colormap will be
506- applied to the active image when the manager is closed.
470+ plot: Plot instance
471+ checked: True if tool is checked, False otherwise
507472 """
508- plot = self .get_active_plot ()
509473 if (
510474 plot is None
511475 or not isinstance (self .action , QC .QObject )
512476 or not isinstance (self .action .text (), str )
513477 ):
514478 return
515- manager = ColorMapManagerDialog (
479+ manager = ColorMapManager (
516480 plot .parent (), active_colormap = self ._active_colormap .name
517481 )
518- manager .exec_ ()
519-
520- self ._active_colormap = manager .get_colormap ()
521- self .menu = self .create_action_menu (plot .manager )
522- self .action .setMenu (self .menu )
523- self .activate_cmap ()
524-
525- def activate_command (self , plot : BasePlot , checked : bool ) -> None :
526- """Triggers tool action.
527-
528- Args:
529- plot: Plot instance
530- checked: True if tool is checked, False otherwise
531- """
532- pass
482+ if exec_dialog (manager ):
483+ self .activate_cmap (manager .get_colormap ())
533484
534485 def get_selected_images (self , plot : BasePlot ) -> list [IBasePlotItem ]:
535486 """Returns the currently selected images in the given plot.
@@ -547,30 +498,24 @@ def get_selected_images(self, plot: BasePlot) -> list[IBasePlotItem]:
547498 items = [active_image ]
548499 return items
549500
550- def activate_cmap (
551- self , cmap : QAction | str | CustomQwtLinearColormap | None = None
552- ) -> None :
501+ def activate_cmap (self , cmap : str | CustomQwtLinearColormap ) -> None :
553502 """Activate the given colormap. Supports mutliple input types.
554503
555504 Args:
556- cmap: Cmap to apply for currently selected images. If None, the tool's
557- active colormap will be used. Defaults to None.
505+ cmap: Cmap to apply for currently selected images.
558506 """
559- assert isinstance (cmap , (QAction , str , CustomQwtLinearColormap )) or cmap is None
560- if isinstance (cmap , QAction ):
561- self ._active_colormap = cmap .data () # type: ignore
562- elif isinstance (cmap , str ):
507+ assert isinstance (cmap , (str , CustomQwtLinearColormap ))
508+ if isinstance (cmap , str ):
563509 self ._active_colormap = get_cmap (cmap )
564- elif isinstance ( cmap , CustomQwtLinearColormap ) :
510+ else :
565511 self ._active_colormap = cmap
566-
567512 plot = self .get_active_plot ()
568513 if self ._active_colormap is not None and plot is not None :
569514 items = self .get_selected_images (plot )
570515 for item in items :
571516 item .param .colormap = self ._active_colormap .name
572517 item .param .update_item (item )
573- self .action .setText (self ._active_colormap .name )
518+ self .action .setText (_ ( "Colormap: %s" ) % self ._active_colormap .name )
574519 plot .invalidate ()
575520 self .update_status (plot )
576521
0 commit comments