From 46d9be2f93ef90c24f7c713ace7a45d9ca8b752f Mon Sep 17 00:00:00 2001 From: Hanjin Liu Date: Mon, 9 Jan 2023 13:44:10 +0900 Subject: [PATCH] add column command group --- rst/main/columnwise_settings.rst | 39 ++++++ tabulous/_qt/_mainwindow/_command_palette.py | 2 + tabulous/commands/__init__.py | 3 +- tabulous/commands/_dialogs.py | 5 + tabulous/commands/column.py | 122 +++++++++++++++++++ tabulous/commands/selection.py | 58 --------- tabulous/widgets/_table.py | 12 +- 7 files changed, 176 insertions(+), 65 deletions(-) create mode 100644 tabulous/commands/column.py diff --git a/rst/main/columnwise_settings.rst b/rst/main/columnwise_settings.rst index 57e047a0..93dd818d 100644 --- a/rst/main/columnwise_settings.rst +++ b/rst/main/columnwise_settings.rst @@ -135,6 +135,38 @@ Set Colormaps in GUI Some basic colormaps are available in the right-click context menu of the columns, such as :menu:`Color > Set background colormap`. +Update Colormaps +---------------- + +There are several ways to update the existing colormap. + +1. Update opacity + + :meth:`set_opacity` method takes a value between 0 and 1 to changes the alpha + channel of the output color. + + .. code-block:: python + + table.text_color.set_opacity("A", 0.5) + +2. Invert colormap + + :meth:`invert` method invert the returned RGB value of the colormap. + + .. code-block:: python + + table.text_color.invert("A") + +3. Adjust brightness of colormap + + :meth:`adjust_brightness` takes a value between -1 and 1 to change the brightness + of the current colormap. + + .. code-block:: python + + table.text_color.adjust_brightness("A", 0.5) # brighter + table.text_color.adjust_brightness("A", -0.5) # darker + Validator ========= @@ -290,3 +322,10 @@ and they can be considered as partial table fields. print(sheet["sepal_length"].formatter) # get formatter function sheet["sepal_length"].formatter = f"{:2f} cm" # set formatter del sheet["sepal_length"].formatter # reset formatter + +Colormap can also be updated this way. + +.. code-block:: python + + sheet["sepal_length"].background_color.set(interp_from=["violet", "teal"]) + sheet["sepal_length"].background_color.set_opacity(0.5) diff --git a/tabulous/_qt/_mainwindow/_command_palette.py b/tabulous/_qt/_mainwindow/_command_palette.py index d5ef8f2f..16fbd4c7 100644 --- a/tabulous/_qt/_mainwindow/_command_palette.py +++ b/tabulous/_qt/_mainwindow/_command_palette.py @@ -37,6 +37,7 @@ def load_all_commands(): view_group = palette.add_group("View") plot_group = palette.add_group("Plot") selection_group = palette.add_group("Selection") + column_group = palette.add_group("Column") _groups = { "window": window_group, @@ -47,6 +48,7 @@ def load_all_commands(): "view": view_group, "plot": plot_group, "selection": selection_group, + "column": column_group, } kb = get_config().keybindings.copy() diff --git a/tabulous/commands/__init__.py b/tabulous/commands/__init__.py index 4eef4cd2..ab45229c 100644 --- a/tabulous/commands/__init__.py +++ b/tabulous/commands/__init__.py @@ -2,7 +2,7 @@ from typing import Any, Callable, Iterator, TYPE_CHECKING -from . import file, plot, selection, tab, table, view, analysis, window +from . import file, plot, column, selection, tab, table, view, analysis, window from types import FunctionType, ModuleType from qt_command_palette import get_palette @@ -13,6 +13,7 @@ _SUB_MODULES: list[ModuleType] = [ file, plot, + column, selection, tab, table, diff --git a/tabulous/commands/_dialogs.py b/tabulous/commands/_dialogs.py index 8708c200..9c2ef338 100644 --- a/tabulous/commands/_dialogs.py +++ b/tabulous/commands/_dialogs.py @@ -360,6 +360,11 @@ def choose_multiple(choices: List): return choices +@dialog_factory +def get_float(x: float): + return x + + @dialog_factory def spinbox(min: str = "0", max: str = "1000", step: str = "") -> dict: min = int(min) diff --git a/tabulous/commands/column.py b/tabulous/commands/column.py new file mode 100644 index 00000000..ba653e00 --- /dev/null +++ b/tabulous/commands/column.py @@ -0,0 +1,122 @@ +from __future__ import annotations +from typing import TYPE_CHECKING +from . import _utils, _dialogs + +if TYPE_CHECKING: + from tabulous.widgets import TableViewerBase + +_OPACITY_CONFIG = { + "min": 0, + "max": 1, + "step": 0.01, + "value": 0.8, + "label": "opacity", + "widget_type": "FloatSlider", +} + +_BRIGHTNESS_CONFIG = { + "min": -1, + "max": 1, + "step": 0.01, + "value": 0.0, + "label": "opacity", + "widget_type": "FloatSlider", +} + + +def set_text_colormap(viewer: TableViewerBase) -> None: + """Set text colormap to a column""" + from tabulous._colormap import exec_colormap_dialog + + table, column_name = _utils.get_table_and_column_name(viewer) + if cmap := exec_colormap_dialog( + table.native._get_sub_frame(column_name), + table.native, + ): + table.text_color.set(column_name, cmap, infer_parser=False) + return None + + +def reset_text_colormap(viewer: TableViewerBase) -> None: + """Reset text colormap""" + table, column_name = _utils.get_table_and_column_name(viewer) + del table.text_color[column_name] + + +def set_text_colormap_opacity(viewer: TableViewerBase) -> None: + """Set opacity to the text colormap""" + table, column_name = _utils.get_table_and_column_name(viewer) + if val := _dialogs.get_float(x=_OPACITY_CONFIG, parent=viewer.native): + table.text_color.set_opacity(column_name, val) + + +def invert_text_colormap(viewer: TableViewerBase) -> None: + """Invert text colormap""" + table, column_name = _utils.get_table_and_column_name(viewer) + table.text_color.invert(column_name) + + +def adjust_brightness_text_colormap(viewer: TableViewerBase) -> None: + """Adjust brightness of the text colormap""" + table, column_name = _utils.get_table_and_column_name(viewer) + if val := _dialogs.get_float(x=_BRIGHTNESS_CONFIG, parent=viewer.native): + table.text_color.adjust_brightness(column_name, val) + + +def set_background_colormap(viewer: TableViewerBase) -> None: + """Set background colormap to a column""" + from tabulous._colormap import exec_colormap_dialog + + table, column_name = _utils.get_table_and_column_name(viewer) + if cmap := exec_colormap_dialog( + table.native._get_sub_frame(column_name), + table.native, + ): + table.background_color.set(column_name, cmap, infer_parser=False) + return None + + +def reset_background_colormap(viewer: TableViewerBase) -> None: + """Reset background colormap""" + table, column_name = _utils.get_table_and_column_name(viewer) + del table.background_color[column_name] + + +def set_background_colormap_opacity(viewer: TableViewerBase) -> None: + """Set opacity to the background colormap""" + table, column_name = _utils.get_table_and_column_name(viewer) + if val := _dialogs.get_float(x=_OPACITY_CONFIG, parent=viewer.native): + table.background_color.set_opacity(column_name, val) + + +def invert_background_colormap(viewer: TableViewerBase) -> None: + """Invert background colormap""" + table, column_name = _utils.get_table_and_column_name(viewer) + table.background_color.invert(column_name) + + +def adjust_brightness_background_colormap(viewer: TableViewerBase) -> None: + """Adjust brightness of the background colormap""" + table, column_name = _utils.get_table_and_column_name(viewer) + if val := _dialogs.get_float(x=_BRIGHTNESS_CONFIG, parent=viewer.native): + table.background_color.adjust_brightness(column_name, val) + + +def set_text_formatter(viewer: TableViewerBase) -> None: + """Set text formatter""" + from tabulous._text_formatter import exec_formatter_dialog + + table, column_name = _utils.get_table_and_column_name(viewer) + + if fmt := exec_formatter_dialog( + table.native._get_sub_frame(column_name), + table.native, + ): + table.formatter.set(column_name, fmt) + return None + + +def reset_text_formatter(viewer: TableViewerBase) -> None: + """Reset text formatter""" + table, column_name = _utils.get_table_and_column_name(viewer) + del table.formatter[column_name] diff --git a/tabulous/commands/selection.py b/tabulous/commands/selection.py index 5f1d183c..28d5d46f 100644 --- a/tabulous/commands/selection.py +++ b/tabulous/commands/selection.py @@ -329,64 +329,6 @@ def remove_selected_columns(viewer: TableViewerBase): raise ValueError("No selection under cursor.") -def set_foreground_colormap(viewer: TableViewerBase) -> None: - """Set foreground colormap to a column""" - from tabulous._colormap import exec_colormap_dialog - - table, column_name = _utils.get_table_and_column_name(viewer) - if cmap := exec_colormap_dialog( - table.native._get_sub_frame(column_name), - table.native, - ): - table.text_color.set(column_name, cmap, infer_parser=False) - return None - - -def reset_foreground_colormap(viewer: TableViewerBase) -> None: - """Reset foreground colormap""" - table, column_name = _utils.get_table_and_column_name(viewer) - del table.text_color[column_name] - - -def set_background_colormap(viewer: TableViewerBase) -> None: - """Set background colormap to a column""" - from tabulous._colormap import exec_colormap_dialog - - table, column_name = _utils.get_table_and_column_name(viewer) - if cmap := exec_colormap_dialog( - table.native._get_sub_frame(column_name), - table.native, - ): - table.background_color.set(column_name, cmap, infer_parser=False) - return None - - -def reset_background_colormap(viewer: TableViewerBase) -> None: - """Reset background colormap""" - table, column_name = _utils.get_table_and_column_name(viewer) - del table.background_color[column_name] - - -def set_text_formatter(viewer: TableViewerBase) -> None: - """Set text formatter""" - from tabulous._text_formatter import exec_formatter_dialog - - table, column_name = _utils.get_table_and_column_name(viewer) - - if fmt := exec_formatter_dialog( - table.native._get_sub_frame(column_name), - table.native, - ): - table.formatter.set(column_name, fmt) - return None - - -def reset_text_formatter(viewer: TableViewerBase) -> None: - """Reset text formatter""" - table, column_name = _utils.get_table_and_column_name(viewer) - del table.formatter[column_name] - - def write_data_signal_in_console(viewer: TableViewerBase): """Write data signal connection to console""" from qtpy import QtCore diff --git a/tabulous/widgets/_table.py b/tabulous/widgets/_table.py index 72637216..5e628bd4 100644 --- a/tabulous/widgets/_table.py +++ b/tabulous/widgets/_table.py @@ -555,12 +555,12 @@ def _install_actions(self): # fmt: off _hheader_register = self.columns.register_action - _hheader_register("Color > Set foreground colormap")(_wrap(cmds.selection.set_foreground_colormap)) # noqa: E501 - _hheader_register("Color > Reset foreground colormap")(_wrap(cmds.selection.reset_foreground_colormap)) # noqa: E501 - _hheader_register("Color > Set background colormap")(_wrap(cmds.selection.set_background_colormap)) # noqa: E501 - _hheader_register("Color > Reset background colormap")(_wrap(cmds.selection.reset_background_colormap)) # noqa: E501 - _hheader_register("Formatter > Set text formatter")(_wrap(cmds.selection.set_text_formatter)) # noqa: E501 - _hheader_register("Formatter > Reset text formatter")(_wrap(cmds.selection.reset_text_formatter)) # noqa: E501 + _hheader_register("Color > Set text colormap")(_wrap(cmds.column.set_text_colormap)) # noqa: E501 + _hheader_register("Color > Reset text colormap")(_wrap(cmds.column.reset_text_colormap)) # noqa: E501 + _hheader_register("Color > Set background colormap")(_wrap(cmds.column.set_background_colormap)) # noqa: E501 + _hheader_register("Color > Reset background colormap")(_wrap(cmds.column.reset_background_colormap)) # noqa: E501 + _hheader_register("Formatter > Set text formatter")(_wrap(cmds.column.set_text_formatter)) # noqa: E501 + _hheader_register("Formatter > Reset text formatter")(_wrap(cmds.column.reset_text_formatter)) # noqa: E501 self._qwidget._qtable_view.horizontalHeader().addSeparator() _hheader_register("Sort")(_wrap(cmds.selection.sort_by_columns)) _hheader_register("Filter")(_wrap(cmds.selection.filter_by_columns))