LegendItem: Enable customization of label text size and tests (#1397)

* LegendItem: Enable customization of label text size and tests

* One more CI

* Remove deleteLater of QAPP in legend item test

* Remove assert statement in setLabelTextSize

* Modify legend test without assertion
This commit is contained in:
Dennis Göries 2020-10-15 19:56:34 +02:00 committed by GitHub
parent 5eb671217c
commit b5de577c28
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 121 additions and 2 deletions

View File

@ -29,7 +29,7 @@ class LegendItem(GraphicsWidget, GraphicsWidgetAnchor):
def __init__(self, size=None, offset=None, horSpacing=25, verSpacing=0,
pen=None, brush=None, labelTextColor=None, frame=True,
rowCount=1, colCount=1, **kwargs):
labelTextSize='9pt', rowCount=1, colCount=1, **kwargs):
"""
============== ===============================================================
**Arguments:**
@ -50,6 +50,8 @@ class LegendItem(GraphicsWidget, GraphicsWidgetAnchor):
accepted by :func:`mkBrush <pyqtgraph.mkBrush>` is allowed.
labelTextColor Pen to use when drawing legend text. Any single argument
accepted by :func:`mkPen <pyqtgraph.mkPen>` is allowed.
labelTextSize Size to use when drawing legend text. Accepts CSS style
string arguments, e.g. '9pt'.
============== ===============================================================
"""
@ -75,6 +77,7 @@ class LegendItem(GraphicsWidget, GraphicsWidgetAnchor):
'pen': fn.mkPen(pen),
'brush': fn.mkBrush(brush),
'labelTextColor': labelTextColor,
'labelTextSize': labelTextSize,
'offset': offset,
}
@ -139,6 +142,21 @@ class LegendItem(GraphicsWidget, GraphicsWidgetAnchor):
self.update()
def labelTextSize(self):
"""Get the `labelTextSize` used for the item labels."""
return self.opts['labelTextSize']
def setLabelTextSize(self, size):
"""Set the `size` of the item labels.
Accepts the CSS style string arguments, e.g. '8pt'.
"""
self.opts['labelTextSize'] = size
for _, label in self.items:
label.setAttr('size', self.opts['labelTextSize'])
self.update()
def setParentItem(self, p):
"""Set the parent."""
ret = GraphicsWidget.setParentItem(self, p)
@ -164,7 +182,7 @@ class LegendItem(GraphicsWidget, GraphicsWidgetAnchor):
============== ========================================================
"""
label = LabelItem(name, color=self.opts['labelTextColor'],
justify='left')
justify='left', size=self.opts['labelTextSize'])
if isinstance(item, ItemSample):
sample = item
else:

View File

@ -0,0 +1,101 @@
import pytest
import pyqtgraph as pg
def test_legend_item_basics():
app = pg.mkQApp()
legend = pg.LegendItem()
assert legend.opts['pen'] == pg.mkPen(None)
assert legend.opts['brush'] == pg.mkBrush(None)
assert legend.opts['labelTextColor'] is None
assert legend.opts['labelTextSize'] == '9pt'
assert legend.opts['offset'] is None
assert legend.columnCount == 1
assert legend.rowCount == 1
assert legend.curRow == 0
assert legend.labelTextColor() is None
assert legend.labelTextSize() == '9pt'
assert legend.brush() == pg.mkBrush(None)
assert legend.pen() == pg.mkPen(None)
# Set brush
# ----------------------------------------------------
brush = pg.mkBrush('b')
legend.setBrush(brush)
assert legend.brush() == brush
assert legend.opts['brush'] == brush
# Set pen
# ----------------------------------------------------
pen = pg.mkPen('b')
legend.setPen(pen)
assert legend.pen() == pen
assert legend.opts['pen'] == pen
# Set labelTextColor
# ----------------------------------------------------
text_color = pg.mkColor('b')
legend.setLabelTextColor(text_color)
assert legend.labelTextColor() == text_color
assert legend.opts['labelTextColor'] == text_color
# Set labelTextSize
# ----------------------------------------------------
text_size = '12pt'
legend.setLabelTextSize(text_size)
assert legend.labelTextSize() == text_size
assert legend.opts['labelTextSize'] == text_size
# Add items
# ----------------------------------------------------
assert len(legend.items) == 0
plot = pg.PlotDataItem(name="Plot")
legend.addItem(plot, name="Plot")
assert len(legend.items) == 1
scatter = pg.PlotDataItem(name="Scatter")
legend.addItem(scatter, name="Scatter")
assert len(legend.items) == 2
assert legend.columnCount == 1
assert legend.rowCount == 1
curve = pg.PlotDataItem(name="Curve")
legend.addItem(curve, name="Curve")
assert len(legend.items) == 3
scrabble = pg.PlotDataItem(name="Scrabble")
legend.addItem(scrabble, name="Scrabble")
assert len(legend.items) == 4
assert legend.layout.rowCount() == 4
legend.setColumnCount(2)
assert legend.rowCount == 2
assert legend.layout.rowCount() == 3
# Remove items
# ----------------------------------------------------
legend.removeItem(scrabble)
assert legend.rowCount == 2
assert legend.layout.rowCount() == 2
assert scrabble not in legend.items
assert len(legend.items) == 3
legend.removeItem(curve)
assert legend.rowCount == 2
assert legend.layout.rowCount() == 1
assert curve not in legend.items
assert len(legend.items) == 2
legend.clear()
assert legend.items == []