Skip to content

Commit

Permalink
add column command group
Browse files Browse the repository at this point in the history
  • Loading branch information
hanjinliu committed Jan 9, 2023
1 parent d88e3bd commit 46d9be2
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 65 deletions.
39 changes: 39 additions & 0 deletions rst/main/columnwise_settings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
=========

Expand Down Expand Up @@ -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)
2 changes: 2 additions & 0 deletions tabulous/_qt/_mainwindow/_command_palette.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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()
Expand Down
3 changes: 2 additions & 1 deletion tabulous/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -13,6 +13,7 @@
_SUB_MODULES: list[ModuleType] = [
file,
plot,
column,
selection,
tab,
table,
Expand Down
5 changes: 5 additions & 0 deletions tabulous/commands/_dialogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
122 changes: 122 additions & 0 deletions tabulous/commands/column.py
Original file line number Diff line number Diff line change
@@ -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]
58 changes: 0 additions & 58 deletions tabulous/commands/selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions tabulous/widgets/_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit 46d9be2

Please sign in to comment.