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

View File

@ -1,6 +1,6 @@
from ... Qt import QtCore, QtGui from ... Qt import QtCore, QtGui
from ... import functions as fn from ... import functions as fn
from OpenGL.GL import * from ... colormap import ColorMap
from ..GLGraphicsItem import GLGraphicsItem from ..GLGraphicsItem import GLGraphicsItem
__all__ = ['GLGradientLegendItem'] __all__ = ['GLGradientLegendItem']
@ -18,7 +18,8 @@ class GLGradientLegendItem(GLGraphicsItem):
gradient: a pg.ColorMap used to color the colorbar gradient: a pg.ColorMap used to color the colorbar
labels: a dict of "text":value to display next to 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. 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: #Todo:
size as percentage size as percentage
legend title legend title
@ -28,10 +29,12 @@ class GLGradientLegendItem(GLGraphicsItem):
self.setGLOptions(glopts) self.setGLOptions(glopts)
self.pos = (10, 10) self.pos = (10, 10)
self.size = (10, 100) self.size = (10, 100)
self.fontColor = (1.0, 1.0, 1.0, 1.0) self.fontColor = QtGui.QColor(QtCore.Qt.GlobalColor.white)
self.stops = None # setup a default trivial gradient
self.colors = None stops = (0.0, 1.0)
self.gradient = None 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) self.setData(**kwds)
def setData(self, **kwds): def setData(self, **kwds):
@ -45,54 +48,31 @@ class GLGradientLegendItem(GLGraphicsItem):
self.antialias = False self.antialias = False
for arg in args: for key in kwds:
if arg in kwds: value = kwds[key]
setattr(self, arg, kwds[arg]) if key == 'fontColor':
value = fn.mkColor(value)
elif key == 'gradient':
self._gradient = None
setattr(self, key, value)
##todo: add title ##todo: add title
##todo: take more gradient types ##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() self.update()
def paint(self): def paint(self):
if self.pos is None or self.stops is None:
return
self.setupGLState() 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 barRect = QtCore.QRectF(self.pos[0], self.pos[1], self.size[0], self.size[1])
glTranslate(self.pos[0], self.pos[1], 0) self._gradient.setStart(barRect.bottomLeft())
glScale(self.size[0], self.size[1], 0) self._gradient.setFinalStop(barRect.topLeft())
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())
painter = QtGui.QPainter(self.view()) painter = QtGui.QPainter(self.view())
painter.fillRect(barRect, self.qgradient) painter.fillRect(barRect, self._gradient)
painter.setPen(fn.mkPen(fontColor)) painter.setPen(self.fontColor)
for labelText, labelPosition in self.labels.items(): for labelText, labelPosition in self.labels.items():
## todo: draw ticks, position text properly ## todo: draw ticks, position text properly
x = 1.1 * self.size[0] + self.pos[0] x = 1.1 * self.size[0] + self.pos[0]
@ -101,7 +81,3 @@ class GLGradientLegendItem(GLGraphicsItem):
painter.drawText(x, y, labelText) painter.drawText(x, y, labelText)
painter.end() painter.end()
glMatrixMode(GL_PROJECTION)
glPopMatrix()
glMatrixMode(GL_MODELVIEW)