Draw GradientLegend in ViewBox coordinates (#1864)
* draw GradientLegend in ViewBox coordinates * some cleanup * do not proceed with dummy values * correct bar and label order, add some styling options * remove debugging code
This commit is contained in:
parent
4b7dfdef88
commit
523b31e97f
@ -104,7 +104,6 @@ def _getFromFile(name):
|
|||||||
else:
|
else:
|
||||||
csv_mode = False
|
csv_mode = False
|
||||||
for line in fh:
|
for line in fh:
|
||||||
name = None
|
|
||||||
line = line.strip()
|
line = line.strip()
|
||||||
if len(line) == 0: continue # empty line
|
if len(line) == 0: continue # empty line
|
||||||
if line[0] == ';': continue # comment
|
if line[0] == ';': continue # comment
|
||||||
|
@ -15,17 +15,25 @@ class GradientLegend(UIGraphicsItem):
|
|||||||
self.offset = offset
|
self.offset = offset
|
||||||
UIGraphicsItem.__init__(self)
|
UIGraphicsItem.__init__(self)
|
||||||
self.setAcceptedMouseButtons(QtCore.Qt.MouseButton.NoButton)
|
self.setAcceptedMouseButtons(QtCore.Qt.MouseButton.NoButton)
|
||||||
self.brush = QtGui.QBrush(QtGui.QColor(200,0,0))
|
self.brush = QtGui.QBrush(QtGui.QColor(255,255,255,100)) # background color
|
||||||
self.pen = QtGui.QPen(QtGui.QColor(0,0,0))
|
self.pen = QtGui.QPen(QtGui.QColor(0,0,0))
|
||||||
|
self.textPen = QtGui.QPen(QtGui.QColor(0,0,0))
|
||||||
self.labels = {'max': 1, 'min': 0}
|
self.labels = {'max': 1, 'min': 0}
|
||||||
self.gradient = QtGui.QLinearGradient()
|
self.gradient = QtGui.QLinearGradient()
|
||||||
self.gradient.setColorAt(0, QtGui.QColor(0,0,0))
|
self.gradient.setColorAt(0, QtGui.QColor(0,0,0))
|
||||||
self.gradient.setColorAt(1, QtGui.QColor(255,0,0))
|
self.gradient.setColorAt(1, QtGui.QColor(255,0,0))
|
||||||
|
self.setZValue(100) # draw on top of ordinary plots
|
||||||
|
|
||||||
def setGradient(self, g):
|
def setGradient(self, g):
|
||||||
self.gradient = g
|
self.gradient = g
|
||||||
self.update()
|
self.update()
|
||||||
|
|
||||||
|
def setColorMap(self, colormap):
|
||||||
|
"""
|
||||||
|
Set displayed gradient from a :class:`~pyqtgraph.ColorMap` object.
|
||||||
|
"""
|
||||||
|
self.gradient = colormap.getGradient()
|
||||||
|
|
||||||
def setIntColorScale(self, minVal, maxVal, *args, **kargs):
|
def setIntColorScale(self, minVal, maxVal, *args, **kargs):
|
||||||
colors = [fn.intColor(i, maxVal-minVal, *args, **kargs) for i in range(minVal, maxVal)]
|
colors = [fn.intColor(i, maxVal-minVal, *args, **kargs) for i in range(minVal, maxVal)]
|
||||||
g = QtGui.QLinearGradient()
|
g = QtGui.QLinearGradient()
|
||||||
@ -45,13 +53,14 @@ class GradientLegend(UIGraphicsItem):
|
|||||||
|
|
||||||
def paint(self, p, opt, widget):
|
def paint(self, p, opt, widget):
|
||||||
UIGraphicsItem.paint(self, p, opt, widget)
|
UIGraphicsItem.paint(self, p, opt, widget)
|
||||||
rect = self.boundingRect() ## Boundaries of visible area in scene coords.
|
|
||||||
unit = self.pixelSize() ## Size of one view pixel in scene coords.
|
|
||||||
if unit[0] is None:
|
|
||||||
return
|
|
||||||
|
|
||||||
## Have to scale painter so that text and gradients are correct size and not upside down
|
view = self.getViewBox()
|
||||||
p.scale(unit[0], -unit[1])
|
if view is None:
|
||||||
|
return
|
||||||
|
p.save() # save painter state before we change transformation
|
||||||
|
trans = view.sceneTransform()
|
||||||
|
p.setTransform( trans ) # draw in ViewBox pixel coordinates
|
||||||
|
rect = view.rect()
|
||||||
|
|
||||||
## determine max width of all labels
|
## determine max width of all labels
|
||||||
labelWidth = 0
|
labelWidth = 0
|
||||||
@ -63,10 +72,10 @@ class GradientLegend(UIGraphicsItem):
|
|||||||
|
|
||||||
textPadding = 2 # in px
|
textPadding = 2 # in px
|
||||||
|
|
||||||
xR = rect.right() / unit[0]
|
xR = rect.right()
|
||||||
xL = rect.left() / unit[0]
|
xL = rect.left()
|
||||||
yB = -(rect.top() / unit[1])
|
yT = rect.top()
|
||||||
yT = -(rect.bottom() / unit[1])
|
yB = rect.bottom()
|
||||||
|
|
||||||
# coordinates describe edges of text and bar, additional margins will be added for background
|
# coordinates describe edges of text and bar, additional margins will be added for background
|
||||||
if self.offset[0] < 0:
|
if self.offset[0] < 0:
|
||||||
@ -87,7 +96,7 @@ class GradientLegend(UIGraphicsItem):
|
|||||||
|
|
||||||
## Draw background
|
## Draw background
|
||||||
p.setPen(self.pen)
|
p.setPen(self.pen)
|
||||||
p.setBrush(QtGui.QBrush(QtGui.QColor(255,255,255,100)))
|
p.setBrush(self.brush) # background color
|
||||||
rect = QtCore.QRectF(
|
rect = QtCore.QRectF(
|
||||||
QtCore.QPointF(x1 - textPadding, y1-labelHeight/2 - textPadding), # extra left/top padding
|
QtCore.QPointF(x1 - textPadding, y1-labelHeight/2 - textPadding), # extra left/top padding
|
||||||
QtCore.QPointF(x3 + textPadding, y2+labelHeight/2 + textPadding) # extra bottom/right padding
|
QtCore.QPointF(x3 + textPadding, y2+labelHeight/2 + textPadding) # extra bottom/right padding
|
||||||
@ -95,8 +104,8 @@ class GradientLegend(UIGraphicsItem):
|
|||||||
p.drawRect(rect)
|
p.drawRect(rect)
|
||||||
|
|
||||||
## Draw color bar
|
## Draw color bar
|
||||||
self.gradient.setStart(0, y1)
|
self.gradient.setStart(0, y2)
|
||||||
self.gradient.setFinalStop(0, y2)
|
self.gradient.setFinalStop(0, y1)
|
||||||
p.setBrush(self.gradient)
|
p.setBrush(self.gradient)
|
||||||
rect = QtCore.QRectF(
|
rect = QtCore.QRectF(
|
||||||
QtCore.QPointF(x1, y1),
|
QtCore.QPointF(x1, y1),
|
||||||
@ -105,10 +114,12 @@ class GradientLegend(UIGraphicsItem):
|
|||||||
p.drawRect(rect)
|
p.drawRect(rect)
|
||||||
|
|
||||||
## draw labels
|
## draw labels
|
||||||
p.setPen(QtGui.QPen(QtGui.QColor(0,0,0)))
|
p.setPen(self.textPen)
|
||||||
tx = x2 + 2 * textPadding # margin between bar and text
|
tx = x2 + 2 * textPadding # margin between bar and text
|
||||||
lh = labelHeight
|
lh = labelHeight
|
||||||
lw = labelWidth
|
lw = labelWidth
|
||||||
for k in self.labels:
|
for k in self.labels:
|
||||||
y = y1 + self.labels[k] * (y2-y1)
|
y = y2 - self.labels[k] * (y2-y1)
|
||||||
p.drawText(QtCore.QRectF(tx, y - lh/2, lw, lh), QtCore.Qt.AlignmentFlag.AlignLeft | QtCore.Qt.AlignmentFlag.AlignVCenter, str(k))
|
p.drawText(QtCore.QRectF(tx, y - lh/2, lw, lh), QtCore.Qt.AlignmentFlag.AlignLeft | QtCore.Qt.AlignmentFlag.AlignVCenter, str(k))
|
||||||
|
|
||||||
|
p.restore() # restore QPainter transform to original state
|
||||||
|
Loading…
Reference in New Issue
Block a user