|
| 1 | +from PyQt6.QtGui import QFont, QColor |
| 2 | +from pyqtgraph.opengl.GLGraphicsItem import GLGraphicsItem |
| 3 | +from pyqtgraph.Qt import QtCore, QtGui |
| 4 | + |
| 5 | +from typing import Optional |
| 6 | + |
| 7 | +from software.thunderscope.gl.graphics.gl_painter import GLPainter |
| 8 | +from software.thunderscope.constants import Colors, THUNDERSCOPE_UI_FONT_NAME |
| 9 | + |
| 10 | + |
| 11 | +class GLLabel(GLPainter): |
| 12 | + """Displays a 2D text label on the viewport""" |
| 13 | + |
| 14 | + def __init__( |
| 15 | + self, |
| 16 | + parent_item: Optional[GLGraphicsItem] = None, |
| 17 | + font: QFont = QFont(THUNDERSCOPE_UI_FONT_NAME, 8), |
| 18 | + text_color: QColor = Colors.PRIMARY_TEXT_COLOR, |
| 19 | + offset: tuple[int, int] = (0, 0), |
| 20 | + text: str = "", |
| 21 | + ) -> None: |
| 22 | + """Initialize the GLLabel |
| 23 | +
|
| 24 | + :param parent_item: The parent item of the graphic |
| 25 | + :param font: The font using to render the text |
| 26 | + :param text_color: The color for rendering the text. |
| 27 | + :param offset: The offset (x, y) from the viewport left and top edge |
| 28 | + to use when positioning the label. |
| 29 | + If x is negative then the x offset is |x| pixels from |
| 30 | + the viewport right edge. |
| 31 | + If y is negative then the y offset is |y| pixels from |
| 32 | + the viewport bottom edge. |
| 33 | + :param text: The optional title to display above the legend |
| 34 | + """ |
| 35 | + super().__init__(parent_item=parent_item) |
| 36 | + |
| 37 | + self.text_pen = QtGui.QPen(text_color) |
| 38 | + self.font = font |
| 39 | + self.offset = offset |
| 40 | + self.text = text |
| 41 | + |
| 42 | + self.add_draw_function(self.draw_label) |
| 43 | + |
| 44 | + def draw_label(self, painter: QtGui.QPainter, viewport_rect: QtCore.QRect) -> None: |
| 45 | + """Draw the label |
| 46 | + :param painter: The QPainter to perform drawing operations with |
| 47 | + :param viewport_rect: The QRect indicating the viewport dimensions |
| 48 | + """ |
| 49 | + # calculate width and height of the label |
| 50 | + painter.setFont(self.font) |
| 51 | + bounds = painter.boundingRect( |
| 52 | + QtCore.QRectF(0, 0, 0, 0), |
| 53 | + QtCore.Qt.AlignmentFlag.AlignLeft | QtCore.Qt.AlignmentFlag.AlignVCenter, |
| 54 | + str(self.text), |
| 55 | + ) |
| 56 | + |
| 57 | + width = round(bounds.width()) |
| 58 | + height = round(bounds.height()) |
| 59 | + |
| 60 | + # Determine x and y coordinates of the label |
| 61 | + if self.offset[0] < 0: |
| 62 | + x = viewport_rect.right() + self.offset[0] - width |
| 63 | + else: |
| 64 | + x = viewport_rect.left() + self.offset[0] |
| 65 | + if self.offset[1] < 0: |
| 66 | + y = viewport_rect.bottom() + self.offset[1] - height |
| 67 | + else: |
| 68 | + y = viewport_rect.top() + self.offset[1] |
| 69 | + |
| 70 | + if self.text: |
| 71 | + painter.drawText(QtCore.QPoint(x, y), self.text) |
| 72 | + |
| 73 | + def set_text(self, new_text: str) -> None: |
| 74 | + """Update the text being displayed |
| 75 | + :param new_text: new text being displayed |
| 76 | + """ |
| 77 | + self.text = new_text |
0 commit comments