Skip to content

Commit 90f55b0

Browse files
committed
Make PlotWidget.get* methods accept item as legend argument
1 parent 0388f74 commit 90f55b0

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

src/silx/gui/plot/PlotWidget.py

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2474,18 +2474,21 @@ def getAllCurves(self, just_legend=False, withhidden=False):
24742474
]
24752475
return [curve.getName() for curve in curves] if just_legend else curves
24762476

2477-
def getCurve(self, legend=None):
2477+
def getCurve(self, legend: str | items.Curve | None = None) -> items.Curve:
24782478
"""Get the object describing a specific curve.
24792479
24802480
It returns None in case no matching curve is found.
24812481
2482-
:param str legend:
2482+
:param legend:
24832483
The legend identifying the curve.
24842484
If not provided or None (the default), the active curve is returned
24852485
or if there is no active curve, the latest updated curve that is
24862486
not hidden is returned if there are curves in the plot.
24872487
:return: None or :class:`.items.Curve` object
24882488
"""
2489+
if isinstance(legend, items.Curve):
2490+
_logger.warning("getCurve call not needed: legend is already an item")
2491+
return legend
24892492
return self._getItem(kind="curve", legend=legend)
24902493

24912494
def getAllImages(self, just_legend=False):
@@ -2506,48 +2509,57 @@ def getAllImages(self, just_legend=False):
25062509
images = [item for item in self.getItems() if isinstance(item, items.ImageBase)]
25072510
return [image.getName() for image in images] if just_legend else images
25082511

2509-
def getImage(self, legend=None):
2512+
def getImage(self, legend: str | items.ImageBase | None = None) -> items.ImageBase:
25102513
"""Get the object describing a specific image.
25112514
25122515
It returns None in case no matching image is found.
25132516
2514-
:param str legend:
2517+
:param legend:
25152518
The legend identifying the image.
25162519
If not provided or None (the default), the active image is returned
25172520
or if there is no active image, the latest updated image
25182521
is returned if there are images in the plot.
25192522
:return: None or :class:`.items.ImageBase` object
25202523
"""
2524+
if isinstance(legend, items.ImageBase):
2525+
_logger.warning("getImage call not needed: legend is already an item")
2526+
return legend
25212527
return self._getItem(kind="image", legend=legend)
25222528

2523-
def getScatter(self, legend=None):
2529+
def getScatter(self, legend: str | items.Scatter | None = None) -> items.Scatter:
25242530
"""Get the object describing a specific scatter.
25252531
25262532
It returns None in case no matching scatter is found.
25272533
2528-
:param str legend:
2534+
:param legend:
25292535
The legend identifying the scatter.
25302536
If not provided or None (the default), the active scatter is
25312537
returned or if there is no active scatter, the latest updated
25322538
scatter is returned if there are scatters in the plot.
25332539
:return: None or :class:`.items.Scatter` object
25342540
"""
2541+
if isinstance(legend, items.Scatter):
2542+
_logger.warning("getScatter call not needed: legend is already an item")
2543+
return legend
25352544
return self._getItem(kind="scatter", legend=legend)
25362545

2537-
def getHistogram(self, legend=None):
2546+
def getHistogram(self, legend: str | items.Histogram | None = None) -> items.Histogram:
25382547
"""Get the object describing a specific histogram.
25392548
25402549
It returns None in case no matching histogram is found.
25412550
2542-
:param str legend:
2551+
:param legend:
25432552
The legend identifying the histogram.
25442553
If not provided or None (the default), the latest updated scatter
25452554
is returned if there are histograms in the plot.
25462555
:return: None or :class:`.items.Histogram` object
25472556
"""
2557+
if isinstance(legend, items.Histogram):
2558+
_logger.warning("getHistogram call not needed: legend is already an item")
2559+
return legend
25482560
return self._getItem(kind="histogram", legend=legend)
25492561

2550-
def _getItem(self, kind, legend=None):
2562+
def _getItem(self, kind, legend=None) -> items.Item:
25512563
"""Get an item from the plot: either an image or a curve.
25522564
25532565
Returns None if no match found.
@@ -2558,6 +2570,10 @@ def _getItem(self, kind, legend=None):
25582570
None to get active or last item
25592571
:return: Object describing the item or None
25602572
"""
2573+
if isinstance(legend, items.Item):
2574+
_logger.warning("_getItem call not needed: legend is already an item")
2575+
return legend
2576+
25612577
assert kind in self.ITEM_KINDS
25622578

25632579
if legend is not None:

0 commit comments

Comments
 (0)