Fix transformations in GradientLegend (#1639)
* rebuilt transformations * restored drawing order to bg > bar > labels (and removed some print statements)
This commit is contained in:
parent
8e32eeb1a5
commit
5fa3901c00
@ -50,6 +50,9 @@ class GradientLegend(UIGraphicsItem):
|
||||
if unit[0] is None:
|
||||
return
|
||||
|
||||
## Have to scale painter so that text and gradients are correct size and not upside down
|
||||
p.scale(unit[0], -unit[1])
|
||||
|
||||
## determine max width of all labels
|
||||
labelWidth = 0
|
||||
labelHeight = 0
|
||||
@ -58,57 +61,54 @@ class GradientLegend(UIGraphicsItem):
|
||||
labelWidth = max(labelWidth, b.width())
|
||||
labelHeight = max(labelHeight, b.height())
|
||||
|
||||
labelWidth *= unit[0]
|
||||
labelHeight *= unit[1]
|
||||
|
||||
textPadding = 2 # in px
|
||||
|
||||
xR = rect.right() / unit[0]
|
||||
xL = rect.left() / unit[0]
|
||||
yB = -(rect.top() / unit[1])
|
||||
yT = -(rect.bottom() / unit[1])
|
||||
|
||||
# coordinates describe edges of text and bar, additional margins will be added for background
|
||||
if self.offset[0] < 0:
|
||||
x3 = rect.right() + unit[0] * self.offset[0]
|
||||
x2 = x3 - labelWidth - unit[0]*textPadding*2
|
||||
x1 = x2 - unit[0] * self.size[0]
|
||||
x3 = xR + self.offset[0] # right edge from right edge of view, offset is negative!
|
||||
x2 = x3 - labelWidth - 2*textPadding # right side of color bar
|
||||
x1 = x2 - self.size[0] # left side of color bar
|
||||
else:
|
||||
x1 = rect.left() + unit[0] * self.offset[0]
|
||||
x2 = x1 + unit[0] * self.size[0]
|
||||
x3 = x2 + labelWidth + unit[0]*textPadding*2
|
||||
x1 = xL + self.offset[0] # left edge from left edge of view
|
||||
x2 = x1 + self.size[0]
|
||||
x3 = x2 + labelWidth + 2*textPadding # leave room for 2x textpadding between bar and text
|
||||
if self.offset[1] < 0:
|
||||
y2 = rect.top() - unit[1] * self.offset[1]
|
||||
y1 = y2 + unit[1] * self.size[1]
|
||||
y2 = yB + self.offset[1] # bottom edge from bottom of view, offset is negative!
|
||||
y1 = y2 - self.size[1]
|
||||
else:
|
||||
y1 = rect.bottom() - unit[1] * self.offset[1]
|
||||
y2 = y1 - unit[1] * self.size[1]
|
||||
y1 = yT + self.offset[1] # top edge from top of view
|
||||
y2 = y1 + self.size[1]
|
||||
self.b = [x1,x2,x3,y1,y2,labelWidth]
|
||||
|
||||
## Draw background
|
||||
p.setPen(self.pen)
|
||||
p.setBrush(QtGui.QBrush(QtGui.QColor(255,255,255,100)))
|
||||
rect = QtCore.QRectF(
|
||||
QtCore.QPointF(x1 - unit[0]*textPadding, y1 + labelHeight/2 + unit[1]*textPadding),
|
||||
QtCore.QPointF(x3, y2 - labelHeight/2 - unit[1]*textPadding)
|
||||
QtCore.QPointF(x1 - textPadding, y1-labelHeight/2 - textPadding), # extra left/top padding
|
||||
QtCore.QPointF(x3 + textPadding, y2+labelHeight/2 + textPadding) # extra bottom/right padding
|
||||
)
|
||||
p.drawRect(rect)
|
||||
|
||||
|
||||
## Have to scale painter so that text and gradients are correct size. Bleh.
|
||||
p.scale(unit[0], unit[1])
|
||||
|
||||
## Draw color bar
|
||||
self.gradient.setStart(0, y1/unit[1])
|
||||
self.gradient.setFinalStop(0, y2/unit[1])
|
||||
self.gradient.setStart(0, y1)
|
||||
self.gradient.setFinalStop(0, y2)
|
||||
p.setBrush(self.gradient)
|
||||
rect = QtCore.QRectF(
|
||||
QtCore.QPointF(x1/unit[0], y1/unit[1]),
|
||||
QtCore.QPointF(x2/unit[0], y2/unit[1])
|
||||
QtCore.QPointF(x1, y1),
|
||||
QtCore.QPointF(x2, y2)
|
||||
)
|
||||
p.drawRect(rect)
|
||||
|
||||
|
||||
## draw labels
|
||||
p.setPen(QtGui.QPen(QtGui.QColor(0,0,0)))
|
||||
tx = x2 + unit[0]*textPadding
|
||||
lh = labelHeight/unit[1]
|
||||
tx = x2 + 2 * textPadding # margin between bar and text
|
||||
lh = labelHeight
|
||||
lw = labelWidth
|
||||
for k in self.labels:
|
||||
y = y1 + self.labels[k] * (y2-y1)
|
||||
p.drawText(QtCore.QRectF(tx/unit[0], y/unit[1] - lh/2.0, 1000, lh), QtCore.Qt.AlignLeft | QtCore.Qt.AlignVCenter, str(k))
|
||||
|
||||
|
||||
p.drawText(QtCore.QRectF(tx, y - lh/2, lw, lh), QtCore.Qt.AlignLeft | QtCore.Qt.AlignVCenter, str(k))
|
||||
|
Loading…
Reference in New Issue
Block a user