File: GLGradientLegendItem.py

package info (click to toggle)
python-pyqtgraph 0.13.7-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,072 kB
  • sloc: python: 54,043; makefile: 127; ansic: 40; sh: 2
file content (82 lines) | stat: -rw-r--r-- 2,992 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from ... import functions as fn
from ...colormap import ColorMap
from ...Qt import QtCore, QtGui
from ..GLGraphicsItem import GLGraphicsItem

__all__ = ['GLGradientLegendItem']

class GLGradientLegendItem(GLGraphicsItem):
    """
    Displays legend colorbar on the screen.
    """

    def __init__(self, parentItem=None, **kwds):
        """
        Arguments:
            pos: position of the colorbar on the screen, from the top left corner, in pixels
            size: size of the colorbar without the text, in pixels
            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. Accepts any single argument accepted by
                :func:`~pyqtgraph.mkColor`
            #Todo:
                size as percentage
                legend title
        """
        super().__init__(parentItem=parentItem)
        glopts = kwds.pop("glOptions", "additive")
        self.setGLOptions(glopts)
        self.pos = (10, 10)
        self.size = (10, 100)
        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):
        args = ["size", "pos", "gradient", "labels", "fontColor"]
        for k in kwds.keys():
            if k not in args:
                raise Exception(
                    "Invalid keyword argument: %s (allowed arguments are %s)"
                    % (k, str(args))
                )

        self.antialias = False

        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
        self.update()

    def paint(self):
        self.setupGLState()

        if self._gradient is None:
            self._gradient = self.gradient.getGradient()

        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._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]
            y = self.size[1] - labelPosition * self.size[1] + self.pos[1] + 8
            ##todo: fonts
            painter.drawText(QtCore.QPointF(x, y), labelText)
        painter.end()