Skip to content

Commit 4862023

Browse files
committed
New colormap selection dialog
1 parent 983cec9 commit 4862023

12 files changed

Lines changed: 672 additions & 1177 deletions

File tree

plotpy/images/cmap_edit.png

495 Bytes
Loading
994 Bytes
Binary file not shown.

plotpy/locale/fr/LC_MESSAGES/plotpy.po

Lines changed: 221 additions & 761 deletions
Large diffs are not rendered by default.

plotpy/locale/plotpy.pot

Lines changed: 195 additions & 159 deletions
Large diffs are not rendered by default.

plotpy/mathutils/colormaps.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def save_colormaps(json_filename: str, colormaps: dict[str, CustomQwtLinearColor
8080

8181

8282
def build_icon_from_cmap(
83-
cmap: CustomQwtLinearColormap, width: int = 24, height: int = 24
83+
cmap: CustomQwtLinearColormap, width: int = 16, height: int = 16
8484
) -> QG.QIcon:
8585
"""Builds an icon representing the colormap
8686

plotpy/tests/features/test_colormap_editor.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
import qtpy.QtCore as QC
1818
import qtpy.QtGui as QG
19-
import qtpy.QtWidgets as QW
2019
from guidata.qthelpers import qt_app_context
2120

2221
from plotpy.widgets.colormap_editor import ColorMapEditor

plotpy/tests/features/test_colormap_manager.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import qtpy.QtCore as QC
1515
import qtpy.QtGui as QG
16+
from guidata.env import execenv
1617
from guidata.qthelpers import qt_app_context
1718

1819
from plotpy.mathutils.colormaps import ALL_COLORMAPS
@@ -22,18 +23,21 @@
2223

2324
def test_colormap_manager() -> None:
2425
"""Test the colormap manager widget."""
25-
with qt_app_context(exec_loop=True):
26+
with qt_app_context():
2627
red = QG.QColor(QC.Qt.GlobalColor.red)
2728
blue = QG.QColor(QC.Qt.GlobalColor.blue)
2829
yellow = QG.QColor(QC.Qt.GlobalColor.yellow)
2930
cmap = CustomQwtLinearColormap(blue, yellow, name="kinda_viridis")
3031
ALL_COLORMAPS["kinda_viridis"] = cmap
31-
editor = ColorMapManager(None, active_colormap="YlGn")
32-
editor.colormap_editor.colormap_widget.add_handle_at_relative_pos(0.5, red)
33-
editor.get_colormap()
34-
editor.colormap_editor.update_colormap_widget()
35-
editor.colormap_editor.update_current_dataset()
36-
editor.show()
32+
dlg = ColorMapManager(None, active_colormap="YlGn")
33+
dlg.colormap_editor.colormap_widget.add_handle_at_relative_pos(0.5, red)
34+
dlg.get_colormap()
35+
dlg.colormap_editor.update_colormap_widget()
36+
dlg.colormap_editor.update_current_dataset()
37+
result = dlg.exec()
38+
execenv.print("Dialog result:", result)
39+
cmap = dlg.get_colormap()
40+
execenv.print("Selected colormap:", cmap.name)
3741

3842

3943
if __name__ == "__main__":

plotpy/tests/unit/test_plot_image.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,9 @@ def test_colormap_tool(qtbot):
134134

135135
# change the colormap
136136
plot.select_item(item)
137-
action = color_map_tool.menu.actions()[0]
138-
color_map_tool.activate_cmap(action)
139-
assert item.get_color_map_name() == "Accent"
137+
cmap_name = "Accent"
138+
color_map_tool.activate_cmap(cmap_name)
139+
assert item.get_color_map_name() == cmap_name
140140
accent_img = plot.grab().toImage()
141141
assert jet_img != accent_img
142142

plotpy/tools/image.py

Lines changed: 15 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from guidata.configtools import get_icon
1010
from guidata.dataset import BoolItem, DataSet, FloatItem
11-
from guidata.qthelpers import add_actions
11+
from guidata.qthelpers import add_actions, exec_dialog
1212
from guidata.widgets.arrayeditor import ArrayEditor
1313
from qtpy import QtCore as QC
1414
from qtpy import QtWidgets as QW
@@ -51,7 +51,7 @@
5151
)
5252
from plotpy.tools.misc import OpenFileTool
5353
from plotpy.tools.shape import CircleTool, RectangleTool, RectangularShapeTool
54-
from plotpy.widgets.colormap_manager import ColorMapManagerDialog
54+
from plotpy.widgets.colormap_manager import ColorMapManager
5555
from plotpy.widgets.colormap_widget import CustomQwtLinearColormap
5656
from 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

plotpy/widgets/colormap_editor.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
# Licensed under the terms of the BSD 3-Clause
44
# (see plotpy/LICENSE for details)
55

6-
"""This module provides a more complete colormap editor widget than the one provided
6+
"""
7+
This module provides a more complete colormap editor widget than the one provided
78
by ColorMapWidget (plotpy/widgets/colormap_widget.py). It allows to edit a colormap
89
by changing its color stops (add/delete/move/change color).
910
"""

0 commit comments

Comments
 (0)