Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added background color for markers #4012

Merged
merged 3 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/silx/gui/plot/backends/BackendBase.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

from collections.abc import Callable
import weakref
from silx.gui.colors import RGBAColorType

from ... import qt

Expand Down Expand Up @@ -218,6 +219,7 @@ def addMarker(
constraint: Callable[[float, float], tuple[float, float]] | None,
yaxis: str,
font: qt.QFont,
bgcolor: RGBAColorType | None,
) -> object:
"""Add a point, vertical line or horizontal line marker to the plot.

Expand All @@ -227,6 +229,7 @@ def addMarker(
If None, the marker is a vertical line.
:param text: Text associated to the marker (or None for no text)
:param color: Color to be used for instance 'blue', 'b', '#FF0000'
:param bgcolor: Text background color to be used for instance 'blue', 'b', '#FF0000'
:param symbol: Symbol representing the marker.
Only relevant for point markers where X and Y are not None.
Value in:
Expand Down
19 changes: 18 additions & 1 deletion src/silx/gui/plot/backends/BackendMatplotlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
# ###########################################################################*/
"""Matplotlib Plot backend."""

from __future__ import annotations

__authors__ = ["V.A. Sole", "T. Vincent, H. Payno"]
__license__ = "MIT"
__date__ = "21/12/2018"
Expand Down Expand Up @@ -67,6 +69,7 @@
)
from ...qt import inspect as qt_inspect
from .... import config
from silx.gui.colors import RGBAColorType

_PATCH_LINESTYLE = {
"-": "solid",
Expand Down Expand Up @@ -941,7 +944,18 @@ def addShape(
return item

def addMarker(
self, x, y, text, color, symbol, linestyle, linewidth, constraint, yaxis, font
self,
x,
y,
text,
color,
symbol,
linestyle,
linewidth,
constraint,
yaxis,
font,
bgcolor: RGBAColorType | None,
):
textArtist = None
fontProperties = None if font is None else qFontToFontProperties(font)
Expand All @@ -968,6 +982,7 @@ def addMarker(
y,
text,
color=color,
backgroundcolor=bgcolor,
vallsv marked this conversation as resolved.
Show resolved Hide resolved
horizontalalignment="left",
fontproperties=fontProperties,
)
Expand All @@ -982,6 +997,7 @@ def addMarker(
1.0,
text,
color=color,
backgroundcolor=bgcolor,
horizontalalignment="left",
verticalalignment="top",
fontproperties=fontProperties,
Expand All @@ -997,6 +1013,7 @@ def addMarker(
y,
text,
color=color,
backgroundcolor=bgcolor,
horizontalalignment="right",
verticalalignment="top",
fontproperties=fontProperties,
Expand Down
59 changes: 46 additions & 13 deletions src/silx/gui/plot/backends/BackendOpenGL.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
# ############################################################################*/
"""OpenGL Plot backend."""

from __future__ import annotations

__authors__ = ["T. Vincent"]
__license__ = "MIT"
__date__ = "21/12/2018"
Expand All @@ -42,6 +44,7 @@
from ... import _glutils as glu
from . import glutils
from .glutils.PlotImageFile import saveImageToFile
from silx.gui.colors import RGBAColorType

_logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -90,7 +93,18 @@ def __init__(

class _MarkerItem(dict):
def __init__(
self, x, y, text, color, symbol, linestyle, linewidth, constraint, yaxis, font
self,
x,
y,
text,
color,
symbol,
linestyle,
linewidth,
constraint,
yaxis,
font,
bgcolor,
):
super(_MarkerItem, self).__init__()

Expand All @@ -114,6 +128,7 @@ def __init__(
"linewidth": linewidth,
"yaxis": yaxis,
"font": font,
"bgcolor": bgcolor,
}
)

Expand Down Expand Up @@ -590,10 +605,7 @@ def _renderItems(self, overlay=False):
continue

color = item["color"]
vallsv marked this conversation as resolved.
Show resolved Hide resolved
intensity = color[0] * 0.299 + color[1] * 0.587 + color[2] * 0.114
bgColor = (
(1.0, 1.0, 1.0, 0.75) if intensity <= 0.5 else (0.0, 0.0, 0.0, 0.75)
)
bgColor = item["bgcolor"]
if xCoord is None or yCoord is None:
if xCoord is None: # Horizontal line in data space
pixelPos = self._plotFrame.dataToPixel(
Expand All @@ -612,7 +624,7 @@ def _renderItems(self, overlay=False):
item["font"],
x,
y,
color=item["color"],
color=color,
bgColor=bgColor,
align=glutils.RIGHT,
valign=glutils.BOTTOM,
Expand All @@ -625,7 +637,7 @@ def _renderItems(self, overlay=False):
(0, width),
(pixelPos[1], pixelPos[1]),
style=item["linestyle"],
color=item["color"],
color=color,
width=item["linewidth"],
)
context.matrix = self.matScreenProj
Expand All @@ -645,7 +657,7 @@ def _renderItems(self, overlay=False):
item["font"],
x,
y,
color=item["color"],
color=color,
bgColor=bgColor,
align=glutils.LEFT,
valign=glutils.TOP,
Expand All @@ -658,7 +670,7 @@ def _renderItems(self, overlay=False):
(pixelPos[0], pixelPos[0]),
(0, height),
style=item["linestyle"],
color=item["color"],
color=color,
width=item["linewidth"],
)
context.matrix = self.matScreenProj
Expand Down Expand Up @@ -687,7 +699,7 @@ def _renderItems(self, overlay=False):
item["font"],
x,
y,
color=item["color"],
color=color,
bgColor=bgColor,
align=glutils.LEFT,
valign=valign,
Expand All @@ -701,7 +713,7 @@ def _renderItems(self, overlay=False):
(pixelPos[0],),
(pixelPos[1],),
marker=item["symbol"],
color=item["color"],
color=color,
size=11,
)
context.matrix = self.matScreenProj
Expand Down Expand Up @@ -1084,11 +1096,32 @@ def addShape(
)

def addMarker(
self, x, y, text, color, symbol, linestyle, linewidth, constraint, yaxis, font
self,
x,
y,
text,
color,
symbol,
linestyle,
linewidth,
constraint,
yaxis,
font,
bgcolor: RGBAColorType | None,
):
font = qt.QApplication.instance().font() if font is None else font
return _MarkerItem(
x, y, text, color, symbol, linestyle, linewidth, constraint, yaxis, font
x,
y,
text,
color,
symbol,
linestyle,
linewidth,
constraint,
yaxis,
font,
bgcolor,
)

# Remove methods
Expand Down
3 changes: 2 additions & 1 deletion src/silx/gui/plot/backends/glutils/GLText.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
from .... import qt
from ...._glutils import font, gl, Context, Program, Texture
from .GLSupport import mat4Translate
from silx.gui.colors import RGBAColorType


class _Cache:
Expand Down Expand Up @@ -140,7 +141,7 @@ def __init__(
x: float = 0.0,
y: float = 0.0,
color: tuple[float, float, float, float] = (0.0, 0.0, 0.0, 1.0),
bgColor: tuple[float, float, float, float] | None = None,
bgColor: RGBAColorType | None = None,
align: str = LEFT,
valign: str = BASELINE,
rotate: float = 0.0,
Expand Down
3 changes: 3 additions & 0 deletions src/silx/gui/plot/items/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ class ItemChangedType(enum.Enum):
FONT = "fontChanged"
"""Item's text font changed flag."""

