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

View File

@ -2,6 +2,7 @@ import pyqtgraph as pg
app = pg.mkQApp() app = pg.mkQApp()
def test_AxisItem_stopAxisAtTick(monkeypatch): def test_AxisItem_stopAxisAtTick(monkeypatch):
def test_bottom(p, axisSpec, tickSpecs, textSpecs): def test_bottom(p, axisSpec, tickSpecs, textSpecs):
assert view.mapToView(axisSpec[1]).x() == 0.25 assert view.mapToView(axisSpec[1]).x() == 0.25
@ -114,3 +115,30 @@ def test_AxisItem_tickFont(monkeypatch):
plot.show() plot.show()
app.processEvents() app.processEvents()
plot.close() 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()