|
| 1 | +from abc import ABCMeta, abstractmethod |
| 2 | +from typing import List, Tuple |
| 3 | + |
| 4 | +import numpy as np |
| 5 | +from qtpy.QtCore import QRect, Qt |
| 6 | +from qtpy.QtGui import QColor, QFont, QImage, QPainter |
| 7 | +from qtpy.QtWidgets import QWidget |
| 8 | + |
| 9 | + |
| 10 | +class QWidgetABCMeta(type(QWidget), ABCMeta): |
| 11 | + """ |
| 12 | + A metaclass that combines the functionality of ABCMeta and QWidget's metaclass. |
| 13 | +
|
| 14 | + This allows the creation of abstract base classes that are also QWidgets. |
| 15 | + """ |
| 16 | + |
| 17 | + pass |
| 18 | + |
| 19 | + |
| 20 | +class WebGPUWidget(QWidget, metaclass=QWidgetABCMeta): |
| 21 | + """ |
| 22 | + An abstract base class for WebGPU widgets. |
| 23 | +
|
| 24 | + This class provides a template for creating WebGPU widgets with methods |
| 25 | + that must be implemented in subclasses. It is designed to be similar to the QOpenGLWidget class. |
| 26 | +
|
| 27 | + Attributes: |
| 28 | + initialized (bool): A flag indicating whether the widget has been initialized, default is False and will allow initializeWebGPU to be called once. |
| 29 | + """ |
| 30 | + |
| 31 | + def __init__(self) -> None: |
| 32 | + """ |
| 33 | + Initialize the AbstractWebGPUWidget. |
| 34 | +
|
| 35 | + This constructor initializes the QWidget and sets the initialized flag to False. |
| 36 | + """ |
| 37 | + super().__init__() |
| 38 | + self.initialized = False |
| 39 | + self.text_buffer: List[Tuple[int, int, str, int, str, QColor]] = [] |
| 40 | + self.buffer = None |
| 41 | + |
| 42 | + @abstractmethod |
| 43 | + def initializeWebGPU(self) -> None: |
| 44 | + """ |
| 45 | + Initialize the WebGPU context. |
| 46 | +
|
| 47 | + This method must be implemented in subclasses to set up the WebGPU context. Will be called once. |
| 48 | + """ |
| 49 | + pass |
| 50 | + |
| 51 | + @abstractmethod |
| 52 | + def paintWebGPU(self) -> None: |
| 53 | + """ |
| 54 | + Paint the WebGPU content. |
| 55 | +
|
| 56 | + This method must be implemented in subclasses to render the WebGPU content. This will be called on every paint event |
| 57 | + and is where all the main rendering code should be placed. |
| 58 | + """ |
| 59 | + pass |
| 60 | + |
| 61 | + @abstractmethod |
| 62 | + def resizeWebGPU(self, width: int, height: int) -> None: |
| 63 | + """ |
| 64 | + Resize the WebGPU context. |
| 65 | +
|
| 66 | + This method must be implemented in subclasses to handle resizing of the WebGPU context. Will be called on a resize of the widget. |
| 67 | +
|
| 68 | + Args: |
| 69 | + width (int): The new width of the widget. |
| 70 | + height (int): The new height of the widget. |
| 71 | + """ |
| 72 | + pass |
| 73 | + |
| 74 | + def paintEvent(self, event) -> None: |
| 75 | + """ |
| 76 | + Handle the paint event to render the WebGPU content. |
| 77 | +
|
| 78 | + Args: |
| 79 | + event (QPaintEvent): The paint event. |
| 80 | + """ |
| 81 | + if not self.initialized: |
| 82 | + self.initializeWebGPU() |
| 83 | + self.initialized = True |
| 84 | + self.paintWebGPU() |
| 85 | + painter = QPainter(self) |
| 86 | + |
| 87 | + if self.buffer is not None: |
| 88 | + self._present_image(painter, self.buffer) |
| 89 | + for x, y, text, size, font, colour in self.text_buffer: |
| 90 | + painter.setPen(colour) |
| 91 | + painter.setFont(QFont("Arial", size)) |
| 92 | + painter.drawText(x, y, text) |
| 93 | + self.text_buffer.clear() |
| 94 | + |
| 95 | + return super().paintEvent(event) |
| 96 | + |
| 97 | + def render_text( |
| 98 | + self, |
| 99 | + x: int, |
| 100 | + y: int, |
| 101 | + text: str, |
| 102 | + size: int = 10, |
| 103 | + font: str = "Arial", |
| 104 | + colour: QColor = Qt.black, |
| 105 | + ) -> None: |
| 106 | + """ |
| 107 | + Add text to the buffer to be rendered on the canvas. |
| 108 | +
|
| 109 | + Args: |
| 110 | + x (int): The x-coordinate of the text. |
| 111 | + y (int): The y-coordinate of the text. |
| 112 | + text (str): The text to render. |
| 113 | + size (int, optional): The font size of the text. Defaults to 10. |
| 114 | + font (str, optional): The font family of the text. Defaults to "Arial". |
| 115 | + colour (QColor, optional): The colour of the text. Defaults to Qt.black. |
| 116 | + """ |
| 117 | + self.text_buffer.append((x, y, text, size, font, colour)) |
| 118 | + |
| 119 | + def resizeEvent(self, event) -> None: |
| 120 | + """ |
| 121 | + Handle the resize event to adjust the WebGPU context. |
| 122 | +
|
| 123 | + Args: |
| 124 | + event (QResizeEvent): The resize event. |
| 125 | + """ |
| 126 | + self.resizeWebGPU(event.size().width(), event.size().height()) |
| 127 | + return super().resizeEvent(event) |
| 128 | + |
| 129 | + def _present_image(self, painter, image_data: np.ndarray) -> None: |
| 130 | + """ |
| 131 | + Present the image data on the canvas. |
| 132 | +
|
| 133 | + Args: |
| 134 | + image_data (np.ndarray): The image data to render. |
| 135 | + """ |
| 136 | + size = image_data.shape[0], image_data.shape[1] # width, height |
| 137 | + # We want to simply blit the image (copy pixels one-to-one on framebuffer). |
| 138 | + # Maybe Qt does this when the sizes match exactly (like they do here). |
| 139 | + # Converting to a QPixmap and painting that only makes it slower. |
| 140 | + |
| 141 | + # Just in case, set render hints that may hurt performance. |
| 142 | + painter.setRenderHints( |
| 143 | + painter.RenderHint.Antialiasing | painter.RenderHint.SmoothPixmapTransform, False |
| 144 | + ) |
| 145 | + |
| 146 | + image = QImage( |
| 147 | + image_data.flatten(), size[0], size[1], size[0] * 4, QImage.Format.Format_RGBA8888 |
| 148 | + ) |
| 149 | + |
| 150 | + rect1 = QRect(0, 0, size[0], size[1]) |
| 151 | + rect2 = self.rect() |
| 152 | + painter.drawImage(rect2, image, rect1) |
0 commit comments