GLGradientLegendItem: remove opengl code

change fontColor to take a mkColor() argument

use hyperlink when referring to other functions
This commit is contained in:
KIU Shueng Chuan 2021-08-03 20:29:11 +08:00
parent efa662415e
commit 482b92d700
1 changed files with 23 additions and 47 deletions

View File

@ -1,6 +1,6 @@
from ... Qt import QtCore, QtGui
from ... import functions as fn
from OpenGL.GL import *
from ... colormap import ColorMap
from ..GLGraphicsItem import GLGraphicsItem
__all__ = ['GLGradientLegendItem']
@ -18,7 +18,8 @@ class GLGradientLegendItem(GLGraphicsItem):
gradient: a pg.ColorMap used to color the colorbar
labels: a dict of "text":value to display next to the colorbar.
The value corresponds to a position in the gradient from 0 to 1.
fontColor: sets the color of the texts
fontColor: sets the color of the texts. Accepts any single argument accepted by
:func:`~pyqtgraph.mkColor`
#Todo:
size as percentage
legend title
@ -28,10 +29,12 @@ class GLGradientLegendItem(GLGraphicsItem):
self.setGLOptions(glopts)
self.pos = (10, 10)
self.size = (10, 100)
self.fontColor = (1.0, 1.0, 1.0, 1.0)
self.stops = None
self.colors = None
self.gradient = None
self.fontColor = QtGui.QColor(QtCore.Qt.GlobalColor.white)
# setup a default trivial gradient
stops = (0.0, 1.0)
self.gradient = ColorMap(pos=stops, color=(0.0, 1.0))
self._gradient = None
self.labels = {str(x) : x for x in stops}
self.setData(**kwds)
def setData(self, **kwds):
@ -45,54 +48,31 @@ class GLGradientLegendItem(GLGraphicsItem):
self.antialias = False
for arg in args:
if arg in kwds:
setattr(self, arg, kwds[arg])
for key in kwds:
value = kwds[key]
if key == 'fontColor':
value = fn.mkColor(value)
elif key == 'gradient':
self._gradient = None
setattr(self, key, value)
##todo: add title
##todo: take more gradient types
if self.gradient is not None and hasattr(self.gradient, "getStops"):
self.stops, self.colors = self.gradient.getStops("float")
self.qgradient = self.gradient.getGradient()
self.update()
def paint(self):
if self.pos is None or self.stops is None:
return
self.setupGLState()
glMatrixMode(GL_PROJECTION)
glPushMatrix()
glLoadIdentity()
glOrtho(0.0, self.view().width(), self.view().height(), 0.0, -1.0, 10.0)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
glDisable(GL_CULL_FACE)
glClear(GL_DEPTH_BUFFER_BIT)
if self._gradient is None:
self._gradient = self.gradient.getGradient()
# draw the colorbar
glTranslate(self.pos[0], self.pos[1], 0)
glScale(self.size[0], self.size[1], 0)
glBegin(GL_QUAD_STRIP)
for p, c in zip(self.stops, self.colors):
glColor4f(*c)
glVertex2d(0, 1 - p)
glColor4f(*c)
glVertex2d(1, 1 - p)
glEnd()
# draw labels
# scaling and translate doesnt work on rendertext
fontColor = QtGui.QColor(*[x * 255 for x in self.fontColor])
# could also draw the gradient using QPainter
barRect = QtCore.QRectF(self.view().width() - 60, self.pos[1], self.size[0], self.size[1])
self.qgradient.setStart(barRect.bottomLeft())
self.qgradient.setFinalStop(barRect.topLeft())
barRect = QtCore.QRectF(self.pos[0], self.pos[1], self.size[0], self.size[1])
self._gradient.setStart(barRect.bottomLeft())
self._gradient.setFinalStop(barRect.topLeft())
painter = QtGui.QPainter(self.view())
painter.fillRect(barRect, self.qgradient)
painter.setPen(fn.mkPen(fontColor))
painter.fillRect(barRect, self._gradient)
painter.setPen(self.fontColor)
for labelText, labelPosition in self.labels.items():
## todo: draw ticks, position text properly
x = 1.1 * self.size[0] + self.pos[0]
@ -101,7 +81,3 @@ class GLGradientLegendItem(GLGraphicsItem):
painter.drawText(x, y, labelText)
painter.end()
glMatrixMode(GL_PROJECTION)
glPopMatrix()
glMatrixMode(GL_MODELVIEW)