Merge pull request #1382 from dgoeries/code-quality-legend

LegendItem: A bit auto flake8
This commit is contained in:
Ogi Moore 2020-10-12 22:28:00 -07:00 committed by GitHub
commit 4946a57987
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -8,6 +8,7 @@ from .ScatterPlotItem import ScatterPlotItem, drawSymbol
from .PlotDataItem import PlotDataItem from .PlotDataItem import PlotDataItem
from .GraphicsWidgetAnchor import GraphicsWidgetAnchor from .GraphicsWidgetAnchor import GraphicsWidgetAnchor
from .BarGraphItem import BarGraphItem from .BarGraphItem import BarGraphItem
__all__ = ['LegendItem'] __all__ = ['LegendItem']
@ -25,8 +26,10 @@ class LegendItem(GraphicsWidget, GraphicsWidgetAnchor):
legend.setParentItem(plotItem) legend.setParentItem(plotItem)
""" """
def __init__(self, size=None, offset=None, horSpacing=25, verSpacing=0, pen=None,
brush=None, labelTextColor=None, frame=True, rowCount=1, colCount=1, **kwargs): def __init__(self, size=None, offset=None, horSpacing=25, verSpacing=0,
pen=None, brush=None, labelTextColor=None, frame=True,
rowCount=1, colCount=1, **kwargs):
""" """
============== =============================================================== ============== ===============================================================
**Arguments:** **Arguments:**
@ -160,11 +163,12 @@ class LegendItem(GraphicsWidget, GraphicsWidgetAnchor):
title The title to display for this item. Simple HTML allowed. title The title to display for this item. Simple HTML allowed.
============== ======================================================== ============== ========================================================
""" """
label = LabelItem(name, color=self.opts['labelTextColor'], justify='left') label = LabelItem(name, color=self.opts['labelTextColor'],
justify='left')
if isinstance(item, ItemSample): if isinstance(item, ItemSample):
sample = item sample = item
else: else:
sample = ItemSample(item) sample = ItemSample(item)
self.items.append((sample, label)) self.items.append((sample, label))
self._addItemToLayout(sample, label) self._addItemToLayout(sample, label)
self.updateSize() self.updateSize()
@ -174,45 +178,45 @@ class LegendItem(GraphicsWidget, GraphicsWidgetAnchor):
row = self.layout.rowCount() row = self.layout.rowCount()
if row: if row:
row -= 1 row -= 1
nCol = self.columnCount*2 nCol = self.columnCount * 2
#FIRST ROW FULL # FIRST ROW FULL
if col == nCol: if col == nCol:
for col in range(0,nCol,2): for col in range(0, nCol, 2):
#FIND RIGHT COLUMN # FIND RIGHT COLUMN
if not self.layout.itemAt(row, col): if not self.layout.itemAt(row, col):
break break
if col+2 == nCol: if col + 2 == nCol:
#MAKE NEW ROW # MAKE NEW ROW
col = 0 col = 0
row += 1 row += 1
self.layout.addItem(sample, row, col) self.layout.addItem(sample, row, col)
self.layout.addItem(label, row, col+1) self.layout.addItem(label, row, col + 1)
def setColumnCount(self, columnCount): def setColumnCount(self, columnCount):
''' """change the orientation of all items of the legend
change the orientation of all items of the legend """
'''
if columnCount != self.columnCount: if columnCount != self.columnCount:
self.columnCount = columnCount self.columnCount = columnCount
self.rowCount = int(len(self.items)/columnCount) self.rowCount = int(len(self.items) / columnCount)
for i in range(self.layout.count()-1,-1,-1): for i in range(self.layout.count() - 1, -1, -1):
self.layout.removeAt(i) #clear layout self.layout.removeAt(i) # clear layout
for sample, label in self.items: for sample, label in self.items:
self._addItemToLayout(sample, label) self._addItemToLayout(sample, label)
self.updateSize() self.updateSize()
def getLabel(self, plotItem): def getLabel(self, plotItem):
"""Return the labelItem inside the legend for a given plotItem
The label-text can be changed via labenItem.setText
""" """
return the labelItem inside the legend for a given plotItem out = [(it, lab) for it, lab in self.items if it.item == plotItem]
the label-text can be changed via labenItem.setText try:
""" return out[0][1]
out = [(it, lab) for it, lab in self.items if it.item==plotItem] except IndexError:
try: return out[0][1] return None
except IndexError: return None
def removeItem(self, item): def removeItem(self, item):
""" """Removes one item from the legend.
Removes one item from the legend.
============== ======================================================== ============== ========================================================
**Arguments:** **Arguments:**
@ -221,13 +225,13 @@ class LegendItem(GraphicsWidget, GraphicsWidgetAnchor):
""" """
for sample, label in self.items: for sample, label in self.items:
if sample.item is item or label.text == item: if sample.item is item or label.text == item:
self.items.remove((sample, label)) # remove from itemlist self.items.remove((sample, label)) # remove from itemlist
self.layout.removeItem(sample) # remove from layout self.layout.removeItem(sample) # remove from layout
sample.close() # remove from drawing sample.close() # remove from drawing
self.layout.removeItem(label) self.layout.removeItem(label)
label.close() label.close()
self.updateSize() # redraq box self.updateSize() # redraq box
return # return after first match return # return after first match
def clear(self): def clear(self):
"""Remove all items from the legend.""" """Remove all items from the legend."""
@ -246,7 +250,7 @@ class LegendItem(GraphicsWidget, GraphicsWidgetAnchor):
height = 0 height = 0
width = 0 width = 0
for row in range(self.layout.rowCount()): for row in range(self.layout.rowCount()):
row_height = 0 row_height = 0
col_witdh = 0 col_witdh = 0
for col in range(self.layout.columnCount()): for col in range(self.layout.columnCount()):
item = self.layout.itemAt(row, col) item = self.layout.itemAt(row, col)
@ -278,11 +282,12 @@ class LegendItem(GraphicsWidget, GraphicsWidgetAnchor):
class ItemSample(GraphicsWidget): class ItemSample(GraphicsWidget):
""" Class responsible for drawing a single item in a LegendItem (sans label). """Class responsible for drawing a single item in a LegendItem (sans label)
This may be subclassed to draw custom graphics in a Legend. This may be subclassed to draw custom graphics in a Legend.
""" """
## Todo: make this more generic; let each item decide how it should be represented.
# TODO: make this more generic; let items decide how it should be look.
def __init__(self, item): def __init__(self, item):
GraphicsWidget.__init__(self) GraphicsWidget.__init__(self)
self.item = item self.item = item
@ -300,18 +305,21 @@ class ItemSample(GraphicsWidget):
p.setPen(fn.mkPen(opts['pen'])) p.setPen(fn.mkPen(opts['pen']))
p.drawLine(0, 11, 20, 11) p.drawLine(0, 11, 20, 11)
if opts.get('fillLevel', None) is not None and opts.get('fillBrush', None) is not None: if (opts.get('fillLevel', None) is not None and
opts.get('fillBrush', None) is not None):
p.setBrush(fn.mkBrush(opts['fillBrush'])) p.setBrush(fn.mkBrush(opts['fillBrush']))
p.setPen(fn.mkPen(opts['fillBrush'])) p.setPen(fn.mkPen(opts['fillBrush']))
p.drawPolygon(QtGui.QPolygonF([QtCore.QPointF(2, 18), QtCore.QPointF(18, 2), QtCore.QPointF(18, 18)])) p.drawPolygon(QtGui.QPolygonF(
[QtCore.QPointF(2, 18), QtCore.QPointF(18, 2),
QtCore.QPointF(18, 18)]))
symbol = opts.get('symbol', None) symbol = opts.get('symbol', None)
if symbol is not None: if symbol is not None:
if isinstance(self.item, PlotDataItem): if isinstance(self.item, PlotDataItem):
opts = self.item.scatter.opts opts = self.item.scatter.opts
p.translate(10, 10) p.translate(10, 10)
drawSymbol(p, symbol, opts['size'], fn.mkPen(opts['pen']), fn.mkBrush(opts['brush'])) drawSymbol(p, symbol, opts['size'], fn.mkPen(opts['pen']),
fn.mkBrush(opts['brush']))
if isinstance(self.item, BarGraphItem): if isinstance(self.item, BarGraphItem):
p.setBrush(fn.mkBrush(opts['brush'])) p.setBrush(fn.mkBrush(opts['brush']))