BACKGROUND_COLOR = "backgroundColorChanged"
"""Item's text background color changed flag."""


class Item(qt.QObject):
"""Description of an item of the plot"""
Expand Down
22 changes: 22 additions & 0 deletions src/silx/gui/plot/items/marker.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@


import logging
import numpy

from ....utils.proxy import docstring
from .core import (
Expand All @@ -44,6 +45,8 @@
)
from silx import config
from silx.gui import qt
from silx.gui import colors


_logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -75,6 +78,7 @@ def __init__(self):

self._x = None
self._y = None
self._bgColor: colors.RGBAColorType | None = None
self._constraint = self._defaultConstraint
self.__isBeingDragged = False

Expand All @@ -91,6 +95,7 @@ def _addRendererCall(self, backend, symbol=None, linestyle="-", linewidth=1):
constraint=self.getConstraint(),
yaxis=self.getYAxis(),
font=self._font, # Do not use getFont to spare creating a new QFont
bgcolor=self.getBackgroundColor(),
)

def _addBackendRenderer(self, backend):
Expand Down Expand Up @@ -141,6 +146,23 @@ def setFont(self, font: qt.QFont | None):
self._font = None if font is None else qt.QFont(font)
self._updated(ItemChangedType.FONT)

def getBackgroundColor(self) -> colors.RGBAColorType | None:
"""Returns the RGBA background color of the item"""
return self._bgColor

def setBackgroundColor(self, color):
"""Set item text background color

:param color: color(s) to be used as a str ("#RRGGBB") or (npoints, 4)
unsigned byte array or one of the predefined color names
defined in colors.py
"""
if color is not None:
color = colors.rgba(color)
if self._bgColor != color:
self._bgColor = color
self._updated(ItemChangedType.BACKGROUND_COLOR)

def getXPosition(self):
"""Returns the X position of the marker line in data coordinates

Expand Down