AxisItem: Account for empty strings in the visibility of text and units (#1367)

* Rebase

* make the tests work

* add test and rather more cleanup

* Cleanup for axisitem visibility test

* Another cleanup in test axis item
This commit is contained in:
Dennis Göries 2020-10-15 05:40:54 +02:00 committed by GitHub
parent e7b11cb39a
commit 3b6eb02520
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 24 deletions

View File

@ -283,19 +283,19 @@ class AxisItem(GraphicsWidget):
axis.setLabel('label text', units='V', **labelStyle)
"""
show_label = False
if text is not None:
self.labelText = text
show_label = True
if units is not None:
self.labelUnits = units
show_label = True
if show_label:
self.showLabel()
if unitPrefix is not None:
self.labelUnitPrefix = unitPrefix
# `None` input is kept for backward compatibility!
self.labelText = text or ""
self.labelUnits = units or ""
self.labelUnitPrefix = unitPrefix or ""
if len(args) > 0:
self.labelStyle = args
# Account empty string and `None` for units and text
visible = True if (text or units) else False
self.showLabel(visible)
self._updateLabel()
def _updateLabel(self):
"""Internal method to update the label according to the text"""
self.label.setHtml(self.labelString())
self._adjustSize()
self.picture = None
@ -428,8 +428,7 @@ class AxisItem(GraphicsWidget):
else:
self._pen = fn.mkPen(getConfigOption('foreground'))
self.labelStyle['color'] = '#' + fn.colorStr(self._pen.color())[:6]
self.setLabel()
self.update()
self._updateLabel()
def textPen(self):
if self._textPen is None:
@ -447,8 +446,7 @@ class AxisItem(GraphicsWidget):
else:
self._textPen = fn.mkPen(getConfigOption('foreground'))
self.labelStyle['color'] = '#' + fn.colorStr(self._textPen.color())[:6]
self.setLabel()
self.update()
self._updateLabel()
def setScale(self, scale=None):
"""
@ -465,9 +463,7 @@ class AxisItem(GraphicsWidget):
if scale != self.scale:
self.scale = scale
self.setLabel()
self.picture = None
self.update()
self._updateLabel()
def enableAutoSIPrefix(self, enable=True):
"""
@ -498,13 +494,11 @@ class AxisItem(GraphicsWidget):
scale = 1.0
prefix = ''
self.autoSIPrefixScale = scale
self.setLabel(unitPrefix=prefix)
self.labelUnitPrefix = prefix
else:
self.autoSIPrefixScale = 1.0
self.picture = None
self.update()
self._updateLabel()
def setRange(self, mn, mx):
"""Set the range of values displayed by the axis.
@ -513,9 +507,11 @@ class AxisItem(GraphicsWidget):
raise Exception("Not setting range to [%s, %s]" % (str(mn), str(mx)))
self.range = [mn, mx]
if self.autoSIPrefix:
# XXX: Will already update once!
self.updateAutoSIPrefix()
self.picture = None
self.update()
else:
self.picture = None
self.update()
def linkedView(self):
"""Return the ViewBox this axis is linked to"""

View File

@ -2,6 +2,7 @@ import pyqtgraph as pg
app = pg.mkQApp()
def test_AxisItem_stopAxisAtTick(monkeypatch):
def test_bottom(p, axisSpec, tickSpecs, textSpecs):
assert view.mapToView(axisSpec[1]).x() == 0.25
@ -114,3 +115,30 @@ def test_AxisItem_tickFont(monkeypatch):
plot.show()
app.processEvents()
plot.close()
def test_AxisItem_label_visibility():
"""Test the visibility of the axis item using `setLabel`"""
axis = pg.AxisItem('left')
assert axis.labelText == ''
assert axis.labelUnits == ''
assert not axis.label.isVisible()
axis.setLabel(text='Position', units='mm')
assert axis.labelText == 'Position'
assert axis.labelUnits == 'mm'
assert axis.label.isVisible()
# XXX: `None` is converted to empty strings.
axis.setLabel(text=None, units=None)
assert axis.labelText == ''
assert axis.labelUnits == ''
assert not axis.label.isVisible()
axis.setLabel(text='Current', units=None)
assert axis.labelText == 'Current'
assert axis.labelUnits == ''
assert axis.label.isVisible()
axis.setLabel(text=None, units=None)
assert not axis.label.isVisible()
axis.setLabel(text='', units='V')
assert axis.labelText == ''
assert axis.labelUnits == 'V'
assert axis.label.isVisible()