Source code for taurus.qt.qtgui.util.tauruscolor

#!/usr/bin/env python

# ###########################################################################
#
# This file is part of Taurus
#
# http://taurus-scada.org
#
# Copyright 2011 CELLS / ALBA Synchrotron, Bellaterra, Spain
#
# Taurus is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Taurus is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Taurus.  If not, see <http://www.gnu.org/licenses/>.
#
# ###########################################################################

"""This module provides Qt color management for taurus"""

__docformat__ = "restructuredtext"

from taurus.core.taurusbasetypes import AttrQuality
from taurus.core.util.colors import (
    ATTRIBUTE_QUALITY_DATA,
    DEVICE_STATE_DATA,
    ColorPalette,
)
from taurus.core.util.colors import (
    get_best_foreground as get_best_foreground_name,
)
from taurus.core.util.log import deprecation_decorator
from taurus.external.qt import Qt


def get_best_foreground(bg: Qt.QColor):
    """Computes the best foreground color as a `QColor` for given background color as a `QColor`."""
    color_name = get_best_foreground_name(*bg.getRgb()[:3])
    return Qt.QColor(color_name)


def update_brush(palette: Qt.QPalette, fg: Qt.QColor, role: Qt.QPalette.ColorRole):
    """Changes a brush color for given `QPalette`."""
    brush = palette.brush(role)
    if brush.color().getRgb() != fg.getRgb():
        brush = Qt.QBrush(brush)
        brush.setColor(fg)
        palette.setBrush(role, brush)


def update_palette_color(
    palette: Qt.QPalette, color: Qt.QColor, role: Qt.QPalette.ColorRole
):
    """Changes the color of given `QPalete`."""
    if palette.color(role).getRgb() != color.getRgb():
        palette.setColor(role, color)


def set_best_foreground_color(palette: Qt.QPalette):
    """Changes the foregroud color of a `QPalette` to set the best one according to background color."""
    bg = palette.color(Qt.QPalette.Base)
    if isinstance(bg, Qt.QColor):
        fg = get_best_foreground(bg)
        # These lines may change the foreground color for different kinds of widgets.
        update_palette_color(palette, fg, Qt.QPalette.Text)
        update_palette_color(palette, fg, Qt.QPalette.WindowText)
        update_brush(palette, fg, Qt.QPalette.Text)
        update_brush(palette, bg, Qt.QPalette.Base)
        # This line is the one that really does change the foreground color for TaurusLabel.
        update_brush(palette, fg, Qt.QPalette.WindowText)


[docs] class QtColorPalette(ColorPalette): def __init__(self, dat, int_decoder_dict): ColorPalette.__init__(self, dat, int_decoder_dict) self._qcolor_cache_fg = dict() self._qcolor_cache_bg = dict() self._qbrush_cache_fg = dict() self._qbrush_cache_bg = dict()
[docs] def qbrush(self, stoq): """Returns the brush for the specified state or quality""" name = self._decoder(stoq) f = self._qbrush_cache_fg b = self._qbrush_cache_bg if name not in f: f[name] = Qt.QBrush(self.qcolor(stoq)[1]) if name not in b: b[name] = Qt.QBrush(self.qcolor(stoq)[0]) if name == "None": b[name].setStyle(Qt.Qt.BDiagPattern) return (b[name], f[name])
[docs] def qcolor(self, stoq): """Returns the color for the specified state or quality""" name = self._decoder(stoq) f = self._qcolor_cache_fg b = self._qcolor_cache_bg if name not in f: f[name] = Qt.QColor(self.number(name, True)) if name not in b: b[name] = Qt.QColor(self.number(name)) return (b[name], f[name])
@deprecation_decorator(alt="QtColorPalette.qcolor()", rel="4.5") def qvariant(self, stoq): """Returns the color for the specified state or quality""" return self.qcolor(stoq)
QT_ATTRIBUTE_QUALITY_PALETTE = QtColorPalette(ATTRIBUTE_QUALITY_DATA, AttrQuality) try: # TODO: Tango-centric from taurus.core.tango import DevState QT_DEVICE_STATE_PALETTE = QtColorPalette(DEVICE_STATE_DATA, DevState) except ImportError: QT_DEVICE_STATE_PALETTE = QtColorPalette(DEVICE_STATE_DATA, {})